blob: 6c0a24641e84d9aa6106d585d2c63acc11cc7326 [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
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700857struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
858 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859{
860 struct wpa_bss *selected = NULL;
861 int prio;
862
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700863 if (wpa_s->last_scan_res == NULL ||
864 wpa_s->last_scan_res_used == 0)
865 return NULL; /* no scan results from last update */
866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 while (selected == NULL) {
868 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
869 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700870 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 selected_ssid);
872 if (selected)
873 break;
874 }
875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800876 if (selected == NULL && wpa_s->blacklist &&
877 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
879 "blacklist and try again");
880 wpa_blacklist_clear(wpa_s);
881 wpa_s->blacklist_cleared++;
882 } else if (selected == NULL)
883 break;
884 }
885
886 return selected;
887}
888
889
890static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
891 int timeout_sec, int timeout_usec)
892{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 /*
895 * No networks are enabled; short-circuit request so
896 * we don't wait timeout seconds before transitioning
897 * to INACTIVE state.
898 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700899 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
900 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700902#ifdef CONFIG_P2P
903 wpa_s->sta_scan_pending = 0;
904#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 return;
906 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800907
908 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
910}
911
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800912
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700913int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800914 struct wpa_bss *selected,
915 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916{
917 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
918 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
919 "PBC session overlap");
920#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800921 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700922 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923#endif /* CONFIG_P2P */
924
925#ifdef CONFIG_WPS
926 wpas_wps_cancel(wpa_s);
927#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700928 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 }
930
931 /*
932 * Do not trigger new association unless the BSSID has changed or if
933 * reassociation is requested. If we are in process of associating with
934 * the selected BSSID, do not trigger new attempt.
935 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
938 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
939 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
940 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
941 0))) {
942 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
943 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700944 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 }
946 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
947 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
948 " pending: " MACSTR " wpa_state: %s",
949 wpa_s->reassociate, MAC2STR(selected->bssid),
950 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
951 wpa_supplicant_state_txt(wpa_s->wpa_state));
952 wpa_supplicant_associate(wpa_s, selected, ssid);
953 } else {
954 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
955 "selected AP");
956 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800957
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700958 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959}
960
961
962static struct wpa_ssid *
963wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
964{
965 int prio;
966 struct wpa_ssid *ssid;
967
968 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
969 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
970 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700971 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 continue;
973 if (ssid->mode == IEEE80211_MODE_IBSS ||
974 ssid->mode == IEEE80211_MODE_AP)
975 return ssid;
976 }
977 }
978 return NULL;
979}
980
981
982/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
983 * on BSS added and BSS changed events */
984static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300985 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300987 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988
989 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
990 return;
991
Jouni Malinen87fd2792011-05-16 18:35:42 +0300992 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994
Jouni Malinen87fd2792011-05-16 18:35:42 +0300995 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 if (ssid == NULL)
997 continue;
998
Jouni Malinen87fd2792011-05-16 18:35:42 +0300999 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 if (rsn == NULL)
1001 continue;
1002
Jouni Malinen87fd2792011-05-16 18:35:42 +03001003 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 }
1005
1006}
1007
1008
1009static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1010 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001011 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001013 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 int min_diff;
1015
1016 if (wpa_s->reassociate)
1017 return 1; /* explicit request to reassociate */
1018 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1019 return 1; /* we are not associated; continue */
1020 if (wpa_s->current_ssid == NULL)
1021 return 1; /* unknown current SSID */
1022 if (wpa_s->current_ssid != ssid)
1023 return 1; /* different network block */
1024
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 if (wpas_driver_bss_selection(wpa_s))
1026 return 0; /* Driver-based roaming */
1027
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001028 if (wpa_s->current_ssid->ssid)
1029 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1030 wpa_s->current_ssid->ssid,
1031 wpa_s->current_ssid->ssid_len);
1032 if (!current_bss)
1033 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034
1035 if (!current_bss)
1036 return 1; /* current BSS not seen in scan results */
1037
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001038 if (current_bss == selected)
1039 return 0;
1040
1041 if (selected->last_update_idx > current_bss->last_update_idx)
1042 return 1; /* current BSS not seen in the last scan */
1043
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001044#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1046 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1047 MAC2STR(current_bss->bssid), current_bss->level);
1048 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1049 MAC2STR(selected->bssid), selected->level);
1050
1051 if (wpa_s->current_ssid->bssid_set &&
1052 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1053 0) {
1054 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1055 "has preferred BSSID");
1056 return 1;
1057 }
1058
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001059 if (current_bss->level < 0 && current_bss->level > selected->level) {
1060 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1061 "signal level");
1062 return 0;
1063 }
1064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 min_diff = 2;
1066 if (current_bss->level < 0) {
1067 if (current_bss->level < -85)
1068 min_diff = 1;
1069 else if (current_bss->level < -80)
1070 min_diff = 2;
1071 else if (current_bss->level < -75)
1072 min_diff = 3;
1073 else if (current_bss->level < -70)
1074 min_diff = 4;
1075 else
1076 min_diff = 5;
1077 }
1078 if (abs(current_bss->level - selected->level) < min_diff) {
1079 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1080 "in signal level");
1081 return 0;
1082 }
1083
1084 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001085#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001086 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001087#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088}
1089
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001090
Jouni Malinen89ca7022012-09-14 13:03:12 -07001091/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001092 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001093static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001094 union wpa_event_data *data,
1095 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 struct wpa_scan_results *scan_res;
1098 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001099#ifndef CONFIG_NO_RANDOM_POOL
1100 size_t i, num;
1101#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102
1103#ifdef CONFIG_AP
1104 if (wpa_s->ap_iface)
1105 ap = 1;
1106#endif /* CONFIG_AP */
1107
1108 wpa_supplicant_notify_scanning(wpa_s, 0);
1109
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001111 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001112 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001113 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1114 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001115 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001116 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1117 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1118 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001119 wpa_s->sta_scan_pending = 1;
1120 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001121 return -1;
1122 }
1123 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001124 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125#endif /* CONFIG_P2P */
1126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1128 data ? &data->scan_info :
1129 NULL, 1);
1130 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001131 if (wpa_s->conf->ap_scan == 2 || ap ||
1132 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001134 if (!own_request)
1135 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1137 "scanning again");
1138 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1139 return -1;
1140 }
1141
1142#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 num = scan_res->num;
1144 if (num > 10)
1145 num = 10;
1146 for (i = 0; i < num; i++) {
1147 u8 buf[5];
1148 struct wpa_scan_res *res = scan_res->res[i];
1149 buf[0] = res->bssid[5];
1150 buf[1] = res->qual & 0xff;
1151 buf[2] = res->noise & 0xff;
1152 buf[3] = res->level & 0xff;
1153 buf[4] = res->tsf & 0xff;
1154 random_add_randomness(buf, sizeof(buf));
1155 }
1156#endif /* CONFIG_NO_RANDOM_POOL */
1157
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001158 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1160 struct wpa_scan_results *scan_res);
1161
1162 scan_res_handler = wpa_s->scan_res_handler;
1163 wpa_s->scan_res_handler = NULL;
1164 scan_res_handler(wpa_s, scan_res);
1165
1166 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001167 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 }
1169
1170 if (ap) {
1171 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1172#ifdef CONFIG_AP
1173 if (wpa_s->ap_iface->scan_cb)
1174 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1175#endif /* CONFIG_AP */
1176 wpa_scan_results_free(scan_res);
1177 return 0;
1178 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001179
Dmitry Shmidt04949592012-07-19 12:16:46 -07001180 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1181 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1182 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183
1184 wpas_notify_scan_done(wpa_s, 1);
1185
Dmitry Shmidt04949592012-07-19 12:16:46 -07001186 if (sme_proc_obss_scan(wpa_s) > 0) {
1187 wpa_scan_results_free(scan_res);
1188 return 0;
1189 }
1190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1192 wpa_scan_results_free(scan_res);
1193 return 0;
1194 }
1195
Dmitry Shmidt04949592012-07-19 12:16:46 -07001196 if (autoscan_notify_scan(wpa_s, scan_res)) {
1197 wpa_scan_results_free(scan_res);
1198 return 0;
1199 }
1200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 if (wpa_s->disconnected) {
1202 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1203 wpa_scan_results_free(scan_res);
1204 return 0;
1205 }
1206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207 if (!wpas_driver_bss_selection(wpa_s) &&
1208 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 wpa_scan_results_free(scan_res);
1210 return 0;
1211 }
1212
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213 wpas_wps_update_ap_info(wpa_s, scan_res);
1214
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001215 wpa_scan_results_free(scan_res);
1216
1217 return wpas_select_network_from_last_scan(wpa_s);
1218}
1219
1220
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001221static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001222{
1223 struct wpa_bss *selected;
1224 struct wpa_ssid *ssid = NULL;
1225
1226 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227
1228 if (selected) {
1229 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001230 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231 if (skip) {
1232 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001234 }
1235
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001236 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001238 return -1;
1239 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001240 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001241 /*
1242 * Do not notify other virtual radios of scan results since we do not
1243 * want them to start other associations at the same time.
1244 */
1245 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1248 ssid = wpa_supplicant_pick_new_network(wpa_s);
1249 if (ssid) {
1250 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1251 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001252 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 } else {
1254 int timeout_sec = wpa_s->scan_interval;
1255 int timeout_usec = 0;
1256#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001257 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1258 return 0;
1259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 if (wpa_s->p2p_in_provisioning) {
1261 /*
1262 * Use shorter wait during P2P Provisioning
1263 * state to speed up group formation.
1264 */
1265 timeout_sec = 0;
1266 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001267 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1268 timeout_usec);
1269 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 }
1271#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001272#ifdef CONFIG_INTERWORKING
1273 if (wpa_s->conf->auto_interworking &&
1274 wpa_s->conf->interworking &&
1275 wpa_s->conf->cred) {
1276 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1277 "start ANQP fetch since no matching "
1278 "networks found");
1279 wpa_s->network_select = 1;
1280 wpa_s->auto_network_select = 1;
1281 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001282 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001283 }
1284#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001285 if (wpa_supplicant_req_sched_scan(wpa_s))
1286 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1287 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 }
1289 }
1290 return 0;
1291}
1292
1293
1294static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1295 union wpa_event_data *data)
1296{
1297 const char *rn, *rn2;
1298 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001299
1300 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 /*
1302 * If no scan results could be fetched, then no need to
1303 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001304 * this scan. Similarly, if scan results started a new operation on this
1305 * interface, do not notify other interfaces to avoid concurrent
1306 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 */
1308 return;
1309 }
1310
1311 /*
1312 * Check other interfaces to see if they have the same radio-name. If
1313 * so, they get updated with this same scan info.
1314 */
1315 if (!wpa_s->driver->get_radio_name)
1316 return;
1317
1318 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1319 if (rn == NULL || rn[0] == '\0')
1320 return;
1321
1322 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1323 "sharing same radio (%s) in event_scan_results", rn);
1324
1325 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1326 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1327 continue;
1328
1329 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1330 if (rn2 && os_strcmp(rn, rn2) == 0) {
1331 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1332 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001333 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 }
1335 }
1336}
1337
1338#endif /* CONFIG_NO_SCAN_PROCESSING */
1339
1340
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001341int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1342{
1343#ifdef CONFIG_NO_SCAN_PROCESSING
1344 return -1;
1345#else /* CONFIG_NO_SCAN_PROCESSING */
1346 struct os_time now;
1347
1348 if (wpa_s->last_scan_res_used <= 0)
1349 return -1;
1350
1351 os_get_time(&now);
1352 if (now.sec - wpa_s->last_scan.sec > 5) {
1353 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1354 return -1;
1355 }
1356
1357 return wpas_select_network_from_last_scan(wpa_s);
1358#endif /* CONFIG_NO_SCAN_PROCESSING */
1359}
1360
Dmitry Shmidt04949592012-07-19 12:16:46 -07001361#ifdef CONFIG_WNM
1362
1363static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1364{
1365 struct wpa_supplicant *wpa_s = eloop_ctx;
1366
1367 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1368 return;
1369
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001370 if (!wpa_s->no_keep_alive) {
1371 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1372 MAC2STR(wpa_s->bssid));
1373 /* TODO: could skip this if normal data traffic has been sent */
1374 /* TODO: Consider using some more appropriate data frame for
1375 * this */
1376 if (wpa_s->l2)
1377 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1378 (u8 *) "", 0);
1379 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001380
1381#ifdef CONFIG_SME
1382 if (wpa_s->sme.bss_max_idle_period) {
1383 unsigned int msec;
1384 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1385 if (msec > 100)
1386 msec -= 100;
1387 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1388 wnm_bss_keep_alive, wpa_s, NULL);
1389 }
1390#endif /* CONFIG_SME */
1391}
1392
1393
1394static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1395 const u8 *ies, size_t ies_len)
1396{
1397 struct ieee802_11_elems elems;
1398
1399 if (ies == NULL)
1400 return;
1401
1402 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1403 return;
1404
1405#ifdef CONFIG_SME
1406 if (elems.bss_max_idle_period) {
1407 unsigned int msec;
1408 wpa_s->sme.bss_max_idle_period =
1409 WPA_GET_LE16(elems.bss_max_idle_period);
1410 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1411 "TU)%s", wpa_s->sme.bss_max_idle_period,
1412 (elems.bss_max_idle_period[2] & 0x01) ?
1413 " (protected keep-live required)" : "");
1414 if (wpa_s->sme.bss_max_idle_period == 0)
1415 wpa_s->sme.bss_max_idle_period = 1;
1416 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1417 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1418 /* msec times 1000 */
1419 msec = wpa_s->sme.bss_max_idle_period * 1024;
1420 if (msec > 100)
1421 msec -= 100;
1422 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1423 wnm_bss_keep_alive, wpa_s,
1424 NULL);
1425 }
1426 }
1427#endif /* CONFIG_SME */
1428}
1429
1430#endif /* CONFIG_WNM */
1431
1432
1433void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1434{
1435#ifdef CONFIG_WNM
1436 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1437#endif /* CONFIG_WNM */
1438}
1439
1440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1442 union wpa_event_data *data)
1443{
1444 int l, len, found = 0, wpa_found, rsn_found;
1445 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001446#ifdef CONFIG_IEEE80211R
1447 u8 bssid[ETH_ALEN];
1448#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449
1450 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1451 if (data->assoc_info.req_ies)
1452 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1453 data->assoc_info.req_ies_len);
1454 if (data->assoc_info.resp_ies) {
1455 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1456 data->assoc_info.resp_ies_len);
1457#ifdef CONFIG_TDLS
1458 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1459 data->assoc_info.resp_ies_len);
1460#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001461#ifdef CONFIG_WNM
1462 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1463 data->assoc_info.resp_ies_len);
1464#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 }
1466 if (data->assoc_info.beacon_ies)
1467 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1468 data->assoc_info.beacon_ies,
1469 data->assoc_info.beacon_ies_len);
1470 if (data->assoc_info.freq)
1471 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1472 data->assoc_info.freq);
1473
1474 p = data->assoc_info.req_ies;
1475 l = data->assoc_info.req_ies_len;
1476
1477 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1478 while (p && l >= 2) {
1479 len = p[1] + 2;
1480 if (len > l) {
1481 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1482 p, l);
1483 break;
1484 }
1485 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1486 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1487 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1488 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1489 break;
1490 found = 1;
1491 wpa_find_assoc_pmkid(wpa_s);
1492 break;
1493 }
1494 l -= len;
1495 p += len;
1496 }
1497 if (!found && data->assoc_info.req_ies)
1498 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1499
1500#ifdef CONFIG_IEEE80211R
1501#ifdef CONFIG_SME
1502 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1504 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1505 data->assoc_info.resp_ies,
1506 data->assoc_info.resp_ies_len,
1507 bssid) < 0) {
1508 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1509 "Reassociation Response failed");
1510 wpa_supplicant_deauthenticate(
1511 wpa_s, WLAN_REASON_INVALID_IE);
1512 return -1;
1513 }
1514 }
1515
1516 p = data->assoc_info.resp_ies;
1517 l = data->assoc_info.resp_ies_len;
1518
1519#ifdef CONFIG_WPS_STRICT
1520 if (p && wpa_s->current_ssid &&
1521 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1522 struct wpabuf *wps;
1523 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1524 if (wps == NULL) {
1525 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1526 "include WPS IE in (Re)Association Response");
1527 return -1;
1528 }
1529
1530 if (wps_validate_assoc_resp(wps) < 0) {
1531 wpabuf_free(wps);
1532 wpa_supplicant_deauthenticate(
1533 wpa_s, WLAN_REASON_INVALID_IE);
1534 return -1;
1535 }
1536 wpabuf_free(wps);
1537 }
1538#endif /* CONFIG_WPS_STRICT */
1539
1540 /* Go through the IEs and make a copy of the MDIE, if present. */
1541 while (p && l >= 2) {
1542 len = p[1] + 2;
1543 if (len > l) {
1544 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1545 p, l);
1546 break;
1547 }
1548 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1549 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1550 wpa_s->sme.ft_used = 1;
1551 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1552 MOBILITY_DOMAIN_ID_LEN);
1553 break;
1554 }
1555 l -= len;
1556 p += len;
1557 }
1558#endif /* CONFIG_SME */
1559
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001560 /* Process FT when SME is in the driver */
1561 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1562 wpa_ft_is_completed(wpa_s->wpa)) {
1563 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1564 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1565 data->assoc_info.resp_ies,
1566 data->assoc_info.resp_ies_len,
1567 bssid) < 0) {
1568 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1569 "Reassociation Response failed");
1570 wpa_supplicant_deauthenticate(
1571 wpa_s, WLAN_REASON_INVALID_IE);
1572 return -1;
1573 }
1574 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1575 }
1576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1578 data->assoc_info.resp_ies_len);
1579#endif /* CONFIG_IEEE80211R */
1580
1581 /* WPA/RSN IE from Beacon/ProbeResp */
1582 p = data->assoc_info.beacon_ies;
1583 l = data->assoc_info.beacon_ies_len;
1584
1585 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1586 */
1587 wpa_found = rsn_found = 0;
1588 while (p && l >= 2) {
1589 len = p[1] + 2;
1590 if (len > l) {
1591 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1592 p, l);
1593 break;
1594 }
1595 if (!wpa_found &&
1596 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1597 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1598 wpa_found = 1;
1599 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1600 }
1601
1602 if (!rsn_found &&
1603 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1604 rsn_found = 1;
1605 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1606 }
1607
1608 l -= len;
1609 p += len;
1610 }
1611
1612 if (!wpa_found && data->assoc_info.beacon_ies)
1613 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1614 if (!rsn_found && data->assoc_info.beacon_ies)
1615 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1616 if (wpa_found || rsn_found)
1617 wpa_s->ap_ies_from_associnfo = 1;
1618
Jouni Malinen87fd2792011-05-16 18:35:42 +03001619 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1620 wpa_s->assoc_freq != data->assoc_info.freq) {
1621 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1622 "%u to %u MHz",
1623 wpa_s->assoc_freq, data->assoc_info.freq);
1624 wpa_supplicant_update_scan_results(wpa_s);
1625 }
1626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 wpa_s->assoc_freq = data->assoc_info.freq;
1628
1629 return 0;
1630}
1631
1632
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001633static struct wpa_bss * wpa_supplicant_get_new_bss(
1634 struct wpa_supplicant *wpa_s, const u8 *bssid)
1635{
1636 struct wpa_bss *bss = NULL;
1637 struct wpa_ssid *ssid = wpa_s->current_ssid;
1638
1639 if (ssid->ssid_len > 0)
1640 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1641 if (!bss)
1642 bss = wpa_bss_get_bssid(wpa_s, bssid);
1643
1644 return bss;
1645}
1646
1647
1648static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1649{
1650 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1651
1652 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1653 return -1;
1654
1655 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1656 return 0;
1657
1658 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1659 WPA_IE_VENDOR_TYPE);
1660 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1661
1662 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1663 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1664 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1665 bss_rsn ? 2 + bss_rsn[1] : 0))
1666 return -1;
1667
1668 return 0;
1669}
1670
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1673 union wpa_event_data *data)
1674{
1675 u8 bssid[ETH_ALEN];
1676 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 struct wpa_driver_capa capa;
1678
1679#ifdef CONFIG_AP
1680 if (wpa_s->ap_iface) {
1681 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1682 data->assoc_info.addr,
1683 data->assoc_info.req_ies,
1684 data->assoc_info.req_ies_len,
1685 data->assoc_info.reassoc);
1686 return;
1687 }
1688#endif /* CONFIG_AP */
1689
1690 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1691 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1692 return;
1693
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001694 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1695 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001696 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001697 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1698 return;
1699 }
1700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001702 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1704 MACSTR, MAC2STR(bssid));
1705 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1707 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001708 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709
1710 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1711 wpa_clear_keys(wpa_s, bssid);
1712 }
1713 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001714 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1716 return;
1717 }
1718 if (wpa_s->current_ssid) {
1719 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001720
1721 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1722 if (!bss) {
1723 wpa_supplicant_update_scan_results(wpa_s);
1724
1725 /* Get the BSS from the new scan results */
1726 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1727 }
1728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 if (bss)
1730 wpa_s->current_bss = bss;
1731 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001732
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001733#ifdef ANDROID
1734 if (wpa_s->conf->ap_scan == 1) {
1735#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001736 if (wpa_s->conf->ap_scan == 1 &&
1737 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001738#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001739 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1740 wpa_msg(wpa_s, MSG_WARNING,
1741 "WPA/RSN IEs not updated");
1742 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 }
1744
1745#ifdef CONFIG_SME
1746 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1747 wpa_s->sme.prev_bssid_set = 1;
1748#endif /* CONFIG_SME */
1749
1750 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1751 if (wpa_s->current_ssid) {
1752 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1753 * initialized before association, but for other modes,
1754 * initialize PC/SC here, if the current configuration needs
1755 * smartcard or SIM/USIM. */
1756 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1757 }
1758 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1759 if (wpa_s->l2)
1760 l2_packet_notify_auth_start(wpa_s->l2);
1761
1762 /*
1763 * Set portEnabled first to FALSE in order to get EAP state machine out
1764 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1765 * state machine may transit to AUTHENTICATING state based on obsolete
1766 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1767 * AUTHENTICATED without ever giving chance to EAP state machine to
1768 * reset the state.
1769 */
1770 if (!ft_completed) {
1771 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1772 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1773 }
1774 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1775 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1776 /* 802.1X::portControl = Auto */
1777 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1778 wpa_s->eapol_received = 0;
1779 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1780 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1781 (wpa_s->current_ssid &&
1782 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1783 wpa_supplicant_cancel_auth_timeout(wpa_s);
1784 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1785 } else if (!ft_completed) {
1786 /* Timeout for receiving the first EAPOL packet */
1787 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1788 }
1789 wpa_supplicant_cancel_scan(wpa_s);
1790
1791 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1792 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1793 /*
1794 * We are done; the driver will take care of RSN 4-way
1795 * handshake.
1796 */
1797 wpa_supplicant_cancel_auth_timeout(wpa_s);
1798 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1799 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1800 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1801 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1802 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1803 /*
1804 * The driver will take care of RSN 4-way handshake, so we need
1805 * to allow EAPOL supplicant to complete its work without
1806 * waiting for WPA supplicant.
1807 */
1808 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1809 } else if (ft_completed) {
1810 /*
1811 * FT protocol completed - make sure EAPOL state machine ends
1812 * up in authenticated.
1813 */
1814 wpa_supplicant_cancel_auth_timeout(wpa_s);
1815 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1816 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1817 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1818 }
1819
Jouni Malinena05074c2012-12-21 21:35:35 +02001820 wpa_s->last_eapol_matches_bssid = 0;
1821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 if (wpa_s->pending_eapol_rx) {
1823 struct os_time now, age;
1824 os_get_time(&now);
1825 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1826 if (age.sec == 0 && age.usec < 100000 &&
1827 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1828 0) {
1829 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1830 "frame that was received just before "
1831 "association notification");
1832 wpa_supplicant_rx_eapol(
1833 wpa_s, wpa_s->pending_eapol_rx_src,
1834 wpabuf_head(wpa_s->pending_eapol_rx),
1835 wpabuf_len(wpa_s->pending_eapol_rx));
1836 }
1837 wpabuf_free(wpa_s->pending_eapol_rx);
1838 wpa_s->pending_eapol_rx = NULL;
1839 }
1840
1841 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1842 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1843 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1844 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1845 /* Set static WEP keys again */
1846 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1847 }
1848
1849#ifdef CONFIG_IBSS_RSN
1850 if (wpa_s->current_ssid &&
1851 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1852 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1853 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1854 wpa_s->ibss_rsn == NULL) {
1855 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1856 if (!wpa_s->ibss_rsn) {
1857 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1858 wpa_supplicant_deauthenticate(
1859 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1860 return;
1861 }
1862
1863 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1864 }
1865#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001866
1867 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868}
1869
1870
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001871static int disconnect_reason_recoverable(u16 reason_code)
1872{
1873 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1874 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1875 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1876}
1877
1878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001880 u16 reason_code,
1881 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882{
1883 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001884
1885 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1886 /*
1887 * At least Host AP driver and a Prism3 card seemed to be
1888 * generating streams of disconnected events when configuring
1889 * IBSS for WPA-None. Ignore them for now.
1890 */
1891 return;
1892 }
1893
1894 bssid = wpa_s->bssid;
1895 if (is_zero_ether_addr(bssid))
1896 bssid = wpa_s->pending_bssid;
1897
1898 if (!is_zero_ether_addr(bssid) ||
1899 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1900 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1901 " reason=%d%s",
1902 MAC2STR(bssid), reason_code,
1903 locally_generated ? " locally_generated=1" : "");
1904 }
1905}
1906
1907
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001908static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1909 int locally_generated)
1910{
1911 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1912 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1913 return 0; /* Not in 4-way handshake with PSK */
1914
1915 /*
1916 * It looks like connection was lost while trying to go through PSK
1917 * 4-way handshake. Filter out known disconnection cases that are caused
1918 * by something else than PSK mismatch to avoid confusing reports.
1919 */
1920
1921 if (locally_generated) {
1922 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1923 return 0;
1924 }
1925
1926 return 1;
1927}
1928
1929
Jouni Malinen2b89da82012-08-31 22:04:41 +03001930static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1931 u16 reason_code,
1932 int locally_generated)
1933{
1934 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 int authenticating;
1936 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001937 struct wpa_bss *fast_reconnect = NULL;
1938 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001939 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940
1941 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1942 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1943
1944 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1945 /*
1946 * At least Host AP driver and a Prism3 card seemed to be
1947 * generating streams of disconnected events when configuring
1948 * IBSS for WPA-None. Ignore them for now.
1949 */
1950 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1951 "IBSS/WPA-None mode");
1952 return;
1953 }
1954
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001955 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1957 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001958 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 }
1960 if (!wpa_s->auto_reconnect_disabled ||
1961 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001962 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1963 "reconnect (wps=%d wpa_state=%d)",
1964 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1965 wpa_s->wpa_state);
1966 if (wpa_s->wpa_state == WPA_COMPLETED &&
1967 wpa_s->current_ssid &&
1968 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001969 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001970 disconnect_reason_recoverable(reason_code)) {
1971 /*
1972 * It looks like the AP has dropped association with
1973 * us, but could allow us to get back in. Try to
1974 * reconnect to the same BSS without full scan to save
1975 * time for some common cases.
1976 */
1977 fast_reconnect = wpa_s->current_bss;
1978 fast_reconnect_ssid = wpa_s->current_ssid;
1979 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001980#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001981 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001982#else
1983 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1984#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985 else
1986 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1987 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001988 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 "try to re-connect");
1991 wpa_s->reassociate = 0;
1992 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001993 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 }
1995 bssid = wpa_s->bssid;
1996 if (is_zero_ether_addr(bssid))
1997 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001998 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1999 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002001 if (locally_generated)
2002 wpa_s->disconnect_reason = -reason_code;
2003 else
2004 wpa_s->disconnect_reason = reason_code;
2005 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2007 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2008 wpa_s->keys_cleared = 0;
2009 wpa_clear_keys(wpa_s, wpa_s->bssid);
2010 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002011 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpa_supplicant_mark_disassoc(wpa_s);
2013
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002014 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002016 wpa_s->current_ssid = last_ssid;
2017 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018
2019 if (fast_reconnect) {
2020#ifndef CONFIG_NO_SCAN_PROCESSING
2021 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2022 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2023 fast_reconnect_ssid) < 0) {
2024 /* Recover through full scan */
2025 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2026 }
2027#endif /* CONFIG_NO_SCAN_PROCESSING */
2028 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029}
2030
2031
2032#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002033void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034{
2035 struct wpa_supplicant *wpa_s = eloop_ctx;
2036
2037 if (!wpa_s->pending_mic_error_report)
2038 return;
2039
2040 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2041 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2042 wpa_s->pending_mic_error_report = 0;
2043}
2044#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2045
2046
2047static void
2048wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2049 union wpa_event_data *data)
2050{
2051 int pairwise;
2052 struct os_time t;
2053
2054 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2055 pairwise = (data && data->michael_mic_failure.unicast);
2056 os_get_time(&t);
2057 if ((wpa_s->last_michael_mic_error &&
2058 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2059 wpa_s->pending_mic_error_report) {
2060 if (wpa_s->pending_mic_error_report) {
2061 /*
2062 * Send the pending MIC error report immediately since
2063 * we are going to start countermeasures and AP better
2064 * do the same.
2065 */
2066 wpa_sm_key_request(wpa_s->wpa, 1,
2067 wpa_s->pending_mic_error_pairwise);
2068 }
2069
2070 /* Send the new MIC error report immediately since we are going
2071 * to start countermeasures and AP better do the same.
2072 */
2073 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2074
2075 /* initialize countermeasures */
2076 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002077
2078 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2079
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2081
2082 /*
2083 * Need to wait for completion of request frame. We do not get
2084 * any callback for the message completion, so just wait a
2085 * short while and hope for the best. */
2086 os_sleep(0, 10000);
2087
2088 wpa_drv_set_countermeasures(wpa_s, 1);
2089 wpa_supplicant_deauthenticate(wpa_s,
2090 WLAN_REASON_MICHAEL_MIC_FAILURE);
2091 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2092 wpa_s, NULL);
2093 eloop_register_timeout(60, 0,
2094 wpa_supplicant_stop_countermeasures,
2095 wpa_s, NULL);
2096 /* TODO: mark the AP rejected for 60 second. STA is
2097 * allowed to associate with another AP.. */
2098 } else {
2099#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2100 if (wpa_s->mic_errors_seen) {
2101 /*
2102 * Reduce the effectiveness of Michael MIC error
2103 * reports as a means for attacking against TKIP if
2104 * more than one MIC failure is noticed with the same
2105 * PTK. We delay the transmission of the reports by a
2106 * random time between 0 and 60 seconds in order to
2107 * force the attacker wait 60 seconds before getting
2108 * the information on whether a frame resulted in a MIC
2109 * failure.
2110 */
2111 u8 rval[4];
2112 int sec;
2113
2114 if (os_get_random(rval, sizeof(rval)) < 0)
2115 sec = os_random() % 60;
2116 else
2117 sec = WPA_GET_BE32(rval) % 60;
2118 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2119 "report %d seconds", sec);
2120 wpa_s->pending_mic_error_report = 1;
2121 wpa_s->pending_mic_error_pairwise = pairwise;
2122 eloop_cancel_timeout(
2123 wpa_supplicant_delayed_mic_error_report,
2124 wpa_s, NULL);
2125 eloop_register_timeout(
2126 sec, os_random() % 1000000,
2127 wpa_supplicant_delayed_mic_error_report,
2128 wpa_s, NULL);
2129 } else {
2130 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2131 }
2132#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2133 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2134#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2135 }
2136 wpa_s->last_michael_mic_error = t.sec;
2137 wpa_s->mic_errors_seen++;
2138}
2139
2140
2141#ifdef CONFIG_TERMINATE_ONLASTIF
2142static int any_interfaces(struct wpa_supplicant *head)
2143{
2144 struct wpa_supplicant *wpa_s;
2145
2146 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2147 if (!wpa_s->interface_removed)
2148 return 1;
2149 return 0;
2150}
2151#endif /* CONFIG_TERMINATE_ONLASTIF */
2152
2153
2154static void
2155wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2156 union wpa_event_data *data)
2157{
2158 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2159 return;
2160
2161 switch (data->interface_status.ievent) {
2162 case EVENT_INTERFACE_ADDED:
2163 if (!wpa_s->interface_removed)
2164 break;
2165 wpa_s->interface_removed = 0;
2166 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2167 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2168 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2169 "driver after interface was added");
2170 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002171 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 break;
2173 case EVENT_INTERFACE_REMOVED:
2174 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2175 wpa_s->interface_removed = 1;
2176 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002177 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 l2_packet_deinit(wpa_s->l2);
2179 wpa_s->l2 = NULL;
2180#ifdef CONFIG_IBSS_RSN
2181 ibss_rsn_deinit(wpa_s->ibss_rsn);
2182 wpa_s->ibss_rsn = NULL;
2183#endif /* CONFIG_IBSS_RSN */
2184#ifdef CONFIG_TERMINATE_ONLASTIF
2185 /* check if last interface */
2186 if (!any_interfaces(wpa_s->global->ifaces))
2187 eloop_terminate();
2188#endif /* CONFIG_TERMINATE_ONLASTIF */
2189 break;
2190 }
2191}
2192
2193
2194#ifdef CONFIG_PEERKEY
2195static void
2196wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2197 union wpa_event_data *data)
2198{
2199 if (data == NULL)
2200 return;
2201 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2202}
2203#endif /* CONFIG_PEERKEY */
2204
2205
2206#ifdef CONFIG_TDLS
2207static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2208 union wpa_event_data *data)
2209{
2210 if (data == NULL)
2211 return;
2212 switch (data->tdls.oper) {
2213 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002214 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2215 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2216 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2217 else
2218 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 break;
2220 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002221 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002222 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 break;
2224 }
2225}
2226#endif /* CONFIG_TDLS */
2227
2228
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002229#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002230static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2231 union wpa_event_data *data)
2232{
2233 if (data == NULL)
2234 return;
2235 switch (data->wnm.oper) {
2236 case WNM_OPER_SLEEP:
2237 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2238 "(action=%d, intval=%d)",
2239 data->wnm.sleep_action, data->wnm.sleep_intval);
2240 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002241 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002242 break;
2243 }
2244}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002245#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002246
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248#ifdef CONFIG_IEEE80211R
2249static void
2250wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2251 union wpa_event_data *data)
2252{
2253 if (data == NULL)
2254 return;
2255
2256 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2257 data->ft_ies.ies_len,
2258 data->ft_ies.ft_action,
2259 data->ft_ies.target_ap,
2260 data->ft_ies.ric_ies,
2261 data->ft_ies.ric_ies_len) < 0) {
2262 /* TODO: prevent MLME/driver from trying to associate? */
2263 }
2264}
2265#endif /* CONFIG_IEEE80211R */
2266
2267
2268#ifdef CONFIG_IBSS_RSN
2269static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2270 union wpa_event_data *data)
2271{
2272 struct wpa_ssid *ssid;
2273 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2274 return;
2275 if (data == NULL)
2276 return;
2277 ssid = wpa_s->current_ssid;
2278 if (ssid == NULL)
2279 return;
2280 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2281 return;
2282
2283 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2284}
2285#endif /* CONFIG_IBSS_RSN */
2286
2287
2288#ifdef CONFIG_IEEE80211R
2289static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2290 size_t len)
2291{
2292 const u8 *sta_addr, *target_ap_addr;
2293 u16 status;
2294
2295 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2296 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2297 return; /* only SME case supported for now */
2298 if (len < 1 + 2 * ETH_ALEN + 2)
2299 return;
2300 if (data[0] != 2)
2301 return; /* Only FT Action Response is supported for now */
2302 sta_addr = data + 1;
2303 target_ap_addr = data + 1 + ETH_ALEN;
2304 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2305 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2306 MACSTR " TargetAP " MACSTR " status %u",
2307 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2308
2309 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2310 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2311 " in FT Action Response", MAC2STR(sta_addr));
2312 return;
2313 }
2314
2315 if (status) {
2316 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2317 "failure (status code %d)", status);
2318 /* TODO: report error to FT code(?) */
2319 return;
2320 }
2321
2322 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2323 len - (1 + 2 * ETH_ALEN + 2), 1,
2324 target_ap_addr, NULL, 0) < 0)
2325 return;
2326
2327#ifdef CONFIG_SME
2328 {
2329 struct wpa_bss *bss;
2330 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2331 if (bss)
2332 wpa_s->sme.freq = bss->freq;
2333 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2334 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2335 WLAN_AUTH_FT);
2336 }
2337#endif /* CONFIG_SME */
2338}
2339#endif /* CONFIG_IEEE80211R */
2340
2341
2342static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2343 struct unprot_deauth *e)
2344{
2345#ifdef CONFIG_IEEE80211W
2346 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2347 "dropped: " MACSTR " -> " MACSTR
2348 " (reason code %u)",
2349 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2350 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2351#endif /* CONFIG_IEEE80211W */
2352}
2353
2354
2355static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2356 struct unprot_disassoc *e)
2357{
2358#ifdef CONFIG_IEEE80211W
2359 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2360 "dropped: " MACSTR " -> " MACSTR
2361 " (reason code %u)",
2362 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2363 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2364#endif /* CONFIG_IEEE80211W */
2365}
2366
2367
2368void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2369 union wpa_event_data *data)
2370{
2371 struct wpa_supplicant *wpa_s = ctx;
2372 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002373 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374
2375 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2376 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377 event != EVENT_INTERFACE_STATUS &&
2378 event != EVENT_SCHED_SCAN_STOPPED) {
2379 wpa_dbg(wpa_s, MSG_DEBUG,
2380 "Ignore event %s (%d) while interface is disabled",
2381 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 return;
2383 }
2384
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385#ifndef CONFIG_NO_STDOUT_DEBUG
2386{
2387 int level = MSG_DEBUG;
2388
Dmitry Shmidt04949592012-07-19 12:16:46 -07002389 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390 const struct ieee80211_hdr *hdr;
2391 u16 fc;
2392 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2393 fc = le_to_host16(hdr->frame_control);
2394 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2395 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2396 level = MSG_EXCESSIVE;
2397 }
2398
2399 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2400 event_to_string(event), event);
2401}
2402#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403
2404 switch (event) {
2405 case EVENT_AUTH:
2406 sme_event_auth(wpa_s, data);
2407 break;
2408 case EVENT_ASSOC:
2409 wpa_supplicant_event_assoc(wpa_s, data);
2410 break;
2411 case EVENT_DISASSOC:
2412 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2413 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002414 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2415 data->disassoc_info.reason_code,
2416 data->disassoc_info.locally_generated ?
2417 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418 if (data->disassoc_info.addr)
2419 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2420 MAC2STR(data->disassoc_info.addr));
2421 }
2422#ifdef CONFIG_AP
2423 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2424 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2425 data->disassoc_info.addr);
2426 break;
2427 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 if (wpa_s->ap_iface) {
2429 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2430 "AP mode");
2431 break;
2432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433#endif /* CONFIG_AP */
2434 if (data) {
2435 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002436 locally_generated =
2437 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2439 data->disassoc_info.ie,
2440 data->disassoc_info.ie_len);
2441#ifdef CONFIG_P2P
2442 wpas_p2p_disassoc_notif(
2443 wpa_s, data->disassoc_info.addr, reason_code,
2444 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002445 data->disassoc_info.ie_len,
2446 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447#endif /* CONFIG_P2P */
2448 }
2449 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2450 sme_event_disassoc(wpa_s, data);
2451 /* fall through */
2452 case EVENT_DEAUTH:
2453 if (event == EVENT_DEAUTH) {
2454 wpa_dbg(wpa_s, MSG_DEBUG,
2455 "Deauthentication notification");
2456 if (data) {
2457 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002458 locally_generated =
2459 data->deauth_info.locally_generated;
2460 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2461 data->deauth_info.reason_code,
2462 data->deauth_info.locally_generated ?
2463 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 if (data->deauth_info.addr) {
2465 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2466 MACSTR,
2467 MAC2STR(data->deauth_info.
2468 addr));
2469 }
2470 wpa_hexdump(MSG_DEBUG,
2471 "Deauthentication frame IE(s)",
2472 data->deauth_info.ie,
2473 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 }
2475 }
2476#ifdef CONFIG_AP
2477 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2478 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2479 data->deauth_info.addr);
2480 break;
2481 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002482 if (wpa_s->ap_iface) {
2483 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2484 "AP mode");
2485 break;
2486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002488 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2489 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002490 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2491 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2492 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2493 eapol_sm_failed(wpa_s->eapol)))
2494 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495#ifdef CONFIG_P2P
2496 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002497 if (wpas_p2p_deauth_notif(wpa_s,
2498 data->deauth_info.addr,
2499 reason_code,
2500 data->deauth_info.ie,
2501 data->deauth_info.ie_len,
2502 locally_generated) > 0) {
2503 /*
2504 * The interface was removed, so cannot
2505 * continue processing any additional
2506 * operations after this.
2507 */
2508 break;
2509 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002510 }
2511#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002512 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2513 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 break;
2515 case EVENT_MICHAEL_MIC_FAILURE:
2516 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2517 break;
2518#ifndef CONFIG_NO_SCAN_PROCESSING
2519 case EVENT_SCAN_RESULTS:
2520 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002521 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2522 wpa_s->wpa_state != WPA_ASSOCIATING)
2523 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 break;
2525#endif /* CONFIG_NO_SCAN_PROCESSING */
2526 case EVENT_ASSOCINFO:
2527 wpa_supplicant_event_associnfo(wpa_s, data);
2528 break;
2529 case EVENT_INTERFACE_STATUS:
2530 wpa_supplicant_event_interface_status(wpa_s, data);
2531 break;
2532 case EVENT_PMKID_CANDIDATE:
2533 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2534 break;
2535#ifdef CONFIG_PEERKEY
2536 case EVENT_STKSTART:
2537 wpa_supplicant_event_stkstart(wpa_s, data);
2538 break;
2539#endif /* CONFIG_PEERKEY */
2540#ifdef CONFIG_TDLS
2541 case EVENT_TDLS:
2542 wpa_supplicant_event_tdls(wpa_s, data);
2543 break;
2544#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002545#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002546 case EVENT_WNM:
2547 wpa_supplicant_event_wnm(wpa_s, data);
2548 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002549#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550#ifdef CONFIG_IEEE80211R
2551 case EVENT_FT_RESPONSE:
2552 wpa_supplicant_event_ft_response(wpa_s, data);
2553 break;
2554#endif /* CONFIG_IEEE80211R */
2555#ifdef CONFIG_IBSS_RSN
2556 case EVENT_IBSS_RSN_START:
2557 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2558 break;
2559#endif /* CONFIG_IBSS_RSN */
2560 case EVENT_ASSOC_REJECT:
2561 if (data->assoc_reject.bssid)
2562 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2563 "bssid=" MACSTR " status_code=%u",
2564 MAC2STR(data->assoc_reject.bssid),
2565 data->assoc_reject.status_code);
2566 else
2567 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2568 "status_code=%u",
2569 data->assoc_reject.status_code);
2570 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2571 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002572 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002573#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002574 if(!wpa_s->current_ssid) {
2575 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2576 break;
2577 }
2578 /* If assoc reject is reported by the driver, then avoid
2579 * waiting for the authentication timeout. Cancel the
2580 * authentication timeout and retry the assoc.
2581 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002582 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002583 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2584 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002585
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002586 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002587
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002588 /* Clear the states */
2589 wpa_sm_notify_disassoc(wpa_s->wpa);
2590 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2591
2592 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002593 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002594 const u8 *bl_bssid = data->assoc_reject.bssid;
2595 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2596 bl_bssid = wpa_s->pending_bssid;
2597 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002598 wpa_supplicant_req_scan(wpa_s, 0, 0);
2599 } else {
2600 wpa_supplicant_req_scan(wpa_s, 1, 0);
2601 }
2602 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002603 /* If we ASSOC_REJECT's hits threshold, disable the
2604 * network
2605 */
2606 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2607 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002608 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002609 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002610 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002611 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002612#else
2613 const u8 *bssid = data->assoc_reject.bssid;
2614 if (bssid == NULL || is_zero_ether_addr(bssid))
2615 bssid = wpa_s->pending_bssid;
2616 wpas_connection_failed(wpa_s, bssid);
2617 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002618#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 break;
2621 case EVENT_AUTH_TIMED_OUT:
2622 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2623 sme_event_auth_timed_out(wpa_s, data);
2624 break;
2625 case EVENT_ASSOC_TIMED_OUT:
2626 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2627 sme_event_assoc_timed_out(wpa_s, data);
2628 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 case EVENT_TX_STATUS:
2630 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2631 " type=%d stype=%d",
2632 MAC2STR(data->tx_status.dst),
2633 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002634#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002636#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2638 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002639 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 wpa_s, data->tx_status.dst,
2641 data->tx_status.data,
2642 data->tx_status.data_len,
2643 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002644 OFFCHANNEL_SEND_ACTION_SUCCESS :
2645 OFFCHANNEL_SEND_ACTION_NO_ACK);
2646#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 break;
2648 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002649#endif /* CONFIG_AP */
2650#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2652 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2653 /*
2654 * Catch TX status events for Action frames we sent via group
2655 * interface in GO mode.
2656 */
2657 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2658 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2659 os_memcmp(wpa_s->parent->pending_action_dst,
2660 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002661 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 wpa_s->parent, data->tx_status.dst,
2663 data->tx_status.data,
2664 data->tx_status.data_len,
2665 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002666 OFFCHANNEL_SEND_ACTION_SUCCESS :
2667 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668 break;
2669 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002670#endif /* CONFIG_OFFCHANNEL */
2671#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 switch (data->tx_status.type) {
2673 case WLAN_FC_TYPE_MGMT:
2674 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2675 data->tx_status.data_len,
2676 data->tx_status.stype,
2677 data->tx_status.ack);
2678 break;
2679 case WLAN_FC_TYPE_DATA:
2680 ap_tx_status(wpa_s, data->tx_status.dst,
2681 data->tx_status.data,
2682 data->tx_status.data_len,
2683 data->tx_status.ack);
2684 break;
2685 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002686#endif /* CONFIG_AP */
2687 break;
2688#ifdef CONFIG_AP
2689 case EVENT_EAPOL_TX_STATUS:
2690 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2691 data->eapol_tx_status.data,
2692 data->eapol_tx_status.data_len,
2693 data->eapol_tx_status.ack);
2694 break;
2695 case EVENT_DRIVER_CLIENT_POLL_OK:
2696 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 break;
2698 case EVENT_RX_FROM_UNKNOWN:
2699 if (wpa_s->ap_iface == NULL)
2700 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002701 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2702 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002704 case EVENT_CH_SWITCH:
2705 if (!data)
2706 break;
2707 if (!wpa_s->ap_iface) {
2708 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2709 "event in non-AP mode");
2710 break;
2711 }
2712
2713#ifdef CONFIG_AP
2714 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2715 data->ch_switch.ht_enabled,
2716 data->ch_switch.ch_offset);
2717#endif /* CONFIG_AP */
2718 break;
2719 case EVENT_RX_MGMT: {
2720 u16 fc, stype;
2721 const struct ieee80211_mgmt *mgmt;
2722
2723 mgmt = (const struct ieee80211_mgmt *)
2724 data->rx_mgmt.frame;
2725 fc = le_to_host16(mgmt->frame_control);
2726 stype = WLAN_FC_GET_STYPE(fc);
2727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 if (wpa_s->ap_iface == NULL) {
2729#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2731 data->rx_mgmt.frame_len > 24) {
2732 const u8 *src = mgmt->sa;
2733 const u8 *ie = mgmt->u.probe_req.variable;
2734 size_t ie_len = data->rx_mgmt.frame_len -
2735 (mgmt->u.probe_req.variable -
2736 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002737 wpas_p2p_probe_req_rx(
2738 wpa_s, src, mgmt->da,
2739 mgmt->bssid, ie, ie_len,
2740 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 break;
2742 }
2743#endif /* CONFIG_P2P */
2744 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2745 "management frame in non-AP mode");
2746 break;
2747 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002748
2749 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2750 data->rx_mgmt.frame_len > 24) {
2751 const u8 *ie = mgmt->u.probe_req.variable;
2752 size_t ie_len = data->rx_mgmt.frame_len -
2753 (mgmt->u.probe_req.variable -
2754 data->rx_mgmt.frame);
2755
2756 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2757 mgmt->bssid, ie, ie_len,
2758 data->rx_mgmt.ssi_signal);
2759 }
2760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2762 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002763 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764#endif /* CONFIG_AP */
2765 case EVENT_RX_ACTION:
2766 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2767 " Category=%u DataLen=%d freq=%d MHz",
2768 MAC2STR(data->rx_action.sa),
2769 data->rx_action.category, (int) data->rx_action.len,
2770 data->rx_action.freq);
2771#ifdef CONFIG_IEEE80211R
2772 if (data->rx_action.category == WLAN_ACTION_FT) {
2773 ft_rx_action(wpa_s, data->rx_action.data,
2774 data->rx_action.len);
2775 break;
2776 }
2777#endif /* CONFIG_IEEE80211R */
2778#ifdef CONFIG_IEEE80211W
2779#ifdef CONFIG_SME
2780 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2781 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2782 data->rx_action.data,
2783 data->rx_action.len);
2784 break;
2785 }
2786#endif /* CONFIG_SME */
2787#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002788#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002789 if (data->rx_action.category == WLAN_ACTION_WNM) {
2790 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2791 break;
2792 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002793#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002794#ifdef CONFIG_GAS
2795 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2796 gas_query_rx(wpa_s->gas, data->rx_action.da,
2797 data->rx_action.sa, data->rx_action.bssid,
2798 data->rx_action.data, data->rx_action.len,
2799 data->rx_action.freq) == 0)
2800 break;
2801#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802#ifdef CONFIG_TDLS
2803 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2804 data->rx_action.len >= 4 &&
2805 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2806 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2807 "Response from " MACSTR,
2808 MAC2STR(data->rx_action.sa));
2809 break;
2810 }
2811#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812#ifdef CONFIG_P2P
2813 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2814 data->rx_action.sa,
2815 data->rx_action.bssid,
2816 data->rx_action.category,
2817 data->rx_action.data,
2818 data->rx_action.len, data->rx_action.freq);
2819#endif /* CONFIG_P2P */
2820 break;
2821 case EVENT_RX_PROBE_REQ:
2822 if (data->rx_probe_req.sa == NULL ||
2823 data->rx_probe_req.ie == NULL)
2824 break;
2825#ifdef CONFIG_AP
2826 if (wpa_s->ap_iface) {
2827 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2828 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002829 data->rx_probe_req.da,
2830 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002832 data->rx_probe_req.ie_len,
2833 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 break;
2835 }
2836#endif /* CONFIG_AP */
2837#ifdef CONFIG_P2P
2838 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002839 data->rx_probe_req.da,
2840 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002842 data->rx_probe_req.ie_len,
2843 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844#endif /* CONFIG_P2P */
2845 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847#ifdef CONFIG_OFFCHANNEL
2848 offchannel_remain_on_channel_cb(
2849 wpa_s, data->remain_on_channel.freq,
2850 data->remain_on_channel.duration);
2851#endif /* CONFIG_OFFCHANNEL */
2852#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 wpas_p2p_remain_on_channel_cb(
2854 wpa_s, data->remain_on_channel.freq,
2855 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002856#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 break;
2858 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859#ifdef CONFIG_OFFCHANNEL
2860 offchannel_cancel_remain_on_channel_cb(
2861 wpa_s, data->remain_on_channel.freq);
2862#endif /* CONFIG_OFFCHANNEL */
2863#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 wpas_p2p_cancel_remain_on_channel_cb(
2865 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002868#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 case EVENT_P2P_DEV_FOUND: {
2870 struct p2p_peer_info peer_info;
2871
2872 os_memset(&peer_info, 0, sizeof(peer_info));
2873 if (data->p2p_dev_found.dev_addr)
2874 os_memcpy(peer_info.p2p_device_addr,
2875 data->p2p_dev_found.dev_addr, ETH_ALEN);
2876 if (data->p2p_dev_found.pri_dev_type)
2877 os_memcpy(peer_info.pri_dev_type,
2878 data->p2p_dev_found.pri_dev_type,
2879 sizeof(peer_info.pri_dev_type));
2880 if (data->p2p_dev_found.dev_name)
2881 os_strlcpy(peer_info.device_name,
2882 data->p2p_dev_found.dev_name,
2883 sizeof(peer_info.device_name));
2884 peer_info.config_methods = data->p2p_dev_found.config_methods;
2885 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2886 peer_info.group_capab = data->p2p_dev_found.group_capab;
2887
2888 /*
2889 * FIX: new_device=1 is not necessarily correct. We should
2890 * maintain a P2P peer database in wpa_supplicant and update
2891 * this information based on whether the peer is truly new.
2892 */
2893 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2894 break;
2895 }
2896 case EVENT_P2P_GO_NEG_REQ_RX:
2897 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2898 data->p2p_go_neg_req_rx.dev_passwd_id);
2899 break;
2900 case EVENT_P2P_GO_NEG_COMPLETED:
2901 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2902 break;
2903 case EVENT_P2P_PROV_DISC_REQUEST:
2904 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2905 data->p2p_prov_disc_req.config_methods,
2906 data->p2p_prov_disc_req.dev_addr,
2907 data->p2p_prov_disc_req.pri_dev_type,
2908 data->p2p_prov_disc_req.dev_name,
2909 data->p2p_prov_disc_req.supp_config_methods,
2910 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002911 data->p2p_prov_disc_req.group_capab,
2912 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 break;
2914 case EVENT_P2P_PROV_DISC_RESPONSE:
2915 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2916 data->p2p_prov_disc_resp.config_methods);
2917 break;
2918 case EVENT_P2P_SD_REQUEST:
2919 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2920 data->p2p_sd_req.sa,
2921 data->p2p_sd_req.dialog_token,
2922 data->p2p_sd_req.update_indic,
2923 data->p2p_sd_req.tlvs,
2924 data->p2p_sd_req.tlvs_len);
2925 break;
2926 case EVENT_P2P_SD_RESPONSE:
2927 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2928 data->p2p_sd_resp.update_indic,
2929 data->p2p_sd_resp.tlvs,
2930 data->p2p_sd_resp.tlvs_len);
2931 break;
2932#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 case EVENT_EAPOL_RX:
2934 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2935 data->eapol_rx.data,
2936 data->eapol_rx.data_len);
2937 break;
2938 case EVENT_SIGNAL_CHANGE:
2939 bgscan_notify_signal_change(
2940 wpa_s, data->signal_change.above_threshold,
2941 data->signal_change.current_signal,
2942 data->signal_change.current_noise,
2943 data->signal_change.current_txrate);
2944 break;
2945 case EVENT_INTERFACE_ENABLED:
2946 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2947 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002948 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949#ifdef CONFIG_AP
2950 if (!wpa_s->ap_iface) {
2951 wpa_supplicant_set_state(wpa_s,
2952 WPA_DISCONNECTED);
2953 wpa_supplicant_req_scan(wpa_s, 0, 0);
2954 } else
2955 wpa_supplicant_set_state(wpa_s,
2956 WPA_COMPLETED);
2957#else /* CONFIG_AP */
2958 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2959 wpa_supplicant_req_scan(wpa_s, 0, 0);
2960#endif /* CONFIG_AP */
2961 }
2962 break;
2963 case EVENT_INTERFACE_DISABLED:
2964 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2965 wpa_supplicant_mark_disassoc(wpa_s);
2966 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2967 break;
2968 case EVENT_CHANNEL_LIST_CHANGED:
2969 if (wpa_s->drv_priv == NULL)
2970 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002971
2972 free_hw_features(wpa_s);
2973 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2974 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976#ifdef CONFIG_P2P
2977 wpas_p2p_update_channel_list(wpa_s);
2978#endif /* CONFIG_P2P */
2979 break;
2980 case EVENT_INTERFACE_UNAVAILABLE:
2981#ifdef CONFIG_P2P
2982 wpas_p2p_interface_unavailable(wpa_s);
2983#endif /* CONFIG_P2P */
2984 break;
2985 case EVENT_BEST_CHANNEL:
2986 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2987 "(%d %d %d)",
2988 data->best_chan.freq_24, data->best_chan.freq_5,
2989 data->best_chan.freq_overall);
2990 wpa_s->best_24_freq = data->best_chan.freq_24;
2991 wpa_s->best_5_freq = data->best_chan.freq_5;
2992 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2993#ifdef CONFIG_P2P
2994 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2995 data->best_chan.freq_5,
2996 data->best_chan.freq_overall);
2997#endif /* CONFIG_P2P */
2998 break;
2999 case EVENT_UNPROT_DEAUTH:
3000 wpa_supplicant_event_unprot_deauth(wpa_s,
3001 &data->unprot_deauth);
3002 break;
3003 case EVENT_UNPROT_DISASSOC:
3004 wpa_supplicant_event_unprot_disassoc(wpa_s,
3005 &data->unprot_disassoc);
3006 break;
3007 case EVENT_STATION_LOW_ACK:
3008#ifdef CONFIG_AP
3009 if (wpa_s->ap_iface && data)
3010 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3011 data->low_ack.addr);
3012#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003013#ifdef CONFIG_TDLS
3014 if (data)
3015 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3016#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 break;
3018 case EVENT_IBSS_PEER_LOST:
3019#ifdef CONFIG_IBSS_RSN
3020 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3021#endif /* CONFIG_IBSS_RSN */
3022 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003023 case EVENT_DRIVER_GTK_REKEY:
3024 if (os_memcmp(data->driver_gtk_rekey.bssid,
3025 wpa_s->bssid, ETH_ALEN))
3026 break;
3027 if (!wpa_s->wpa)
3028 break;
3029 wpa_sm_update_replay_ctr(wpa_s->wpa,
3030 data->driver_gtk_rekey.replay_ctr);
3031 break;
3032 case EVENT_SCHED_SCAN_STOPPED:
3033 wpa_s->sched_scanning = 0;
3034 wpa_supplicant_notify_scanning(wpa_s, 0);
3035
3036 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3037 break;
3038
3039 /*
3040 * If we timed out, start a new sched scan to continue
3041 * searching for more SSIDs.
3042 */
3043 if (wpa_s->sched_scan_timed_out)
3044 wpa_supplicant_req_sched_scan(wpa_s);
3045 break;
3046 case EVENT_WPS_BUTTON_PUSHED:
3047#ifdef CONFIG_WPS
3048 wpas_wps_start_pbc(wpa_s, NULL, 0);
3049#endif /* CONFIG_WPS */
3050 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003051 case EVENT_CONNECT_FAILED_REASON:
3052#ifdef CONFIG_AP
3053 if (!wpa_s->ap_iface || !data)
3054 break;
3055 hostapd_event_connect_failed_reason(
3056 wpa_s->ap_iface->bss[0],
3057 data->connect_failed_reason.addr,
3058 data->connect_failed_reason.code);
3059#endif /* CONFIG_AP */
3060 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 default:
3062 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3063 break;
3064 }
3065}