blob: b117b62190501e4274653b4f5b9102ef6307e203 [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
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080047static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s);
48
49
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070050static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
51 struct wpa_ssid *ssid)
52{
53 struct os_time now;
54
55 if (ssid == NULL || ssid->disabled_until.sec == 0)
56 return 0;
57
58 os_get_time(&now);
59 if (ssid->disabled_until.sec > now.sec)
60 return ssid->disabled_until.sec - now.sec;
61
62 wpas_clear_temp_disabled(wpa_s, ssid, 0);
63
64 return 0;
65}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066
67
68static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
69{
70 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070071 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
73 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
74 return 0;
75
76 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
77 "information");
78 ssid = wpa_supplicant_get_ssid(wpa_s);
79 if (ssid == NULL) {
80 wpa_msg(wpa_s, MSG_INFO,
81 "No network configuration found for the current AP");
82 return -1;
83 }
84
Dmitry Shmidt04949592012-07-19 12:16:46 -070085 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
87 return -1;
88 }
89
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080090 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
91 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
92 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
93 return -1;
94 }
95
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070096 res = wpas_temp_disabled(wpa_s, ssid);
97 if (res > 0) {
98 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
99 "disabled for %d second(s)", res);
100 return -1;
101 }
102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
104 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800105 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 u8 wpa_ie[80];
107 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800108 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
109 wpa_ie, &wpa_ie_len) < 0)
110 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 } else {
112 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
113 }
114
115 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
116 eapol_sm_invalidate_cached_session(wpa_s->eapol);
117 old_ssid = wpa_s->current_ssid;
118 wpa_s->current_ssid = ssid;
119 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
120 wpa_supplicant_initiate_eapol(wpa_s);
121 if (old_ssid != wpa_s->current_ssid)
122 wpas_notify_network_changed(wpa_s);
123
124 return 0;
125}
126
127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800128void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129{
130 struct wpa_supplicant *wpa_s = eloop_ctx;
131
132 if (wpa_s->countermeasures) {
133 wpa_s->countermeasures = 0;
134 wpa_drv_set_countermeasures(wpa_s, 0);
135 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
136 wpa_supplicant_req_scan(wpa_s, 0, 0);
137 }
138}
139
140
141void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
142{
143 int bssid_changed;
144
Dmitry Shmidt04949592012-07-19 12:16:46 -0700145 wnm_bss_keep_alive_deinit(wpa_s);
146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147#ifdef CONFIG_IBSS_RSN
148 ibss_rsn_deinit(wpa_s->ibss_rsn);
149 wpa_s->ibss_rsn = NULL;
150#endif /* CONFIG_IBSS_RSN */
151
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700152#ifdef CONFIG_AP
153 wpa_supplicant_ap_deinit(wpa_s);
154#endif /* CONFIG_AP */
155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
157 return;
158
159 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800160#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700161 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800162#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
164 os_memset(wpa_s->bssid, 0, ETH_ALEN);
165 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700166#ifdef CONFIG_SME
167 wpa_s->sme.prev_bssid_set = 0;
168#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800169#ifdef CONFIG_P2P
170 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
171#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700172 wpa_s->current_bss = NULL;
173 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700174#ifdef CONFIG_IEEE80211R
175#ifdef CONFIG_SME
176 if (wpa_s->sme.ft_ies)
177 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
178#endif /* CONFIG_SME */
179#endif /* CONFIG_IEEE80211R */
180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181 if (bssid_changed)
182 wpas_notify_bssid_changed(wpa_s);
183
184 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
185 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
186 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
187 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
188 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189 wpa_s->current_ssid = NULL;
190 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191}
192
193
194static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
195{
196 struct wpa_ie_data ie;
197 int pmksa_set = -1;
198 size_t i;
199
200 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
201 ie.pmkid == NULL)
202 return;
203
204 for (i = 0; i < ie.num_pmkid; i++) {
205 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
206 ie.pmkid + i * PMKID_LEN,
207 NULL, NULL, 0);
208 if (pmksa_set == 0) {
209 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
210 break;
211 }
212 }
213
214 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
215 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
216}
217
218
219static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
220 union wpa_event_data *data)
221{
222 if (data == NULL) {
223 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
224 "event");
225 return;
226 }
227 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
228 " index=%d preauth=%d",
229 MAC2STR(data->pmkid_candidate.bssid),
230 data->pmkid_candidate.index,
231 data->pmkid_candidate.preauth);
232
233 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
234 data->pmkid_candidate.index,
235 data->pmkid_candidate.preauth);
236}
237
238
239static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
240{
241 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
242 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
243 return 0;
244
245#ifdef IEEE8021X_EAPOL
246 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
247 wpa_s->current_ssid &&
248 !(wpa_s->current_ssid->eapol_flags &
249 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
250 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
251 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
252 * plaintext or static WEP keys). */
253 return 0;
254 }
255#endif /* IEEE8021X_EAPOL */
256
257 return 1;
258}
259
260
261/**
262 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
263 * @wpa_s: pointer to wpa_supplicant data
264 * @ssid: Configuration data for the network
265 * Returns: 0 on success, -1 on failure
266 *
267 * This function is called when starting authentication with a network that is
268 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
269 */
270int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
271 struct wpa_ssid *ssid)
272{
273#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800274#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 int aka = 0, sim = 0, type;
276
277 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
278 return 0;
279
280 if (ssid->eap.eap_methods == NULL) {
281 sim = 1;
282 aka = 1;
283 } else {
284 struct eap_method_type *eap = ssid->eap.eap_methods;
285 while (eap->vendor != EAP_VENDOR_IETF ||
286 eap->method != EAP_TYPE_NONE) {
287 if (eap->vendor == EAP_VENDOR_IETF) {
288 if (eap->method == EAP_TYPE_SIM)
289 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700290 else if (eap->method == EAP_TYPE_AKA ||
291 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 aka = 1;
293 }
294 eap++;
295 }
296 }
297
298 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
299 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700300 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
301 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
302 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 aka = 0;
304
305 if (!sim && !aka) {
306 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
307 "use SIM, but neither EAP-SIM nor EAP-AKA are "
308 "enabled");
309 return 0;
310 }
311
312 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
313 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
314 if (sim && aka)
315 type = SCARD_TRY_BOTH;
316 else if (aka)
317 type = SCARD_USIM_ONLY;
318 else
319 type = SCARD_GSM_SIM_ONLY;
320
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 if (wpa_s->scard == NULL) {
323 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
324 "(pcsc-lite)");
325 return -1;
326 }
327 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
328 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330#endif /* IEEE8021X_EAPOL */
331
332 return 0;
333}
334
335
336#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700337static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 struct wpa_ssid *ssid)
339{
340 int i, privacy = 0;
341
342 if (ssid->mixed_cell)
343 return 1;
344
345#ifdef CONFIG_WPS
346 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
347 return 1;
348#endif /* CONFIG_WPS */
349
350 for (i = 0; i < NUM_WEP_KEYS; i++) {
351 if (ssid->wep_key_len[i]) {
352 privacy = 1;
353 break;
354 }
355 }
356#ifdef IEEE8021X_EAPOL
357 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
358 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
359 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
360 privacy = 1;
361#endif /* IEEE8021X_EAPOL */
362
Jouni Malinen75ecf522011-06-27 15:19:46 -0700363 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
364 privacy = 1;
365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 if (bss->caps & IEEE80211_CAP_PRIVACY)
367 return privacy;
368 return !privacy;
369}
370
371
372static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
373 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700374 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375{
376 struct wpa_ie_data ie;
377 int proto_match = 0;
378 const u8 *rsn_ie, *wpa_ie;
379 int ret;
380 int wep_ok;
381
382 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
383 if (ret >= 0)
384 return ret;
385
386 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
387 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
388 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
389 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
390 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
391
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700392 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
394 proto_match++;
395
396 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
397 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
398 "failed");
399 break;
400 }
401
402 if (wep_ok &&
403 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
404 {
405 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
406 "in RSN IE");
407 return 1;
408 }
409
410 if (!(ie.proto & ssid->proto)) {
411 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
412 "mismatch");
413 break;
414 }
415
416 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
417 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
418 "cipher mismatch");
419 break;
420 }
421
422 if (!(ie.group_cipher & ssid->group_cipher)) {
423 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
424 "cipher mismatch");
425 break;
426 }
427
428 if (!(ie.key_mgmt & ssid->key_mgmt)) {
429 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
430 "mismatch");
431 break;
432 }
433
434#ifdef CONFIG_IEEE80211W
435 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800436 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
437 wpa_s->conf->pmf : ssid->ieee80211w) ==
438 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
440 "frame protection");
441 break;
442 }
443#endif /* CONFIG_IEEE80211W */
444
445 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
446 return 1;
447 }
448
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700449 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
451 proto_match++;
452
453 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
454 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
455 "failed");
456 break;
457 }
458
459 if (wep_ok &&
460 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
461 {
462 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
463 "in WPA IE");
464 return 1;
465 }
466
467 if (!(ie.proto & ssid->proto)) {
468 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
469 "mismatch");
470 break;
471 }
472
473 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
474 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
475 "cipher mismatch");
476 break;
477 }
478
479 if (!(ie.group_cipher & ssid->group_cipher)) {
480 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
481 "cipher mismatch");
482 break;
483 }
484
485 if (!(ie.key_mgmt & ssid->key_mgmt)) {
486 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
487 "mismatch");
488 break;
489 }
490
491 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
492 return 1;
493 }
494
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700495 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
496 !rsn_ie) {
497 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
498 return 1;
499 }
500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
502 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
503 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
504 return 0;
505 }
506
507 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
508 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
509 return 1;
510 }
511
512 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
513 "WPA/WPA2");
514
515 return 0;
516}
517
518
519static int freq_allowed(int *freqs, int freq)
520{
521 int i;
522
523 if (freqs == NULL)
524 return 1;
525
526 for (i = 0; freqs[i]; i++)
527 if (freqs[i] == freq)
528 return 1;
529 return 0;
530}
531
532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800533static int ht_supported(const struct hostapd_hw_modes *mode)
534{
535 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
536 /*
537 * The driver did not indicate whether it supports HT. Assume
538 * it does to avoid connection issues.
539 */
540 return 1;
541 }
542
543 /*
544 * IEEE Std 802.11n-2009 20.1.1:
545 * An HT non-AP STA shall support all EQM rates for one spatial stream.
546 */
547 return mode->mcs_set[0] == 0xff;
548}
549
550
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700551static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800552{
553 const struct hostapd_hw_modes *mode = NULL, *modes;
554 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
555 const u8 *rate_ie;
556 int i, j, k;
557
558 if (bss->freq == 0)
559 return 1; /* Cannot do matching without knowing band */
560
561 modes = wpa_s->hw.modes;
562 if (modes == NULL) {
563 /*
564 * The driver does not provide any additional information
565 * about the utilized hardware, so allow the connection attempt
566 * to continue.
567 */
568 return 1;
569 }
570
571 for (i = 0; i < wpa_s->hw.num_modes; i++) {
572 for (j = 0; j < modes[i].num_channels; j++) {
573 int freq = modes[i].channels[j].freq;
574 if (freq == bss->freq) {
575 if (mode &&
576 mode->mode == HOSTAPD_MODE_IEEE80211G)
577 break; /* do not allow 802.11b replace
578 * 802.11g */
579 mode = &modes[i];
580 break;
581 }
582 }
583 }
584
585 if (mode == NULL)
586 return 0;
587
588 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700589 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 if (rate_ie == NULL)
591 continue;
592
593 for (j = 2; j < rate_ie[1] + 2; j++) {
594 int flagged = !!(rate_ie[j] & 0x80);
595 int r = (rate_ie[j] & 0x7f) * 5;
596
597 /*
598 * IEEE Std 802.11n-2009 7.3.2.2:
599 * The new BSS Membership selector value is encoded
600 * like a legacy basic rate, but it is not a rate and
601 * only indicates if the BSS members are required to
602 * support the mandatory features of Clause 20 [HT PHY]
603 * in order to join the BSS.
604 */
605 if (flagged && ((rate_ie[j] & 0x7f) ==
606 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
607 if (!ht_supported(mode)) {
608 wpa_dbg(wpa_s, MSG_DEBUG,
609 " hardware does not support "
610 "HT PHY");
611 return 0;
612 }
613 continue;
614 }
615
616 if (!flagged)
617 continue;
618
619 /* check for legacy basic rates */
620 for (k = 0; k < mode->num_rates; k++) {
621 if (mode->rates[k] == r)
622 break;
623 }
624 if (k == mode->num_rates) {
625 /*
626 * IEEE Std 802.11-2007 7.3.2.2 demands that in
627 * order to join a BSS all required rates
628 * have to be supported by the hardware.
629 */
630 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
631 "not support required rate %d.%d Mbps",
632 r / 10, r % 10);
633 return 0;
634 }
635 }
636 }
637
638 return 1;
639}
640
641
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800642static int bss_is_dmg(struct wpa_bss *bss)
643{
644 return bss->freq > 45000;
645}
646
647
648/*
649 * Test whether BSS is in an ESS.
650 * This is done differently in DMG (60 GHz) and non-DMG bands
651 */
652static int bss_is_ess(struct wpa_bss *bss)
653{
654 if (bss_is_dmg(bss)) {
655 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
656 IEEE80211_CAP_DMG_AP;
657 }
658
659 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
660 IEEE80211_CAP_ESS);
661}
662
663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700665 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 struct wpa_ssid *group)
667{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700668 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 int wpa;
670 struct wpa_blacklist *e;
671 const u8 *ie;
672 struct wpa_ssid *ssid;
673
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700674 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 wpa_ie_len = ie ? ie[1] : 0;
676
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700677 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 rsn_ie_len = ie ? ie[1] : 0;
679
680 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
681 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700682 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700684 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685
686 e = wpa_blacklist_get(wpa_s, bss->bssid);
687 if (e) {
688 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 /*
691 * When only a single network is enabled, we can
692 * trigger blacklisting on the first failure. This
693 * should not be done with multiple enabled networks to
694 * avoid getting forced to move into a worse ESS on
695 * single error if there are no other BSSes of the
696 * current ESS.
697 */
698 limit = 0;
699 }
700 if (e->count > limit) {
701 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
702 "(count=%d limit=%d)", e->count, limit);
703 return NULL;
704 }
705 }
706
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700707 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
709 return NULL;
710 }
711
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800712 if (disallowed_bssid(wpa_s, bss->bssid)) {
713 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
714 return NULL;
715 }
716
717 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
718 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
719 return NULL;
720 }
721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
723
724 for (ssid = group; ssid; ssid = ssid->pnext) {
725 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700726 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727
Dmitry Shmidt04949592012-07-19 12:16:46 -0700728 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
730 continue;
731 }
732
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700733 res = wpas_temp_disabled(wpa_s, ssid);
734 if (res > 0) {
735 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
736 "temporarily for %d second(s)", res);
737 continue;
738 }
739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740#ifdef CONFIG_WPS
741 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
742 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
743 "(WPS)");
744 continue;
745 }
746
747 if (wpa && ssid->ssid_len == 0 &&
748 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
749 check_ssid = 0;
750
751 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
752 /* Only allow wildcard SSID match if an AP
753 * advertises active WPS operation that matches
754 * with our mode. */
755 check_ssid = 1;
756 if (ssid->ssid_len == 0 &&
757 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
758 check_ssid = 0;
759 }
760#endif /* CONFIG_WPS */
761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762 if (ssid->bssid_set && ssid->ssid_len == 0 &&
763 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
764 check_ssid = 0;
765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700767 (bss->ssid_len != ssid->ssid_len ||
768 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
770 continue;
771 }
772
773 if (ssid->bssid_set &&
774 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
775 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
776 continue;
777 }
778
779 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
780 continue;
781
782 if (!wpa &&
783 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
784 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
785 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
786 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
787 "not allowed");
788 continue;
789 }
790
Jouni Malinen75ecf522011-06-27 15:19:46 -0700791 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
793 "mismatch");
794 continue;
795 }
796
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800797 if (!bss_is_ess(bss)) {
798 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 continue;
800 }
801
802 if (!freq_allowed(ssid->freq_list, bss->freq)) {
803 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
804 "allowed");
805 continue;
806 }
807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800808 if (!rate_match(wpa_s, bss)) {
809 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
810 "not match");
811 continue;
812 }
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814#ifdef CONFIG_P2P
815 /*
816 * TODO: skip the AP if its P2P IE has Group Formation
817 * bit set in the P2P Group Capability Bitmap and we
818 * are not in Group Formation with that device.
819 */
820#endif /* CONFIG_P2P */
821
822 /* Matching configuration found */
823 return ssid;
824 }
825
826 /* No matching configuration found */
827 return NULL;
828}
829
830
831static struct wpa_bss *
832wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 struct wpa_ssid *group,
834 struct wpa_ssid **selected_ssid)
835{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700836 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837
838 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
839 group->priority);
840
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700841 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
842 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
844 if (!*selected_ssid)
845 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
847 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700848 MAC2STR(bss->bssid),
849 wpa_ssid_txt(bss->ssid, bss->ssid_len));
850 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 }
852
853 return NULL;
854}
855
856
857static struct wpa_bss *
858wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859 struct wpa_ssid **selected_ssid)
860{
861 struct wpa_bss *selected = NULL;
862 int prio;
863
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700864 if (wpa_s->last_scan_res == NULL ||
865 wpa_s->last_scan_res_used == 0)
866 return NULL; /* no scan results from last update */
867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 while (selected == NULL) {
869 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
870 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700871 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 selected_ssid);
873 if (selected)
874 break;
875 }
876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877 if (selected == NULL && wpa_s->blacklist &&
878 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
880 "blacklist and try again");
881 wpa_blacklist_clear(wpa_s);
882 wpa_s->blacklist_cleared++;
883 } else if (selected == NULL)
884 break;
885 }
886
887 return selected;
888}
889
890
891static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
892 int timeout_sec, int timeout_usec)
893{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 /*
896 * No networks are enabled; short-circuit request so
897 * we don't wait timeout seconds before transitioning
898 * to INACTIVE state.
899 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700900 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
901 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700903#ifdef CONFIG_P2P
904 wpa_s->sta_scan_pending = 0;
905#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return;
907 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800908
909 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
911}
912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700914int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 struct wpa_bss *selected,
916 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917{
918 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
919 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
920 "PBC session overlap");
921#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700923 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924#endif /* CONFIG_P2P */
925
926#ifdef CONFIG_WPS
927 wpas_wps_cancel(wpa_s);
928#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700929 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 }
931
932 /*
933 * Do not trigger new association unless the BSSID has changed or if
934 * reassociation is requested. If we are in process of associating with
935 * the selected BSSID, do not trigger new attempt.
936 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
939 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
940 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
941 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
942 0))) {
943 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
944 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700945 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 }
947 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
948 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
949 " pending: " MACSTR " wpa_state: %s",
950 wpa_s->reassociate, MAC2STR(selected->bssid),
951 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
952 wpa_supplicant_state_txt(wpa_s->wpa_state));
953 wpa_supplicant_associate(wpa_s, selected, ssid);
954 } else {
955 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
956 "selected AP");
957 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800958
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700959 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960}
961
962
963static struct wpa_ssid *
964wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
965{
966 int prio;
967 struct wpa_ssid *ssid;
968
969 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
970 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
971 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 continue;
974 if (ssid->mode == IEEE80211_MODE_IBSS ||
975 ssid->mode == IEEE80211_MODE_AP)
976 return ssid;
977 }
978 }
979 return NULL;
980}
981
982
983/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
984 * on BSS added and BSS changed events */
985static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300986 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300988 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
990 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
991 return;
992
Jouni Malinen87fd2792011-05-16 18:35:42 +0300993 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995
Jouni Malinen87fd2792011-05-16 18:35:42 +0300996 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 if (ssid == NULL)
998 continue;
999
Jouni Malinen87fd2792011-05-16 18:35:42 +03001000 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 if (rsn == NULL)
1002 continue;
1003
Jouni Malinen87fd2792011-05-16 18:35:42 +03001004 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
1006
1007}
1008
1009
1010static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1011 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001012 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001014 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 int min_diff;
1016
1017 if (wpa_s->reassociate)
1018 return 1; /* explicit request to reassociate */
1019 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1020 return 1; /* we are not associated; continue */
1021 if (wpa_s->current_ssid == NULL)
1022 return 1; /* unknown current SSID */
1023 if (wpa_s->current_ssid != ssid)
1024 return 1; /* different network block */
1025
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026 if (wpas_driver_bss_selection(wpa_s))
1027 return 0; /* Driver-based roaming */
1028
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001029 if (wpa_s->current_ssid->ssid)
1030 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1031 wpa_s->current_ssid->ssid,
1032 wpa_s->current_ssid->ssid_len);
1033 if (!current_bss)
1034 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035
1036 if (!current_bss)
1037 return 1; /* current BSS not seen in scan results */
1038
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001039 if (current_bss == selected)
1040 return 0;
1041
1042 if (selected->last_update_idx > current_bss->last_update_idx)
1043 return 1; /* current BSS not seen in the last scan */
1044
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001045#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1047 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1048 MAC2STR(current_bss->bssid), current_bss->level);
1049 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1050 MAC2STR(selected->bssid), selected->level);
1051
1052 if (wpa_s->current_ssid->bssid_set &&
1053 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1054 0) {
1055 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1056 "has preferred BSSID");
1057 return 1;
1058 }
1059
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001060 if (current_bss->level < 0 && current_bss->level > selected->level) {
1061 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1062 "signal level");
1063 return 0;
1064 }
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 min_diff = 2;
1067 if (current_bss->level < 0) {
1068 if (current_bss->level < -85)
1069 min_diff = 1;
1070 else if (current_bss->level < -80)
1071 min_diff = 2;
1072 else if (current_bss->level < -75)
1073 min_diff = 3;
1074 else if (current_bss->level < -70)
1075 min_diff = 4;
1076 else
1077 min_diff = 5;
1078 }
1079 if (abs(current_bss->level - selected->level) < min_diff) {
1080 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1081 "in signal level");
1082 return 0;
1083 }
1084
1085 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001087 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001088#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001091
Jouni Malinen89ca7022012-09-14 13:03:12 -07001092/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001093 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001094static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001095 union wpa_event_data *data,
1096 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 struct wpa_scan_results *scan_res;
1099 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001100#ifndef CONFIG_NO_RANDOM_POOL
1101 size_t i, num;
1102#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
1104#ifdef CONFIG_AP
1105 if (wpa_s->ap_iface)
1106 ap = 1;
1107#endif /* CONFIG_AP */
1108
1109 wpa_supplicant_notify_scanning(wpa_s, 0);
1110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001111#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001112 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001113 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001114 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1115 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001116 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1118 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1119 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001120 wpa_s->sta_scan_pending = 1;
1121 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001122 return -1;
1123 }
1124 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001125 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001126#endif /* CONFIG_P2P */
1127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1129 data ? &data->scan_info :
1130 NULL, 1);
1131 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001132 if (wpa_s->conf->ap_scan == 2 || ap ||
1133 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001135 if (!own_request)
1136 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1138 "scanning again");
1139 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1140 return -1;
1141 }
1142
1143#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 num = scan_res->num;
1145 if (num > 10)
1146 num = 10;
1147 for (i = 0; i < num; i++) {
1148 u8 buf[5];
1149 struct wpa_scan_res *res = scan_res->res[i];
1150 buf[0] = res->bssid[5];
1151 buf[1] = res->qual & 0xff;
1152 buf[2] = res->noise & 0xff;
1153 buf[3] = res->level & 0xff;
1154 buf[4] = res->tsf & 0xff;
1155 random_add_randomness(buf, sizeof(buf));
1156 }
1157#endif /* CONFIG_NO_RANDOM_POOL */
1158
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001159 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1161 struct wpa_scan_results *scan_res);
1162
1163 scan_res_handler = wpa_s->scan_res_handler;
1164 wpa_s->scan_res_handler = NULL;
1165 scan_res_handler(wpa_s, scan_res);
1166
1167 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001168 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 }
1170
1171 if (ap) {
1172 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1173#ifdef CONFIG_AP
1174 if (wpa_s->ap_iface->scan_cb)
1175 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1176#endif /* CONFIG_AP */
1177 wpa_scan_results_free(scan_res);
1178 return 0;
1179 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001180
Dmitry Shmidt04949592012-07-19 12:16:46 -07001181 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1182 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1183 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184
1185 wpas_notify_scan_done(wpa_s, 1);
1186
Dmitry Shmidt04949592012-07-19 12:16:46 -07001187 if (sme_proc_obss_scan(wpa_s) > 0) {
1188 wpa_scan_results_free(scan_res);
1189 return 0;
1190 }
1191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1193 wpa_scan_results_free(scan_res);
1194 return 0;
1195 }
1196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 if (autoscan_notify_scan(wpa_s, scan_res)) {
1198 wpa_scan_results_free(scan_res);
1199 return 0;
1200 }
1201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 if (wpa_s->disconnected) {
1203 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1204 wpa_scan_results_free(scan_res);
1205 return 0;
1206 }
1207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001208 if (!wpas_driver_bss_selection(wpa_s) &&
1209 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_scan_results_free(scan_res);
1211 return 0;
1212 }
1213
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001214 wpas_wps_update_ap_info(wpa_s, scan_res);
1215
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001216 wpa_scan_results_free(scan_res);
1217
1218 return wpas_select_network_from_last_scan(wpa_s);
1219}
1220
1221
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001222static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001223{
1224 struct wpa_bss *selected;
1225 struct wpa_ssid *ssid = NULL;
1226
1227 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228
1229 if (selected) {
1230 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001231 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 if (skip) {
1233 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001235 }
1236
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001237 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001239 return -1;
1240 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001241 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001242 /*
1243 * Do not notify other virtual radios of scan results since we do not
1244 * want them to start other associations at the same time.
1245 */
1246 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1249 ssid = wpa_supplicant_pick_new_network(wpa_s);
1250 if (ssid) {
1251 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1252 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001253 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 } else {
1255 int timeout_sec = wpa_s->scan_interval;
1256 int timeout_usec = 0;
1257#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001258 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1259 return 0;
1260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 if (wpa_s->p2p_in_provisioning) {
1262 /*
1263 * Use shorter wait during P2P Provisioning
1264 * state to speed up group formation.
1265 */
1266 timeout_sec = 0;
1267 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001268 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1269 timeout_usec);
1270 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 }
1272#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001273#ifdef CONFIG_INTERWORKING
1274 if (wpa_s->conf->auto_interworking &&
1275 wpa_s->conf->interworking &&
1276 wpa_s->conf->cred) {
1277 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1278 "start ANQP fetch since no matching "
1279 "networks found");
1280 wpa_s->network_select = 1;
1281 wpa_s->auto_network_select = 1;
1282 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001283 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001284 }
1285#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001286 if (wpa_supplicant_req_sched_scan(wpa_s))
1287 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1288 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 }
1290 }
1291 return 0;
1292}
1293
1294
1295static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1296 union wpa_event_data *data)
1297{
1298 const char *rn, *rn2;
1299 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001300
1301 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 /*
1303 * If no scan results could be fetched, then no need to
1304 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001305 * this scan. Similarly, if scan results started a new operation on this
1306 * interface, do not notify other interfaces to avoid concurrent
1307 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 */
1309 return;
1310 }
1311
1312 /*
1313 * Check other interfaces to see if they have the same radio-name. If
1314 * so, they get updated with this same scan info.
1315 */
1316 if (!wpa_s->driver->get_radio_name)
1317 return;
1318
1319 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1320 if (rn == NULL || rn[0] == '\0')
1321 return;
1322
1323 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1324 "sharing same radio (%s) in event_scan_results", rn);
1325
1326 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1327 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1328 continue;
1329
1330 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1331 if (rn2 && os_strcmp(rn, rn2) == 0) {
1332 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1333 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001334 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 }
1336 }
1337}
1338
1339#endif /* CONFIG_NO_SCAN_PROCESSING */
1340
1341
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001342int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1343{
1344#ifdef CONFIG_NO_SCAN_PROCESSING
1345 return -1;
1346#else /* CONFIG_NO_SCAN_PROCESSING */
1347 struct os_time now;
1348
1349 if (wpa_s->last_scan_res_used <= 0)
1350 return -1;
1351
1352 os_get_time(&now);
1353 if (now.sec - wpa_s->last_scan.sec > 5) {
1354 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1355 return -1;
1356 }
1357
1358 return wpas_select_network_from_last_scan(wpa_s);
1359#endif /* CONFIG_NO_SCAN_PROCESSING */
1360}
1361
Dmitry Shmidt04949592012-07-19 12:16:46 -07001362#ifdef CONFIG_WNM
1363
1364static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1365{
1366 struct wpa_supplicant *wpa_s = eloop_ctx;
1367
1368 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1369 return;
1370
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001371 if (!wpa_s->no_keep_alive) {
1372 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1373 MAC2STR(wpa_s->bssid));
1374 /* TODO: could skip this if normal data traffic has been sent */
1375 /* TODO: Consider using some more appropriate data frame for
1376 * this */
1377 if (wpa_s->l2)
1378 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1379 (u8 *) "", 0);
1380 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001381
1382#ifdef CONFIG_SME
1383 if (wpa_s->sme.bss_max_idle_period) {
1384 unsigned int msec;
1385 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1386 if (msec > 100)
1387 msec -= 100;
1388 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1389 wnm_bss_keep_alive, wpa_s, NULL);
1390 }
1391#endif /* CONFIG_SME */
1392}
1393
1394
1395static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1396 const u8 *ies, size_t ies_len)
1397{
1398 struct ieee802_11_elems elems;
1399
1400 if (ies == NULL)
1401 return;
1402
1403 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1404 return;
1405
1406#ifdef CONFIG_SME
1407 if (elems.bss_max_idle_period) {
1408 unsigned int msec;
1409 wpa_s->sme.bss_max_idle_period =
1410 WPA_GET_LE16(elems.bss_max_idle_period);
1411 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1412 "TU)%s", wpa_s->sme.bss_max_idle_period,
1413 (elems.bss_max_idle_period[2] & 0x01) ?
1414 " (protected keep-live required)" : "");
1415 if (wpa_s->sme.bss_max_idle_period == 0)
1416 wpa_s->sme.bss_max_idle_period = 1;
1417 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1418 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1419 /* msec times 1000 */
1420 msec = wpa_s->sme.bss_max_idle_period * 1024;
1421 if (msec > 100)
1422 msec -= 100;
1423 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1424 wnm_bss_keep_alive, wpa_s,
1425 NULL);
1426 }
1427 }
1428#endif /* CONFIG_SME */
1429}
1430
1431#endif /* CONFIG_WNM */
1432
1433
1434void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1435{
1436#ifdef CONFIG_WNM
1437 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1438#endif /* CONFIG_WNM */
1439}
1440
1441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1443 union wpa_event_data *data)
1444{
1445 int l, len, found = 0, wpa_found, rsn_found;
1446 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001447#ifdef CONFIG_IEEE80211R
1448 u8 bssid[ETH_ALEN];
1449#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450
1451 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1452 if (data->assoc_info.req_ies)
1453 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1454 data->assoc_info.req_ies_len);
1455 if (data->assoc_info.resp_ies) {
1456 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1457 data->assoc_info.resp_ies_len);
1458#ifdef CONFIG_TDLS
1459 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1460 data->assoc_info.resp_ies_len);
1461#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001462#ifdef CONFIG_WNM
1463 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1464 data->assoc_info.resp_ies_len);
1465#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 }
1467 if (data->assoc_info.beacon_ies)
1468 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1469 data->assoc_info.beacon_ies,
1470 data->assoc_info.beacon_ies_len);
1471 if (data->assoc_info.freq)
1472 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1473 data->assoc_info.freq);
1474
1475 p = data->assoc_info.req_ies;
1476 l = data->assoc_info.req_ies_len;
1477
1478 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1479 while (p && l >= 2) {
1480 len = p[1] + 2;
1481 if (len > l) {
1482 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1483 p, l);
1484 break;
1485 }
1486 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1487 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1488 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1489 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1490 break;
1491 found = 1;
1492 wpa_find_assoc_pmkid(wpa_s);
1493 break;
1494 }
1495 l -= len;
1496 p += len;
1497 }
1498 if (!found && data->assoc_info.req_ies)
1499 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1500
1501#ifdef CONFIG_IEEE80211R
1502#ifdef CONFIG_SME
1503 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1505 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1506 data->assoc_info.resp_ies,
1507 data->assoc_info.resp_ies_len,
1508 bssid) < 0) {
1509 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1510 "Reassociation Response failed");
1511 wpa_supplicant_deauthenticate(
1512 wpa_s, WLAN_REASON_INVALID_IE);
1513 return -1;
1514 }
1515 }
1516
1517 p = data->assoc_info.resp_ies;
1518 l = data->assoc_info.resp_ies_len;
1519
1520#ifdef CONFIG_WPS_STRICT
1521 if (p && wpa_s->current_ssid &&
1522 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1523 struct wpabuf *wps;
1524 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1525 if (wps == NULL) {
1526 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1527 "include WPS IE in (Re)Association Response");
1528 return -1;
1529 }
1530
1531 if (wps_validate_assoc_resp(wps) < 0) {
1532 wpabuf_free(wps);
1533 wpa_supplicant_deauthenticate(
1534 wpa_s, WLAN_REASON_INVALID_IE);
1535 return -1;
1536 }
1537 wpabuf_free(wps);
1538 }
1539#endif /* CONFIG_WPS_STRICT */
1540
1541 /* Go through the IEs and make a copy of the MDIE, if present. */
1542 while (p && l >= 2) {
1543 len = p[1] + 2;
1544 if (len > l) {
1545 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1546 p, l);
1547 break;
1548 }
1549 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1550 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1551 wpa_s->sme.ft_used = 1;
1552 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1553 MOBILITY_DOMAIN_ID_LEN);
1554 break;
1555 }
1556 l -= len;
1557 p += len;
1558 }
1559#endif /* CONFIG_SME */
1560
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001561 /* Process FT when SME is in the driver */
1562 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1563 wpa_ft_is_completed(wpa_s->wpa)) {
1564 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1565 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1566 data->assoc_info.resp_ies,
1567 data->assoc_info.resp_ies_len,
1568 bssid) < 0) {
1569 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1570 "Reassociation Response failed");
1571 wpa_supplicant_deauthenticate(
1572 wpa_s, WLAN_REASON_INVALID_IE);
1573 return -1;
1574 }
1575 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1576 }
1577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1579 data->assoc_info.resp_ies_len);
1580#endif /* CONFIG_IEEE80211R */
1581
1582 /* WPA/RSN IE from Beacon/ProbeResp */
1583 p = data->assoc_info.beacon_ies;
1584 l = data->assoc_info.beacon_ies_len;
1585
1586 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1587 */
1588 wpa_found = rsn_found = 0;
1589 while (p && l >= 2) {
1590 len = p[1] + 2;
1591 if (len > l) {
1592 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1593 p, l);
1594 break;
1595 }
1596 if (!wpa_found &&
1597 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1598 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1599 wpa_found = 1;
1600 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1601 }
1602
1603 if (!rsn_found &&
1604 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1605 rsn_found = 1;
1606 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1607 }
1608
1609 l -= len;
1610 p += len;
1611 }
1612
1613 if (!wpa_found && data->assoc_info.beacon_ies)
1614 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1615 if (!rsn_found && data->assoc_info.beacon_ies)
1616 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1617 if (wpa_found || rsn_found)
1618 wpa_s->ap_ies_from_associnfo = 1;
1619
Jouni Malinen87fd2792011-05-16 18:35:42 +03001620 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1621 wpa_s->assoc_freq != data->assoc_info.freq) {
1622 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1623 "%u to %u MHz",
1624 wpa_s->assoc_freq, data->assoc_info.freq);
1625 wpa_supplicant_update_scan_results(wpa_s);
1626 }
1627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 wpa_s->assoc_freq = data->assoc_info.freq;
1629
1630 return 0;
1631}
1632
1633
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001634static struct wpa_bss * wpa_supplicant_get_new_bss(
1635 struct wpa_supplicant *wpa_s, const u8 *bssid)
1636{
1637 struct wpa_bss *bss = NULL;
1638 struct wpa_ssid *ssid = wpa_s->current_ssid;
1639
1640 if (ssid->ssid_len > 0)
1641 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1642 if (!bss)
1643 bss = wpa_bss_get_bssid(wpa_s, bssid);
1644
1645 return bss;
1646}
1647
1648
1649static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1650{
1651 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1652
1653 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1654 return -1;
1655
1656 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1657 return 0;
1658
1659 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1660 WPA_IE_VENDOR_TYPE);
1661 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1662
1663 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1664 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1665 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1666 bss_rsn ? 2 + bss_rsn[1] : 0))
1667 return -1;
1668
1669 return 0;
1670}
1671
1672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1674 union wpa_event_data *data)
1675{
1676 u8 bssid[ETH_ALEN];
1677 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 struct wpa_driver_capa capa;
1679
1680#ifdef CONFIG_AP
1681 if (wpa_s->ap_iface) {
1682 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1683 data->assoc_info.addr,
1684 data->assoc_info.req_ies,
1685 data->assoc_info.req_ies_len,
1686 data->assoc_info.reassoc);
1687 return;
1688 }
1689#endif /* CONFIG_AP */
1690
1691 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1692 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1693 return;
1694
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001695 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1696 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001697 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001698 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1699 return;
1700 }
1701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001703 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1705 MACSTR, MAC2STR(bssid));
1706 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1708 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001709 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710
1711 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1712 wpa_clear_keys(wpa_s, bssid);
1713 }
1714 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001715 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1717 return;
1718 }
1719 if (wpa_s->current_ssid) {
1720 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001721
1722 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1723 if (!bss) {
1724 wpa_supplicant_update_scan_results(wpa_s);
1725
1726 /* Get the BSS from the new scan results */
1727 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1728 }
1729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 if (bss)
1731 wpa_s->current_bss = bss;
1732 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001733
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001734#ifdef ANDROID
1735 if (wpa_s->conf->ap_scan == 1) {
1736#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001737 if (wpa_s->conf->ap_scan == 1 &&
1738 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001739#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001740 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1741 wpa_msg(wpa_s, MSG_WARNING,
1742 "WPA/RSN IEs not updated");
1743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001744 }
1745
1746#ifdef CONFIG_SME
1747 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1748 wpa_s->sme.prev_bssid_set = 1;
1749#endif /* CONFIG_SME */
1750
1751 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1752 if (wpa_s->current_ssid) {
1753 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1754 * initialized before association, but for other modes,
1755 * initialize PC/SC here, if the current configuration needs
1756 * smartcard or SIM/USIM. */
1757 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1758 }
1759 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1760 if (wpa_s->l2)
1761 l2_packet_notify_auth_start(wpa_s->l2);
1762
1763 /*
1764 * Set portEnabled first to FALSE in order to get EAP state machine out
1765 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1766 * state machine may transit to AUTHENTICATING state based on obsolete
1767 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1768 * AUTHENTICATED without ever giving chance to EAP state machine to
1769 * reset the state.
1770 */
1771 if (!ft_completed) {
1772 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1773 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1774 }
1775 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1776 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1777 /* 802.1X::portControl = Auto */
1778 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1779 wpa_s->eapol_received = 0;
1780 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1781 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1782 (wpa_s->current_ssid &&
1783 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1784 wpa_supplicant_cancel_auth_timeout(wpa_s);
1785 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1786 } else if (!ft_completed) {
1787 /* Timeout for receiving the first EAPOL packet */
1788 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1789 }
1790 wpa_supplicant_cancel_scan(wpa_s);
1791
1792 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1793 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1794 /*
1795 * We are done; the driver will take care of RSN 4-way
1796 * handshake.
1797 */
1798 wpa_supplicant_cancel_auth_timeout(wpa_s);
1799 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1800 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1801 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1802 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1803 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1804 /*
1805 * The driver will take care of RSN 4-way handshake, so we need
1806 * to allow EAPOL supplicant to complete its work without
1807 * waiting for WPA supplicant.
1808 */
1809 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1810 } else if (ft_completed) {
1811 /*
1812 * FT protocol completed - make sure EAPOL state machine ends
1813 * up in authenticated.
1814 */
1815 wpa_supplicant_cancel_auth_timeout(wpa_s);
1816 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1817 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1818 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1819 }
1820
Jouni Malinena05074c2012-12-21 21:35:35 +02001821 wpa_s->last_eapol_matches_bssid = 0;
1822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 if (wpa_s->pending_eapol_rx) {
1824 struct os_time now, age;
1825 os_get_time(&now);
1826 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1827 if (age.sec == 0 && age.usec < 100000 &&
1828 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1829 0) {
1830 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1831 "frame that was received just before "
1832 "association notification");
1833 wpa_supplicant_rx_eapol(
1834 wpa_s, wpa_s->pending_eapol_rx_src,
1835 wpabuf_head(wpa_s->pending_eapol_rx),
1836 wpabuf_len(wpa_s->pending_eapol_rx));
1837 }
1838 wpabuf_free(wpa_s->pending_eapol_rx);
1839 wpa_s->pending_eapol_rx = NULL;
1840 }
1841
1842 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1843 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1844 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1845 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1846 /* Set static WEP keys again */
1847 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1848 }
1849
1850#ifdef CONFIG_IBSS_RSN
1851 if (wpa_s->current_ssid &&
1852 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1853 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1854 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1855 wpa_s->ibss_rsn == NULL) {
1856 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1857 if (!wpa_s->ibss_rsn) {
1858 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1859 wpa_supplicant_deauthenticate(
1860 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1861 return;
1862 }
1863
1864 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1865 }
1866#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001867
1868 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869}
1870
1871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001872static int disconnect_reason_recoverable(u16 reason_code)
1873{
1874 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1875 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1876 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1877}
1878
1879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001881 u16 reason_code,
1882 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883{
1884 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001885
1886 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1887 /*
1888 * At least Host AP driver and a Prism3 card seemed to be
1889 * generating streams of disconnected events when configuring
1890 * IBSS for WPA-None. Ignore them for now.
1891 */
1892 return;
1893 }
1894
1895 bssid = wpa_s->bssid;
1896 if (is_zero_ether_addr(bssid))
1897 bssid = wpa_s->pending_bssid;
1898
1899 if (!is_zero_ether_addr(bssid) ||
1900 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1901 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1902 " reason=%d%s",
1903 MAC2STR(bssid), reason_code,
1904 locally_generated ? " locally_generated=1" : "");
1905 }
1906}
1907
1908
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001909static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1910 int locally_generated)
1911{
1912 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1913 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1914 return 0; /* Not in 4-way handshake with PSK */
1915
1916 /*
1917 * It looks like connection was lost while trying to go through PSK
1918 * 4-way handshake. Filter out known disconnection cases that are caused
1919 * by something else than PSK mismatch to avoid confusing reports.
1920 */
1921
1922 if (locally_generated) {
1923 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1924 return 0;
1925 }
1926
1927 return 1;
1928}
1929
1930
Jouni Malinen2b89da82012-08-31 22:04:41 +03001931static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1932 u16 reason_code,
1933 int locally_generated)
1934{
1935 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936 int authenticating;
1937 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001938 struct wpa_bss *fast_reconnect = NULL;
1939 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001940 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941
1942 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1943 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1944
1945 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1946 /*
1947 * At least Host AP driver and a Prism3 card seemed to be
1948 * generating streams of disconnected events when configuring
1949 * IBSS for WPA-None. Ignore them for now.
1950 */
1951 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1952 "IBSS/WPA-None mode");
1953 return;
1954 }
1955
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001956 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1958 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001959 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 }
1961 if (!wpa_s->auto_reconnect_disabled ||
1962 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1964 "reconnect (wps=%d wpa_state=%d)",
1965 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1966 wpa_s->wpa_state);
1967 if (wpa_s->wpa_state == WPA_COMPLETED &&
1968 wpa_s->current_ssid &&
1969 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001970 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001971 disconnect_reason_recoverable(reason_code)) {
1972 /*
1973 * It looks like the AP has dropped association with
1974 * us, but could allow us to get back in. Try to
1975 * reconnect to the same BSS without full scan to save
1976 * time for some common cases.
1977 */
1978 fast_reconnect = wpa_s->current_bss;
1979 fast_reconnect_ssid = wpa_s->current_ssid;
1980 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001981#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001982 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001983#else
1984 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1985#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986 else
1987 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1988 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001991 "try to re-connect");
1992 wpa_s->reassociate = 0;
1993 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001994 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 }
1996 bssid = wpa_s->bssid;
1997 if (is_zero_ether_addr(bssid))
1998 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001999 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2000 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002002 if (locally_generated)
2003 wpa_s->disconnect_reason = -reason_code;
2004 else
2005 wpa_s->disconnect_reason = reason_code;
2006 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2008 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2009 wpa_s->keys_cleared = 0;
2010 wpa_clear_keys(wpa_s, wpa_s->bssid);
2011 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002012 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 wpa_supplicant_mark_disassoc(wpa_s);
2014
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002015 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002017 wpa_s->current_ssid = last_ssid;
2018 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002019
2020 if (fast_reconnect) {
2021#ifndef CONFIG_NO_SCAN_PROCESSING
2022 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2023 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2024 fast_reconnect_ssid) < 0) {
2025 /* Recover through full scan */
2026 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2027 }
2028#endif /* CONFIG_NO_SCAN_PROCESSING */
2029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030}
2031
2032
2033#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002034void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035{
2036 struct wpa_supplicant *wpa_s = eloop_ctx;
2037
2038 if (!wpa_s->pending_mic_error_report)
2039 return;
2040
2041 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2042 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2043 wpa_s->pending_mic_error_report = 0;
2044}
2045#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2046
2047
2048static void
2049wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2050 union wpa_event_data *data)
2051{
2052 int pairwise;
2053 struct os_time t;
2054
2055 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2056 pairwise = (data && data->michael_mic_failure.unicast);
2057 os_get_time(&t);
2058 if ((wpa_s->last_michael_mic_error &&
2059 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2060 wpa_s->pending_mic_error_report) {
2061 if (wpa_s->pending_mic_error_report) {
2062 /*
2063 * Send the pending MIC error report immediately since
2064 * we are going to start countermeasures and AP better
2065 * do the same.
2066 */
2067 wpa_sm_key_request(wpa_s->wpa, 1,
2068 wpa_s->pending_mic_error_pairwise);
2069 }
2070
2071 /* Send the new MIC error report immediately since we are going
2072 * to start countermeasures and AP better do the same.
2073 */
2074 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2075
2076 /* initialize countermeasures */
2077 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002078
2079 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2082
2083 /*
2084 * Need to wait for completion of request frame. We do not get
2085 * any callback for the message completion, so just wait a
2086 * short while and hope for the best. */
2087 os_sleep(0, 10000);
2088
2089 wpa_drv_set_countermeasures(wpa_s, 1);
2090 wpa_supplicant_deauthenticate(wpa_s,
2091 WLAN_REASON_MICHAEL_MIC_FAILURE);
2092 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2093 wpa_s, NULL);
2094 eloop_register_timeout(60, 0,
2095 wpa_supplicant_stop_countermeasures,
2096 wpa_s, NULL);
2097 /* TODO: mark the AP rejected for 60 second. STA is
2098 * allowed to associate with another AP.. */
2099 } else {
2100#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2101 if (wpa_s->mic_errors_seen) {
2102 /*
2103 * Reduce the effectiveness of Michael MIC error
2104 * reports as a means for attacking against TKIP if
2105 * more than one MIC failure is noticed with the same
2106 * PTK. We delay the transmission of the reports by a
2107 * random time between 0 and 60 seconds in order to
2108 * force the attacker wait 60 seconds before getting
2109 * the information on whether a frame resulted in a MIC
2110 * failure.
2111 */
2112 u8 rval[4];
2113 int sec;
2114
2115 if (os_get_random(rval, sizeof(rval)) < 0)
2116 sec = os_random() % 60;
2117 else
2118 sec = WPA_GET_BE32(rval) % 60;
2119 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2120 "report %d seconds", sec);
2121 wpa_s->pending_mic_error_report = 1;
2122 wpa_s->pending_mic_error_pairwise = pairwise;
2123 eloop_cancel_timeout(
2124 wpa_supplicant_delayed_mic_error_report,
2125 wpa_s, NULL);
2126 eloop_register_timeout(
2127 sec, os_random() % 1000000,
2128 wpa_supplicant_delayed_mic_error_report,
2129 wpa_s, NULL);
2130 } else {
2131 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2132 }
2133#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2134 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2135#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2136 }
2137 wpa_s->last_michael_mic_error = t.sec;
2138 wpa_s->mic_errors_seen++;
2139}
2140
2141
2142#ifdef CONFIG_TERMINATE_ONLASTIF
2143static int any_interfaces(struct wpa_supplicant *head)
2144{
2145 struct wpa_supplicant *wpa_s;
2146
2147 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2148 if (!wpa_s->interface_removed)
2149 return 1;
2150 return 0;
2151}
2152#endif /* CONFIG_TERMINATE_ONLASTIF */
2153
2154
2155static void
2156wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2157 union wpa_event_data *data)
2158{
2159 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2160 return;
2161
2162 switch (data->interface_status.ievent) {
2163 case EVENT_INTERFACE_ADDED:
2164 if (!wpa_s->interface_removed)
2165 break;
2166 wpa_s->interface_removed = 0;
2167 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2168 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2169 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2170 "driver after interface was added");
2171 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002172 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 break;
2174 case EVENT_INTERFACE_REMOVED:
2175 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2176 wpa_s->interface_removed = 1;
2177 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002178 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 l2_packet_deinit(wpa_s->l2);
2180 wpa_s->l2 = NULL;
2181#ifdef CONFIG_IBSS_RSN
2182 ibss_rsn_deinit(wpa_s->ibss_rsn);
2183 wpa_s->ibss_rsn = NULL;
2184#endif /* CONFIG_IBSS_RSN */
2185#ifdef CONFIG_TERMINATE_ONLASTIF
2186 /* check if last interface */
2187 if (!any_interfaces(wpa_s->global->ifaces))
2188 eloop_terminate();
2189#endif /* CONFIG_TERMINATE_ONLASTIF */
2190 break;
2191 }
2192}
2193
2194
2195#ifdef CONFIG_PEERKEY
2196static void
2197wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2198 union wpa_event_data *data)
2199{
2200 if (data == NULL)
2201 return;
2202 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2203}
2204#endif /* CONFIG_PEERKEY */
2205
2206
2207#ifdef CONFIG_TDLS
2208static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2209 union wpa_event_data *data)
2210{
2211 if (data == NULL)
2212 return;
2213 switch (data->tdls.oper) {
2214 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002215 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2216 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2217 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2218 else
2219 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220 break;
2221 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002222 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002223 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 break;
2225 }
2226}
2227#endif /* CONFIG_TDLS */
2228
2229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002230#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002231static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2232 union wpa_event_data *data)
2233{
2234 if (data == NULL)
2235 return;
2236 switch (data->wnm.oper) {
2237 case WNM_OPER_SLEEP:
2238 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2239 "(action=%d, intval=%d)",
2240 data->wnm.sleep_action, data->wnm.sleep_intval);
2241 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002242 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002243 break;
2244 }
2245}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002246#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002247
2248
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249#ifdef CONFIG_IEEE80211R
2250static void
2251wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2252 union wpa_event_data *data)
2253{
2254 if (data == NULL)
2255 return;
2256
2257 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2258 data->ft_ies.ies_len,
2259 data->ft_ies.ft_action,
2260 data->ft_ies.target_ap,
2261 data->ft_ies.ric_ies,
2262 data->ft_ies.ric_ies_len) < 0) {
2263 /* TODO: prevent MLME/driver from trying to associate? */
2264 }
2265}
2266#endif /* CONFIG_IEEE80211R */
2267
2268
2269#ifdef CONFIG_IBSS_RSN
2270static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2271 union wpa_event_data *data)
2272{
2273 struct wpa_ssid *ssid;
2274 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2275 return;
2276 if (data == NULL)
2277 return;
2278 ssid = wpa_s->current_ssid;
2279 if (ssid == NULL)
2280 return;
2281 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2282 return;
2283
2284 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2285}
2286#endif /* CONFIG_IBSS_RSN */
2287
2288
2289#ifdef CONFIG_IEEE80211R
2290static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2291 size_t len)
2292{
2293 const u8 *sta_addr, *target_ap_addr;
2294 u16 status;
2295
2296 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2297 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2298 return; /* only SME case supported for now */
2299 if (len < 1 + 2 * ETH_ALEN + 2)
2300 return;
2301 if (data[0] != 2)
2302 return; /* Only FT Action Response is supported for now */
2303 sta_addr = data + 1;
2304 target_ap_addr = data + 1 + ETH_ALEN;
2305 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2306 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2307 MACSTR " TargetAP " MACSTR " status %u",
2308 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2309
2310 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2311 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2312 " in FT Action Response", MAC2STR(sta_addr));
2313 return;
2314 }
2315
2316 if (status) {
2317 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2318 "failure (status code %d)", status);
2319 /* TODO: report error to FT code(?) */
2320 return;
2321 }
2322
2323 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2324 len - (1 + 2 * ETH_ALEN + 2), 1,
2325 target_ap_addr, NULL, 0) < 0)
2326 return;
2327
2328#ifdef CONFIG_SME
2329 {
2330 struct wpa_bss *bss;
2331 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2332 if (bss)
2333 wpa_s->sme.freq = bss->freq;
2334 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2335 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2336 WLAN_AUTH_FT);
2337 }
2338#endif /* CONFIG_SME */
2339}
2340#endif /* CONFIG_IEEE80211R */
2341
2342
2343static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2344 struct unprot_deauth *e)
2345{
2346#ifdef CONFIG_IEEE80211W
2347 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2348 "dropped: " MACSTR " -> " MACSTR
2349 " (reason code %u)",
2350 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2351 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2352#endif /* CONFIG_IEEE80211W */
2353}
2354
2355
2356static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2357 struct unprot_disassoc *e)
2358{
2359#ifdef CONFIG_IEEE80211W
2360 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2361 "dropped: " MACSTR " -> " MACSTR
2362 " (reason code %u)",
2363 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2364 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2365#endif /* CONFIG_IEEE80211W */
2366}
2367
2368
2369void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2370 union wpa_event_data *data)
2371{
2372 struct wpa_supplicant *wpa_s = ctx;
2373 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002374 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375
2376 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2377 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 event != EVENT_INTERFACE_STATUS &&
2379 event != EVENT_SCHED_SCAN_STOPPED) {
2380 wpa_dbg(wpa_s, MSG_DEBUG,
2381 "Ignore event %s (%d) while interface is disabled",
2382 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 return;
2384 }
2385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386#ifndef CONFIG_NO_STDOUT_DEBUG
2387{
2388 int level = MSG_DEBUG;
2389
Dmitry Shmidt04949592012-07-19 12:16:46 -07002390 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002391 const struct ieee80211_hdr *hdr;
2392 u16 fc;
2393 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2394 fc = le_to_host16(hdr->frame_control);
2395 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2396 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2397 level = MSG_EXCESSIVE;
2398 }
2399
2400 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2401 event_to_string(event), event);
2402}
2403#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404
2405 switch (event) {
2406 case EVENT_AUTH:
2407 sme_event_auth(wpa_s, data);
2408 break;
2409 case EVENT_ASSOC:
2410 wpa_supplicant_event_assoc(wpa_s, data);
2411 break;
2412 case EVENT_DISASSOC:
2413 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2414 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002415 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2416 data->disassoc_info.reason_code,
2417 data->disassoc_info.locally_generated ?
2418 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 if (data->disassoc_info.addr)
2420 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2421 MAC2STR(data->disassoc_info.addr));
2422 }
2423#ifdef CONFIG_AP
2424 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2425 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2426 data->disassoc_info.addr);
2427 break;
2428 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002429 if (wpa_s->ap_iface) {
2430 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2431 "AP mode");
2432 break;
2433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434#endif /* CONFIG_AP */
2435 if (data) {
2436 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002437 locally_generated =
2438 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2440 data->disassoc_info.ie,
2441 data->disassoc_info.ie_len);
2442#ifdef CONFIG_P2P
2443 wpas_p2p_disassoc_notif(
2444 wpa_s, data->disassoc_info.addr, reason_code,
2445 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002446 data->disassoc_info.ie_len,
2447 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448#endif /* CONFIG_P2P */
2449 }
2450 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2451 sme_event_disassoc(wpa_s, data);
2452 /* fall through */
2453 case EVENT_DEAUTH:
2454 if (event == EVENT_DEAUTH) {
2455 wpa_dbg(wpa_s, MSG_DEBUG,
2456 "Deauthentication notification");
2457 if (data) {
2458 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002459 locally_generated =
2460 data->deauth_info.locally_generated;
2461 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2462 data->deauth_info.reason_code,
2463 data->deauth_info.locally_generated ?
2464 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465 if (data->deauth_info.addr) {
2466 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2467 MACSTR,
2468 MAC2STR(data->deauth_info.
2469 addr));
2470 }
2471 wpa_hexdump(MSG_DEBUG,
2472 "Deauthentication frame IE(s)",
2473 data->deauth_info.ie,
2474 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 }
2476 }
2477#ifdef CONFIG_AP
2478 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2479 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2480 data->deauth_info.addr);
2481 break;
2482 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002483 if (wpa_s->ap_iface) {
2484 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2485 "AP mode");
2486 break;
2487 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002489 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2490 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002491 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2492 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2493 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2494 eapol_sm_failed(wpa_s->eapol)))
2495 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496#ifdef CONFIG_P2P
2497 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002498 if (wpas_p2p_deauth_notif(wpa_s,
2499 data->deauth_info.addr,
2500 reason_code,
2501 data->deauth_info.ie,
2502 data->deauth_info.ie_len,
2503 locally_generated) > 0) {
2504 /*
2505 * The interface was removed, so cannot
2506 * continue processing any additional
2507 * operations after this.
2508 */
2509 break;
2510 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002511 }
2512#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002513 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2514 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 break;
2516 case EVENT_MICHAEL_MIC_FAILURE:
2517 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2518 break;
2519#ifndef CONFIG_NO_SCAN_PROCESSING
2520 case EVENT_SCAN_RESULTS:
2521 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002522 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2523 wpa_s->wpa_state != WPA_ASSOCIATING)
2524 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525 break;
2526#endif /* CONFIG_NO_SCAN_PROCESSING */
2527 case EVENT_ASSOCINFO:
2528 wpa_supplicant_event_associnfo(wpa_s, data);
2529 break;
2530 case EVENT_INTERFACE_STATUS:
2531 wpa_supplicant_event_interface_status(wpa_s, data);
2532 break;
2533 case EVENT_PMKID_CANDIDATE:
2534 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2535 break;
2536#ifdef CONFIG_PEERKEY
2537 case EVENT_STKSTART:
2538 wpa_supplicant_event_stkstart(wpa_s, data);
2539 break;
2540#endif /* CONFIG_PEERKEY */
2541#ifdef CONFIG_TDLS
2542 case EVENT_TDLS:
2543 wpa_supplicant_event_tdls(wpa_s, data);
2544 break;
2545#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002546#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002547 case EVENT_WNM:
2548 wpa_supplicant_event_wnm(wpa_s, data);
2549 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002550#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551#ifdef CONFIG_IEEE80211R
2552 case EVENT_FT_RESPONSE:
2553 wpa_supplicant_event_ft_response(wpa_s, data);
2554 break;
2555#endif /* CONFIG_IEEE80211R */
2556#ifdef CONFIG_IBSS_RSN
2557 case EVENT_IBSS_RSN_START:
2558 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2559 break;
2560#endif /* CONFIG_IBSS_RSN */
2561 case EVENT_ASSOC_REJECT:
2562 if (data->assoc_reject.bssid)
2563 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2564 "bssid=" MACSTR " status_code=%u",
2565 MAC2STR(data->assoc_reject.bssid),
2566 data->assoc_reject.status_code);
2567 else
2568 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2569 "status_code=%u",
2570 data->assoc_reject.status_code);
2571 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2572 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002573 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002574#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002575 if(!wpa_s->current_ssid) {
2576 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2577 break;
2578 }
2579 /* If assoc reject is reported by the driver, then avoid
2580 * waiting for the authentication timeout. Cancel the
2581 * authentication timeout and retry the assoc.
2582 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002583 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002584 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2585 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002586
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002587 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002588
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002589 /* Clear the states */
2590 wpa_sm_notify_disassoc(wpa_s->wpa);
2591 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2592
2593 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002594 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002595 const u8 *bl_bssid = data->assoc_reject.bssid;
2596 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2597 bl_bssid = wpa_s->pending_bssid;
2598 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002599 wpa_supplicant_req_scan(wpa_s, 0, 0);
2600 } else {
2601 wpa_supplicant_req_scan(wpa_s, 1, 0);
2602 }
2603 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002604 /* If we ASSOC_REJECT's hits threshold, disable the
2605 * network
2606 */
2607 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2608 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002609 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002610 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002611 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002612 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002613#else
2614 const u8 *bssid = data->assoc_reject.bssid;
2615 if (bssid == NULL || is_zero_ether_addr(bssid))
2616 bssid = wpa_s->pending_bssid;
2617 wpas_connection_failed(wpa_s, bssid);
2618 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002619#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002620 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 break;
2622 case EVENT_AUTH_TIMED_OUT:
2623 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2624 sme_event_auth_timed_out(wpa_s, data);
2625 break;
2626 case EVENT_ASSOC_TIMED_OUT:
2627 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2628 sme_event_assoc_timed_out(wpa_s, data);
2629 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 case EVENT_TX_STATUS:
2631 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2632 " type=%d stype=%d",
2633 MAC2STR(data->tx_status.dst),
2634 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002637#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2639 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 wpa_s, data->tx_status.dst,
2642 data->tx_status.data,
2643 data->tx_status.data_len,
2644 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645 OFFCHANNEL_SEND_ACTION_SUCCESS :
2646 OFFCHANNEL_SEND_ACTION_NO_ACK);
2647#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 break;
2649 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002650#endif /* CONFIG_AP */
2651#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2653 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2654 /*
2655 * Catch TX status events for Action frames we sent via group
2656 * interface in GO mode.
2657 */
2658 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2659 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2660 os_memcmp(wpa_s->parent->pending_action_dst,
2661 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002662 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 wpa_s->parent, data->tx_status.dst,
2664 data->tx_status.data,
2665 data->tx_status.data_len,
2666 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667 OFFCHANNEL_SEND_ACTION_SUCCESS :
2668 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 break;
2670 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671#endif /* CONFIG_OFFCHANNEL */
2672#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 switch (data->tx_status.type) {
2674 case WLAN_FC_TYPE_MGMT:
2675 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2676 data->tx_status.data_len,
2677 data->tx_status.stype,
2678 data->tx_status.ack);
2679 break;
2680 case WLAN_FC_TYPE_DATA:
2681 ap_tx_status(wpa_s, data->tx_status.dst,
2682 data->tx_status.data,
2683 data->tx_status.data_len,
2684 data->tx_status.ack);
2685 break;
2686 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002687#endif /* CONFIG_AP */
2688 break;
2689#ifdef CONFIG_AP
2690 case EVENT_EAPOL_TX_STATUS:
2691 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2692 data->eapol_tx_status.data,
2693 data->eapol_tx_status.data_len,
2694 data->eapol_tx_status.ack);
2695 break;
2696 case EVENT_DRIVER_CLIENT_POLL_OK:
2697 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 break;
2699 case EVENT_RX_FROM_UNKNOWN:
2700 if (wpa_s->ap_iface == NULL)
2701 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002702 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2703 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002705 case EVENT_CH_SWITCH:
2706 if (!data)
2707 break;
2708 if (!wpa_s->ap_iface) {
2709 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2710 "event in non-AP mode");
2711 break;
2712 }
2713
2714#ifdef CONFIG_AP
2715 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2716 data->ch_switch.ht_enabled,
2717 data->ch_switch.ch_offset);
2718#endif /* CONFIG_AP */
2719 break;
2720 case EVENT_RX_MGMT: {
2721 u16 fc, stype;
2722 const struct ieee80211_mgmt *mgmt;
2723
2724 mgmt = (const struct ieee80211_mgmt *)
2725 data->rx_mgmt.frame;
2726 fc = le_to_host16(mgmt->frame_control);
2727 stype = WLAN_FC_GET_STYPE(fc);
2728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729 if (wpa_s->ap_iface == NULL) {
2730#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2732 data->rx_mgmt.frame_len > 24) {
2733 const u8 *src = mgmt->sa;
2734 const u8 *ie = mgmt->u.probe_req.variable;
2735 size_t ie_len = data->rx_mgmt.frame_len -
2736 (mgmt->u.probe_req.variable -
2737 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002738 wpas_p2p_probe_req_rx(
2739 wpa_s, src, mgmt->da,
2740 mgmt->bssid, ie, ie_len,
2741 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 break;
2743 }
2744#endif /* CONFIG_P2P */
2745 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2746 "management frame in non-AP mode");
2747 break;
2748 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002749
2750 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2751 data->rx_mgmt.frame_len > 24) {
2752 const u8 *ie = mgmt->u.probe_req.variable;
2753 size_t ie_len = data->rx_mgmt.frame_len -
2754 (mgmt->u.probe_req.variable -
2755 data->rx_mgmt.frame);
2756
2757 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2758 mgmt->bssid, ie, ie_len,
2759 data->rx_mgmt.ssi_signal);
2760 }
2761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2763 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765#endif /* CONFIG_AP */
2766 case EVENT_RX_ACTION:
2767 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2768 " Category=%u DataLen=%d freq=%d MHz",
2769 MAC2STR(data->rx_action.sa),
2770 data->rx_action.category, (int) data->rx_action.len,
2771 data->rx_action.freq);
2772#ifdef CONFIG_IEEE80211R
2773 if (data->rx_action.category == WLAN_ACTION_FT) {
2774 ft_rx_action(wpa_s, data->rx_action.data,
2775 data->rx_action.len);
2776 break;
2777 }
2778#endif /* CONFIG_IEEE80211R */
2779#ifdef CONFIG_IEEE80211W
2780#ifdef CONFIG_SME
2781 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2782 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2783 data->rx_action.data,
2784 data->rx_action.len);
2785 break;
2786 }
2787#endif /* CONFIG_SME */
2788#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002789#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002790 if (data->rx_action.category == WLAN_ACTION_WNM) {
2791 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2792 break;
2793 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002794#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002795#ifdef CONFIG_GAS
2796 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2797 gas_query_rx(wpa_s->gas, data->rx_action.da,
2798 data->rx_action.sa, data->rx_action.bssid,
2799 data->rx_action.data, data->rx_action.len,
2800 data->rx_action.freq) == 0)
2801 break;
2802#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002803#ifdef CONFIG_TDLS
2804 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2805 data->rx_action.len >= 4 &&
2806 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2807 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2808 "Response from " MACSTR,
2809 MAC2STR(data->rx_action.sa));
2810 break;
2811 }
2812#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813#ifdef CONFIG_P2P
2814 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2815 data->rx_action.sa,
2816 data->rx_action.bssid,
2817 data->rx_action.category,
2818 data->rx_action.data,
2819 data->rx_action.len, data->rx_action.freq);
2820#endif /* CONFIG_P2P */
2821 break;
2822 case EVENT_RX_PROBE_REQ:
2823 if (data->rx_probe_req.sa == NULL ||
2824 data->rx_probe_req.ie == NULL)
2825 break;
2826#ifdef CONFIG_AP
2827 if (wpa_s->ap_iface) {
2828 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2829 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002830 data->rx_probe_req.da,
2831 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002833 data->rx_probe_req.ie_len,
2834 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 break;
2836 }
2837#endif /* CONFIG_AP */
2838#ifdef CONFIG_P2P
2839 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840 data->rx_probe_req.da,
2841 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843 data->rx_probe_req.ie_len,
2844 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845#endif /* CONFIG_P2P */
2846 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002848#ifdef CONFIG_OFFCHANNEL
2849 offchannel_remain_on_channel_cb(
2850 wpa_s, data->remain_on_channel.freq,
2851 data->remain_on_channel.duration);
2852#endif /* CONFIG_OFFCHANNEL */
2853#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 wpas_p2p_remain_on_channel_cb(
2855 wpa_s, data->remain_on_channel.freq,
2856 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002857#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 break;
2859 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002860#ifdef CONFIG_OFFCHANNEL
2861 offchannel_cancel_remain_on_channel_cb(
2862 wpa_s, data->remain_on_channel.freq);
2863#endif /* CONFIG_OFFCHANNEL */
2864#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 wpas_p2p_cancel_remain_on_channel_cb(
2866 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002867#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002869#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 case EVENT_P2P_DEV_FOUND: {
2871 struct p2p_peer_info peer_info;
2872
2873 os_memset(&peer_info, 0, sizeof(peer_info));
2874 if (data->p2p_dev_found.dev_addr)
2875 os_memcpy(peer_info.p2p_device_addr,
2876 data->p2p_dev_found.dev_addr, ETH_ALEN);
2877 if (data->p2p_dev_found.pri_dev_type)
2878 os_memcpy(peer_info.pri_dev_type,
2879 data->p2p_dev_found.pri_dev_type,
2880 sizeof(peer_info.pri_dev_type));
2881 if (data->p2p_dev_found.dev_name)
2882 os_strlcpy(peer_info.device_name,
2883 data->p2p_dev_found.dev_name,
2884 sizeof(peer_info.device_name));
2885 peer_info.config_methods = data->p2p_dev_found.config_methods;
2886 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2887 peer_info.group_capab = data->p2p_dev_found.group_capab;
2888
2889 /*
2890 * FIX: new_device=1 is not necessarily correct. We should
2891 * maintain a P2P peer database in wpa_supplicant and update
2892 * this information based on whether the peer is truly new.
2893 */
2894 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2895 break;
2896 }
2897 case EVENT_P2P_GO_NEG_REQ_RX:
2898 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2899 data->p2p_go_neg_req_rx.dev_passwd_id);
2900 break;
2901 case EVENT_P2P_GO_NEG_COMPLETED:
2902 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2903 break;
2904 case EVENT_P2P_PROV_DISC_REQUEST:
2905 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2906 data->p2p_prov_disc_req.config_methods,
2907 data->p2p_prov_disc_req.dev_addr,
2908 data->p2p_prov_disc_req.pri_dev_type,
2909 data->p2p_prov_disc_req.dev_name,
2910 data->p2p_prov_disc_req.supp_config_methods,
2911 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002912 data->p2p_prov_disc_req.group_capab,
2913 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 break;
2915 case EVENT_P2P_PROV_DISC_RESPONSE:
2916 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2917 data->p2p_prov_disc_resp.config_methods);
2918 break;
2919 case EVENT_P2P_SD_REQUEST:
2920 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2921 data->p2p_sd_req.sa,
2922 data->p2p_sd_req.dialog_token,
2923 data->p2p_sd_req.update_indic,
2924 data->p2p_sd_req.tlvs,
2925 data->p2p_sd_req.tlvs_len);
2926 break;
2927 case EVENT_P2P_SD_RESPONSE:
2928 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2929 data->p2p_sd_resp.update_indic,
2930 data->p2p_sd_resp.tlvs,
2931 data->p2p_sd_resp.tlvs_len);
2932 break;
2933#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 case EVENT_EAPOL_RX:
2935 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2936 data->eapol_rx.data,
2937 data->eapol_rx.data_len);
2938 break;
2939 case EVENT_SIGNAL_CHANGE:
2940 bgscan_notify_signal_change(
2941 wpa_s, data->signal_change.above_threshold,
2942 data->signal_change.current_signal,
2943 data->signal_change.current_noise,
2944 data->signal_change.current_txrate);
2945 break;
2946 case EVENT_INTERFACE_ENABLED:
2947 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2948 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002949 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950#ifdef CONFIG_AP
2951 if (!wpa_s->ap_iface) {
2952 wpa_supplicant_set_state(wpa_s,
2953 WPA_DISCONNECTED);
2954 wpa_supplicant_req_scan(wpa_s, 0, 0);
2955 } else
2956 wpa_supplicant_set_state(wpa_s,
2957 WPA_COMPLETED);
2958#else /* CONFIG_AP */
2959 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2960 wpa_supplicant_req_scan(wpa_s, 0, 0);
2961#endif /* CONFIG_AP */
2962 }
2963 break;
2964 case EVENT_INTERFACE_DISABLED:
2965 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2966 wpa_supplicant_mark_disassoc(wpa_s);
2967 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2968 break;
2969 case EVENT_CHANNEL_LIST_CHANGED:
2970 if (wpa_s->drv_priv == NULL)
2971 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002972
2973 free_hw_features(wpa_s);
2974 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2975 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2976
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977#ifdef CONFIG_P2P
2978 wpas_p2p_update_channel_list(wpa_s);
2979#endif /* CONFIG_P2P */
2980 break;
2981 case EVENT_INTERFACE_UNAVAILABLE:
2982#ifdef CONFIG_P2P
2983 wpas_p2p_interface_unavailable(wpa_s);
2984#endif /* CONFIG_P2P */
2985 break;
2986 case EVENT_BEST_CHANNEL:
2987 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2988 "(%d %d %d)",
2989 data->best_chan.freq_24, data->best_chan.freq_5,
2990 data->best_chan.freq_overall);
2991 wpa_s->best_24_freq = data->best_chan.freq_24;
2992 wpa_s->best_5_freq = data->best_chan.freq_5;
2993 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2994#ifdef CONFIG_P2P
2995 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2996 data->best_chan.freq_5,
2997 data->best_chan.freq_overall);
2998#endif /* CONFIG_P2P */
2999 break;
3000 case EVENT_UNPROT_DEAUTH:
3001 wpa_supplicant_event_unprot_deauth(wpa_s,
3002 &data->unprot_deauth);
3003 break;
3004 case EVENT_UNPROT_DISASSOC:
3005 wpa_supplicant_event_unprot_disassoc(wpa_s,
3006 &data->unprot_disassoc);
3007 break;
3008 case EVENT_STATION_LOW_ACK:
3009#ifdef CONFIG_AP
3010 if (wpa_s->ap_iface && data)
3011 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3012 data->low_ack.addr);
3013#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003014#ifdef CONFIG_TDLS
3015 if (data)
3016 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3017#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 break;
3019 case EVENT_IBSS_PEER_LOST:
3020#ifdef CONFIG_IBSS_RSN
3021 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3022#endif /* CONFIG_IBSS_RSN */
3023 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003024 case EVENT_DRIVER_GTK_REKEY:
3025 if (os_memcmp(data->driver_gtk_rekey.bssid,
3026 wpa_s->bssid, ETH_ALEN))
3027 break;
3028 if (!wpa_s->wpa)
3029 break;
3030 wpa_sm_update_replay_ctr(wpa_s->wpa,
3031 data->driver_gtk_rekey.replay_ctr);
3032 break;
3033 case EVENT_SCHED_SCAN_STOPPED:
3034 wpa_s->sched_scanning = 0;
3035 wpa_supplicant_notify_scanning(wpa_s, 0);
3036
3037 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3038 break;
3039
3040 /*
3041 * If we timed out, start a new sched scan to continue
3042 * searching for more SSIDs.
3043 */
3044 if (wpa_s->sched_scan_timed_out)
3045 wpa_supplicant_req_sched_scan(wpa_s);
3046 break;
3047 case EVENT_WPS_BUTTON_PUSHED:
3048#ifdef CONFIG_WPS
3049 wpas_wps_start_pbc(wpa_s, NULL, 0);
3050#endif /* CONFIG_WPS */
3051 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003052 case EVENT_CONNECT_FAILED_REASON:
3053#ifdef CONFIG_AP
3054 if (!wpa_s->ap_iface || !data)
3055 break;
3056 hostapd_event_connect_failed_reason(
3057 wpa_s->ap_iface->bss[0],
3058 data->connect_failed_reason.addr,
3059 data->connect_failed_reason.code);
3060#endif /* CONFIG_AP */
3061 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 default:
3063 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3064 break;
3065 }
3066}