blob: 23b91d2d194051ad6762ef8f96852139110fb102 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eapol_supp/eapol_supp_sm.h"
13#include "rsn_supp/wpa.h"
14#include "eloop.h"
15#include "config.h"
16#include "l2_packet/l2_packet.h"
17#include "wpa_supplicant_i.h"
18#include "driver_i.h"
19#include "pcsc_funcs.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "common/wpa_ctrl.h"
23#include "eap_peer/eap.h"
24#include "ap/hostapd.h"
25#include "p2p/p2p.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "notify.h"
28#include "common/ieee802_11_defs.h"
29#include "common/ieee802_11_common.h"
30#include "crypto/random.h"
31#include "blacklist.h"
32#include "wpas_glue.h"
33#include "wps_supplicant.h"
34#include "ibss_rsn.h"
35#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "p2p_supplicant.h"
38#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "ap.h"
41#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070044#include "interworking.h"
45
46
47static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
48 struct wpa_ssid *ssid)
49{
50 struct os_time now;
51
52 if (ssid == NULL || ssid->disabled_until.sec == 0)
53 return 0;
54
55 os_get_time(&now);
56 if (ssid->disabled_until.sec > now.sec)
57 return ssid->disabled_until.sec - now.sec;
58
59 wpas_clear_temp_disabled(wpa_s, ssid, 0);
60
61 return 0;
62}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
64
65static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
66{
67 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070068 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
71 return 0;
72
73 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
74 "information");
75 ssid = wpa_supplicant_get_ssid(wpa_s);
76 if (ssid == NULL) {
77 wpa_msg(wpa_s, MSG_INFO,
78 "No network configuration found for the current AP");
79 return -1;
80 }
81
Dmitry Shmidt04949592012-07-19 12:16:46 -070082 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
84 return -1;
85 }
86
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080087 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
88 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
89 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
90 return -1;
91 }
92
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070093 res = wpas_temp_disabled(wpa_s, ssid);
94 if (res > 0) {
95 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
96 "disabled for %d second(s)", res);
97 return -1;
98 }
99
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
101 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800102 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 u8 wpa_ie[80];
104 size_t wpa_ie_len = sizeof(wpa_ie);
105 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
106 wpa_ie, &wpa_ie_len);
107 } else {
108 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
109 }
110
111 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
112 eapol_sm_invalidate_cached_session(wpa_s->eapol);
113 old_ssid = wpa_s->current_ssid;
114 wpa_s->current_ssid = ssid;
115 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
116 wpa_supplicant_initiate_eapol(wpa_s);
117 if (old_ssid != wpa_s->current_ssid)
118 wpas_notify_network_changed(wpa_s);
119
120 return 0;
121}
122
123
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125{
126 struct wpa_supplicant *wpa_s = eloop_ctx;
127
128 if (wpa_s->countermeasures) {
129 wpa_s->countermeasures = 0;
130 wpa_drv_set_countermeasures(wpa_s, 0);
131 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
132 wpa_supplicant_req_scan(wpa_s, 0, 0);
133 }
134}
135
136
137void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
138{
139 int bssid_changed;
140
Dmitry Shmidt04949592012-07-19 12:16:46 -0700141 wnm_bss_keep_alive_deinit(wpa_s);
142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700143#ifdef CONFIG_IBSS_RSN
144 ibss_rsn_deinit(wpa_s->ibss_rsn);
145 wpa_s->ibss_rsn = NULL;
146#endif /* CONFIG_IBSS_RSN */
147
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700148#ifdef CONFIG_AP
149 wpa_supplicant_ap_deinit(wpa_s);
150#endif /* CONFIG_AP */
151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
153 return;
154
155 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800156#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700157 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800158#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
160 os_memset(wpa_s->bssid, 0, ETH_ALEN);
161 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700162#ifdef CONFIG_SME
163 wpa_s->sme.prev_bssid_set = 0;
164#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800165#ifdef CONFIG_P2P
166 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
167#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 wpa_s->current_bss = NULL;
169 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700170#ifdef CONFIG_IEEE80211R
171#ifdef CONFIG_SME
172 if (wpa_s->sme.ft_ies)
173 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
174#endif /* CONFIG_SME */
175#endif /* CONFIG_IEEE80211R */
176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 if (bssid_changed)
178 wpas_notify_bssid_changed(wpa_s);
179
180 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
181 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
182 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
183 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
184 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700185 wpa_s->current_ssid = NULL;
186 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187}
188
189
190static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
191{
192 struct wpa_ie_data ie;
193 int pmksa_set = -1;
194 size_t i;
195
196 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
197 ie.pmkid == NULL)
198 return;
199
200 for (i = 0; i < ie.num_pmkid; i++) {
201 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
202 ie.pmkid + i * PMKID_LEN,
203 NULL, NULL, 0);
204 if (pmksa_set == 0) {
205 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
206 break;
207 }
208 }
209
210 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
211 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
212}
213
214
215static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
216 union wpa_event_data *data)
217{
218 if (data == NULL) {
219 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
220 "event");
221 return;
222 }
223 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
224 " index=%d preauth=%d",
225 MAC2STR(data->pmkid_candidate.bssid),
226 data->pmkid_candidate.index,
227 data->pmkid_candidate.preauth);
228
229 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
230 data->pmkid_candidate.index,
231 data->pmkid_candidate.preauth);
232}
233
234
235static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
236{
237 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
238 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
239 return 0;
240
241#ifdef IEEE8021X_EAPOL
242 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
243 wpa_s->current_ssid &&
244 !(wpa_s->current_ssid->eapol_flags &
245 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
246 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
247 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
248 * plaintext or static WEP keys). */
249 return 0;
250 }
251#endif /* IEEE8021X_EAPOL */
252
253 return 1;
254}
255
256
257/**
258 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
259 * @wpa_s: pointer to wpa_supplicant data
260 * @ssid: Configuration data for the network
261 * Returns: 0 on success, -1 on failure
262 *
263 * This function is called when starting authentication with a network that is
264 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
265 */
266int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
267 struct wpa_ssid *ssid)
268{
269#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271 int aka = 0, sim = 0, type;
272
273 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
274 return 0;
275
276 if (ssid->eap.eap_methods == NULL) {
277 sim = 1;
278 aka = 1;
279 } else {
280 struct eap_method_type *eap = ssid->eap.eap_methods;
281 while (eap->vendor != EAP_VENDOR_IETF ||
282 eap->method != EAP_TYPE_NONE) {
283 if (eap->vendor == EAP_VENDOR_IETF) {
284 if (eap->method == EAP_TYPE_SIM)
285 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700286 else if (eap->method == EAP_TYPE_AKA ||
287 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 aka = 1;
289 }
290 eap++;
291 }
292 }
293
294 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
295 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700296 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
297 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
298 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 aka = 0;
300
301 if (!sim && !aka) {
302 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
303 "use SIM, but neither EAP-SIM nor EAP-AKA are "
304 "enabled");
305 return 0;
306 }
307
308 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
309 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
310 if (sim && aka)
311 type = SCARD_TRY_BOTH;
312 else if (aka)
313 type = SCARD_USIM_ONLY;
314 else
315 type = SCARD_GSM_SIM_ONLY;
316
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 if (wpa_s->scard == NULL) {
319 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
320 "(pcsc-lite)");
321 return -1;
322 }
323 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
324 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326#endif /* IEEE8021X_EAPOL */
327
328 return 0;
329}
330
331
332#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700333static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334 struct wpa_ssid *ssid)
335{
336 int i, privacy = 0;
337
338 if (ssid->mixed_cell)
339 return 1;
340
341#ifdef CONFIG_WPS
342 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
343 return 1;
344#endif /* CONFIG_WPS */
345
346 for (i = 0; i < NUM_WEP_KEYS; i++) {
347 if (ssid->wep_key_len[i]) {
348 privacy = 1;
349 break;
350 }
351 }
352#ifdef IEEE8021X_EAPOL
353 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
354 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
355 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
356 privacy = 1;
357#endif /* IEEE8021X_EAPOL */
358
Jouni Malinen75ecf522011-06-27 15:19:46 -0700359 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
360 privacy = 1;
361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 if (bss->caps & IEEE80211_CAP_PRIVACY)
363 return privacy;
364 return !privacy;
365}
366
367
368static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
369 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700370 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371{
372 struct wpa_ie_data ie;
373 int proto_match = 0;
374 const u8 *rsn_ie, *wpa_ie;
375 int ret;
376 int wep_ok;
377
378 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
379 if (ret >= 0)
380 return ret;
381
382 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
383 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
384 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
385 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
386 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
387
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700388 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
390 proto_match++;
391
392 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
393 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
394 "failed");
395 break;
396 }
397
398 if (wep_ok &&
399 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
400 {
401 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
402 "in RSN IE");
403 return 1;
404 }
405
406 if (!(ie.proto & ssid->proto)) {
407 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
408 "mismatch");
409 break;
410 }
411
412 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
413 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
414 "cipher mismatch");
415 break;
416 }
417
418 if (!(ie.group_cipher & ssid->group_cipher)) {
419 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
420 "cipher mismatch");
421 break;
422 }
423
424 if (!(ie.key_mgmt & ssid->key_mgmt)) {
425 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
426 "mismatch");
427 break;
428 }
429
430#ifdef CONFIG_IEEE80211W
431 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800432 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
433 wpa_s->conf->pmf : ssid->ieee80211w) ==
434 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
436 "frame protection");
437 break;
438 }
439#endif /* CONFIG_IEEE80211W */
440
441 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
442 return 1;
443 }
444
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700445 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
447 proto_match++;
448
449 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
450 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
451 "failed");
452 break;
453 }
454
455 if (wep_ok &&
456 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
457 {
458 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
459 "in WPA IE");
460 return 1;
461 }
462
463 if (!(ie.proto & ssid->proto)) {
464 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
465 "mismatch");
466 break;
467 }
468
469 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
470 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
471 "cipher mismatch");
472 break;
473 }
474
475 if (!(ie.group_cipher & ssid->group_cipher)) {
476 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
477 "cipher mismatch");
478 break;
479 }
480
481 if (!(ie.key_mgmt & ssid->key_mgmt)) {
482 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
483 "mismatch");
484 break;
485 }
486
487 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
488 return 1;
489 }
490
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700491 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
492 !rsn_ie) {
493 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
494 return 1;
495 }
496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
498 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
499 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
500 return 0;
501 }
502
503 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
504 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
505 return 1;
506 }
507
508 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
509 "WPA/WPA2");
510
511 return 0;
512}
513
514
515static int freq_allowed(int *freqs, int freq)
516{
517 int i;
518
519 if (freqs == NULL)
520 return 1;
521
522 for (i = 0; freqs[i]; i++)
523 if (freqs[i] == freq)
524 return 1;
525 return 0;
526}
527
528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800529static int ht_supported(const struct hostapd_hw_modes *mode)
530{
531 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
532 /*
533 * The driver did not indicate whether it supports HT. Assume
534 * it does to avoid connection issues.
535 */
536 return 1;
537 }
538
539 /*
540 * IEEE Std 802.11n-2009 20.1.1:
541 * An HT non-AP STA shall support all EQM rates for one spatial stream.
542 */
543 return mode->mcs_set[0] == 0xff;
544}
545
546
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700547static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800548{
549 const struct hostapd_hw_modes *mode = NULL, *modes;
550 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
551 const u8 *rate_ie;
552 int i, j, k;
553
554 if (bss->freq == 0)
555 return 1; /* Cannot do matching without knowing band */
556
557 modes = wpa_s->hw.modes;
558 if (modes == NULL) {
559 /*
560 * The driver does not provide any additional information
561 * about the utilized hardware, so allow the connection attempt
562 * to continue.
563 */
564 return 1;
565 }
566
567 for (i = 0; i < wpa_s->hw.num_modes; i++) {
568 for (j = 0; j < modes[i].num_channels; j++) {
569 int freq = modes[i].channels[j].freq;
570 if (freq == bss->freq) {
571 if (mode &&
572 mode->mode == HOSTAPD_MODE_IEEE80211G)
573 break; /* do not allow 802.11b replace
574 * 802.11g */
575 mode = &modes[i];
576 break;
577 }
578 }
579 }
580
581 if (mode == NULL)
582 return 0;
583
584 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700585 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586 if (rate_ie == NULL)
587 continue;
588
589 for (j = 2; j < rate_ie[1] + 2; j++) {
590 int flagged = !!(rate_ie[j] & 0x80);
591 int r = (rate_ie[j] & 0x7f) * 5;
592
593 /*
594 * IEEE Std 802.11n-2009 7.3.2.2:
595 * The new BSS Membership selector value is encoded
596 * like a legacy basic rate, but it is not a rate and
597 * only indicates if the BSS members are required to
598 * support the mandatory features of Clause 20 [HT PHY]
599 * in order to join the BSS.
600 */
601 if (flagged && ((rate_ie[j] & 0x7f) ==
602 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
603 if (!ht_supported(mode)) {
604 wpa_dbg(wpa_s, MSG_DEBUG,
605 " hardware does not support "
606 "HT PHY");
607 return 0;
608 }
609 continue;
610 }
611
612 if (!flagged)
613 continue;
614
615 /* check for legacy basic rates */
616 for (k = 0; k < mode->num_rates; k++) {
617 if (mode->rates[k] == r)
618 break;
619 }
620 if (k == mode->num_rates) {
621 /*
622 * IEEE Std 802.11-2007 7.3.2.2 demands that in
623 * order to join a BSS all required rates
624 * have to be supported by the hardware.
625 */
626 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
627 "not support required rate %d.%d Mbps",
628 r / 10, r % 10);
629 return 0;
630 }
631 }
632 }
633
634 return 1;
635}
636
637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700639 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 struct wpa_ssid *group)
641{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700642 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 int wpa;
644 struct wpa_blacklist *e;
645 const u8 *ie;
646 struct wpa_ssid *ssid;
647
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700648 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 wpa_ie_len = ie ? ie[1] : 0;
650
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700651 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652 rsn_ie_len = ie ? ie[1] : 0;
653
654 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
655 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700656 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700658 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659
660 e = wpa_blacklist_get(wpa_s, bss->bssid);
661 if (e) {
662 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700663 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 /*
665 * When only a single network is enabled, we can
666 * trigger blacklisting on the first failure. This
667 * should not be done with multiple enabled networks to
668 * avoid getting forced to move into a worse ESS on
669 * single error if there are no other BSSes of the
670 * current ESS.
671 */
672 limit = 0;
673 }
674 if (e->count > limit) {
675 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
676 "(count=%d limit=%d)", e->count, limit);
677 return NULL;
678 }
679 }
680
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700681 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
683 return NULL;
684 }
685
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800686 if (disallowed_bssid(wpa_s, bss->bssid)) {
687 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
688 return NULL;
689 }
690
691 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
692 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
693 return NULL;
694 }
695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
697
698 for (ssid = group; ssid; ssid = ssid->pnext) {
699 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700700 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701
Dmitry Shmidt04949592012-07-19 12:16:46 -0700702 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
704 continue;
705 }
706
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700707 res = wpas_temp_disabled(wpa_s, ssid);
708 if (res > 0) {
709 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
710 "temporarily for %d second(s)", res);
711 continue;
712 }
713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714#ifdef CONFIG_WPS
715 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
716 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
717 "(WPS)");
718 continue;
719 }
720
721 if (wpa && ssid->ssid_len == 0 &&
722 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
723 check_ssid = 0;
724
725 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
726 /* Only allow wildcard SSID match if an AP
727 * advertises active WPS operation that matches
728 * with our mode. */
729 check_ssid = 1;
730 if (ssid->ssid_len == 0 &&
731 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
732 check_ssid = 0;
733 }
734#endif /* CONFIG_WPS */
735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736 if (ssid->bssid_set && ssid->ssid_len == 0 &&
737 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
738 check_ssid = 0;
739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700741 (bss->ssid_len != ssid->ssid_len ||
742 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
744 continue;
745 }
746
747 if (ssid->bssid_set &&
748 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
749 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
750 continue;
751 }
752
753 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
754 continue;
755
756 if (!wpa &&
757 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
758 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
759 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
760 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
761 "not allowed");
762 continue;
763 }
764
Jouni Malinen75ecf522011-06-27 15:19:46 -0700765 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
767 "mismatch");
768 continue;
769 }
770
Jouni Malinen75ecf522011-06-27 15:19:46 -0700771 if (bss->caps & IEEE80211_CAP_IBSS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
773 "network");
774 continue;
775 }
776
777 if (!freq_allowed(ssid->freq_list, bss->freq)) {
778 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
779 "allowed");
780 continue;
781 }
782
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 if (!rate_match(wpa_s, bss)) {
784 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
785 "not match");
786 continue;
787 }
788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789#ifdef CONFIG_P2P
790 /*
791 * TODO: skip the AP if its P2P IE has Group Formation
792 * bit set in the P2P Group Capability Bitmap and we
793 * are not in Group Formation with that device.
794 */
795#endif /* CONFIG_P2P */
796
797 /* Matching configuration found */
798 return ssid;
799 }
800
801 /* No matching configuration found */
802 return NULL;
803}
804
805
806static struct wpa_bss *
807wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 struct wpa_ssid *group,
809 struct wpa_ssid **selected_ssid)
810{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700811 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812
813 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
814 group->priority);
815
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700816 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
817 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
819 if (!*selected_ssid)
820 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
822 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700823 MAC2STR(bss->bssid),
824 wpa_ssid_txt(bss->ssid, bss->ssid_len));
825 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 }
827
828 return NULL;
829}
830
831
832static struct wpa_bss *
833wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 struct wpa_ssid **selected_ssid)
835{
836 struct wpa_bss *selected = NULL;
837 int prio;
838
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700839 if (wpa_s->last_scan_res == NULL ||
840 wpa_s->last_scan_res_used == 0)
841 return NULL; /* no scan results from last update */
842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 while (selected == NULL) {
844 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
845 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700846 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 selected_ssid);
848 if (selected)
849 break;
850 }
851
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800852 if (selected == NULL && wpa_s->blacklist &&
853 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
855 "blacklist and try again");
856 wpa_blacklist_clear(wpa_s);
857 wpa_s->blacklist_cleared++;
858 } else if (selected == NULL)
859 break;
860 }
861
862 return selected;
863}
864
865
866static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
867 int timeout_sec, int timeout_usec)
868{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700869 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 /*
871 * No networks are enabled; short-circuit request so
872 * we don't wait timeout seconds before transitioning
873 * to INACTIVE state.
874 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700875 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
876 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700878#ifdef CONFIG_P2P
879 wpa_s->sta_scan_pending = 0;
880#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 return;
882 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800883
884 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
886}
887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700889int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800890 struct wpa_bss *selected,
891 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892{
893 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
894 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
895 "PBC session overlap");
896#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700898 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899#endif /* CONFIG_P2P */
900
901#ifdef CONFIG_WPS
902 wpas_wps_cancel(wpa_s);
903#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700904 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 }
906
907 /*
908 * Do not trigger new association unless the BSSID has changed or if
909 * reassociation is requested. If we are in process of associating with
910 * the selected BSSID, do not trigger new attempt.
911 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800912 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
914 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
915 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
916 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
917 0))) {
918 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
919 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700920 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 }
922 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
923 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
924 " pending: " MACSTR " wpa_state: %s",
925 wpa_s->reassociate, MAC2STR(selected->bssid),
926 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
927 wpa_supplicant_state_txt(wpa_s->wpa_state));
928 wpa_supplicant_associate(wpa_s, selected, ssid);
929 } else {
930 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
931 "selected AP");
932 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700934 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935}
936
937
938static struct wpa_ssid *
939wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
940{
941 int prio;
942 struct wpa_ssid *ssid;
943
944 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
945 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
946 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700947 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 continue;
949 if (ssid->mode == IEEE80211_MODE_IBSS ||
950 ssid->mode == IEEE80211_MODE_AP)
951 return ssid;
952 }
953 }
954 return NULL;
955}
956
957
958/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
959 * on BSS added and BSS changed events */
960static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300961 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300963 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964
965 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
966 return;
967
Jouni Malinen87fd2792011-05-16 18:35:42 +0300968 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970
Jouni Malinen87fd2792011-05-16 18:35:42 +0300971 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 if (ssid == NULL)
973 continue;
974
Jouni Malinen87fd2792011-05-16 18:35:42 +0300975 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 if (rsn == NULL)
977 continue;
978
Jouni Malinen87fd2792011-05-16 18:35:42 +0300979 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 }
981
982}
983
984
985static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
986 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700987 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700989 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 int min_diff;
991
992 if (wpa_s->reassociate)
993 return 1; /* explicit request to reassociate */
994 if (wpa_s->wpa_state < WPA_ASSOCIATED)
995 return 1; /* we are not associated; continue */
996 if (wpa_s->current_ssid == NULL)
997 return 1; /* unknown current SSID */
998 if (wpa_s->current_ssid != ssid)
999 return 1; /* different network block */
1000
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001001 if (wpas_driver_bss_selection(wpa_s))
1002 return 0; /* Driver-based roaming */
1003
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001004 if (wpa_s->current_ssid->ssid)
1005 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1006 wpa_s->current_ssid->ssid,
1007 wpa_s->current_ssid->ssid_len);
1008 if (!current_bss)
1009 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010
1011 if (!current_bss)
1012 return 1; /* current BSS not seen in scan results */
1013
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001014 if (current_bss == selected)
1015 return 0;
1016
1017 if (selected->last_update_idx > current_bss->last_update_idx)
1018 return 1; /* current BSS not seen in the last scan */
1019
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001020#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1022 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1023 MAC2STR(current_bss->bssid), current_bss->level);
1024 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1025 MAC2STR(selected->bssid), selected->level);
1026
1027 if (wpa_s->current_ssid->bssid_set &&
1028 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1029 0) {
1030 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1031 "has preferred BSSID");
1032 return 1;
1033 }
1034
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001035 if (current_bss->level < 0 && current_bss->level > selected->level) {
1036 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1037 "signal level");
1038 return 0;
1039 }
1040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 min_diff = 2;
1042 if (current_bss->level < 0) {
1043 if (current_bss->level < -85)
1044 min_diff = 1;
1045 else if (current_bss->level < -80)
1046 min_diff = 2;
1047 else if (current_bss->level < -75)
1048 min_diff = 3;
1049 else if (current_bss->level < -70)
1050 min_diff = 4;
1051 else
1052 min_diff = 5;
1053 }
1054 if (abs(current_bss->level - selected->level) < min_diff) {
1055 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1056 "in signal level");
1057 return 0;
1058 }
1059
1060 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001061#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001062 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001063#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064}
1065
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001066
Jouni Malinen89ca7022012-09-14 13:03:12 -07001067/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001068 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001069#ifdef ANDROID_P2P
1070static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1071 union wpa_event_data *data, int suppress_event)
1072#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1074 union wpa_event_data *data)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001075#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 struct wpa_scan_results *scan_res;
1078 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001079#ifndef CONFIG_NO_RANDOM_POOL
1080 size_t i, num;
1081#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082
1083#ifdef CONFIG_AP
1084 if (wpa_s->ap_iface)
1085 ap = 1;
1086#endif /* CONFIG_AP */
1087
1088 wpa_supplicant_notify_scanning(wpa_s, 0);
1089
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001090#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07001091 if (wpa_s->global->p2p_cb_on_scan_complete &&
1092 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001093 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1094 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001095 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1098 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001099 wpa_s->sta_scan_pending = 1;
1100 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001101 return -1;
1102 }
1103 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001104 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001105#endif /* CONFIG_P2P */
1106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1108 data ? &data->scan_info :
1109 NULL, 1);
1110 if (scan_res == NULL) {
1111 if (wpa_s->conf->ap_scan == 2 || ap)
1112 return -1;
1113 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1114 "scanning again");
1115 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1116 return -1;
1117 }
1118
1119#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 num = scan_res->num;
1121 if (num > 10)
1122 num = 10;
1123 for (i = 0; i < num; i++) {
1124 u8 buf[5];
1125 struct wpa_scan_res *res = scan_res->res[i];
1126 buf[0] = res->bssid[5];
1127 buf[1] = res->qual & 0xff;
1128 buf[2] = res->noise & 0xff;
1129 buf[3] = res->level & 0xff;
1130 buf[4] = res->tsf & 0xff;
1131 random_add_randomness(buf, sizeof(buf));
1132 }
1133#endif /* CONFIG_NO_RANDOM_POOL */
1134
1135 if (wpa_s->scan_res_handler) {
1136 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1137 struct wpa_scan_results *scan_res);
1138
1139 scan_res_handler = wpa_s->scan_res_handler;
1140 wpa_s->scan_res_handler = NULL;
1141 scan_res_handler(wpa_s, scan_res);
1142
1143 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001144 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 }
1146
1147 if (ap) {
1148 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1149#ifdef CONFIG_AP
1150 if (wpa_s->ap_iface->scan_cb)
1151 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1152#endif /* CONFIG_AP */
1153 wpa_scan_results_free(scan_res);
1154 return 0;
1155 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001156#ifdef ANDROID_P2P
1157 if(!suppress_event)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001158 {
1159 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1160 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1161 wpas_notify_scan_results(wpa_s);
1162 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001163#else
1164 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1165 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1166 wpas_notify_scan_results(wpa_s);
1167#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168
1169 wpas_notify_scan_done(wpa_s, 1);
1170
Dmitry Shmidt04949592012-07-19 12:16:46 -07001171 if (sme_proc_obss_scan(wpa_s) > 0) {
1172 wpa_scan_results_free(scan_res);
1173 return 0;
1174 }
1175
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1177 wpa_scan_results_free(scan_res);
1178 return 0;
1179 }
1180
Dmitry Shmidt04949592012-07-19 12:16:46 -07001181 if (autoscan_notify_scan(wpa_s, scan_res)) {
1182 wpa_scan_results_free(scan_res);
1183 return 0;
1184 }
1185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 if (wpa_s->disconnected) {
1187 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1188 wpa_scan_results_free(scan_res);
1189 return 0;
1190 }
1191
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001192 if (!wpas_driver_bss_selection(wpa_s) &&
1193 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 wpa_scan_results_free(scan_res);
1195 return 0;
1196 }
1197
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001198 wpas_wps_update_ap_info(wpa_s, scan_res);
1199
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001200 wpa_scan_results_free(scan_res);
1201
1202 return wpas_select_network_from_last_scan(wpa_s);
1203}
1204
1205
1206int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
1207{
1208 struct wpa_bss *selected;
1209 struct wpa_ssid *ssid = NULL;
1210
1211 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212
1213 if (selected) {
1214 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001215 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216 if (skip) {
1217 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001219 }
1220
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001221 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001222 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001223 return -1;
1224 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001225 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001226 /*
1227 * Do not notify other virtual radios of scan results since we do not
1228 * want them to start other associations at the same time.
1229 */
1230 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1233 ssid = wpa_supplicant_pick_new_network(wpa_s);
1234 if (ssid) {
1235 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1236 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001237 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 } else {
1239 int timeout_sec = wpa_s->scan_interval;
1240 int timeout_usec = 0;
1241#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001242 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1243 return 0;
1244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 if (wpa_s->p2p_in_provisioning) {
1246 /*
1247 * Use shorter wait during P2P Provisioning
1248 * state to speed up group formation.
1249 */
1250 timeout_sec = 0;
1251 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1253 timeout_usec);
1254 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 }
1256#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001257#ifdef CONFIG_INTERWORKING
1258 if (wpa_s->conf->auto_interworking &&
1259 wpa_s->conf->interworking &&
1260 wpa_s->conf->cred) {
1261 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1262 "start ANQP fetch since no matching "
1263 "networks found");
1264 wpa_s->network_select = 1;
1265 wpa_s->auto_network_select = 1;
1266 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001267 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001268 }
1269#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001270 if (wpa_supplicant_req_sched_scan(wpa_s))
1271 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1272 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 }
1274 }
1275 return 0;
1276}
1277
1278
1279static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1280 union wpa_event_data *data)
1281{
1282 const char *rn, *rn2;
1283 struct wpa_supplicant *ifs;
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001284#ifdef ANDROID_P2P
Jouni Malinen89ca7022012-09-14 13:03:12 -07001285 if (_wpa_supplicant_event_scan_results(wpa_s, data, 0) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001286#else
Jouni Malinen89ca7022012-09-14 13:03:12 -07001287 if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001288#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 /*
1290 * If no scan results could be fetched, then no need to
1291 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001292 * this scan. Similarly, if scan results started a new operation on this
1293 * interface, do not notify other interfaces to avoid concurrent
1294 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 */
1296 return;
1297 }
1298
1299 /*
1300 * Check other interfaces to see if they have the same radio-name. If
1301 * so, they get updated with this same scan info.
1302 */
1303 if (!wpa_s->driver->get_radio_name)
1304 return;
1305
1306 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1307 if (rn == NULL || rn[0] == '\0')
1308 return;
1309
1310 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1311 "sharing same radio (%s) in event_scan_results", rn);
1312
1313 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1314 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1315 continue;
1316
1317 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1318 if (rn2 && os_strcmp(rn, rn2) == 0) {
1319 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1320 "sibling", ifs->ifname);
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001321#ifdef ANDROID_P2P
1322 if ( (ifs->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) || (ifs->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
1323 /* Do not update the scan results from STA interface to p2p interfaces */
1324 wpa_printf(MSG_DEBUG, "Not Updating scan results on interface %s from "
1325 "sibling %s", ifs->ifname, wpa_s->ifname);
1326 continue;
1327 }
1328 else {
1329 /* P2P_FIND will result in too many SCAN_RESULT_EVENTS within
1330 * no time. Avoid announcing it to application as it may not
1331 * be that useful (since results will be that of only 1,6,11).
1332 * over to any other interface as it
1333 */
1334 if(p2p_search_in_progress(wpa_s->global->p2p))
1335 _wpa_supplicant_event_scan_results(ifs, data, 1);
1336 else
1337 _wpa_supplicant_event_scan_results(ifs, data, 0);
1338 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001339#else
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001340 _wpa_supplicant_event_scan_results(ifs, data);
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001341#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 }
1343 }
1344}
1345
1346#endif /* CONFIG_NO_SCAN_PROCESSING */
1347
1348
Dmitry Shmidt04949592012-07-19 12:16:46 -07001349#ifdef CONFIG_WNM
1350
1351static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1352{
1353 struct wpa_supplicant *wpa_s = eloop_ctx;
1354
1355 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1356 return;
1357
1358 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1359 MAC2STR(wpa_s->bssid));
1360 /* TODO: could skip this if normal data traffic has been sent */
1361 /* TODO: Consider using some more appropriate data frame for this */
1362 if (wpa_s->l2)
1363 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800, (u8 *) "", 0);
1364
1365#ifdef CONFIG_SME
1366 if (wpa_s->sme.bss_max_idle_period) {
1367 unsigned int msec;
1368 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1369 if (msec > 100)
1370 msec -= 100;
1371 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1372 wnm_bss_keep_alive, wpa_s, NULL);
1373 }
1374#endif /* CONFIG_SME */
1375}
1376
1377
1378static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1379 const u8 *ies, size_t ies_len)
1380{
1381 struct ieee802_11_elems elems;
1382
1383 if (ies == NULL)
1384 return;
1385
1386 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1387 return;
1388
1389#ifdef CONFIG_SME
1390 if (elems.bss_max_idle_period) {
1391 unsigned int msec;
1392 wpa_s->sme.bss_max_idle_period =
1393 WPA_GET_LE16(elems.bss_max_idle_period);
1394 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1395 "TU)%s", wpa_s->sme.bss_max_idle_period,
1396 (elems.bss_max_idle_period[2] & 0x01) ?
1397 " (protected keep-live required)" : "");
1398 if (wpa_s->sme.bss_max_idle_period == 0)
1399 wpa_s->sme.bss_max_idle_period = 1;
1400 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1401 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1402 /* msec times 1000 */
1403 msec = wpa_s->sme.bss_max_idle_period * 1024;
1404 if (msec > 100)
1405 msec -= 100;
1406 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1407 wnm_bss_keep_alive, wpa_s,
1408 NULL);
1409 }
1410 }
1411#endif /* CONFIG_SME */
1412}
1413
1414#endif /* CONFIG_WNM */
1415
1416
1417void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1418{
1419#ifdef CONFIG_WNM
1420 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1421#endif /* CONFIG_WNM */
1422}
1423
1424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1426 union wpa_event_data *data)
1427{
1428 int l, len, found = 0, wpa_found, rsn_found;
1429 const u8 *p;
1430
1431 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1432 if (data->assoc_info.req_ies)
1433 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1434 data->assoc_info.req_ies_len);
1435 if (data->assoc_info.resp_ies) {
1436 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1437 data->assoc_info.resp_ies_len);
1438#ifdef CONFIG_TDLS
1439 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1440 data->assoc_info.resp_ies_len);
1441#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001442#ifdef CONFIG_WNM
1443 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1444 data->assoc_info.resp_ies_len);
1445#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 }
1447 if (data->assoc_info.beacon_ies)
1448 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1449 data->assoc_info.beacon_ies,
1450 data->assoc_info.beacon_ies_len);
1451 if (data->assoc_info.freq)
1452 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1453 data->assoc_info.freq);
1454
1455 p = data->assoc_info.req_ies;
1456 l = data->assoc_info.req_ies_len;
1457
1458 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1459 while (p && l >= 2) {
1460 len = p[1] + 2;
1461 if (len > l) {
1462 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1463 p, l);
1464 break;
1465 }
1466 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1467 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1468 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1469 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1470 break;
1471 found = 1;
1472 wpa_find_assoc_pmkid(wpa_s);
1473 break;
1474 }
1475 l -= len;
1476 p += len;
1477 }
1478 if (!found && data->assoc_info.req_ies)
1479 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1480
1481#ifdef CONFIG_IEEE80211R
1482#ifdef CONFIG_SME
1483 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1484 u8 bssid[ETH_ALEN];
1485 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1486 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1487 data->assoc_info.resp_ies,
1488 data->assoc_info.resp_ies_len,
1489 bssid) < 0) {
1490 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1491 "Reassociation Response failed");
1492 wpa_supplicant_deauthenticate(
1493 wpa_s, WLAN_REASON_INVALID_IE);
1494 return -1;
1495 }
1496 }
1497
1498 p = data->assoc_info.resp_ies;
1499 l = data->assoc_info.resp_ies_len;
1500
1501#ifdef CONFIG_WPS_STRICT
1502 if (p && wpa_s->current_ssid &&
1503 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1504 struct wpabuf *wps;
1505 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1506 if (wps == NULL) {
1507 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1508 "include WPS IE in (Re)Association Response");
1509 return -1;
1510 }
1511
1512 if (wps_validate_assoc_resp(wps) < 0) {
1513 wpabuf_free(wps);
1514 wpa_supplicant_deauthenticate(
1515 wpa_s, WLAN_REASON_INVALID_IE);
1516 return -1;
1517 }
1518 wpabuf_free(wps);
1519 }
1520#endif /* CONFIG_WPS_STRICT */
1521
1522 /* Go through the IEs and make a copy of the MDIE, if present. */
1523 while (p && l >= 2) {
1524 len = p[1] + 2;
1525 if (len > l) {
1526 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1527 p, l);
1528 break;
1529 }
1530 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1531 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1532 wpa_s->sme.ft_used = 1;
1533 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1534 MOBILITY_DOMAIN_ID_LEN);
1535 break;
1536 }
1537 l -= len;
1538 p += len;
1539 }
1540#endif /* CONFIG_SME */
1541
1542 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1543 data->assoc_info.resp_ies_len);
1544#endif /* CONFIG_IEEE80211R */
1545
1546 /* WPA/RSN IE from Beacon/ProbeResp */
1547 p = data->assoc_info.beacon_ies;
1548 l = data->assoc_info.beacon_ies_len;
1549
1550 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1551 */
1552 wpa_found = rsn_found = 0;
1553 while (p && l >= 2) {
1554 len = p[1] + 2;
1555 if (len > l) {
1556 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1557 p, l);
1558 break;
1559 }
1560 if (!wpa_found &&
1561 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1562 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1563 wpa_found = 1;
1564 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1565 }
1566
1567 if (!rsn_found &&
1568 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1569 rsn_found = 1;
1570 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1571 }
1572
1573 l -= len;
1574 p += len;
1575 }
1576
1577 if (!wpa_found && data->assoc_info.beacon_ies)
1578 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1579 if (!rsn_found && data->assoc_info.beacon_ies)
1580 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1581 if (wpa_found || rsn_found)
1582 wpa_s->ap_ies_from_associnfo = 1;
1583
Jouni Malinen87fd2792011-05-16 18:35:42 +03001584 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1585 wpa_s->assoc_freq != data->assoc_info.freq) {
1586 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1587 "%u to %u MHz",
1588 wpa_s->assoc_freq, data->assoc_info.freq);
1589 wpa_supplicant_update_scan_results(wpa_s);
1590 }
1591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpa_s->assoc_freq = data->assoc_info.freq;
1593
1594 return 0;
1595}
1596
1597
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001598static struct wpa_bss * wpa_supplicant_get_new_bss(
1599 struct wpa_supplicant *wpa_s, const u8 *bssid)
1600{
1601 struct wpa_bss *bss = NULL;
1602 struct wpa_ssid *ssid = wpa_s->current_ssid;
1603
1604 if (ssid->ssid_len > 0)
1605 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1606 if (!bss)
1607 bss = wpa_bss_get_bssid(wpa_s, bssid);
1608
1609 return bss;
1610}
1611
1612
1613static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1614{
1615 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1616
1617 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1618 return -1;
1619
1620 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1621 return 0;
1622
1623 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1624 WPA_IE_VENDOR_TYPE);
1625 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1626
1627 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1628 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1629 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1630 bss_rsn ? 2 + bss_rsn[1] : 0))
1631 return -1;
1632
1633 return 0;
1634}
1635
1636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1638 union wpa_event_data *data)
1639{
1640 u8 bssid[ETH_ALEN];
1641 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 struct wpa_driver_capa capa;
1643
1644#ifdef CONFIG_AP
1645 if (wpa_s->ap_iface) {
1646 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1647 data->assoc_info.addr,
1648 data->assoc_info.req_ies,
1649 data->assoc_info.req_ies_len,
1650 data->assoc_info.reassoc);
1651 return;
1652 }
1653#endif /* CONFIG_AP */
1654
1655 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1656 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1657 return;
1658
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001659 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1660 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001661 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001662 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1663 return;
1664 }
1665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001667 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1669 MACSTR, MAC2STR(bssid));
1670 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1672 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001673 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674
1675 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1676 wpa_clear_keys(wpa_s, bssid);
1677 }
1678 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001679 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1681 return;
1682 }
1683 if (wpa_s->current_ssid) {
1684 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001685
1686 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1687 if (!bss) {
1688 wpa_supplicant_update_scan_results(wpa_s);
1689
1690 /* Get the BSS from the new scan results */
1691 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1692 }
1693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 if (bss)
1695 wpa_s->current_bss = bss;
1696 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001697
1698 if (wpa_s->conf->ap_scan == 1 &&
1699 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1700 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1701 wpa_msg(wpa_s, MSG_WARNING,
1702 "WPA/RSN IEs not updated");
1703 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 }
1705
1706#ifdef CONFIG_SME
1707 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1708 wpa_s->sme.prev_bssid_set = 1;
1709#endif /* CONFIG_SME */
1710
1711 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1712 if (wpa_s->current_ssid) {
1713 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1714 * initialized before association, but for other modes,
1715 * initialize PC/SC here, if the current configuration needs
1716 * smartcard or SIM/USIM. */
1717 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1718 }
1719 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1720 if (wpa_s->l2)
1721 l2_packet_notify_auth_start(wpa_s->l2);
1722
1723 /*
1724 * Set portEnabled first to FALSE in order to get EAP state machine out
1725 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1726 * state machine may transit to AUTHENTICATING state based on obsolete
1727 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1728 * AUTHENTICATED without ever giving chance to EAP state machine to
1729 * reset the state.
1730 */
1731 if (!ft_completed) {
1732 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1733 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1734 }
1735 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1736 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1737 /* 802.1X::portControl = Auto */
1738 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1739 wpa_s->eapol_received = 0;
1740 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1741 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1742 (wpa_s->current_ssid &&
1743 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1744 wpa_supplicant_cancel_auth_timeout(wpa_s);
1745 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1746 } else if (!ft_completed) {
1747 /* Timeout for receiving the first EAPOL packet */
1748 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1749 }
1750 wpa_supplicant_cancel_scan(wpa_s);
1751
1752 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1753 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1754 /*
1755 * We are done; the driver will take care of RSN 4-way
1756 * handshake.
1757 */
1758 wpa_supplicant_cancel_auth_timeout(wpa_s);
1759 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1760 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1761 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1762 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1763 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1764 /*
1765 * The driver will take care of RSN 4-way handshake, so we need
1766 * to allow EAPOL supplicant to complete its work without
1767 * waiting for WPA supplicant.
1768 */
1769 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1770 } else if (ft_completed) {
1771 /*
1772 * FT protocol completed - make sure EAPOL state machine ends
1773 * up in authenticated.
1774 */
1775 wpa_supplicant_cancel_auth_timeout(wpa_s);
1776 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1777 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1778 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1779 }
1780
1781 if (wpa_s->pending_eapol_rx) {
1782 struct os_time now, age;
1783 os_get_time(&now);
1784 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1785 if (age.sec == 0 && age.usec < 100000 &&
1786 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1787 0) {
1788 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1789 "frame that was received just before "
1790 "association notification");
1791 wpa_supplicant_rx_eapol(
1792 wpa_s, wpa_s->pending_eapol_rx_src,
1793 wpabuf_head(wpa_s->pending_eapol_rx),
1794 wpabuf_len(wpa_s->pending_eapol_rx));
1795 }
1796 wpabuf_free(wpa_s->pending_eapol_rx);
1797 wpa_s->pending_eapol_rx = NULL;
1798 }
1799
1800 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1801 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1802 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1803 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1804 /* Set static WEP keys again */
1805 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1806 }
1807
1808#ifdef CONFIG_IBSS_RSN
1809 if (wpa_s->current_ssid &&
1810 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1811 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1812 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1813 wpa_s->ibss_rsn == NULL) {
1814 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1815 if (!wpa_s->ibss_rsn) {
1816 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1817 wpa_supplicant_deauthenticate(
1818 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1819 return;
1820 }
1821
1822 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1823 }
1824#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001825
1826 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827}
1828
1829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001830static int disconnect_reason_recoverable(u16 reason_code)
1831{
1832 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1833 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1834 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1835}
1836
1837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001839 u16 reason_code,
1840 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841{
1842 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001843
1844 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1845 /*
1846 * At least Host AP driver and a Prism3 card seemed to be
1847 * generating streams of disconnected events when configuring
1848 * IBSS for WPA-None. Ignore them for now.
1849 */
1850 return;
1851 }
1852
1853 bssid = wpa_s->bssid;
1854 if (is_zero_ether_addr(bssid))
1855 bssid = wpa_s->pending_bssid;
1856
1857 if (!is_zero_ether_addr(bssid) ||
1858 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1859 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1860 " reason=%d%s",
1861 MAC2STR(bssid), reason_code,
1862 locally_generated ? " locally_generated=1" : "");
1863 }
1864}
1865
1866
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001867static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1868 int locally_generated)
1869{
1870 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1871 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1872 return 0; /* Not in 4-way handshake with PSK */
1873
1874 /*
1875 * It looks like connection was lost while trying to go through PSK
1876 * 4-way handshake. Filter out known disconnection cases that are caused
1877 * by something else than PSK mismatch to avoid confusing reports.
1878 */
1879
1880 if (locally_generated) {
1881 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1882 return 0;
1883 }
1884
1885 return 1;
1886}
1887
1888
Jouni Malinen2b89da82012-08-31 22:04:41 +03001889static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1890 u16 reason_code,
1891 int locally_generated)
1892{
1893 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 int authenticating;
1895 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001896 struct wpa_bss *fast_reconnect = NULL;
1897 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001898 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899
1900 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1901 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1902
1903 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1904 /*
1905 * At least Host AP driver and a Prism3 card seemed to be
1906 * generating streams of disconnected events when configuring
1907 * IBSS for WPA-None. Ignore them for now.
1908 */
1909 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1910 "IBSS/WPA-None mode");
1911 return;
1912 }
1913
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001914 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1916 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001917 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 }
1919 if (!wpa_s->auto_reconnect_disabled ||
1920 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001921 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1922 "reconnect (wps=%d wpa_state=%d)",
1923 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1924 wpa_s->wpa_state);
1925 if (wpa_s->wpa_state == WPA_COMPLETED &&
1926 wpa_s->current_ssid &&
1927 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001928 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 disconnect_reason_recoverable(reason_code)) {
1930 /*
1931 * It looks like the AP has dropped association with
1932 * us, but could allow us to get back in. Try to
1933 * reconnect to the same BSS without full scan to save
1934 * time for some common cases.
1935 */
1936 fast_reconnect = wpa_s->current_bss;
1937 fast_reconnect_ssid = wpa_s->current_ssid;
1938 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001939#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001940 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001941#else
1942 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1943#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001944 else
1945 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1946 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001948 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 "try to re-connect");
1950 wpa_s->reassociate = 0;
1951 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001952 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 }
1954 bssid = wpa_s->bssid;
1955 if (is_zero_ether_addr(bssid))
1956 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001957 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1958 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001960 if (locally_generated)
1961 wpa_s->disconnect_reason = -reason_code;
1962 else
1963 wpa_s->disconnect_reason = reason_code;
1964 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1966 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1967 wpa_s->keys_cleared = 0;
1968 wpa_clear_keys(wpa_s, wpa_s->bssid);
1969 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001970 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 wpa_supplicant_mark_disassoc(wpa_s);
1972
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001973 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001975 wpa_s->current_ssid = last_ssid;
1976 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001977
1978 if (fast_reconnect) {
1979#ifndef CONFIG_NO_SCAN_PROCESSING
1980 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1981 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1982 fast_reconnect_ssid) < 0) {
1983 /* Recover through full scan */
1984 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1985 }
1986#endif /* CONFIG_NO_SCAN_PROCESSING */
1987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988}
1989
1990
1991#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001992void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993{
1994 struct wpa_supplicant *wpa_s = eloop_ctx;
1995
1996 if (!wpa_s->pending_mic_error_report)
1997 return;
1998
1999 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2000 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2001 wpa_s->pending_mic_error_report = 0;
2002}
2003#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2004
2005
2006static void
2007wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2008 union wpa_event_data *data)
2009{
2010 int pairwise;
2011 struct os_time t;
2012
2013 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2014 pairwise = (data && data->michael_mic_failure.unicast);
2015 os_get_time(&t);
2016 if ((wpa_s->last_michael_mic_error &&
2017 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2018 wpa_s->pending_mic_error_report) {
2019 if (wpa_s->pending_mic_error_report) {
2020 /*
2021 * Send the pending MIC error report immediately since
2022 * we are going to start countermeasures and AP better
2023 * do the same.
2024 */
2025 wpa_sm_key_request(wpa_s->wpa, 1,
2026 wpa_s->pending_mic_error_pairwise);
2027 }
2028
2029 /* Send the new MIC error report immediately since we are going
2030 * to start countermeasures and AP better do the same.
2031 */
2032 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2033
2034 /* initialize countermeasures */
2035 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002036
2037 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2040
2041 /*
2042 * Need to wait for completion of request frame. We do not get
2043 * any callback for the message completion, so just wait a
2044 * short while and hope for the best. */
2045 os_sleep(0, 10000);
2046
2047 wpa_drv_set_countermeasures(wpa_s, 1);
2048 wpa_supplicant_deauthenticate(wpa_s,
2049 WLAN_REASON_MICHAEL_MIC_FAILURE);
2050 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2051 wpa_s, NULL);
2052 eloop_register_timeout(60, 0,
2053 wpa_supplicant_stop_countermeasures,
2054 wpa_s, NULL);
2055 /* TODO: mark the AP rejected for 60 second. STA is
2056 * allowed to associate with another AP.. */
2057 } else {
2058#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2059 if (wpa_s->mic_errors_seen) {
2060 /*
2061 * Reduce the effectiveness of Michael MIC error
2062 * reports as a means for attacking against TKIP if
2063 * more than one MIC failure is noticed with the same
2064 * PTK. We delay the transmission of the reports by a
2065 * random time between 0 and 60 seconds in order to
2066 * force the attacker wait 60 seconds before getting
2067 * the information on whether a frame resulted in a MIC
2068 * failure.
2069 */
2070 u8 rval[4];
2071 int sec;
2072
2073 if (os_get_random(rval, sizeof(rval)) < 0)
2074 sec = os_random() % 60;
2075 else
2076 sec = WPA_GET_BE32(rval) % 60;
2077 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2078 "report %d seconds", sec);
2079 wpa_s->pending_mic_error_report = 1;
2080 wpa_s->pending_mic_error_pairwise = pairwise;
2081 eloop_cancel_timeout(
2082 wpa_supplicant_delayed_mic_error_report,
2083 wpa_s, NULL);
2084 eloop_register_timeout(
2085 sec, os_random() % 1000000,
2086 wpa_supplicant_delayed_mic_error_report,
2087 wpa_s, NULL);
2088 } else {
2089 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2090 }
2091#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2092 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2093#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2094 }
2095 wpa_s->last_michael_mic_error = t.sec;
2096 wpa_s->mic_errors_seen++;
2097}
2098
2099
2100#ifdef CONFIG_TERMINATE_ONLASTIF
2101static int any_interfaces(struct wpa_supplicant *head)
2102{
2103 struct wpa_supplicant *wpa_s;
2104
2105 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2106 if (!wpa_s->interface_removed)
2107 return 1;
2108 return 0;
2109}
2110#endif /* CONFIG_TERMINATE_ONLASTIF */
2111
2112
2113static void
2114wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2115 union wpa_event_data *data)
2116{
2117 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2118 return;
2119
2120 switch (data->interface_status.ievent) {
2121 case EVENT_INTERFACE_ADDED:
2122 if (!wpa_s->interface_removed)
2123 break;
2124 wpa_s->interface_removed = 0;
2125 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2126 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2127 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2128 "driver after interface was added");
2129 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002130 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 break;
2132 case EVENT_INTERFACE_REMOVED:
2133 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2134 wpa_s->interface_removed = 1;
2135 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002136 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 l2_packet_deinit(wpa_s->l2);
2138 wpa_s->l2 = NULL;
2139#ifdef CONFIG_IBSS_RSN
2140 ibss_rsn_deinit(wpa_s->ibss_rsn);
2141 wpa_s->ibss_rsn = NULL;
2142#endif /* CONFIG_IBSS_RSN */
2143#ifdef CONFIG_TERMINATE_ONLASTIF
2144 /* check if last interface */
2145 if (!any_interfaces(wpa_s->global->ifaces))
2146 eloop_terminate();
2147#endif /* CONFIG_TERMINATE_ONLASTIF */
2148 break;
2149 }
2150}
2151
2152
2153#ifdef CONFIG_PEERKEY
2154static void
2155wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2156 union wpa_event_data *data)
2157{
2158 if (data == NULL)
2159 return;
2160 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2161}
2162#endif /* CONFIG_PEERKEY */
2163
2164
2165#ifdef CONFIG_TDLS
2166static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2167 union wpa_event_data *data)
2168{
2169 if (data == NULL)
2170 return;
2171 switch (data->tdls.oper) {
2172 case TDLS_REQUEST_SETUP:
2173 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2174 break;
2175 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002176 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2177 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 break;
2179 }
2180}
2181#endif /* CONFIG_TDLS */
2182
2183
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002184#ifdef CONFIG_IEEE80211V
2185static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2186 union wpa_event_data *data)
2187{
2188 if (data == NULL)
2189 return;
2190 switch (data->wnm.oper) {
2191 case WNM_OPER_SLEEP:
2192 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2193 "(action=%d, intval=%d)",
2194 data->wnm.sleep_action, data->wnm.sleep_intval);
2195 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2196 data->wnm.sleep_intval);
2197 break;
2198 }
2199}
2200#endif /* CONFIG_IEEE80211V */
2201
2202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203#ifdef CONFIG_IEEE80211R
2204static void
2205wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2206 union wpa_event_data *data)
2207{
2208 if (data == NULL)
2209 return;
2210
2211 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2212 data->ft_ies.ies_len,
2213 data->ft_ies.ft_action,
2214 data->ft_ies.target_ap,
2215 data->ft_ies.ric_ies,
2216 data->ft_ies.ric_ies_len) < 0) {
2217 /* TODO: prevent MLME/driver from trying to associate? */
2218 }
2219}
2220#endif /* CONFIG_IEEE80211R */
2221
2222
2223#ifdef CONFIG_IBSS_RSN
2224static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2225 union wpa_event_data *data)
2226{
2227 struct wpa_ssid *ssid;
2228 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2229 return;
2230 if (data == NULL)
2231 return;
2232 ssid = wpa_s->current_ssid;
2233 if (ssid == NULL)
2234 return;
2235 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2236 return;
2237
2238 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2239}
2240#endif /* CONFIG_IBSS_RSN */
2241
2242
2243#ifdef CONFIG_IEEE80211R
2244static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2245 size_t len)
2246{
2247 const u8 *sta_addr, *target_ap_addr;
2248 u16 status;
2249
2250 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2251 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2252 return; /* only SME case supported for now */
2253 if (len < 1 + 2 * ETH_ALEN + 2)
2254 return;
2255 if (data[0] != 2)
2256 return; /* Only FT Action Response is supported for now */
2257 sta_addr = data + 1;
2258 target_ap_addr = data + 1 + ETH_ALEN;
2259 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2260 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2261 MACSTR " TargetAP " MACSTR " status %u",
2262 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2263
2264 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2265 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2266 " in FT Action Response", MAC2STR(sta_addr));
2267 return;
2268 }
2269
2270 if (status) {
2271 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2272 "failure (status code %d)", status);
2273 /* TODO: report error to FT code(?) */
2274 return;
2275 }
2276
2277 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2278 len - (1 + 2 * ETH_ALEN + 2), 1,
2279 target_ap_addr, NULL, 0) < 0)
2280 return;
2281
2282#ifdef CONFIG_SME
2283 {
2284 struct wpa_bss *bss;
2285 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2286 if (bss)
2287 wpa_s->sme.freq = bss->freq;
2288 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2289 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2290 WLAN_AUTH_FT);
2291 }
2292#endif /* CONFIG_SME */
2293}
2294#endif /* CONFIG_IEEE80211R */
2295
2296
2297static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2298 struct unprot_deauth *e)
2299{
2300#ifdef CONFIG_IEEE80211W
2301 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2302 "dropped: " MACSTR " -> " MACSTR
2303 " (reason code %u)",
2304 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2305 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2306#endif /* CONFIG_IEEE80211W */
2307}
2308
2309
2310static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2311 struct unprot_disassoc *e)
2312{
2313#ifdef CONFIG_IEEE80211W
2314 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2315 "dropped: " MACSTR " -> " MACSTR
2316 " (reason code %u)",
2317 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2318 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2319#endif /* CONFIG_IEEE80211W */
2320}
2321
2322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002323static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2324{
2325 u8 action, mode;
2326 const u8 *pos, *end;
2327
2328 if (rx->data == NULL || rx->len == 0)
2329 return;
2330
2331 pos = rx->data;
2332 end = pos + rx->len;
2333 action = *pos++;
2334
2335 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2336 action, MAC2STR(rx->sa));
2337 switch (action) {
2338 case WNM_BSS_TRANS_MGMT_REQ:
2339 if (pos + 5 > end)
2340 break;
2341 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2342 "Request: dialog_token=%u request_mode=0x%x "
2343 "disassoc_timer=%u validity_interval=%u",
2344 pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2345 mode = pos[1];
2346 pos += 5;
2347 if (mode & 0x08)
2348 pos += 12; /* BSS Termination Duration */
2349 if (mode & 0x10) {
2350 char url[256];
2351 if (pos + 1 > end || pos + 1 + pos[0] > end) {
2352 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2353 "Transition Management Request "
2354 "(URL)");
2355 break;
2356 }
2357 os_memcpy(url, pos + 1, pos[0]);
2358 url[pos[0]] = '\0';
2359 wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2360 "Imminent - session_info_url=%s", url);
2361 }
2362 break;
2363 }
2364}
2365
2366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2368 union wpa_event_data *data)
2369{
2370 struct wpa_supplicant *wpa_s = ctx;
2371 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002372 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373
2374 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2375 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 event != EVENT_INTERFACE_STATUS &&
2377 event != EVENT_SCHED_SCAN_STOPPED) {
2378 wpa_dbg(wpa_s, MSG_DEBUG,
2379 "Ignore event %s (%d) while interface is disabled",
2380 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 return;
2382 }
2383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002384#ifndef CONFIG_NO_STDOUT_DEBUG
2385{
2386 int level = MSG_DEBUG;
2387
Dmitry Shmidt04949592012-07-19 12:16:46 -07002388 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002389 const struct ieee80211_hdr *hdr;
2390 u16 fc;
2391 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2392 fc = le_to_host16(hdr->frame_control);
2393 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2394 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2395 level = MSG_EXCESSIVE;
2396 }
2397
2398 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2399 event_to_string(event), event);
2400}
2401#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402
2403 switch (event) {
2404 case EVENT_AUTH:
2405 sme_event_auth(wpa_s, data);
2406 break;
2407 case EVENT_ASSOC:
2408 wpa_supplicant_event_assoc(wpa_s, data);
2409 break;
2410 case EVENT_DISASSOC:
2411 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2412 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002413 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2414 data->disassoc_info.reason_code,
2415 data->disassoc_info.locally_generated ?
2416 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 if (data->disassoc_info.addr)
2418 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2419 MAC2STR(data->disassoc_info.addr));
2420 }
2421#ifdef CONFIG_AP
2422 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2423 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2424 data->disassoc_info.addr);
2425 break;
2426 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427 if (wpa_s->ap_iface) {
2428 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2429 "AP mode");
2430 break;
2431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432#endif /* CONFIG_AP */
2433 if (data) {
2434 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002435 locally_generated =
2436 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2438 data->disassoc_info.ie,
2439 data->disassoc_info.ie_len);
2440#ifdef CONFIG_P2P
2441 wpas_p2p_disassoc_notif(
2442 wpa_s, data->disassoc_info.addr, reason_code,
2443 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002444 data->disassoc_info.ie_len,
2445 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446#endif /* CONFIG_P2P */
2447 }
2448 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2449 sme_event_disassoc(wpa_s, data);
2450 /* fall through */
2451 case EVENT_DEAUTH:
2452 if (event == EVENT_DEAUTH) {
2453 wpa_dbg(wpa_s, MSG_DEBUG,
2454 "Deauthentication notification");
2455 if (data) {
2456 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002457 locally_generated =
2458 data->deauth_info.locally_generated;
2459 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2460 data->deauth_info.reason_code,
2461 data->deauth_info.locally_generated ?
2462 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 if (data->deauth_info.addr) {
2464 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2465 MACSTR,
2466 MAC2STR(data->deauth_info.
2467 addr));
2468 }
2469 wpa_hexdump(MSG_DEBUG,
2470 "Deauthentication frame IE(s)",
2471 data->deauth_info.ie,
2472 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 }
2474 }
2475#ifdef CONFIG_AP
2476 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2477 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2478 data->deauth_info.addr);
2479 break;
2480 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002481 if (wpa_s->ap_iface) {
2482 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2483 "AP mode");
2484 break;
2485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002487 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2488 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002489 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2490 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2491 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2492 eapol_sm_failed(wpa_s->eapol)))
2493 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002494#ifdef CONFIG_P2P
2495 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002496 if (wpas_p2p_deauth_notif(wpa_s,
2497 data->deauth_info.addr,
2498 reason_code,
2499 data->deauth_info.ie,
2500 data->deauth_info.ie_len,
2501 locally_generated) > 0) {
2502 /*
2503 * The interface was removed, so cannot
2504 * continue processing any additional
2505 * operations after this.
2506 */
2507 break;
2508 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002509 }
2510#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002511 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2512 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 break;
2514 case EVENT_MICHAEL_MIC_FAILURE:
2515 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2516 break;
2517#ifndef CONFIG_NO_SCAN_PROCESSING
2518 case EVENT_SCAN_RESULTS:
2519 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002520#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07002521 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002522 wpa_s->global->p2p != NULL &&
2523 wpa_s->wpa_state != WPA_AUTHENTICATING &&
2524 wpa_s->wpa_state != WPA_ASSOCIATING) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07002525 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002526 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2527 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2528 "continued after scan result processing");
2529 }
2530 }
2531#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 break;
2533#endif /* CONFIG_NO_SCAN_PROCESSING */
2534 case EVENT_ASSOCINFO:
2535 wpa_supplicant_event_associnfo(wpa_s, data);
2536 break;
2537 case EVENT_INTERFACE_STATUS:
2538 wpa_supplicant_event_interface_status(wpa_s, data);
2539 break;
2540 case EVENT_PMKID_CANDIDATE:
2541 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2542 break;
2543#ifdef CONFIG_PEERKEY
2544 case EVENT_STKSTART:
2545 wpa_supplicant_event_stkstart(wpa_s, data);
2546 break;
2547#endif /* CONFIG_PEERKEY */
2548#ifdef CONFIG_TDLS
2549 case EVENT_TDLS:
2550 wpa_supplicant_event_tdls(wpa_s, data);
2551 break;
2552#endif /* CONFIG_TDLS */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002553#ifdef CONFIG_IEEE80211V
2554 case EVENT_WNM:
2555 wpa_supplicant_event_wnm(wpa_s, data);
2556 break;
2557#endif /* CONFIG_IEEE80211V */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558#ifdef CONFIG_IEEE80211R
2559 case EVENT_FT_RESPONSE:
2560 wpa_supplicant_event_ft_response(wpa_s, data);
2561 break;
2562#endif /* CONFIG_IEEE80211R */
2563#ifdef CONFIG_IBSS_RSN
2564 case EVENT_IBSS_RSN_START:
2565 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2566 break;
2567#endif /* CONFIG_IBSS_RSN */
2568 case EVENT_ASSOC_REJECT:
2569 if (data->assoc_reject.bssid)
2570 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2571 "bssid=" MACSTR " status_code=%u",
2572 MAC2STR(data->assoc_reject.bssid),
2573 data->assoc_reject.status_code);
2574 else
2575 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2576 "status_code=%u",
2577 data->assoc_reject.status_code);
2578 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2579 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002580 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002581#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002582 if(!wpa_s->current_ssid) {
2583 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2584 break;
2585 }
2586 /* If assoc reject is reported by the driver, then avoid
2587 * waiting for the authentication timeout. Cancel the
2588 * authentication timeout and retry the assoc.
2589 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002590 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002591 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2592 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002593
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002594 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002595
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002596 /* Clear the states */
2597 wpa_sm_notify_disassoc(wpa_s->wpa);
2598 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2599
2600 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002601 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002602 const u8 *bl_bssid = data->assoc_reject.bssid;
2603 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2604 bl_bssid = wpa_s->pending_bssid;
2605 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002606 wpa_supplicant_req_scan(wpa_s, 0, 0);
2607 } else {
2608 wpa_supplicant_req_scan(wpa_s, 1, 0);
2609 }
2610 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002611 /* If we ASSOC_REJECT's hits threshold, disable the
2612 * network
2613 */
2614 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2615 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002616 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002617 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002618 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002619 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002620#else
2621 const u8 *bssid = data->assoc_reject.bssid;
2622 if (bssid == NULL || is_zero_ether_addr(bssid))
2623 bssid = wpa_s->pending_bssid;
2624 wpas_connection_failed(wpa_s, bssid);
2625 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002626#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 break;
2629 case EVENT_AUTH_TIMED_OUT:
2630 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2631 sme_event_auth_timed_out(wpa_s, data);
2632 break;
2633 case EVENT_ASSOC_TIMED_OUT:
2634 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2635 sme_event_assoc_timed_out(wpa_s, data);
2636 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 case EVENT_TX_STATUS:
2638 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2639 " type=%d stype=%d",
2640 MAC2STR(data->tx_status.dst),
2641 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002644#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2646 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002647 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 wpa_s, data->tx_status.dst,
2649 data->tx_status.data,
2650 data->tx_status.data_len,
2651 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652 OFFCHANNEL_SEND_ACTION_SUCCESS :
2653 OFFCHANNEL_SEND_ACTION_NO_ACK);
2654#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655 break;
2656 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002657#endif /* CONFIG_AP */
2658#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2660 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2661 /*
2662 * Catch TX status events for Action frames we sent via group
2663 * interface in GO mode.
2664 */
2665 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2666 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2667 os_memcmp(wpa_s->parent->pending_action_dst,
2668 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 wpa_s->parent, data->tx_status.dst,
2671 data->tx_status.data,
2672 data->tx_status.data_len,
2673 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002674 OFFCHANNEL_SEND_ACTION_SUCCESS :
2675 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 break;
2677 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002678#endif /* CONFIG_OFFCHANNEL */
2679#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 switch (data->tx_status.type) {
2681 case WLAN_FC_TYPE_MGMT:
2682 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2683 data->tx_status.data_len,
2684 data->tx_status.stype,
2685 data->tx_status.ack);
2686 break;
2687 case WLAN_FC_TYPE_DATA:
2688 ap_tx_status(wpa_s, data->tx_status.dst,
2689 data->tx_status.data,
2690 data->tx_status.data_len,
2691 data->tx_status.ack);
2692 break;
2693 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002694#endif /* CONFIG_AP */
2695 break;
2696#ifdef CONFIG_AP
2697 case EVENT_EAPOL_TX_STATUS:
2698 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2699 data->eapol_tx_status.data,
2700 data->eapol_tx_status.data_len,
2701 data->eapol_tx_status.ack);
2702 break;
2703 case EVENT_DRIVER_CLIENT_POLL_OK:
2704 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 break;
2706 case EVENT_RX_FROM_UNKNOWN:
2707 if (wpa_s->ap_iface == NULL)
2708 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002709 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2710 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712 case EVENT_CH_SWITCH:
2713 if (!data)
2714 break;
2715 if (!wpa_s->ap_iface) {
2716 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2717 "event in non-AP mode");
2718 break;
2719 }
2720
2721#ifdef CONFIG_AP
2722 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2723 data->ch_switch.ht_enabled,
2724 data->ch_switch.ch_offset);
2725#endif /* CONFIG_AP */
2726 break;
2727 case EVENT_RX_MGMT: {
2728 u16 fc, stype;
2729 const struct ieee80211_mgmt *mgmt;
2730
2731 mgmt = (const struct ieee80211_mgmt *)
2732 data->rx_mgmt.frame;
2733 fc = le_to_host16(mgmt->frame_control);
2734 stype = WLAN_FC_GET_STYPE(fc);
2735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736 if (wpa_s->ap_iface == NULL) {
2737#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2739 data->rx_mgmt.frame_len > 24) {
2740 const u8 *src = mgmt->sa;
2741 const u8 *ie = mgmt->u.probe_req.variable;
2742 size_t ie_len = data->rx_mgmt.frame_len -
2743 (mgmt->u.probe_req.variable -
2744 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002745 wpas_p2p_probe_req_rx(
2746 wpa_s, src, mgmt->da,
2747 mgmt->bssid, ie, ie_len,
2748 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 break;
2750 }
2751#endif /* CONFIG_P2P */
2752 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2753 "management frame in non-AP mode");
2754 break;
2755 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002756
2757 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2758 data->rx_mgmt.frame_len > 24) {
2759 const u8 *ie = mgmt->u.probe_req.variable;
2760 size_t ie_len = data->rx_mgmt.frame_len -
2761 (mgmt->u.probe_req.variable -
2762 data->rx_mgmt.frame);
2763
2764 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2765 mgmt->bssid, ie, ie_len,
2766 data->rx_mgmt.ssi_signal);
2767 }
2768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2770 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772#endif /* CONFIG_AP */
2773 case EVENT_RX_ACTION:
2774 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2775 " Category=%u DataLen=%d freq=%d MHz",
2776 MAC2STR(data->rx_action.sa),
2777 data->rx_action.category, (int) data->rx_action.len,
2778 data->rx_action.freq);
2779#ifdef CONFIG_IEEE80211R
2780 if (data->rx_action.category == WLAN_ACTION_FT) {
2781 ft_rx_action(wpa_s, data->rx_action.data,
2782 data->rx_action.len);
2783 break;
2784 }
2785#endif /* CONFIG_IEEE80211R */
2786#ifdef CONFIG_IEEE80211W
2787#ifdef CONFIG_SME
2788 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2789 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2790 data->rx_action.data,
2791 data->rx_action.len);
2792 break;
2793 }
2794#endif /* CONFIG_SME */
2795#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002796#ifdef CONFIG_IEEE80211V
2797 if (data->rx_action.category == WLAN_ACTION_WNM) {
2798 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2799 break;
2800 }
2801#endif /* CONFIG_IEEE80211V */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802#ifdef CONFIG_GAS
2803 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2804 gas_query_rx(wpa_s->gas, data->rx_action.da,
2805 data->rx_action.sa, data->rx_action.bssid,
2806 data->rx_action.data, data->rx_action.len,
2807 data->rx_action.freq) == 0)
2808 break;
2809#endif /* CONFIG_GAS */
2810 if (data->rx_action.category == WLAN_ACTION_WNM) {
2811 wnm_action_rx(wpa_s, &data->rx_action);
2812 break;
2813 }
2814#ifdef CONFIG_TDLS
2815 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2816 data->rx_action.len >= 4 &&
2817 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2818 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2819 "Response from " MACSTR,
2820 MAC2STR(data->rx_action.sa));
2821 break;
2822 }
2823#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824#ifdef CONFIG_P2P
2825 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2826 data->rx_action.sa,
2827 data->rx_action.bssid,
2828 data->rx_action.category,
2829 data->rx_action.data,
2830 data->rx_action.len, data->rx_action.freq);
2831#endif /* CONFIG_P2P */
2832 break;
2833 case EVENT_RX_PROBE_REQ:
2834 if (data->rx_probe_req.sa == NULL ||
2835 data->rx_probe_req.ie == NULL)
2836 break;
2837#ifdef CONFIG_AP
2838 if (wpa_s->ap_iface) {
2839 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2840 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002841 data->rx_probe_req.da,
2842 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002844 data->rx_probe_req.ie_len,
2845 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846 break;
2847 }
2848#endif /* CONFIG_AP */
2849#ifdef CONFIG_P2P
2850 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002851 data->rx_probe_req.da,
2852 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002854 data->rx_probe_req.ie_len,
2855 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856#endif /* CONFIG_P2P */
2857 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859#ifdef CONFIG_OFFCHANNEL
2860 offchannel_remain_on_channel_cb(
2861 wpa_s, data->remain_on_channel.freq,
2862 data->remain_on_channel.duration);
2863#endif /* CONFIG_OFFCHANNEL */
2864#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 wpas_p2p_remain_on_channel_cb(
2866 wpa_s, data->remain_on_channel.freq,
2867 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 break;
2870 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002871#ifdef CONFIG_OFFCHANNEL
2872 offchannel_cancel_remain_on_channel_cb(
2873 wpa_s, data->remain_on_channel.freq);
2874#endif /* CONFIG_OFFCHANNEL */
2875#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 wpas_p2p_cancel_remain_on_channel_cb(
2877 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002878#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 case EVENT_P2P_DEV_FOUND: {
2882 struct p2p_peer_info peer_info;
2883
2884 os_memset(&peer_info, 0, sizeof(peer_info));
2885 if (data->p2p_dev_found.dev_addr)
2886 os_memcpy(peer_info.p2p_device_addr,
2887 data->p2p_dev_found.dev_addr, ETH_ALEN);
2888 if (data->p2p_dev_found.pri_dev_type)
2889 os_memcpy(peer_info.pri_dev_type,
2890 data->p2p_dev_found.pri_dev_type,
2891 sizeof(peer_info.pri_dev_type));
2892 if (data->p2p_dev_found.dev_name)
2893 os_strlcpy(peer_info.device_name,
2894 data->p2p_dev_found.dev_name,
2895 sizeof(peer_info.device_name));
2896 peer_info.config_methods = data->p2p_dev_found.config_methods;
2897 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2898 peer_info.group_capab = data->p2p_dev_found.group_capab;
2899
2900 /*
2901 * FIX: new_device=1 is not necessarily correct. We should
2902 * maintain a P2P peer database in wpa_supplicant and update
2903 * this information based on whether the peer is truly new.
2904 */
2905 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2906 break;
2907 }
2908 case EVENT_P2P_GO_NEG_REQ_RX:
2909 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2910 data->p2p_go_neg_req_rx.dev_passwd_id);
2911 break;
2912 case EVENT_P2P_GO_NEG_COMPLETED:
2913 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2914 break;
2915 case EVENT_P2P_PROV_DISC_REQUEST:
2916 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2917 data->p2p_prov_disc_req.config_methods,
2918 data->p2p_prov_disc_req.dev_addr,
2919 data->p2p_prov_disc_req.pri_dev_type,
2920 data->p2p_prov_disc_req.dev_name,
2921 data->p2p_prov_disc_req.supp_config_methods,
2922 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002923 data->p2p_prov_disc_req.group_capab,
2924 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 break;
2926 case EVENT_P2P_PROV_DISC_RESPONSE:
2927 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2928 data->p2p_prov_disc_resp.config_methods);
2929 break;
2930 case EVENT_P2P_SD_REQUEST:
2931 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2932 data->p2p_sd_req.sa,
2933 data->p2p_sd_req.dialog_token,
2934 data->p2p_sd_req.update_indic,
2935 data->p2p_sd_req.tlvs,
2936 data->p2p_sd_req.tlvs_len);
2937 break;
2938 case EVENT_P2P_SD_RESPONSE:
2939 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2940 data->p2p_sd_resp.update_indic,
2941 data->p2p_sd_resp.tlvs,
2942 data->p2p_sd_resp.tlvs_len);
2943 break;
2944#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945 case EVENT_EAPOL_RX:
2946 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2947 data->eapol_rx.data,
2948 data->eapol_rx.data_len);
2949 break;
2950 case EVENT_SIGNAL_CHANGE:
2951 bgscan_notify_signal_change(
2952 wpa_s, data->signal_change.above_threshold,
2953 data->signal_change.current_signal,
2954 data->signal_change.current_noise,
2955 data->signal_change.current_txrate);
2956 break;
2957 case EVENT_INTERFACE_ENABLED:
2958 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2959 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002960 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961#ifdef CONFIG_AP
2962 if (!wpa_s->ap_iface) {
2963 wpa_supplicant_set_state(wpa_s,
2964 WPA_DISCONNECTED);
2965 wpa_supplicant_req_scan(wpa_s, 0, 0);
2966 } else
2967 wpa_supplicant_set_state(wpa_s,
2968 WPA_COMPLETED);
2969#else /* CONFIG_AP */
2970 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2971 wpa_supplicant_req_scan(wpa_s, 0, 0);
2972#endif /* CONFIG_AP */
2973 }
2974 break;
2975 case EVENT_INTERFACE_DISABLED:
2976 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2977 wpa_supplicant_mark_disassoc(wpa_s);
2978 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2979 break;
2980 case EVENT_CHANNEL_LIST_CHANGED:
2981 if (wpa_s->drv_priv == NULL)
2982 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983
2984 free_hw_features(wpa_s);
2985 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2986 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988#ifdef CONFIG_P2P
2989 wpas_p2p_update_channel_list(wpa_s);
2990#endif /* CONFIG_P2P */
2991 break;
2992 case EVENT_INTERFACE_UNAVAILABLE:
2993#ifdef CONFIG_P2P
2994 wpas_p2p_interface_unavailable(wpa_s);
2995#endif /* CONFIG_P2P */
2996 break;
2997 case EVENT_BEST_CHANNEL:
2998 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2999 "(%d %d %d)",
3000 data->best_chan.freq_24, data->best_chan.freq_5,
3001 data->best_chan.freq_overall);
3002 wpa_s->best_24_freq = data->best_chan.freq_24;
3003 wpa_s->best_5_freq = data->best_chan.freq_5;
3004 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3005#ifdef CONFIG_P2P
3006 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3007 data->best_chan.freq_5,
3008 data->best_chan.freq_overall);
3009#endif /* CONFIG_P2P */
3010 break;
3011 case EVENT_UNPROT_DEAUTH:
3012 wpa_supplicant_event_unprot_deauth(wpa_s,
3013 &data->unprot_deauth);
3014 break;
3015 case EVENT_UNPROT_DISASSOC:
3016 wpa_supplicant_event_unprot_disassoc(wpa_s,
3017 &data->unprot_disassoc);
3018 break;
3019 case EVENT_STATION_LOW_ACK:
3020#ifdef CONFIG_AP
3021 if (wpa_s->ap_iface && data)
3022 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3023 data->low_ack.addr);
3024#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025#ifdef CONFIG_TDLS
3026 if (data)
3027 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3028#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 break;
3030 case EVENT_IBSS_PEER_LOST:
3031#ifdef CONFIG_IBSS_RSN
3032 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3033#endif /* CONFIG_IBSS_RSN */
3034 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003035 case EVENT_DRIVER_GTK_REKEY:
3036 if (os_memcmp(data->driver_gtk_rekey.bssid,
3037 wpa_s->bssid, ETH_ALEN))
3038 break;
3039 if (!wpa_s->wpa)
3040 break;
3041 wpa_sm_update_replay_ctr(wpa_s->wpa,
3042 data->driver_gtk_rekey.replay_ctr);
3043 break;
3044 case EVENT_SCHED_SCAN_STOPPED:
3045 wpa_s->sched_scanning = 0;
3046 wpa_supplicant_notify_scanning(wpa_s, 0);
3047
3048 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3049 break;
3050
3051 /*
3052 * If we timed out, start a new sched scan to continue
3053 * searching for more SSIDs.
3054 */
3055 if (wpa_s->sched_scan_timed_out)
3056 wpa_supplicant_req_sched_scan(wpa_s);
3057 break;
3058 case EVENT_WPS_BUTTON_PUSHED:
3059#ifdef CONFIG_WPS
3060 wpas_wps_start_pbc(wpa_s, NULL, 0);
3061#endif /* CONFIG_WPS */
3062 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 default:
3064 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3065 break;
3066 }
3067}