blob: 2b62e6b62e6c3dee90b6b0b4143424e8ad372c74 [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);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800105 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
106 wpa_ie, &wpa_ie_len) < 0)
107 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 } else {
109 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
110 }
111
112 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
113 eapol_sm_invalidate_cached_session(wpa_s->eapol);
114 old_ssid = wpa_s->current_ssid;
115 wpa_s->current_ssid = ssid;
116 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
117 wpa_supplicant_initiate_eapol(wpa_s);
118 if (old_ssid != wpa_s->current_ssid)
119 wpas_notify_network_changed(wpa_s);
120
121 return 0;
122}
123
124
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800125void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126{
127 struct wpa_supplicant *wpa_s = eloop_ctx;
128
129 if (wpa_s->countermeasures) {
130 wpa_s->countermeasures = 0;
131 wpa_drv_set_countermeasures(wpa_s, 0);
132 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
133 wpa_supplicant_req_scan(wpa_s, 0, 0);
134 }
135}
136
137
138void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
139{
140 int bssid_changed;
141
Dmitry Shmidt04949592012-07-19 12:16:46 -0700142 wnm_bss_keep_alive_deinit(wpa_s);
143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144#ifdef CONFIG_IBSS_RSN
145 ibss_rsn_deinit(wpa_s->ibss_rsn);
146 wpa_s->ibss_rsn = NULL;
147#endif /* CONFIG_IBSS_RSN */
148
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700149#ifdef CONFIG_AP
150 wpa_supplicant_ap_deinit(wpa_s);
151#endif /* CONFIG_AP */
152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
154 return;
155
156 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800157#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700158 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800159#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
161 os_memset(wpa_s->bssid, 0, ETH_ALEN);
162 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700163#ifdef CONFIG_SME
164 wpa_s->sme.prev_bssid_set = 0;
165#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800166#ifdef CONFIG_P2P
167 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
168#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 wpa_s->current_bss = NULL;
170 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700171#ifdef CONFIG_IEEE80211R
172#ifdef CONFIG_SME
173 if (wpa_s->sme.ft_ies)
174 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
175#endif /* CONFIG_SME */
176#endif /* CONFIG_IEEE80211R */
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 if (bssid_changed)
179 wpas_notify_bssid_changed(wpa_s);
180
181 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
182 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
183 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
184 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
185 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700186 wpa_s->current_ssid = NULL;
187 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188}
189
190
191static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
192{
193 struct wpa_ie_data ie;
194 int pmksa_set = -1;
195 size_t i;
196
197 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
198 ie.pmkid == NULL)
199 return;
200
201 for (i = 0; i < ie.num_pmkid; i++) {
202 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
203 ie.pmkid + i * PMKID_LEN,
204 NULL, NULL, 0);
205 if (pmksa_set == 0) {
206 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
207 break;
208 }
209 }
210
211 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
212 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
213}
214
215
216static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
217 union wpa_event_data *data)
218{
219 if (data == NULL) {
220 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
221 "event");
222 return;
223 }
224 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
225 " index=%d preauth=%d",
226 MAC2STR(data->pmkid_candidate.bssid),
227 data->pmkid_candidate.index,
228 data->pmkid_candidate.preauth);
229
230 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
231 data->pmkid_candidate.index,
232 data->pmkid_candidate.preauth);
233}
234
235
236static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
237{
238 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
239 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
240 return 0;
241
242#ifdef IEEE8021X_EAPOL
243 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
244 wpa_s->current_ssid &&
245 !(wpa_s->current_ssid->eapol_flags &
246 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
247 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
248 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
249 * plaintext or static WEP keys). */
250 return 0;
251 }
252#endif /* IEEE8021X_EAPOL */
253
254 return 1;
255}
256
257
258/**
259 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
260 * @wpa_s: pointer to wpa_supplicant data
261 * @ssid: Configuration data for the network
262 * Returns: 0 on success, -1 on failure
263 *
264 * This function is called when starting authentication with a network that is
265 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
266 */
267int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
268 struct wpa_ssid *ssid)
269{
270#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800271#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 int aka = 0, sim = 0, type;
273
274 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
275 return 0;
276
277 if (ssid->eap.eap_methods == NULL) {
278 sim = 1;
279 aka = 1;
280 } else {
281 struct eap_method_type *eap = ssid->eap.eap_methods;
282 while (eap->vendor != EAP_VENDOR_IETF ||
283 eap->method != EAP_TYPE_NONE) {
284 if (eap->vendor == EAP_VENDOR_IETF) {
285 if (eap->method == EAP_TYPE_SIM)
286 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700287 else if (eap->method == EAP_TYPE_AKA ||
288 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 aka = 1;
290 }
291 eap++;
292 }
293 }
294
295 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
296 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700297 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
298 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
299 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 aka = 0;
301
302 if (!sim && !aka) {
303 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
304 "use SIM, but neither EAP-SIM nor EAP-AKA are "
305 "enabled");
306 return 0;
307 }
308
309 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
310 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
311 if (sim && aka)
312 type = SCARD_TRY_BOTH;
313 else if (aka)
314 type = SCARD_USIM_ONLY;
315 else
316 type = SCARD_GSM_SIM_ONLY;
317
Dmitry Shmidt04949592012-07-19 12:16:46 -0700318 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (wpa_s->scard == NULL) {
320 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
321 "(pcsc-lite)");
322 return -1;
323 }
324 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
325 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800326#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327#endif /* IEEE8021X_EAPOL */
328
329 return 0;
330}
331
332
333#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700334static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 struct wpa_ssid *ssid)
336{
337 int i, privacy = 0;
338
339 if (ssid->mixed_cell)
340 return 1;
341
342#ifdef CONFIG_WPS
343 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
344 return 1;
345#endif /* CONFIG_WPS */
346
347 for (i = 0; i < NUM_WEP_KEYS; i++) {
348 if (ssid->wep_key_len[i]) {
349 privacy = 1;
350 break;
351 }
352 }
353#ifdef IEEE8021X_EAPOL
354 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
355 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
356 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
357 privacy = 1;
358#endif /* IEEE8021X_EAPOL */
359
Jouni Malinen75ecf522011-06-27 15:19:46 -0700360 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
361 privacy = 1;
362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 if (bss->caps & IEEE80211_CAP_PRIVACY)
364 return privacy;
365 return !privacy;
366}
367
368
369static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
370 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700371 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372{
373 struct wpa_ie_data ie;
374 int proto_match = 0;
375 const u8 *rsn_ie, *wpa_ie;
376 int ret;
377 int wep_ok;
378
379 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
380 if (ret >= 0)
381 return ret;
382
383 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
384 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
385 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
386 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
387 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
388
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700389 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
391 proto_match++;
392
393 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
394 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
395 "failed");
396 break;
397 }
398
399 if (wep_ok &&
400 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
401 {
402 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
403 "in RSN IE");
404 return 1;
405 }
406
407 if (!(ie.proto & ssid->proto)) {
408 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
409 "mismatch");
410 break;
411 }
412
413 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
414 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
415 "cipher mismatch");
416 break;
417 }
418
419 if (!(ie.group_cipher & ssid->group_cipher)) {
420 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
421 "cipher mismatch");
422 break;
423 }
424
425 if (!(ie.key_mgmt & ssid->key_mgmt)) {
426 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
427 "mismatch");
428 break;
429 }
430
431#ifdef CONFIG_IEEE80211W
432 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800433 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
434 wpa_s->conf->pmf : ssid->ieee80211w) ==
435 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
437 "frame protection");
438 break;
439 }
440#endif /* CONFIG_IEEE80211W */
441
442 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
443 return 1;
444 }
445
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700446 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
448 proto_match++;
449
450 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
451 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
452 "failed");
453 break;
454 }
455
456 if (wep_ok &&
457 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
458 {
459 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
460 "in WPA IE");
461 return 1;
462 }
463
464 if (!(ie.proto & ssid->proto)) {
465 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
466 "mismatch");
467 break;
468 }
469
470 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
471 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
472 "cipher mismatch");
473 break;
474 }
475
476 if (!(ie.group_cipher & ssid->group_cipher)) {
477 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
478 "cipher mismatch");
479 break;
480 }
481
482 if (!(ie.key_mgmt & ssid->key_mgmt)) {
483 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
484 "mismatch");
485 break;
486 }
487
488 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
489 return 1;
490 }
491
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700492 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
493 !rsn_ie) {
494 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
495 return 1;
496 }
497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
499 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
500 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
501 return 0;
502 }
503
504 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
505 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
506 return 1;
507 }
508
509 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
510 "WPA/WPA2");
511
512 return 0;
513}
514
515
516static int freq_allowed(int *freqs, int freq)
517{
518 int i;
519
520 if (freqs == NULL)
521 return 1;
522
523 for (i = 0; freqs[i]; i++)
524 if (freqs[i] == freq)
525 return 1;
526 return 0;
527}
528
529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800530static int ht_supported(const struct hostapd_hw_modes *mode)
531{
532 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
533 /*
534 * The driver did not indicate whether it supports HT. Assume
535 * it does to avoid connection issues.
536 */
537 return 1;
538 }
539
540 /*
541 * IEEE Std 802.11n-2009 20.1.1:
542 * An HT non-AP STA shall support all EQM rates for one spatial stream.
543 */
544 return mode->mcs_set[0] == 0xff;
545}
546
547
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700548static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800549{
550 const struct hostapd_hw_modes *mode = NULL, *modes;
551 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
552 const u8 *rate_ie;
553 int i, j, k;
554
555 if (bss->freq == 0)
556 return 1; /* Cannot do matching without knowing band */
557
558 modes = wpa_s->hw.modes;
559 if (modes == NULL) {
560 /*
561 * The driver does not provide any additional information
562 * about the utilized hardware, so allow the connection attempt
563 * to continue.
564 */
565 return 1;
566 }
567
568 for (i = 0; i < wpa_s->hw.num_modes; i++) {
569 for (j = 0; j < modes[i].num_channels; j++) {
570 int freq = modes[i].channels[j].freq;
571 if (freq == bss->freq) {
572 if (mode &&
573 mode->mode == HOSTAPD_MODE_IEEE80211G)
574 break; /* do not allow 802.11b replace
575 * 802.11g */
576 mode = &modes[i];
577 break;
578 }
579 }
580 }
581
582 if (mode == NULL)
583 return 0;
584
585 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700586 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800587 if (rate_ie == NULL)
588 continue;
589
590 for (j = 2; j < rate_ie[1] + 2; j++) {
591 int flagged = !!(rate_ie[j] & 0x80);
592 int r = (rate_ie[j] & 0x7f) * 5;
593
594 /*
595 * IEEE Std 802.11n-2009 7.3.2.2:
596 * The new BSS Membership selector value is encoded
597 * like a legacy basic rate, but it is not a rate and
598 * only indicates if the BSS members are required to
599 * support the mandatory features of Clause 20 [HT PHY]
600 * in order to join the BSS.
601 */
602 if (flagged && ((rate_ie[j] & 0x7f) ==
603 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
604 if (!ht_supported(mode)) {
605 wpa_dbg(wpa_s, MSG_DEBUG,
606 " hardware does not support "
607 "HT PHY");
608 return 0;
609 }
610 continue;
611 }
612
613 if (!flagged)
614 continue;
615
616 /* check for legacy basic rates */
617 for (k = 0; k < mode->num_rates; k++) {
618 if (mode->rates[k] == r)
619 break;
620 }
621 if (k == mode->num_rates) {
622 /*
623 * IEEE Std 802.11-2007 7.3.2.2 demands that in
624 * order to join a BSS all required rates
625 * have to be supported by the hardware.
626 */
627 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
628 "not support required rate %d.%d Mbps",
629 r / 10, r % 10);
630 return 0;
631 }
632 }
633 }
634
635 return 1;
636}
637
638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700640 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 struct wpa_ssid *group)
642{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700643 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 int wpa;
645 struct wpa_blacklist *e;
646 const u8 *ie;
647 struct wpa_ssid *ssid;
648
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700649 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 wpa_ie_len = ie ? ie[1] : 0;
651
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700652 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 rsn_ie_len = ie ? ie[1] : 0;
654
655 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
656 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700657 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700659 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700660
661 e = wpa_blacklist_get(wpa_s, bss->bssid);
662 if (e) {
663 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700664 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 /*
666 * When only a single network is enabled, we can
667 * trigger blacklisting on the first failure. This
668 * should not be done with multiple enabled networks to
669 * avoid getting forced to move into a worse ESS on
670 * single error if there are no other BSSes of the
671 * current ESS.
672 */
673 limit = 0;
674 }
675 if (e->count > limit) {
676 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
677 "(count=%d limit=%d)", e->count, limit);
678 return NULL;
679 }
680 }
681
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700682 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
684 return NULL;
685 }
686
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800687 if (disallowed_bssid(wpa_s, bss->bssid)) {
688 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
689 return NULL;
690 }
691
692 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
693 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
694 return NULL;
695 }
696
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
698
699 for (ssid = group; ssid; ssid = ssid->pnext) {
700 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700701 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702
Dmitry Shmidt04949592012-07-19 12:16:46 -0700703 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
705 continue;
706 }
707
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700708 res = wpas_temp_disabled(wpa_s, ssid);
709 if (res > 0) {
710 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
711 "temporarily for %d second(s)", res);
712 continue;
713 }
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715#ifdef CONFIG_WPS
716 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
717 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
718 "(WPS)");
719 continue;
720 }
721
722 if (wpa && ssid->ssid_len == 0 &&
723 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
724 check_ssid = 0;
725
726 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
727 /* Only allow wildcard SSID match if an AP
728 * advertises active WPS operation that matches
729 * with our mode. */
730 check_ssid = 1;
731 if (ssid->ssid_len == 0 &&
732 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
733 check_ssid = 0;
734 }
735#endif /* CONFIG_WPS */
736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737 if (ssid->bssid_set && ssid->ssid_len == 0 &&
738 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
739 check_ssid = 0;
740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700742 (bss->ssid_len != ssid->ssid_len ||
743 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
745 continue;
746 }
747
748 if (ssid->bssid_set &&
749 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
750 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
751 continue;
752 }
753
754 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
755 continue;
756
757 if (!wpa &&
758 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
759 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
760 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
761 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
762 "not allowed");
763 continue;
764 }
765
Jouni Malinen75ecf522011-06-27 15:19:46 -0700766 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
768 "mismatch");
769 continue;
770 }
771
Jouni Malinen75ecf522011-06-27 15:19:46 -0700772 if (bss->caps & IEEE80211_CAP_IBSS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
774 "network");
775 continue;
776 }
777
778 if (!freq_allowed(ssid->freq_list, bss->freq)) {
779 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
780 "allowed");
781 continue;
782 }
783
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784 if (!rate_match(wpa_s, bss)) {
785 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
786 "not match");
787 continue;
788 }
789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790#ifdef CONFIG_P2P
791 /*
792 * TODO: skip the AP if its P2P IE has Group Formation
793 * bit set in the P2P Group Capability Bitmap and we
794 * are not in Group Formation with that device.
795 */
796#endif /* CONFIG_P2P */
797
798 /* Matching configuration found */
799 return ssid;
800 }
801
802 /* No matching configuration found */
803 return NULL;
804}
805
806
807static struct wpa_bss *
808wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 struct wpa_ssid *group,
810 struct wpa_ssid **selected_ssid)
811{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700812 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813
814 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
815 group->priority);
816
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700817 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
818 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
820 if (!*selected_ssid)
821 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
823 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700824 MAC2STR(bss->bssid),
825 wpa_ssid_txt(bss->ssid, bss->ssid_len));
826 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 }
828
829 return NULL;
830}
831
832
833static struct wpa_bss *
834wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 struct wpa_ssid **selected_ssid)
836{
837 struct wpa_bss *selected = NULL;
838 int prio;
839
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700840 if (wpa_s->last_scan_res == NULL ||
841 wpa_s->last_scan_res_used == 0)
842 return NULL; /* no scan results from last update */
843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 while (selected == NULL) {
845 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
846 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700847 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 selected_ssid);
849 if (selected)
850 break;
851 }
852
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 if (selected == NULL && wpa_s->blacklist &&
854 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
856 "blacklist and try again");
857 wpa_blacklist_clear(wpa_s);
858 wpa_s->blacklist_cleared++;
859 } else if (selected == NULL)
860 break;
861 }
862
863 return selected;
864}
865
866
867static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
868 int timeout_sec, int timeout_usec)
869{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700870 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 /*
872 * No networks are enabled; short-circuit request so
873 * we don't wait timeout seconds before transitioning
874 * to INACTIVE state.
875 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700876 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
877 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700879#ifdef CONFIG_P2P
880 wpa_s->sta_scan_pending = 0;
881#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 return;
883 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800884
885 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
887}
888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800889
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700890int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800891 struct wpa_bss *selected,
892 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893{
894 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
895 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
896 "PBC session overlap");
897#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700899 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900#endif /* CONFIG_P2P */
901
902#ifdef CONFIG_WPS
903 wpas_wps_cancel(wpa_s);
904#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700905 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
907
908 /*
909 * Do not trigger new association unless the BSSID has changed or if
910 * reassociation is requested. If we are in process of associating with
911 * the selected BSSID, do not trigger new attempt.
912 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
915 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
916 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
917 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
918 0))) {
919 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
920 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700921 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 }
923 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
924 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
925 " pending: " MACSTR " wpa_state: %s",
926 wpa_s->reassociate, MAC2STR(selected->bssid),
927 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
928 wpa_supplicant_state_txt(wpa_s->wpa_state));
929 wpa_supplicant_associate(wpa_s, selected, ssid);
930 } else {
931 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
932 "selected AP");
933 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700935 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936}
937
938
939static struct wpa_ssid *
940wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
941{
942 int prio;
943 struct wpa_ssid *ssid;
944
945 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
946 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
947 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700948 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 continue;
950 if (ssid->mode == IEEE80211_MODE_IBSS ||
951 ssid->mode == IEEE80211_MODE_AP)
952 return ssid;
953 }
954 }
955 return NULL;
956}
957
958
959/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
960 * on BSS added and BSS changed events */
961static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300962 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300964 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965
966 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
967 return;
968
Jouni Malinen87fd2792011-05-16 18:35:42 +0300969 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
Jouni Malinen87fd2792011-05-16 18:35:42 +0300972 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (ssid == NULL)
974 continue;
975
Jouni Malinen87fd2792011-05-16 18:35:42 +0300976 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977 if (rsn == NULL)
978 continue;
979
Jouni Malinen87fd2792011-05-16 18:35:42 +0300980 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 }
982
983}
984
985
986static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
987 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700988 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700990 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 int min_diff;
992
993 if (wpa_s->reassociate)
994 return 1; /* explicit request to reassociate */
995 if (wpa_s->wpa_state < WPA_ASSOCIATED)
996 return 1; /* we are not associated; continue */
997 if (wpa_s->current_ssid == NULL)
998 return 1; /* unknown current SSID */
999 if (wpa_s->current_ssid != ssid)
1000 return 1; /* different network block */
1001
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001002 if (wpas_driver_bss_selection(wpa_s))
1003 return 0; /* Driver-based roaming */
1004
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001005 if (wpa_s->current_ssid->ssid)
1006 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1007 wpa_s->current_ssid->ssid,
1008 wpa_s->current_ssid->ssid_len);
1009 if (!current_bss)
1010 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011
1012 if (!current_bss)
1013 return 1; /* current BSS not seen in scan results */
1014
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001015 if (current_bss == selected)
1016 return 0;
1017
1018 if (selected->last_update_idx > current_bss->last_update_idx)
1019 return 1; /* current BSS not seen in the last scan */
1020
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001021#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1023 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1024 MAC2STR(current_bss->bssid), current_bss->level);
1025 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1026 MAC2STR(selected->bssid), selected->level);
1027
1028 if (wpa_s->current_ssid->bssid_set &&
1029 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1030 0) {
1031 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1032 "has preferred BSSID");
1033 return 1;
1034 }
1035
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001036 if (current_bss->level < 0 && current_bss->level > selected->level) {
1037 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1038 "signal level");
1039 return 0;
1040 }
1041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 min_diff = 2;
1043 if (current_bss->level < 0) {
1044 if (current_bss->level < -85)
1045 min_diff = 1;
1046 else if (current_bss->level < -80)
1047 min_diff = 2;
1048 else if (current_bss->level < -75)
1049 min_diff = 3;
1050 else if (current_bss->level < -70)
1051 min_diff = 4;
1052 else
1053 min_diff = 5;
1054 }
1055 if (abs(current_bss->level - selected->level) < min_diff) {
1056 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1057 "in signal level");
1058 return 0;
1059 }
1060
1061 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001062#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001063 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001064#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065}
1066
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001067
Jouni Malinen89ca7022012-09-14 13:03:12 -07001068/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001069 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001070#ifdef ANDROID_P2P
1071static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1072 union wpa_event_data *data, int suppress_event)
1073#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1075 union wpa_event_data *data)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001076#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 struct wpa_scan_results *scan_res;
1079 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001080#ifndef CONFIG_NO_RANDOM_POOL
1081 size_t i, num;
1082#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083
1084#ifdef CONFIG_AP
1085 if (wpa_s->ap_iface)
1086 ap = 1;
1087#endif /* CONFIG_AP */
1088
1089 wpa_supplicant_notify_scanning(wpa_s, 0);
1090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001091#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07001092 if (wpa_s->global->p2p_cb_on_scan_complete &&
1093 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001094 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1095 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001096 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001097 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1099 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001100 wpa_s->sta_scan_pending = 1;
1101 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001102 return -1;
1103 }
1104 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001105 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106#endif /* CONFIG_P2P */
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1109 data ? &data->scan_info :
1110 NULL, 1);
1111 if (scan_res == NULL) {
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001112 if ((wpa_s->conf->ap_scan == 2 || ap) ||
1113 (wpa_s->scan_res_handler == scan_only_handler))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 return -1;
1115 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1116 "scanning again");
1117 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1118 return -1;
1119 }
1120
1121#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 num = scan_res->num;
1123 if (num > 10)
1124 num = 10;
1125 for (i = 0; i < num; i++) {
1126 u8 buf[5];
1127 struct wpa_scan_res *res = scan_res->res[i];
1128 buf[0] = res->bssid[5];
1129 buf[1] = res->qual & 0xff;
1130 buf[2] = res->noise & 0xff;
1131 buf[3] = res->level & 0xff;
1132 buf[4] = res->tsf & 0xff;
1133 random_add_randomness(buf, sizeof(buf));
1134 }
1135#endif /* CONFIG_NO_RANDOM_POOL */
1136
1137 if (wpa_s->scan_res_handler) {
1138 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1139 struct wpa_scan_results *scan_res);
1140
1141 scan_res_handler = wpa_s->scan_res_handler;
1142 wpa_s->scan_res_handler = NULL;
1143 scan_res_handler(wpa_s, scan_res);
1144
1145 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001146 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 }
1148
1149 if (ap) {
1150 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1151#ifdef CONFIG_AP
1152 if (wpa_s->ap_iface->scan_cb)
1153 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1154#endif /* CONFIG_AP */
1155 wpa_scan_results_free(scan_res);
1156 return 0;
1157 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001158#ifdef ANDROID_P2P
1159 if(!suppress_event)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001160 {
1161 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1162 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1163 wpas_notify_scan_results(wpa_s);
1164 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001165#else
1166 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1167 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1168 wpas_notify_scan_results(wpa_s);
1169#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170
1171 wpas_notify_scan_done(wpa_s, 1);
1172
Dmitry Shmidt04949592012-07-19 12:16:46 -07001173 if (sme_proc_obss_scan(wpa_s) > 0) {
1174 wpa_scan_results_free(scan_res);
1175 return 0;
1176 }
1177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1179 wpa_scan_results_free(scan_res);
1180 return 0;
1181 }
1182
Dmitry Shmidt04949592012-07-19 12:16:46 -07001183 if (autoscan_notify_scan(wpa_s, scan_res)) {
1184 wpa_scan_results_free(scan_res);
1185 return 0;
1186 }
1187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 if (wpa_s->disconnected) {
1189 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1190 wpa_scan_results_free(scan_res);
1191 return 0;
1192 }
1193
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194 if (!wpas_driver_bss_selection(wpa_s) &&
1195 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 wpa_scan_results_free(scan_res);
1197 return 0;
1198 }
1199
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001200 wpas_wps_update_ap_info(wpa_s, scan_res);
1201
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001202 wpa_scan_results_free(scan_res);
1203
1204 return wpas_select_network_from_last_scan(wpa_s);
1205}
1206
1207
1208int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
1209{
1210 struct wpa_bss *selected;
1211 struct wpa_ssid *ssid = NULL;
1212
1213 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214
1215 if (selected) {
1216 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001217 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001218 if (skip) {
1219 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 }
1222
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001223 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001224 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001225 return -1;
1226 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001227 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001228 /*
1229 * Do not notify other virtual radios of scan results since we do not
1230 * want them to start other associations at the same time.
1231 */
1232 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1235 ssid = wpa_supplicant_pick_new_network(wpa_s);
1236 if (ssid) {
1237 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1238 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001239 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 } else {
1241 int timeout_sec = wpa_s->scan_interval;
1242 int timeout_usec = 0;
1243#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001244 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1245 return 0;
1246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 if (wpa_s->p2p_in_provisioning) {
1248 /*
1249 * Use shorter wait during P2P Provisioning
1250 * state to speed up group formation.
1251 */
1252 timeout_sec = 0;
1253 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1255 timeout_usec);
1256 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 }
1258#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001259#ifdef CONFIG_INTERWORKING
1260 if (wpa_s->conf->auto_interworking &&
1261 wpa_s->conf->interworking &&
1262 wpa_s->conf->cred) {
1263 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1264 "start ANQP fetch since no matching "
1265 "networks found");
1266 wpa_s->network_select = 1;
1267 wpa_s->auto_network_select = 1;
1268 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001269 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001270 }
1271#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 if (wpa_supplicant_req_sched_scan(wpa_s))
1273 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1274 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 }
1276 }
1277 return 0;
1278}
1279
1280
1281static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1282 union wpa_event_data *data)
1283{
1284 const char *rn, *rn2;
1285 struct wpa_supplicant *ifs;
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001286#ifdef ANDROID_P2P
Jouni Malinen89ca7022012-09-14 13:03:12 -07001287 if (_wpa_supplicant_event_scan_results(wpa_s, data, 0) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001288#else
Jouni Malinen89ca7022012-09-14 13:03:12 -07001289 if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001290#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 /*
1292 * If no scan results could be fetched, then no need to
1293 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001294 * this scan. Similarly, if scan results started a new operation on this
1295 * interface, do not notify other interfaces to avoid concurrent
1296 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 */
1298 return;
1299 }
1300
1301 /*
1302 * Check other interfaces to see if they have the same radio-name. If
1303 * so, they get updated with this same scan info.
1304 */
1305 if (!wpa_s->driver->get_radio_name)
1306 return;
1307
1308 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1309 if (rn == NULL || rn[0] == '\0')
1310 return;
1311
1312 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1313 "sharing same radio (%s) in event_scan_results", rn);
1314
1315 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1316 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1317 continue;
1318
1319 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1320 if (rn2 && os_strcmp(rn, rn2) == 0) {
1321 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1322 "sibling", ifs->ifname);
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001323#ifdef ANDROID_P2P
1324 if ( (ifs->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) || (ifs->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
1325 /* Do not update the scan results from STA interface to p2p interfaces */
1326 wpa_printf(MSG_DEBUG, "Not Updating scan results on interface %s from "
1327 "sibling %s", ifs->ifname, wpa_s->ifname);
1328 continue;
1329 }
1330 else {
1331 /* P2P_FIND will result in too many SCAN_RESULT_EVENTS within
1332 * no time. Avoid announcing it to application as it may not
1333 * be that useful (since results will be that of only 1,6,11).
1334 * over to any other interface as it
1335 */
1336 if(p2p_search_in_progress(wpa_s->global->p2p))
1337 _wpa_supplicant_event_scan_results(ifs, data, 1);
1338 else
1339 _wpa_supplicant_event_scan_results(ifs, data, 0);
1340 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001341#else
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001342 _wpa_supplicant_event_scan_results(ifs, data);
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001343#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 }
1345 }
1346}
1347
1348#endif /* CONFIG_NO_SCAN_PROCESSING */
1349
1350
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351#ifdef CONFIG_WNM
1352
1353static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1354{
1355 struct wpa_supplicant *wpa_s = eloop_ctx;
1356
1357 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1358 return;
1359
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001360 if (!wpa_s->no_keep_alive) {
1361 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1362 MAC2STR(wpa_s->bssid));
1363 /* TODO: could skip this if normal data traffic has been sent */
1364 /* TODO: Consider using some more appropriate data frame for
1365 * this */
1366 if (wpa_s->l2)
1367 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1368 (u8 *) "", 0);
1369 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001370
1371#ifdef CONFIG_SME
1372 if (wpa_s->sme.bss_max_idle_period) {
1373 unsigned int msec;
1374 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1375 if (msec > 100)
1376 msec -= 100;
1377 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1378 wnm_bss_keep_alive, wpa_s, NULL);
1379 }
1380#endif /* CONFIG_SME */
1381}
1382
1383
1384static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1385 const u8 *ies, size_t ies_len)
1386{
1387 struct ieee802_11_elems elems;
1388
1389 if (ies == NULL)
1390 return;
1391
1392 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1393 return;
1394
1395#ifdef CONFIG_SME
1396 if (elems.bss_max_idle_period) {
1397 unsigned int msec;
1398 wpa_s->sme.bss_max_idle_period =
1399 WPA_GET_LE16(elems.bss_max_idle_period);
1400 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1401 "TU)%s", wpa_s->sme.bss_max_idle_period,
1402 (elems.bss_max_idle_period[2] & 0x01) ?
1403 " (protected keep-live required)" : "");
1404 if (wpa_s->sme.bss_max_idle_period == 0)
1405 wpa_s->sme.bss_max_idle_period = 1;
1406 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1407 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1408 /* msec times 1000 */
1409 msec = wpa_s->sme.bss_max_idle_period * 1024;
1410 if (msec > 100)
1411 msec -= 100;
1412 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1413 wnm_bss_keep_alive, wpa_s,
1414 NULL);
1415 }
1416 }
1417#endif /* CONFIG_SME */
1418}
1419
1420#endif /* CONFIG_WNM */
1421
1422
1423void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1424{
1425#ifdef CONFIG_WNM
1426 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1427#endif /* CONFIG_WNM */
1428}
1429
1430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1432 union wpa_event_data *data)
1433{
1434 int l, len, found = 0, wpa_found, rsn_found;
1435 const u8 *p;
1436
1437 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1438 if (data->assoc_info.req_ies)
1439 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1440 data->assoc_info.req_ies_len);
1441 if (data->assoc_info.resp_ies) {
1442 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1443 data->assoc_info.resp_ies_len);
1444#ifdef CONFIG_TDLS
1445 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1446 data->assoc_info.resp_ies_len);
1447#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001448#ifdef CONFIG_WNM
1449 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1450 data->assoc_info.resp_ies_len);
1451#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 }
1453 if (data->assoc_info.beacon_ies)
1454 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1455 data->assoc_info.beacon_ies,
1456 data->assoc_info.beacon_ies_len);
1457 if (data->assoc_info.freq)
1458 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1459 data->assoc_info.freq);
1460
1461 p = data->assoc_info.req_ies;
1462 l = data->assoc_info.req_ies_len;
1463
1464 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1465 while (p && l >= 2) {
1466 len = p[1] + 2;
1467 if (len > l) {
1468 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1469 p, l);
1470 break;
1471 }
1472 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1473 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1474 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1475 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1476 break;
1477 found = 1;
1478 wpa_find_assoc_pmkid(wpa_s);
1479 break;
1480 }
1481 l -= len;
1482 p += len;
1483 }
1484 if (!found && data->assoc_info.req_ies)
1485 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1486
1487#ifdef CONFIG_IEEE80211R
1488#ifdef CONFIG_SME
1489 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1490 u8 bssid[ETH_ALEN];
1491 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1492 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1493 data->assoc_info.resp_ies,
1494 data->assoc_info.resp_ies_len,
1495 bssid) < 0) {
1496 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1497 "Reassociation Response failed");
1498 wpa_supplicant_deauthenticate(
1499 wpa_s, WLAN_REASON_INVALID_IE);
1500 return -1;
1501 }
1502 }
1503
1504 p = data->assoc_info.resp_ies;
1505 l = data->assoc_info.resp_ies_len;
1506
1507#ifdef CONFIG_WPS_STRICT
1508 if (p && wpa_s->current_ssid &&
1509 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1510 struct wpabuf *wps;
1511 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1512 if (wps == NULL) {
1513 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1514 "include WPS IE in (Re)Association Response");
1515 return -1;
1516 }
1517
1518 if (wps_validate_assoc_resp(wps) < 0) {
1519 wpabuf_free(wps);
1520 wpa_supplicant_deauthenticate(
1521 wpa_s, WLAN_REASON_INVALID_IE);
1522 return -1;
1523 }
1524 wpabuf_free(wps);
1525 }
1526#endif /* CONFIG_WPS_STRICT */
1527
1528 /* Go through the IEs and make a copy of the MDIE, if present. */
1529 while (p && l >= 2) {
1530 len = p[1] + 2;
1531 if (len > l) {
1532 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1533 p, l);
1534 break;
1535 }
1536 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1537 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1538 wpa_s->sme.ft_used = 1;
1539 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1540 MOBILITY_DOMAIN_ID_LEN);
1541 break;
1542 }
1543 l -= len;
1544 p += len;
1545 }
1546#endif /* CONFIG_SME */
1547
1548 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1549 data->assoc_info.resp_ies_len);
1550#endif /* CONFIG_IEEE80211R */
1551
1552 /* WPA/RSN IE from Beacon/ProbeResp */
1553 p = data->assoc_info.beacon_ies;
1554 l = data->assoc_info.beacon_ies_len;
1555
1556 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1557 */
1558 wpa_found = rsn_found = 0;
1559 while (p && l >= 2) {
1560 len = p[1] + 2;
1561 if (len > l) {
1562 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1563 p, l);
1564 break;
1565 }
1566 if (!wpa_found &&
1567 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1568 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1569 wpa_found = 1;
1570 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1571 }
1572
1573 if (!rsn_found &&
1574 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1575 rsn_found = 1;
1576 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1577 }
1578
1579 l -= len;
1580 p += len;
1581 }
1582
1583 if (!wpa_found && data->assoc_info.beacon_ies)
1584 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1585 if (!rsn_found && data->assoc_info.beacon_ies)
1586 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1587 if (wpa_found || rsn_found)
1588 wpa_s->ap_ies_from_associnfo = 1;
1589
Jouni Malinen87fd2792011-05-16 18:35:42 +03001590 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1591 wpa_s->assoc_freq != data->assoc_info.freq) {
1592 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1593 "%u to %u MHz",
1594 wpa_s->assoc_freq, data->assoc_info.freq);
1595 wpa_supplicant_update_scan_results(wpa_s);
1596 }
1597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 wpa_s->assoc_freq = data->assoc_info.freq;
1599
1600 return 0;
1601}
1602
1603
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001604static struct wpa_bss * wpa_supplicant_get_new_bss(
1605 struct wpa_supplicant *wpa_s, const u8 *bssid)
1606{
1607 struct wpa_bss *bss = NULL;
1608 struct wpa_ssid *ssid = wpa_s->current_ssid;
1609
1610 if (ssid->ssid_len > 0)
1611 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1612 if (!bss)
1613 bss = wpa_bss_get_bssid(wpa_s, bssid);
1614
1615 return bss;
1616}
1617
1618
1619static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1620{
1621 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1622
1623 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1624 return -1;
1625
1626 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1627 return 0;
1628
1629 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1630 WPA_IE_VENDOR_TYPE);
1631 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1632
1633 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1634 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1635 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1636 bss_rsn ? 2 + bss_rsn[1] : 0))
1637 return -1;
1638
1639 return 0;
1640}
1641
1642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1644 union wpa_event_data *data)
1645{
1646 u8 bssid[ETH_ALEN];
1647 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 struct wpa_driver_capa capa;
1649
1650#ifdef CONFIG_AP
1651 if (wpa_s->ap_iface) {
1652 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1653 data->assoc_info.addr,
1654 data->assoc_info.req_ies,
1655 data->assoc_info.req_ies_len,
1656 data->assoc_info.reassoc);
1657 return;
1658 }
1659#endif /* CONFIG_AP */
1660
1661 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1662 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1663 return;
1664
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001665 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1666 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001667 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001668 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1669 return;
1670 }
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001673 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1675 MACSTR, MAC2STR(bssid));
1676 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1678 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001679 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680
1681 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1682 wpa_clear_keys(wpa_s, bssid);
1683 }
1684 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001685 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1687 return;
1688 }
1689 if (wpa_s->current_ssid) {
1690 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001691
1692 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1693 if (!bss) {
1694 wpa_supplicant_update_scan_results(wpa_s);
1695
1696 /* Get the BSS from the new scan results */
1697 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1698 }
1699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 if (bss)
1701 wpa_s->current_bss = bss;
1702 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001703
1704 if (wpa_s->conf->ap_scan == 1 &&
1705 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1706 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1707 wpa_msg(wpa_s, MSG_WARNING,
1708 "WPA/RSN IEs not updated");
1709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 }
1711
1712#ifdef CONFIG_SME
1713 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1714 wpa_s->sme.prev_bssid_set = 1;
1715#endif /* CONFIG_SME */
1716
1717 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1718 if (wpa_s->current_ssid) {
1719 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1720 * initialized before association, but for other modes,
1721 * initialize PC/SC here, if the current configuration needs
1722 * smartcard or SIM/USIM. */
1723 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1724 }
1725 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1726 if (wpa_s->l2)
1727 l2_packet_notify_auth_start(wpa_s->l2);
1728
1729 /*
1730 * Set portEnabled first to FALSE in order to get EAP state machine out
1731 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1732 * state machine may transit to AUTHENTICATING state based on obsolete
1733 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1734 * AUTHENTICATED without ever giving chance to EAP state machine to
1735 * reset the state.
1736 */
1737 if (!ft_completed) {
1738 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1739 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1740 }
1741 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1742 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1743 /* 802.1X::portControl = Auto */
1744 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1745 wpa_s->eapol_received = 0;
1746 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1747 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1748 (wpa_s->current_ssid &&
1749 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1750 wpa_supplicant_cancel_auth_timeout(wpa_s);
1751 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1752 } else if (!ft_completed) {
1753 /* Timeout for receiving the first EAPOL packet */
1754 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1755 }
1756 wpa_supplicant_cancel_scan(wpa_s);
1757
1758 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1759 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1760 /*
1761 * We are done; the driver will take care of RSN 4-way
1762 * handshake.
1763 */
1764 wpa_supplicant_cancel_auth_timeout(wpa_s);
1765 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1766 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1767 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1768 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1769 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1770 /*
1771 * The driver will take care of RSN 4-way handshake, so we need
1772 * to allow EAPOL supplicant to complete its work without
1773 * waiting for WPA supplicant.
1774 */
1775 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1776 } else if (ft_completed) {
1777 /*
1778 * FT protocol completed - make sure EAPOL state machine ends
1779 * up in authenticated.
1780 */
1781 wpa_supplicant_cancel_auth_timeout(wpa_s);
1782 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1783 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1784 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1785 }
1786
Jouni Malinena05074c2012-12-21 21:35:35 +02001787 wpa_s->last_eapol_matches_bssid = 0;
1788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 if (wpa_s->pending_eapol_rx) {
1790 struct os_time now, age;
1791 os_get_time(&now);
1792 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1793 if (age.sec == 0 && age.usec < 100000 &&
1794 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1795 0) {
1796 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1797 "frame that was received just before "
1798 "association notification");
1799 wpa_supplicant_rx_eapol(
1800 wpa_s, wpa_s->pending_eapol_rx_src,
1801 wpabuf_head(wpa_s->pending_eapol_rx),
1802 wpabuf_len(wpa_s->pending_eapol_rx));
1803 }
1804 wpabuf_free(wpa_s->pending_eapol_rx);
1805 wpa_s->pending_eapol_rx = NULL;
1806 }
1807
1808 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1809 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1810 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1811 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1812 /* Set static WEP keys again */
1813 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1814 }
1815
1816#ifdef CONFIG_IBSS_RSN
1817 if (wpa_s->current_ssid &&
1818 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1819 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1820 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1821 wpa_s->ibss_rsn == NULL) {
1822 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1823 if (!wpa_s->ibss_rsn) {
1824 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1825 wpa_supplicant_deauthenticate(
1826 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1827 return;
1828 }
1829
1830 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1831 }
1832#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001833
1834 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835}
1836
1837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001838static int disconnect_reason_recoverable(u16 reason_code)
1839{
1840 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1841 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1842 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1843}
1844
1845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001847 u16 reason_code,
1848 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849{
1850 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001851
1852 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1853 /*
1854 * At least Host AP driver and a Prism3 card seemed to be
1855 * generating streams of disconnected events when configuring
1856 * IBSS for WPA-None. Ignore them for now.
1857 */
1858 return;
1859 }
1860
1861 bssid = wpa_s->bssid;
1862 if (is_zero_ether_addr(bssid))
1863 bssid = wpa_s->pending_bssid;
1864
1865 if (!is_zero_ether_addr(bssid) ||
1866 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1867 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1868 " reason=%d%s",
1869 MAC2STR(bssid), reason_code,
1870 locally_generated ? " locally_generated=1" : "");
1871 }
1872}
1873
1874
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001875static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1876 int locally_generated)
1877{
1878 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1879 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1880 return 0; /* Not in 4-way handshake with PSK */
1881
1882 /*
1883 * It looks like connection was lost while trying to go through PSK
1884 * 4-way handshake. Filter out known disconnection cases that are caused
1885 * by something else than PSK mismatch to avoid confusing reports.
1886 */
1887
1888 if (locally_generated) {
1889 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1890 return 0;
1891 }
1892
1893 return 1;
1894}
1895
1896
Jouni Malinen2b89da82012-08-31 22:04:41 +03001897static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1898 u16 reason_code,
1899 int locally_generated)
1900{
1901 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 int authenticating;
1903 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 struct wpa_bss *fast_reconnect = NULL;
1905 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001906 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907
1908 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1909 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1910
1911 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1912 /*
1913 * At least Host AP driver and a Prism3 card seemed to be
1914 * generating streams of disconnected events when configuring
1915 * IBSS for WPA-None. Ignore them for now.
1916 */
1917 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1918 "IBSS/WPA-None mode");
1919 return;
1920 }
1921
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001922 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1924 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001925 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 }
1927 if (!wpa_s->auto_reconnect_disabled ||
1928 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001929 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1930 "reconnect (wps=%d wpa_state=%d)",
1931 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1932 wpa_s->wpa_state);
1933 if (wpa_s->wpa_state == WPA_COMPLETED &&
1934 wpa_s->current_ssid &&
1935 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001936 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001937 disconnect_reason_recoverable(reason_code)) {
1938 /*
1939 * It looks like the AP has dropped association with
1940 * us, but could allow us to get back in. Try to
1941 * reconnect to the same BSS without full scan to save
1942 * time for some common cases.
1943 */
1944 fast_reconnect = wpa_s->current_bss;
1945 fast_reconnect_ssid = wpa_s->current_ssid;
1946 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001947#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001948 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001949#else
1950 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1951#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001952 else
1953 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1954 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001956 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 "try to re-connect");
1958 wpa_s->reassociate = 0;
1959 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001960 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 }
1962 bssid = wpa_s->bssid;
1963 if (is_zero_ether_addr(bssid))
1964 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001965 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1966 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001968 if (locally_generated)
1969 wpa_s->disconnect_reason = -reason_code;
1970 else
1971 wpa_s->disconnect_reason = reason_code;
1972 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1974 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1975 wpa_s->keys_cleared = 0;
1976 wpa_clear_keys(wpa_s, wpa_s->bssid);
1977 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001978 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 wpa_supplicant_mark_disassoc(wpa_s);
1980
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001981 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001983 wpa_s->current_ssid = last_ssid;
1984 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985
1986 if (fast_reconnect) {
1987#ifndef CONFIG_NO_SCAN_PROCESSING
1988 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1989 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1990 fast_reconnect_ssid) < 0) {
1991 /* Recover through full scan */
1992 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1993 }
1994#endif /* CONFIG_NO_SCAN_PROCESSING */
1995 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001996}
1997
1998
1999#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002000void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001{
2002 struct wpa_supplicant *wpa_s = eloop_ctx;
2003
2004 if (!wpa_s->pending_mic_error_report)
2005 return;
2006
2007 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2008 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2009 wpa_s->pending_mic_error_report = 0;
2010}
2011#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2012
2013
2014static void
2015wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2016 union wpa_event_data *data)
2017{
2018 int pairwise;
2019 struct os_time t;
2020
2021 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2022 pairwise = (data && data->michael_mic_failure.unicast);
2023 os_get_time(&t);
2024 if ((wpa_s->last_michael_mic_error &&
2025 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2026 wpa_s->pending_mic_error_report) {
2027 if (wpa_s->pending_mic_error_report) {
2028 /*
2029 * Send the pending MIC error report immediately since
2030 * we are going to start countermeasures and AP better
2031 * do the same.
2032 */
2033 wpa_sm_key_request(wpa_s->wpa, 1,
2034 wpa_s->pending_mic_error_pairwise);
2035 }
2036
2037 /* Send the new MIC error report immediately since we are going
2038 * to start countermeasures and AP better do the same.
2039 */
2040 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2041
2042 /* initialize countermeasures */
2043 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002044
2045 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2048
2049 /*
2050 * Need to wait for completion of request frame. We do not get
2051 * any callback for the message completion, so just wait a
2052 * short while and hope for the best. */
2053 os_sleep(0, 10000);
2054
2055 wpa_drv_set_countermeasures(wpa_s, 1);
2056 wpa_supplicant_deauthenticate(wpa_s,
2057 WLAN_REASON_MICHAEL_MIC_FAILURE);
2058 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2059 wpa_s, NULL);
2060 eloop_register_timeout(60, 0,
2061 wpa_supplicant_stop_countermeasures,
2062 wpa_s, NULL);
2063 /* TODO: mark the AP rejected for 60 second. STA is
2064 * allowed to associate with another AP.. */
2065 } else {
2066#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2067 if (wpa_s->mic_errors_seen) {
2068 /*
2069 * Reduce the effectiveness of Michael MIC error
2070 * reports as a means for attacking against TKIP if
2071 * more than one MIC failure is noticed with the same
2072 * PTK. We delay the transmission of the reports by a
2073 * random time between 0 and 60 seconds in order to
2074 * force the attacker wait 60 seconds before getting
2075 * the information on whether a frame resulted in a MIC
2076 * failure.
2077 */
2078 u8 rval[4];
2079 int sec;
2080
2081 if (os_get_random(rval, sizeof(rval)) < 0)
2082 sec = os_random() % 60;
2083 else
2084 sec = WPA_GET_BE32(rval) % 60;
2085 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2086 "report %d seconds", sec);
2087 wpa_s->pending_mic_error_report = 1;
2088 wpa_s->pending_mic_error_pairwise = pairwise;
2089 eloop_cancel_timeout(
2090 wpa_supplicant_delayed_mic_error_report,
2091 wpa_s, NULL);
2092 eloop_register_timeout(
2093 sec, os_random() % 1000000,
2094 wpa_supplicant_delayed_mic_error_report,
2095 wpa_s, NULL);
2096 } else {
2097 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2098 }
2099#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2100 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2101#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2102 }
2103 wpa_s->last_michael_mic_error = t.sec;
2104 wpa_s->mic_errors_seen++;
2105}
2106
2107
2108#ifdef CONFIG_TERMINATE_ONLASTIF
2109static int any_interfaces(struct wpa_supplicant *head)
2110{
2111 struct wpa_supplicant *wpa_s;
2112
2113 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2114 if (!wpa_s->interface_removed)
2115 return 1;
2116 return 0;
2117}
2118#endif /* CONFIG_TERMINATE_ONLASTIF */
2119
2120
2121static void
2122wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2123 union wpa_event_data *data)
2124{
2125 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2126 return;
2127
2128 switch (data->interface_status.ievent) {
2129 case EVENT_INTERFACE_ADDED:
2130 if (!wpa_s->interface_removed)
2131 break;
2132 wpa_s->interface_removed = 0;
2133 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2134 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2135 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2136 "driver after interface was added");
2137 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002138 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 break;
2140 case EVENT_INTERFACE_REMOVED:
2141 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2142 wpa_s->interface_removed = 1;
2143 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002144 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145 l2_packet_deinit(wpa_s->l2);
2146 wpa_s->l2 = NULL;
2147#ifdef CONFIG_IBSS_RSN
2148 ibss_rsn_deinit(wpa_s->ibss_rsn);
2149 wpa_s->ibss_rsn = NULL;
2150#endif /* CONFIG_IBSS_RSN */
2151#ifdef CONFIG_TERMINATE_ONLASTIF
2152 /* check if last interface */
2153 if (!any_interfaces(wpa_s->global->ifaces))
2154 eloop_terminate();
2155#endif /* CONFIG_TERMINATE_ONLASTIF */
2156 break;
2157 }
2158}
2159
2160
2161#ifdef CONFIG_PEERKEY
2162static void
2163wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2164 union wpa_event_data *data)
2165{
2166 if (data == NULL)
2167 return;
2168 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2169}
2170#endif /* CONFIG_PEERKEY */
2171
2172
2173#ifdef CONFIG_TDLS
2174static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2175 union wpa_event_data *data)
2176{
2177 if (data == NULL)
2178 return;
2179 switch (data->tdls.oper) {
2180 case TDLS_REQUEST_SETUP:
2181 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2182 break;
2183 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002184 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2185 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 break;
2187 }
2188}
2189#endif /* CONFIG_TDLS */
2190
2191
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002192#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002193static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2194 union wpa_event_data *data)
2195{
2196 if (data == NULL)
2197 return;
2198 switch (data->wnm.oper) {
2199 case WNM_OPER_SLEEP:
2200 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2201 "(action=%d, intval=%d)",
2202 data->wnm.sleep_action, data->wnm.sleep_intval);
2203 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002204 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002205 break;
2206 }
2207}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002208#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209
2210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211#ifdef CONFIG_IEEE80211R
2212static void
2213wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2214 union wpa_event_data *data)
2215{
2216 if (data == NULL)
2217 return;
2218
2219 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2220 data->ft_ies.ies_len,
2221 data->ft_ies.ft_action,
2222 data->ft_ies.target_ap,
2223 data->ft_ies.ric_ies,
2224 data->ft_ies.ric_ies_len) < 0) {
2225 /* TODO: prevent MLME/driver from trying to associate? */
2226 }
2227}
2228#endif /* CONFIG_IEEE80211R */
2229
2230
2231#ifdef CONFIG_IBSS_RSN
2232static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2233 union wpa_event_data *data)
2234{
2235 struct wpa_ssid *ssid;
2236 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2237 return;
2238 if (data == NULL)
2239 return;
2240 ssid = wpa_s->current_ssid;
2241 if (ssid == NULL)
2242 return;
2243 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2244 return;
2245
2246 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2247}
2248#endif /* CONFIG_IBSS_RSN */
2249
2250
2251#ifdef CONFIG_IEEE80211R
2252static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2253 size_t len)
2254{
2255 const u8 *sta_addr, *target_ap_addr;
2256 u16 status;
2257
2258 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2259 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2260 return; /* only SME case supported for now */
2261 if (len < 1 + 2 * ETH_ALEN + 2)
2262 return;
2263 if (data[0] != 2)
2264 return; /* Only FT Action Response is supported for now */
2265 sta_addr = data + 1;
2266 target_ap_addr = data + 1 + ETH_ALEN;
2267 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2268 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2269 MACSTR " TargetAP " MACSTR " status %u",
2270 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2271
2272 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2273 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2274 " in FT Action Response", MAC2STR(sta_addr));
2275 return;
2276 }
2277
2278 if (status) {
2279 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2280 "failure (status code %d)", status);
2281 /* TODO: report error to FT code(?) */
2282 return;
2283 }
2284
2285 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2286 len - (1 + 2 * ETH_ALEN + 2), 1,
2287 target_ap_addr, NULL, 0) < 0)
2288 return;
2289
2290#ifdef CONFIG_SME
2291 {
2292 struct wpa_bss *bss;
2293 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2294 if (bss)
2295 wpa_s->sme.freq = bss->freq;
2296 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2297 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2298 WLAN_AUTH_FT);
2299 }
2300#endif /* CONFIG_SME */
2301}
2302#endif /* CONFIG_IEEE80211R */
2303
2304
2305static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2306 struct unprot_deauth *e)
2307{
2308#ifdef CONFIG_IEEE80211W
2309 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2310 "dropped: " MACSTR " -> " MACSTR
2311 " (reason code %u)",
2312 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2313 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2314#endif /* CONFIG_IEEE80211W */
2315}
2316
2317
2318static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2319 struct unprot_disassoc *e)
2320{
2321#ifdef CONFIG_IEEE80211W
2322 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2323 "dropped: " MACSTR " -> " MACSTR
2324 " (reason code %u)",
2325 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2326 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2327#endif /* CONFIG_IEEE80211W */
2328}
2329
2330
2331void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2332 union wpa_event_data *data)
2333{
2334 struct wpa_supplicant *wpa_s = ctx;
2335 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002336 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337
2338 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2339 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002340 event != EVENT_INTERFACE_STATUS &&
2341 event != EVENT_SCHED_SCAN_STOPPED) {
2342 wpa_dbg(wpa_s, MSG_DEBUG,
2343 "Ignore event %s (%d) while interface is disabled",
2344 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 return;
2346 }
2347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002348#ifndef CONFIG_NO_STDOUT_DEBUG
2349{
2350 int level = MSG_DEBUG;
2351
Dmitry Shmidt04949592012-07-19 12:16:46 -07002352 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002353 const struct ieee80211_hdr *hdr;
2354 u16 fc;
2355 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2356 fc = le_to_host16(hdr->frame_control);
2357 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2358 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2359 level = MSG_EXCESSIVE;
2360 }
2361
2362 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2363 event_to_string(event), event);
2364}
2365#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366
2367 switch (event) {
2368 case EVENT_AUTH:
2369 sme_event_auth(wpa_s, data);
2370 break;
2371 case EVENT_ASSOC:
2372 wpa_supplicant_event_assoc(wpa_s, data);
2373 break;
2374 case EVENT_DISASSOC:
2375 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2376 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002377 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2378 data->disassoc_info.reason_code,
2379 data->disassoc_info.locally_generated ?
2380 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 if (data->disassoc_info.addr)
2382 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2383 MAC2STR(data->disassoc_info.addr));
2384 }
2385#ifdef CONFIG_AP
2386 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2387 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2388 data->disassoc_info.addr);
2389 break;
2390 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002391 if (wpa_s->ap_iface) {
2392 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2393 "AP mode");
2394 break;
2395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396#endif /* CONFIG_AP */
2397 if (data) {
2398 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002399 locally_generated =
2400 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2402 data->disassoc_info.ie,
2403 data->disassoc_info.ie_len);
2404#ifdef CONFIG_P2P
2405 wpas_p2p_disassoc_notif(
2406 wpa_s, data->disassoc_info.addr, reason_code,
2407 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002408 data->disassoc_info.ie_len,
2409 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410#endif /* CONFIG_P2P */
2411 }
2412 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2413 sme_event_disassoc(wpa_s, data);
2414 /* fall through */
2415 case EVENT_DEAUTH:
2416 if (event == EVENT_DEAUTH) {
2417 wpa_dbg(wpa_s, MSG_DEBUG,
2418 "Deauthentication notification");
2419 if (data) {
2420 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002421 locally_generated =
2422 data->deauth_info.locally_generated;
2423 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2424 data->deauth_info.reason_code,
2425 data->deauth_info.locally_generated ?
2426 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 if (data->deauth_info.addr) {
2428 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2429 MACSTR,
2430 MAC2STR(data->deauth_info.
2431 addr));
2432 }
2433 wpa_hexdump(MSG_DEBUG,
2434 "Deauthentication frame IE(s)",
2435 data->deauth_info.ie,
2436 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 }
2438 }
2439#ifdef CONFIG_AP
2440 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2441 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2442 data->deauth_info.addr);
2443 break;
2444 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002445 if (wpa_s->ap_iface) {
2446 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2447 "AP mode");
2448 break;
2449 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002451 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2452 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002453 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2454 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2455 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2456 eapol_sm_failed(wpa_s->eapol)))
2457 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002458#ifdef CONFIG_P2P
2459 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002460 if (wpas_p2p_deauth_notif(wpa_s,
2461 data->deauth_info.addr,
2462 reason_code,
2463 data->deauth_info.ie,
2464 data->deauth_info.ie_len,
2465 locally_generated) > 0) {
2466 /*
2467 * The interface was removed, so cannot
2468 * continue processing any additional
2469 * operations after this.
2470 */
2471 break;
2472 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002473 }
2474#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002475 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2476 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477 break;
2478 case EVENT_MICHAEL_MIC_FAILURE:
2479 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2480 break;
2481#ifndef CONFIG_NO_SCAN_PROCESSING
2482 case EVENT_SCAN_RESULTS:
2483 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002484#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07002485 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002486 wpa_s->global->p2p != NULL &&
2487 wpa_s->wpa_state != WPA_AUTHENTICATING &&
2488 wpa_s->wpa_state != WPA_ASSOCIATING) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07002489 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002490 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2491 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2492 "continued after scan result processing");
2493 }
2494 }
2495#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 break;
2497#endif /* CONFIG_NO_SCAN_PROCESSING */
2498 case EVENT_ASSOCINFO:
2499 wpa_supplicant_event_associnfo(wpa_s, data);
2500 break;
2501 case EVENT_INTERFACE_STATUS:
2502 wpa_supplicant_event_interface_status(wpa_s, data);
2503 break;
2504 case EVENT_PMKID_CANDIDATE:
2505 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2506 break;
2507#ifdef CONFIG_PEERKEY
2508 case EVENT_STKSTART:
2509 wpa_supplicant_event_stkstart(wpa_s, data);
2510 break;
2511#endif /* CONFIG_PEERKEY */
2512#ifdef CONFIG_TDLS
2513 case EVENT_TDLS:
2514 wpa_supplicant_event_tdls(wpa_s, data);
2515 break;
2516#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002517#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002518 case EVENT_WNM:
2519 wpa_supplicant_event_wnm(wpa_s, data);
2520 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002521#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522#ifdef CONFIG_IEEE80211R
2523 case EVENT_FT_RESPONSE:
2524 wpa_supplicant_event_ft_response(wpa_s, data);
2525 break;
2526#endif /* CONFIG_IEEE80211R */
2527#ifdef CONFIG_IBSS_RSN
2528 case EVENT_IBSS_RSN_START:
2529 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2530 break;
2531#endif /* CONFIG_IBSS_RSN */
2532 case EVENT_ASSOC_REJECT:
2533 if (data->assoc_reject.bssid)
2534 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2535 "bssid=" MACSTR " status_code=%u",
2536 MAC2STR(data->assoc_reject.bssid),
2537 data->assoc_reject.status_code);
2538 else
2539 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2540 "status_code=%u",
2541 data->assoc_reject.status_code);
2542 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2543 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002544 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002545#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002546 if(!wpa_s->current_ssid) {
2547 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2548 break;
2549 }
2550 /* If assoc reject is reported by the driver, then avoid
2551 * waiting for the authentication timeout. Cancel the
2552 * authentication timeout and retry the assoc.
2553 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002554 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002555 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2556 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002557
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002558 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002559
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002560 /* Clear the states */
2561 wpa_sm_notify_disassoc(wpa_s->wpa);
2562 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2563
2564 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002565 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002566 const u8 *bl_bssid = data->assoc_reject.bssid;
2567 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2568 bl_bssid = wpa_s->pending_bssid;
2569 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002570 wpa_supplicant_req_scan(wpa_s, 0, 0);
2571 } else {
2572 wpa_supplicant_req_scan(wpa_s, 1, 0);
2573 }
2574 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002575 /* If we ASSOC_REJECT's hits threshold, disable the
2576 * network
2577 */
2578 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2579 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002580 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002581 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002582 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002583 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002584#else
2585 const u8 *bssid = data->assoc_reject.bssid;
2586 if (bssid == NULL || is_zero_ether_addr(bssid))
2587 bssid = wpa_s->pending_bssid;
2588 wpas_connection_failed(wpa_s, bssid);
2589 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002590#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592 break;
2593 case EVENT_AUTH_TIMED_OUT:
2594 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2595 sme_event_auth_timed_out(wpa_s, data);
2596 break;
2597 case EVENT_ASSOC_TIMED_OUT:
2598 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2599 sme_event_assoc_timed_out(wpa_s, data);
2600 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601 case EVENT_TX_STATUS:
2602 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2603 " type=%d stype=%d",
2604 MAC2STR(data->tx_status.dst),
2605 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002606#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2610 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002611 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 wpa_s, data->tx_status.dst,
2613 data->tx_status.data,
2614 data->tx_status.data_len,
2615 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002616 OFFCHANNEL_SEND_ACTION_SUCCESS :
2617 OFFCHANNEL_SEND_ACTION_NO_ACK);
2618#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 break;
2620 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002621#endif /* CONFIG_AP */
2622#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2624 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2625 /*
2626 * Catch TX status events for Action frames we sent via group
2627 * interface in GO mode.
2628 */
2629 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2630 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2631 os_memcmp(wpa_s->parent->pending_action_dst,
2632 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002633 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 wpa_s->parent, data->tx_status.dst,
2635 data->tx_status.data,
2636 data->tx_status.data_len,
2637 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638 OFFCHANNEL_SEND_ACTION_SUCCESS :
2639 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 break;
2641 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642#endif /* CONFIG_OFFCHANNEL */
2643#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 switch (data->tx_status.type) {
2645 case WLAN_FC_TYPE_MGMT:
2646 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2647 data->tx_status.data_len,
2648 data->tx_status.stype,
2649 data->tx_status.ack);
2650 break;
2651 case WLAN_FC_TYPE_DATA:
2652 ap_tx_status(wpa_s, data->tx_status.dst,
2653 data->tx_status.data,
2654 data->tx_status.data_len,
2655 data->tx_status.ack);
2656 break;
2657 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002658#endif /* CONFIG_AP */
2659 break;
2660#ifdef CONFIG_AP
2661 case EVENT_EAPOL_TX_STATUS:
2662 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2663 data->eapol_tx_status.data,
2664 data->eapol_tx_status.data_len,
2665 data->eapol_tx_status.ack);
2666 break;
2667 case EVENT_DRIVER_CLIENT_POLL_OK:
2668 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 break;
2670 case EVENT_RX_FROM_UNKNOWN:
2671 if (wpa_s->ap_iface == NULL)
2672 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002673 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2674 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002676 case EVENT_CH_SWITCH:
2677 if (!data)
2678 break;
2679 if (!wpa_s->ap_iface) {
2680 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2681 "event in non-AP mode");
2682 break;
2683 }
2684
2685#ifdef CONFIG_AP
2686 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2687 data->ch_switch.ht_enabled,
2688 data->ch_switch.ch_offset);
2689#endif /* CONFIG_AP */
2690 break;
2691 case EVENT_RX_MGMT: {
2692 u16 fc, stype;
2693 const struct ieee80211_mgmt *mgmt;
2694
2695 mgmt = (const struct ieee80211_mgmt *)
2696 data->rx_mgmt.frame;
2697 fc = le_to_host16(mgmt->frame_control);
2698 stype = WLAN_FC_GET_STYPE(fc);
2699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 if (wpa_s->ap_iface == NULL) {
2701#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2703 data->rx_mgmt.frame_len > 24) {
2704 const u8 *src = mgmt->sa;
2705 const u8 *ie = mgmt->u.probe_req.variable;
2706 size_t ie_len = data->rx_mgmt.frame_len -
2707 (mgmt->u.probe_req.variable -
2708 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002709 wpas_p2p_probe_req_rx(
2710 wpa_s, src, mgmt->da,
2711 mgmt->bssid, ie, ie_len,
2712 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713 break;
2714 }
2715#endif /* CONFIG_P2P */
2716 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2717 "management frame in non-AP mode");
2718 break;
2719 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720
2721 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2722 data->rx_mgmt.frame_len > 24) {
2723 const u8 *ie = mgmt->u.probe_req.variable;
2724 size_t ie_len = data->rx_mgmt.frame_len -
2725 (mgmt->u.probe_req.variable -
2726 data->rx_mgmt.frame);
2727
2728 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2729 mgmt->bssid, ie, ie_len,
2730 data->rx_mgmt.ssi_signal);
2731 }
2732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2734 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736#endif /* CONFIG_AP */
2737 case EVENT_RX_ACTION:
2738 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2739 " Category=%u DataLen=%d freq=%d MHz",
2740 MAC2STR(data->rx_action.sa),
2741 data->rx_action.category, (int) data->rx_action.len,
2742 data->rx_action.freq);
2743#ifdef CONFIG_IEEE80211R
2744 if (data->rx_action.category == WLAN_ACTION_FT) {
2745 ft_rx_action(wpa_s, data->rx_action.data,
2746 data->rx_action.len);
2747 break;
2748 }
2749#endif /* CONFIG_IEEE80211R */
2750#ifdef CONFIG_IEEE80211W
2751#ifdef CONFIG_SME
2752 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2753 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2754 data->rx_action.data,
2755 data->rx_action.len);
2756 break;
2757 }
2758#endif /* CONFIG_SME */
2759#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002760#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002761 if (data->rx_action.category == WLAN_ACTION_WNM) {
2762 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2763 break;
2764 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002765#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766#ifdef CONFIG_GAS
2767 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2768 gas_query_rx(wpa_s->gas, data->rx_action.da,
2769 data->rx_action.sa, data->rx_action.bssid,
2770 data->rx_action.data, data->rx_action.len,
2771 data->rx_action.freq) == 0)
2772 break;
2773#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002774#ifdef CONFIG_TDLS
2775 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2776 data->rx_action.len >= 4 &&
2777 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2778 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2779 "Response from " MACSTR,
2780 MAC2STR(data->rx_action.sa));
2781 break;
2782 }
2783#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784#ifdef CONFIG_P2P
2785 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2786 data->rx_action.sa,
2787 data->rx_action.bssid,
2788 data->rx_action.category,
2789 data->rx_action.data,
2790 data->rx_action.len, data->rx_action.freq);
2791#endif /* CONFIG_P2P */
2792 break;
2793 case EVENT_RX_PROBE_REQ:
2794 if (data->rx_probe_req.sa == NULL ||
2795 data->rx_probe_req.ie == NULL)
2796 break;
2797#ifdef CONFIG_AP
2798 if (wpa_s->ap_iface) {
2799 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2800 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002801 data->rx_probe_req.da,
2802 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002804 data->rx_probe_req.ie_len,
2805 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 break;
2807 }
2808#endif /* CONFIG_AP */
2809#ifdef CONFIG_P2P
2810 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002811 data->rx_probe_req.da,
2812 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002814 data->rx_probe_req.ie_len,
2815 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816#endif /* CONFIG_P2P */
2817 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819#ifdef CONFIG_OFFCHANNEL
2820 offchannel_remain_on_channel_cb(
2821 wpa_s, data->remain_on_channel.freq,
2822 data->remain_on_channel.duration);
2823#endif /* CONFIG_OFFCHANNEL */
2824#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 wpas_p2p_remain_on_channel_cb(
2826 wpa_s, data->remain_on_channel.freq,
2827 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829 break;
2830 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002831#ifdef CONFIG_OFFCHANNEL
2832 offchannel_cancel_remain_on_channel_cb(
2833 wpa_s, data->remain_on_channel.freq);
2834#endif /* CONFIG_OFFCHANNEL */
2835#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836 wpas_p2p_cancel_remain_on_channel_cb(
2837 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002838#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 case EVENT_P2P_DEV_FOUND: {
2842 struct p2p_peer_info peer_info;
2843
2844 os_memset(&peer_info, 0, sizeof(peer_info));
2845 if (data->p2p_dev_found.dev_addr)
2846 os_memcpy(peer_info.p2p_device_addr,
2847 data->p2p_dev_found.dev_addr, ETH_ALEN);
2848 if (data->p2p_dev_found.pri_dev_type)
2849 os_memcpy(peer_info.pri_dev_type,
2850 data->p2p_dev_found.pri_dev_type,
2851 sizeof(peer_info.pri_dev_type));
2852 if (data->p2p_dev_found.dev_name)
2853 os_strlcpy(peer_info.device_name,
2854 data->p2p_dev_found.dev_name,
2855 sizeof(peer_info.device_name));
2856 peer_info.config_methods = data->p2p_dev_found.config_methods;
2857 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2858 peer_info.group_capab = data->p2p_dev_found.group_capab;
2859
2860 /*
2861 * FIX: new_device=1 is not necessarily correct. We should
2862 * maintain a P2P peer database in wpa_supplicant and update
2863 * this information based on whether the peer is truly new.
2864 */
2865 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2866 break;
2867 }
2868 case EVENT_P2P_GO_NEG_REQ_RX:
2869 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2870 data->p2p_go_neg_req_rx.dev_passwd_id);
2871 break;
2872 case EVENT_P2P_GO_NEG_COMPLETED:
2873 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2874 break;
2875 case EVENT_P2P_PROV_DISC_REQUEST:
2876 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2877 data->p2p_prov_disc_req.config_methods,
2878 data->p2p_prov_disc_req.dev_addr,
2879 data->p2p_prov_disc_req.pri_dev_type,
2880 data->p2p_prov_disc_req.dev_name,
2881 data->p2p_prov_disc_req.supp_config_methods,
2882 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883 data->p2p_prov_disc_req.group_capab,
2884 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 break;
2886 case EVENT_P2P_PROV_DISC_RESPONSE:
2887 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2888 data->p2p_prov_disc_resp.config_methods);
2889 break;
2890 case EVENT_P2P_SD_REQUEST:
2891 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2892 data->p2p_sd_req.sa,
2893 data->p2p_sd_req.dialog_token,
2894 data->p2p_sd_req.update_indic,
2895 data->p2p_sd_req.tlvs,
2896 data->p2p_sd_req.tlvs_len);
2897 break;
2898 case EVENT_P2P_SD_RESPONSE:
2899 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2900 data->p2p_sd_resp.update_indic,
2901 data->p2p_sd_resp.tlvs,
2902 data->p2p_sd_resp.tlvs_len);
2903 break;
2904#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 case EVENT_EAPOL_RX:
2906 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2907 data->eapol_rx.data,
2908 data->eapol_rx.data_len);
2909 break;
2910 case EVENT_SIGNAL_CHANGE:
2911 bgscan_notify_signal_change(
2912 wpa_s, data->signal_change.above_threshold,
2913 data->signal_change.current_signal,
2914 data->signal_change.current_noise,
2915 data->signal_change.current_txrate);
2916 break;
2917 case EVENT_INTERFACE_ENABLED:
2918 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2919 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002920 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921#ifdef CONFIG_AP
2922 if (!wpa_s->ap_iface) {
2923 wpa_supplicant_set_state(wpa_s,
2924 WPA_DISCONNECTED);
2925 wpa_supplicant_req_scan(wpa_s, 0, 0);
2926 } else
2927 wpa_supplicant_set_state(wpa_s,
2928 WPA_COMPLETED);
2929#else /* CONFIG_AP */
2930 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2931 wpa_supplicant_req_scan(wpa_s, 0, 0);
2932#endif /* CONFIG_AP */
2933 }
2934 break;
2935 case EVENT_INTERFACE_DISABLED:
2936 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2937 wpa_supplicant_mark_disassoc(wpa_s);
2938 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2939 break;
2940 case EVENT_CHANNEL_LIST_CHANGED:
2941 if (wpa_s->drv_priv == NULL)
2942 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002943
2944 free_hw_features(wpa_s);
2945 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2946 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002948#ifdef CONFIG_P2P
2949 wpas_p2p_update_channel_list(wpa_s);
2950#endif /* CONFIG_P2P */
2951 break;
2952 case EVENT_INTERFACE_UNAVAILABLE:
2953#ifdef CONFIG_P2P
2954 wpas_p2p_interface_unavailable(wpa_s);
2955#endif /* CONFIG_P2P */
2956 break;
2957 case EVENT_BEST_CHANNEL:
2958 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2959 "(%d %d %d)",
2960 data->best_chan.freq_24, data->best_chan.freq_5,
2961 data->best_chan.freq_overall);
2962 wpa_s->best_24_freq = data->best_chan.freq_24;
2963 wpa_s->best_5_freq = data->best_chan.freq_5;
2964 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2965#ifdef CONFIG_P2P
2966 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2967 data->best_chan.freq_5,
2968 data->best_chan.freq_overall);
2969#endif /* CONFIG_P2P */
2970 break;
2971 case EVENT_UNPROT_DEAUTH:
2972 wpa_supplicant_event_unprot_deauth(wpa_s,
2973 &data->unprot_deauth);
2974 break;
2975 case EVENT_UNPROT_DISASSOC:
2976 wpa_supplicant_event_unprot_disassoc(wpa_s,
2977 &data->unprot_disassoc);
2978 break;
2979 case EVENT_STATION_LOW_ACK:
2980#ifdef CONFIG_AP
2981 if (wpa_s->ap_iface && data)
2982 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2983 data->low_ack.addr);
2984#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985#ifdef CONFIG_TDLS
2986 if (data)
2987 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2988#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989 break;
2990 case EVENT_IBSS_PEER_LOST:
2991#ifdef CONFIG_IBSS_RSN
2992 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2993#endif /* CONFIG_IBSS_RSN */
2994 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995 case EVENT_DRIVER_GTK_REKEY:
2996 if (os_memcmp(data->driver_gtk_rekey.bssid,
2997 wpa_s->bssid, ETH_ALEN))
2998 break;
2999 if (!wpa_s->wpa)
3000 break;
3001 wpa_sm_update_replay_ctr(wpa_s->wpa,
3002 data->driver_gtk_rekey.replay_ctr);
3003 break;
3004 case EVENT_SCHED_SCAN_STOPPED:
3005 wpa_s->sched_scanning = 0;
3006 wpa_supplicant_notify_scanning(wpa_s, 0);
3007
3008 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3009 break;
3010
3011 /*
3012 * If we timed out, start a new sched scan to continue
3013 * searching for more SSIDs.
3014 */
3015 if (wpa_s->sched_scan_timed_out)
3016 wpa_supplicant_req_sched_scan(wpa_s);
3017 break;
3018 case EVENT_WPS_BUTTON_PUSHED:
3019#ifdef CONFIG_WPS
3020 wpas_wps_start_pbc(wpa_s, NULL, 0);
3021#endif /* CONFIG_WPS */
3022 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 default:
3024 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3025 break;
3026 }
3027}