blob: 650221a077b97a02490cb48644b414daa2defced [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 Shmidt8da800a2013-04-24 12:57:01 -070047static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
48 int new_scan);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080049
50
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070051static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
52 struct wpa_ssid *ssid)
53{
54 struct os_time now;
55
56 if (ssid == NULL || ssid->disabled_until.sec == 0)
57 return 0;
58
59 os_get_time(&now);
60 if (ssid->disabled_until.sec > now.sec)
61 return ssid->disabled_until.sec - now.sec;
62
63 wpas_clear_temp_disabled(wpa_s, ssid, 0);
64
65 return 0;
66}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067
68
69static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
70{
71 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070072 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
74 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
75 return 0;
76
77 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
78 "information");
79 ssid = wpa_supplicant_get_ssid(wpa_s);
80 if (ssid == NULL) {
81 wpa_msg(wpa_s, MSG_INFO,
82 "No network configuration found for the current AP");
83 return -1;
84 }
85
Dmitry Shmidt04949592012-07-19 12:16:46 -070086 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070087 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
88 return -1;
89 }
90
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080091 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
92 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
93 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
94 return -1;
95 }
96
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097 res = wpas_temp_disabled(wpa_s, ssid);
98 if (res > 0) {
99 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
100 "disabled for %d second(s)", res);
101 return -1;
102 }
103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
105 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800106 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 u8 wpa_ie[80];
108 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
110 wpa_ie, &wpa_ie_len) < 0)
111 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112 } else {
113 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
114 }
115
116 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
117 eapol_sm_invalidate_cached_session(wpa_s->eapol);
118 old_ssid = wpa_s->current_ssid;
119 wpa_s->current_ssid = ssid;
120 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
121 wpa_supplicant_initiate_eapol(wpa_s);
122 if (old_ssid != wpa_s->current_ssid)
123 wpas_notify_network_changed(wpa_s);
124
125 return 0;
126}
127
128
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800129void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130{
131 struct wpa_supplicant *wpa_s = eloop_ctx;
132
133 if (wpa_s->countermeasures) {
134 wpa_s->countermeasures = 0;
135 wpa_drv_set_countermeasures(wpa_s, 0);
136 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
137 wpa_supplicant_req_scan(wpa_s, 0, 0);
138 }
139}
140
141
142void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
143{
144 int bssid_changed;
145
Dmitry Shmidt04949592012-07-19 12:16:46 -0700146 wnm_bss_keep_alive_deinit(wpa_s);
147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148#ifdef CONFIG_IBSS_RSN
149 ibss_rsn_deinit(wpa_s->ibss_rsn);
150 wpa_s->ibss_rsn = NULL;
151#endif /* CONFIG_IBSS_RSN */
152
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700153#ifdef CONFIG_AP
154 wpa_supplicant_ap_deinit(wpa_s);
155#endif /* CONFIG_AP */
156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
158 return;
159
160 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800161#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700162 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800163#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
165 os_memset(wpa_s->bssid, 0, ETH_ALEN);
166 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700167#ifdef CONFIG_SME
168 wpa_s->sme.prev_bssid_set = 0;
169#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800170#ifdef CONFIG_P2P
171 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
172#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 wpa_s->current_bss = NULL;
174 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700175#ifdef CONFIG_IEEE80211R
176#ifdef CONFIG_SME
177 if (wpa_s->sme.ft_ies)
178 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
179#endif /* CONFIG_SME */
180#endif /* CONFIG_IEEE80211R */
181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 if (bssid_changed)
183 wpas_notify_bssid_changed(wpa_s);
184
185 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
186 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
187 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
188 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
189 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700190 wpa_s->current_ssid = NULL;
191 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192}
193
194
195static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
196{
197 struct wpa_ie_data ie;
198 int pmksa_set = -1;
199 size_t i;
200
201 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
202 ie.pmkid == NULL)
203 return;
204
205 for (i = 0; i < ie.num_pmkid; i++) {
206 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
207 ie.pmkid + i * PMKID_LEN,
208 NULL, NULL, 0);
209 if (pmksa_set == 0) {
210 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
211 break;
212 }
213 }
214
215 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
216 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
217}
218
219
220static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
221 union wpa_event_data *data)
222{
223 if (data == NULL) {
224 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
225 "event");
226 return;
227 }
228 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
229 " index=%d preauth=%d",
230 MAC2STR(data->pmkid_candidate.bssid),
231 data->pmkid_candidate.index,
232 data->pmkid_candidate.preauth);
233
234 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
235 data->pmkid_candidate.index,
236 data->pmkid_candidate.preauth);
237}
238
239
240static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
241{
242 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
243 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
244 return 0;
245
246#ifdef IEEE8021X_EAPOL
247 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
248 wpa_s->current_ssid &&
249 !(wpa_s->current_ssid->eapol_flags &
250 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
251 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
252 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
253 * plaintext or static WEP keys). */
254 return 0;
255 }
256#endif /* IEEE8021X_EAPOL */
257
258 return 1;
259}
260
261
262/**
263 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
264 * @wpa_s: pointer to wpa_supplicant data
265 * @ssid: Configuration data for the network
266 * Returns: 0 on success, -1 on failure
267 *
268 * This function is called when starting authentication with a network that is
269 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
270 */
271int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
272 struct wpa_ssid *ssid)
273{
274#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800275#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 int aka = 0, sim = 0, type;
277
278 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
279 return 0;
280
281 if (ssid->eap.eap_methods == NULL) {
282 sim = 1;
283 aka = 1;
284 } else {
285 struct eap_method_type *eap = ssid->eap.eap_methods;
286 while (eap->vendor != EAP_VENDOR_IETF ||
287 eap->method != EAP_TYPE_NONE) {
288 if (eap->vendor == EAP_VENDOR_IETF) {
289 if (eap->method == EAP_TYPE_SIM)
290 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700291 else if (eap->method == EAP_TYPE_AKA ||
292 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 aka = 1;
294 }
295 eap++;
296 }
297 }
298
299 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
300 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700301 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
302 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
303 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 aka = 0;
305
306 if (!sim && !aka) {
307 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
308 "use SIM, but neither EAP-SIM nor EAP-AKA are "
309 "enabled");
310 return 0;
311 }
312
313 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
314 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
315 if (sim && aka)
316 type = SCARD_TRY_BOTH;
317 else if (aka)
318 type = SCARD_USIM_ONLY;
319 else
320 type = SCARD_GSM_SIM_ONLY;
321
Dmitry Shmidt04949592012-07-19 12:16:46 -0700322 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 if (wpa_s->scard == NULL) {
324 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
325 "(pcsc-lite)");
326 return -1;
327 }
328 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
329 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800330#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331#endif /* IEEE8021X_EAPOL */
332
333 return 0;
334}
335
336
337#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700338static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 struct wpa_ssid *ssid)
340{
341 int i, privacy = 0;
342
343 if (ssid->mixed_cell)
344 return 1;
345
346#ifdef CONFIG_WPS
347 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
348 return 1;
349#endif /* CONFIG_WPS */
350
351 for (i = 0; i < NUM_WEP_KEYS; i++) {
352 if (ssid->wep_key_len[i]) {
353 privacy = 1;
354 break;
355 }
356 }
357#ifdef IEEE8021X_EAPOL
358 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
359 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
360 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
361 privacy = 1;
362#endif /* IEEE8021X_EAPOL */
363
Jouni Malinen75ecf522011-06-27 15:19:46 -0700364 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
365 privacy = 1;
366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367 if (bss->caps & IEEE80211_CAP_PRIVACY)
368 return privacy;
369 return !privacy;
370}
371
372
373static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
374 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700375 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376{
377 struct wpa_ie_data ie;
378 int proto_match = 0;
379 const u8 *rsn_ie, *wpa_ie;
380 int ret;
381 int wep_ok;
382
383 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
384 if (ret >= 0)
385 return ret;
386
387 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
388 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
389 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
390 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
391 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
392
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700393 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
395 proto_match++;
396
397 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
398 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
399 "failed");
400 break;
401 }
402
403 if (wep_ok &&
404 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
405 {
406 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
407 "in RSN IE");
408 return 1;
409 }
410
411 if (!(ie.proto & ssid->proto)) {
412 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
413 "mismatch");
414 break;
415 }
416
417 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
418 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
419 "cipher mismatch");
420 break;
421 }
422
423 if (!(ie.group_cipher & ssid->group_cipher)) {
424 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
425 "cipher mismatch");
426 break;
427 }
428
429 if (!(ie.key_mgmt & ssid->key_mgmt)) {
430 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
431 "mismatch");
432 break;
433 }
434
435#ifdef CONFIG_IEEE80211W
436 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800437 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
438 wpa_s->conf->pmf : ssid->ieee80211w) ==
439 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
441 "frame protection");
442 break;
443 }
444#endif /* CONFIG_IEEE80211W */
445
446 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
447 return 1;
448 }
449
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700450 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
452 proto_match++;
453
454 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
455 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
456 "failed");
457 break;
458 }
459
460 if (wep_ok &&
461 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
462 {
463 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
464 "in WPA IE");
465 return 1;
466 }
467
468 if (!(ie.proto & ssid->proto)) {
469 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
470 "mismatch");
471 break;
472 }
473
474 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
475 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
476 "cipher mismatch");
477 break;
478 }
479
480 if (!(ie.group_cipher & ssid->group_cipher)) {
481 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
482 "cipher mismatch");
483 break;
484 }
485
486 if (!(ie.key_mgmt & ssid->key_mgmt)) {
487 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
488 "mismatch");
489 break;
490 }
491
492 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
493 return 1;
494 }
495
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700496 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
497 !rsn_ie) {
498 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
499 return 1;
500 }
501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
503 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
504 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
505 return 0;
506 }
507
508 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
509 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
510 return 1;
511 }
512
513 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
514 "WPA/WPA2");
515
516 return 0;
517}
518
519
520static int freq_allowed(int *freqs, int freq)
521{
522 int i;
523
524 if (freqs == NULL)
525 return 1;
526
527 for (i = 0; freqs[i]; i++)
528 if (freqs[i] == freq)
529 return 1;
530 return 0;
531}
532
533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800534static int ht_supported(const struct hostapd_hw_modes *mode)
535{
536 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
537 /*
538 * The driver did not indicate whether it supports HT. Assume
539 * it does to avoid connection issues.
540 */
541 return 1;
542 }
543
544 /*
545 * IEEE Std 802.11n-2009 20.1.1:
546 * An HT non-AP STA shall support all EQM rates for one spatial stream.
547 */
548 return mode->mcs_set[0] == 0xff;
549}
550
551
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700552static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800553{
554 const struct hostapd_hw_modes *mode = NULL, *modes;
555 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
556 const u8 *rate_ie;
557 int i, j, k;
558
559 if (bss->freq == 0)
560 return 1; /* Cannot do matching without knowing band */
561
562 modes = wpa_s->hw.modes;
563 if (modes == NULL) {
564 /*
565 * The driver does not provide any additional information
566 * about the utilized hardware, so allow the connection attempt
567 * to continue.
568 */
569 return 1;
570 }
571
572 for (i = 0; i < wpa_s->hw.num_modes; i++) {
573 for (j = 0; j < modes[i].num_channels; j++) {
574 int freq = modes[i].channels[j].freq;
575 if (freq == bss->freq) {
576 if (mode &&
577 mode->mode == HOSTAPD_MODE_IEEE80211G)
578 break; /* do not allow 802.11b replace
579 * 802.11g */
580 mode = &modes[i];
581 break;
582 }
583 }
584 }
585
586 if (mode == NULL)
587 return 0;
588
589 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700590 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 if (rate_ie == NULL)
592 continue;
593
594 for (j = 2; j < rate_ie[1] + 2; j++) {
595 int flagged = !!(rate_ie[j] & 0x80);
596 int r = (rate_ie[j] & 0x7f) * 5;
597
598 /*
599 * IEEE Std 802.11n-2009 7.3.2.2:
600 * The new BSS Membership selector value is encoded
601 * like a legacy basic rate, but it is not a rate and
602 * only indicates if the BSS members are required to
603 * support the mandatory features of Clause 20 [HT PHY]
604 * in order to join the BSS.
605 */
606 if (flagged && ((rate_ie[j] & 0x7f) ==
607 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
608 if (!ht_supported(mode)) {
609 wpa_dbg(wpa_s, MSG_DEBUG,
610 " hardware does not support "
611 "HT PHY");
612 return 0;
613 }
614 continue;
615 }
616
617 if (!flagged)
618 continue;
619
620 /* check for legacy basic rates */
621 for (k = 0; k < mode->num_rates; k++) {
622 if (mode->rates[k] == r)
623 break;
624 }
625 if (k == mode->num_rates) {
626 /*
627 * IEEE Std 802.11-2007 7.3.2.2 demands that in
628 * order to join a BSS all required rates
629 * have to be supported by the hardware.
630 */
631 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
632 "not support required rate %d.%d Mbps",
633 r / 10, r % 10);
634 return 0;
635 }
636 }
637 }
638
639 return 1;
640}
641
642
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800643static int bss_is_dmg(struct wpa_bss *bss)
644{
645 return bss->freq > 45000;
646}
647
648
649/*
650 * Test whether BSS is in an ESS.
651 * This is done differently in DMG (60 GHz) and non-DMG bands
652 */
653static int bss_is_ess(struct wpa_bss *bss)
654{
655 if (bss_is_dmg(bss)) {
656 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
657 IEEE80211_CAP_DMG_AP;
658 }
659
660 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
661 IEEE80211_CAP_ESS);
662}
663
664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700666 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667 struct wpa_ssid *group)
668{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700669 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 int wpa;
671 struct wpa_blacklist *e;
672 const u8 *ie;
673 struct wpa_ssid *ssid;
674
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700675 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 wpa_ie_len = ie ? ie[1] : 0;
677
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700678 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 rsn_ie_len = ie ? ie[1] : 0;
680
681 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
682 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700683 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700685 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686
687 e = wpa_blacklist_get(wpa_s, bss->bssid);
688 if (e) {
689 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700690 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 /*
692 * When only a single network is enabled, we can
693 * trigger blacklisting on the first failure. This
694 * should not be done with multiple enabled networks to
695 * avoid getting forced to move into a worse ESS on
696 * single error if there are no other BSSes of the
697 * current ESS.
698 */
699 limit = 0;
700 }
701 if (e->count > limit) {
702 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
703 "(count=%d limit=%d)", e->count, limit);
704 return NULL;
705 }
706 }
707
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700708 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
710 return NULL;
711 }
712
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800713 if (disallowed_bssid(wpa_s, bss->bssid)) {
714 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
715 return NULL;
716 }
717
718 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
719 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
720 return NULL;
721 }
722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
724
725 for (ssid = group; ssid; ssid = ssid->pnext) {
726 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700727 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
Dmitry Shmidt04949592012-07-19 12:16:46 -0700729 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
731 continue;
732 }
733
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700734 res = wpas_temp_disabled(wpa_s, ssid);
735 if (res > 0) {
736 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
737 "temporarily for %d second(s)", res);
738 continue;
739 }
740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741#ifdef CONFIG_WPS
742 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
743 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
744 "(WPS)");
745 continue;
746 }
747
748 if (wpa && ssid->ssid_len == 0 &&
749 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
750 check_ssid = 0;
751
752 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
753 /* Only allow wildcard SSID match if an AP
754 * advertises active WPS operation that matches
755 * with our mode. */
756 check_ssid = 1;
757 if (ssid->ssid_len == 0 &&
758 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
759 check_ssid = 0;
760 }
761#endif /* CONFIG_WPS */
762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763 if (ssid->bssid_set && ssid->ssid_len == 0 &&
764 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
765 check_ssid = 0;
766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700768 (bss->ssid_len != ssid->ssid_len ||
769 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
771 continue;
772 }
773
774 if (ssid->bssid_set &&
775 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
776 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
777 continue;
778 }
779
780 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
781 continue;
782
783 if (!wpa &&
784 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
785 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
786 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
787 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
788 "not allowed");
789 continue;
790 }
791
Jouni Malinen75ecf522011-06-27 15:19:46 -0700792 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
794 "mismatch");
795 continue;
796 }
797
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800798 if (!bss_is_ess(bss)) {
799 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 continue;
801 }
802
803 if (!freq_allowed(ssid->freq_list, bss->freq)) {
804 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
805 "allowed");
806 continue;
807 }
808
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800809 if (!rate_match(wpa_s, bss)) {
810 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
811 "not match");
812 continue;
813 }
814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815#ifdef CONFIG_P2P
816 /*
817 * TODO: skip the AP if its P2P IE has Group Formation
818 * bit set in the P2P Group Capability Bitmap and we
819 * are not in Group Formation with that device.
820 */
821#endif /* CONFIG_P2P */
822
823 /* Matching configuration found */
824 return ssid;
825 }
826
827 /* No matching configuration found */
828 return NULL;
829}
830
831
832static struct wpa_bss *
833wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 struct wpa_ssid *group,
835 struct wpa_ssid **selected_ssid)
836{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700837 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838
839 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
840 group->priority);
841
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700842 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
843 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
845 if (!*selected_ssid)
846 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
848 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700849 MAC2STR(bss->bssid),
850 wpa_ssid_txt(bss->ssid, bss->ssid_len));
851 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 }
853
854 return NULL;
855}
856
857
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700858struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
859 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860{
861 struct wpa_bss *selected = NULL;
862 int prio;
863
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700864 if (wpa_s->last_scan_res == NULL ||
865 wpa_s->last_scan_res_used == 0)
866 return NULL; /* no scan results from last update */
867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 while (selected == NULL) {
869 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
870 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700871 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 selected_ssid);
873 if (selected)
874 break;
875 }
876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877 if (selected == NULL && wpa_s->blacklist &&
878 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
880 "blacklist and try again");
881 wpa_blacklist_clear(wpa_s);
882 wpa_s->blacklist_cleared++;
883 } else if (selected == NULL)
884 break;
885 }
886
887 return selected;
888}
889
890
891static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
892 int timeout_sec, int timeout_usec)
893{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700894 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 /*
896 * No networks are enabled; short-circuit request so
897 * we don't wait timeout seconds before transitioning
898 * to INACTIVE state.
899 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700900 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
901 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700903#ifdef CONFIG_P2P
904 wpa_s->sta_scan_pending = 0;
905#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 return;
907 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800908
909 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
911}
912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800913
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700914int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 struct wpa_bss *selected,
916 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917{
918 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
919 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
920 "PBC session overlap");
921#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700923 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924#endif /* CONFIG_P2P */
925
926#ifdef CONFIG_WPS
927 wpas_wps_cancel(wpa_s);
928#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700929 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 }
931
932 /*
933 * Do not trigger new association unless the BSSID has changed or if
934 * reassociation is requested. If we are in process of associating with
935 * the selected BSSID, do not trigger new attempt.
936 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
939 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
940 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
941 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
942 0))) {
943 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
944 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700945 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 }
947 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
948 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
949 " pending: " MACSTR " wpa_state: %s",
950 wpa_s->reassociate, MAC2STR(selected->bssid),
951 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
952 wpa_supplicant_state_txt(wpa_s->wpa_state));
953 wpa_supplicant_associate(wpa_s, selected, ssid);
954 } else {
955 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
956 "selected AP");
957 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800958
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700959 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960}
961
962
963static struct wpa_ssid *
964wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
965{
966 int prio;
967 struct wpa_ssid *ssid;
968
969 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
970 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
971 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700972 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 continue;
974 if (ssid->mode == IEEE80211_MODE_IBSS ||
975 ssid->mode == IEEE80211_MODE_AP)
976 return ssid;
977 }
978 }
979 return NULL;
980}
981
982
983/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
984 * on BSS added and BSS changed events */
985static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300986 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300988 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
990 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
991 return;
992
Jouni Malinen87fd2792011-05-16 18:35:42 +0300993 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995
Jouni Malinen87fd2792011-05-16 18:35:42 +0300996 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 if (ssid == NULL)
998 continue;
999
Jouni Malinen87fd2792011-05-16 18:35:42 +03001000 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 if (rsn == NULL)
1002 continue;
1003
Jouni Malinen87fd2792011-05-16 18:35:42 +03001004 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
1006
1007}
1008
1009
1010static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1011 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001012 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001014 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 int min_diff;
1016
1017 if (wpa_s->reassociate)
1018 return 1; /* explicit request to reassociate */
1019 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1020 return 1; /* we are not associated; continue */
1021 if (wpa_s->current_ssid == NULL)
1022 return 1; /* unknown current SSID */
1023 if (wpa_s->current_ssid != ssid)
1024 return 1; /* different network block */
1025
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001026 if (wpas_driver_bss_selection(wpa_s))
1027 return 0; /* Driver-based roaming */
1028
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001029 if (wpa_s->current_ssid->ssid)
1030 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1031 wpa_s->current_ssid->ssid,
1032 wpa_s->current_ssid->ssid_len);
1033 if (!current_bss)
1034 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035
1036 if (!current_bss)
1037 return 1; /* current BSS not seen in scan results */
1038
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001039 if (current_bss == selected)
1040 return 0;
1041
1042 if (selected->last_update_idx > current_bss->last_update_idx)
1043 return 1; /* current BSS not seen in the last scan */
1044
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001045#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1047 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1048 MAC2STR(current_bss->bssid), current_bss->level);
1049 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1050 MAC2STR(selected->bssid), selected->level);
1051
1052 if (wpa_s->current_ssid->bssid_set &&
1053 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1054 0) {
1055 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1056 "has preferred BSSID");
1057 return 1;
1058 }
1059
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001060 if (current_bss->level < 0 && current_bss->level > selected->level) {
1061 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1062 "signal level");
1063 return 0;
1064 }
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 min_diff = 2;
1067 if (current_bss->level < 0) {
1068 if (current_bss->level < -85)
1069 min_diff = 1;
1070 else if (current_bss->level < -80)
1071 min_diff = 2;
1072 else if (current_bss->level < -75)
1073 min_diff = 3;
1074 else if (current_bss->level < -70)
1075 min_diff = 4;
1076 else
1077 min_diff = 5;
1078 }
1079 if (abs(current_bss->level - selected->level) < min_diff) {
1080 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1081 "in signal level");
1082 return 0;
1083 }
1084
1085 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001086#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001087 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001088#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001091
Jouni Malinen89ca7022012-09-14 13:03:12 -07001092/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001093 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001094static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001095 union wpa_event_data *data,
1096 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 struct wpa_scan_results *scan_res;
1099 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001100#ifndef CONFIG_NO_RANDOM_POOL
1101 size_t i, num;
1102#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103
1104#ifdef CONFIG_AP
1105 if (wpa_s->ap_iface)
1106 ap = 1;
1107#endif /* CONFIG_AP */
1108
1109 wpa_supplicant_notify_scanning(wpa_s, 0);
1110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001111#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001112 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001113 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001114 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1115 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001116 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1118 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1119 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001120 wpa_s->sta_scan_pending = 1;
1121 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001122 return -1;
1123 }
1124 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001125 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001126#endif /* CONFIG_P2P */
1127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1129 data ? &data->scan_info :
1130 NULL, 1);
1131 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001132 if (wpa_s->conf->ap_scan == 2 || ap ||
1133 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001135 if (!own_request)
1136 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1138 "scanning again");
1139 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1140 return -1;
1141 }
1142
1143#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 num = scan_res->num;
1145 if (num > 10)
1146 num = 10;
1147 for (i = 0; i < num; i++) {
1148 u8 buf[5];
1149 struct wpa_scan_res *res = scan_res->res[i];
1150 buf[0] = res->bssid[5];
1151 buf[1] = res->qual & 0xff;
1152 buf[2] = res->noise & 0xff;
1153 buf[3] = res->level & 0xff;
1154 buf[4] = res->tsf & 0xff;
1155 random_add_randomness(buf, sizeof(buf));
1156 }
1157#endif /* CONFIG_NO_RANDOM_POOL */
1158
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001159 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1161 struct wpa_scan_results *scan_res);
1162
1163 scan_res_handler = wpa_s->scan_res_handler;
1164 wpa_s->scan_res_handler = NULL;
1165 scan_res_handler(wpa_s, scan_res);
1166
1167 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001168 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 }
1170
1171 if (ap) {
1172 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1173#ifdef CONFIG_AP
1174 if (wpa_s->ap_iface->scan_cb)
1175 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1176#endif /* CONFIG_AP */
1177 wpa_scan_results_free(scan_res);
1178 return 0;
1179 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001180
Dmitry Shmidt04949592012-07-19 12:16:46 -07001181 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1182 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1183 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184
1185 wpas_notify_scan_done(wpa_s, 1);
1186
Dmitry Shmidt04949592012-07-19 12:16:46 -07001187 if (sme_proc_obss_scan(wpa_s) > 0) {
1188 wpa_scan_results_free(scan_res);
1189 return 0;
1190 }
1191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1193 wpa_scan_results_free(scan_res);
1194 return 0;
1195 }
1196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 if (autoscan_notify_scan(wpa_s, scan_res)) {
1198 wpa_scan_results_free(scan_res);
1199 return 0;
1200 }
1201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 if (wpa_s->disconnected) {
1203 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1204 wpa_scan_results_free(scan_res);
1205 return 0;
1206 }
1207
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001208 if (!wpas_driver_bss_selection(wpa_s) &&
1209 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_scan_results_free(scan_res);
1211 return 0;
1212 }
1213
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001214 wpas_wps_update_ap_info(wpa_s, scan_res);
1215
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001216 wpa_scan_results_free(scan_res);
1217
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001218 return wpas_select_network_from_last_scan(wpa_s, 1);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001219}
1220
1221
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001222static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1223 int new_scan)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001224{
1225 struct wpa_bss *selected;
1226 struct wpa_ssid *ssid = NULL;
1227
1228 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229
1230 if (selected) {
1231 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001232 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001233 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001234 if (new_scan)
1235 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 }
1238
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001239 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001240 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001241 return -1;
1242 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001243 if (new_scan)
1244 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001245 /*
1246 * Do not notify other virtual radios of scan results since we do not
1247 * want them to start other associations at the same time.
1248 */
1249 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1252 ssid = wpa_supplicant_pick_new_network(wpa_s);
1253 if (ssid) {
1254 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1255 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001256 if (new_scan)
1257 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 } else {
1259 int timeout_sec = wpa_s->scan_interval;
1260 int timeout_usec = 0;
1261#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001262 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1263 return 0;
1264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 if (wpa_s->p2p_in_provisioning) {
1266 /*
1267 * Use shorter wait during P2P Provisioning
1268 * state to speed up group formation.
1269 */
1270 timeout_sec = 0;
1271 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1273 timeout_usec);
1274 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 }
1276#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001277#ifdef CONFIG_INTERWORKING
1278 if (wpa_s->conf->auto_interworking &&
1279 wpa_s->conf->interworking &&
1280 wpa_s->conf->cred) {
1281 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1282 "start ANQP fetch since no matching "
1283 "networks found");
1284 wpa_s->network_select = 1;
1285 wpa_s->auto_network_select = 1;
1286 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001287 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001288 }
1289#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001290 if (wpa_supplicant_req_sched_scan(wpa_s))
1291 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1292 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 }
1294 }
1295 return 0;
1296}
1297
1298
1299static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1300 union wpa_event_data *data)
1301{
1302 const char *rn, *rn2;
1303 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001304
1305 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 /*
1307 * If no scan results could be fetched, then no need to
1308 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001309 * this scan. Similarly, if scan results started a new operation on this
1310 * interface, do not notify other interfaces to avoid concurrent
1311 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 */
1313 return;
1314 }
1315
1316 /*
1317 * Check other interfaces to see if they have the same radio-name. If
1318 * so, they get updated with this same scan info.
1319 */
1320 if (!wpa_s->driver->get_radio_name)
1321 return;
1322
1323 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1324 if (rn == NULL || rn[0] == '\0')
1325 return;
1326
1327 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1328 "sharing same radio (%s) in event_scan_results", rn);
1329
1330 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1331 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1332 continue;
1333
1334 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1335 if (rn2 && os_strcmp(rn, rn2) == 0) {
1336 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1337 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001338 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 }
1340 }
1341}
1342
1343#endif /* CONFIG_NO_SCAN_PROCESSING */
1344
1345
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001346int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1347{
1348#ifdef CONFIG_NO_SCAN_PROCESSING
1349 return -1;
1350#else /* CONFIG_NO_SCAN_PROCESSING */
1351 struct os_time now;
1352
1353 if (wpa_s->last_scan_res_used <= 0)
1354 return -1;
1355
1356 os_get_time(&now);
1357 if (now.sec - wpa_s->last_scan.sec > 5) {
1358 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1359 return -1;
1360 }
1361
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001362 return wpas_select_network_from_last_scan(wpa_s, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001363#endif /* CONFIG_NO_SCAN_PROCESSING */
1364}
1365
Dmitry Shmidt04949592012-07-19 12:16:46 -07001366#ifdef CONFIG_WNM
1367
1368static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1369{
1370 struct wpa_supplicant *wpa_s = eloop_ctx;
1371
1372 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1373 return;
1374
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375 if (!wpa_s->no_keep_alive) {
1376 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1377 MAC2STR(wpa_s->bssid));
1378 /* TODO: could skip this if normal data traffic has been sent */
1379 /* TODO: Consider using some more appropriate data frame for
1380 * this */
1381 if (wpa_s->l2)
1382 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1383 (u8 *) "", 0);
1384 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001385
1386#ifdef CONFIG_SME
1387 if (wpa_s->sme.bss_max_idle_period) {
1388 unsigned int msec;
1389 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1390 if (msec > 100)
1391 msec -= 100;
1392 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1393 wnm_bss_keep_alive, wpa_s, NULL);
1394 }
1395#endif /* CONFIG_SME */
1396}
1397
1398
1399static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1400 const u8 *ies, size_t ies_len)
1401{
1402 struct ieee802_11_elems elems;
1403
1404 if (ies == NULL)
1405 return;
1406
1407 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1408 return;
1409
1410#ifdef CONFIG_SME
1411 if (elems.bss_max_idle_period) {
1412 unsigned int msec;
1413 wpa_s->sme.bss_max_idle_period =
1414 WPA_GET_LE16(elems.bss_max_idle_period);
1415 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1416 "TU)%s", wpa_s->sme.bss_max_idle_period,
1417 (elems.bss_max_idle_period[2] & 0x01) ?
1418 " (protected keep-live required)" : "");
1419 if (wpa_s->sme.bss_max_idle_period == 0)
1420 wpa_s->sme.bss_max_idle_period = 1;
1421 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1422 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1423 /* msec times 1000 */
1424 msec = wpa_s->sme.bss_max_idle_period * 1024;
1425 if (msec > 100)
1426 msec -= 100;
1427 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1428 wnm_bss_keep_alive, wpa_s,
1429 NULL);
1430 }
1431 }
1432#endif /* CONFIG_SME */
1433}
1434
1435#endif /* CONFIG_WNM */
1436
1437
1438void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1439{
1440#ifdef CONFIG_WNM
1441 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1442#endif /* CONFIG_WNM */
1443}
1444
1445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1447 union wpa_event_data *data)
1448{
1449 int l, len, found = 0, wpa_found, rsn_found;
1450 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001451#ifdef CONFIG_IEEE80211R
1452 u8 bssid[ETH_ALEN];
1453#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454
1455 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1456 if (data->assoc_info.req_ies)
1457 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1458 data->assoc_info.req_ies_len);
1459 if (data->assoc_info.resp_ies) {
1460 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1461 data->assoc_info.resp_ies_len);
1462#ifdef CONFIG_TDLS
1463 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1464 data->assoc_info.resp_ies_len);
1465#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001466#ifdef CONFIG_WNM
1467 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1468 data->assoc_info.resp_ies_len);
1469#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 }
1471 if (data->assoc_info.beacon_ies)
1472 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1473 data->assoc_info.beacon_ies,
1474 data->assoc_info.beacon_ies_len);
1475 if (data->assoc_info.freq)
1476 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1477 data->assoc_info.freq);
1478
1479 p = data->assoc_info.req_ies;
1480 l = data->assoc_info.req_ies_len;
1481
1482 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1483 while (p && l >= 2) {
1484 len = p[1] + 2;
1485 if (len > l) {
1486 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1487 p, l);
1488 break;
1489 }
1490 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1491 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1492 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1493 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1494 break;
1495 found = 1;
1496 wpa_find_assoc_pmkid(wpa_s);
1497 break;
1498 }
1499 l -= len;
1500 p += len;
1501 }
1502 if (!found && data->assoc_info.req_ies)
1503 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1504
1505#ifdef CONFIG_IEEE80211R
1506#ifdef CONFIG_SME
1507 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1509 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1510 data->assoc_info.resp_ies,
1511 data->assoc_info.resp_ies_len,
1512 bssid) < 0) {
1513 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1514 "Reassociation Response failed");
1515 wpa_supplicant_deauthenticate(
1516 wpa_s, WLAN_REASON_INVALID_IE);
1517 return -1;
1518 }
1519 }
1520
1521 p = data->assoc_info.resp_ies;
1522 l = data->assoc_info.resp_ies_len;
1523
1524#ifdef CONFIG_WPS_STRICT
1525 if (p && wpa_s->current_ssid &&
1526 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1527 struct wpabuf *wps;
1528 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1529 if (wps == NULL) {
1530 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1531 "include WPS IE in (Re)Association Response");
1532 return -1;
1533 }
1534
1535 if (wps_validate_assoc_resp(wps) < 0) {
1536 wpabuf_free(wps);
1537 wpa_supplicant_deauthenticate(
1538 wpa_s, WLAN_REASON_INVALID_IE);
1539 return -1;
1540 }
1541 wpabuf_free(wps);
1542 }
1543#endif /* CONFIG_WPS_STRICT */
1544
1545 /* Go through the IEs and make a copy of the MDIE, if present. */
1546 while (p && l >= 2) {
1547 len = p[1] + 2;
1548 if (len > l) {
1549 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1550 p, l);
1551 break;
1552 }
1553 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1554 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1555 wpa_s->sme.ft_used = 1;
1556 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1557 MOBILITY_DOMAIN_ID_LEN);
1558 break;
1559 }
1560 l -= len;
1561 p += len;
1562 }
1563#endif /* CONFIG_SME */
1564
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001565 /* Process FT when SME is in the driver */
1566 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1567 wpa_ft_is_completed(wpa_s->wpa)) {
1568 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1569 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1570 data->assoc_info.resp_ies,
1571 data->assoc_info.resp_ies_len,
1572 bssid) < 0) {
1573 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1574 "Reassociation Response failed");
1575 wpa_supplicant_deauthenticate(
1576 wpa_s, WLAN_REASON_INVALID_IE);
1577 return -1;
1578 }
1579 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1580 }
1581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1583 data->assoc_info.resp_ies_len);
1584#endif /* CONFIG_IEEE80211R */
1585
1586 /* WPA/RSN IE from Beacon/ProbeResp */
1587 p = data->assoc_info.beacon_ies;
1588 l = data->assoc_info.beacon_ies_len;
1589
1590 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1591 */
1592 wpa_found = rsn_found = 0;
1593 while (p && l >= 2) {
1594 len = p[1] + 2;
1595 if (len > l) {
1596 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1597 p, l);
1598 break;
1599 }
1600 if (!wpa_found &&
1601 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1602 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1603 wpa_found = 1;
1604 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1605 }
1606
1607 if (!rsn_found &&
1608 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1609 rsn_found = 1;
1610 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1611 }
1612
1613 l -= len;
1614 p += len;
1615 }
1616
1617 if (!wpa_found && data->assoc_info.beacon_ies)
1618 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1619 if (!rsn_found && data->assoc_info.beacon_ies)
1620 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1621 if (wpa_found || rsn_found)
1622 wpa_s->ap_ies_from_associnfo = 1;
1623
Jouni Malinen87fd2792011-05-16 18:35:42 +03001624 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1625 wpa_s->assoc_freq != data->assoc_info.freq) {
1626 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1627 "%u to %u MHz",
1628 wpa_s->assoc_freq, data->assoc_info.freq);
1629 wpa_supplicant_update_scan_results(wpa_s);
1630 }
1631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 wpa_s->assoc_freq = data->assoc_info.freq;
1633
1634 return 0;
1635}
1636
1637
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001638static struct wpa_bss * wpa_supplicant_get_new_bss(
1639 struct wpa_supplicant *wpa_s, const u8 *bssid)
1640{
1641 struct wpa_bss *bss = NULL;
1642 struct wpa_ssid *ssid = wpa_s->current_ssid;
1643
1644 if (ssid->ssid_len > 0)
1645 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1646 if (!bss)
1647 bss = wpa_bss_get_bssid(wpa_s, bssid);
1648
1649 return bss;
1650}
1651
1652
1653static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1654{
1655 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1656
1657 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1658 return -1;
1659
1660 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1661 return 0;
1662
1663 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1664 WPA_IE_VENDOR_TYPE);
1665 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1666
1667 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1668 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1669 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1670 bss_rsn ? 2 + bss_rsn[1] : 0))
1671 return -1;
1672
1673 return 0;
1674}
1675
1676
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1678 union wpa_event_data *data)
1679{
1680 u8 bssid[ETH_ALEN];
1681 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 struct wpa_driver_capa capa;
1683
1684#ifdef CONFIG_AP
1685 if (wpa_s->ap_iface) {
1686 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1687 data->assoc_info.addr,
1688 data->assoc_info.req_ies,
1689 data->assoc_info.req_ies_len,
1690 data->assoc_info.reassoc);
1691 return;
1692 }
1693#endif /* CONFIG_AP */
1694
1695 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1696 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1697 return;
1698
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001699 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1700 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001701 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001702 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1703 return;
1704 }
1705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001707 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1709 MACSTR, MAC2STR(bssid));
1710 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1712 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001713 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714
1715 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1716 wpa_clear_keys(wpa_s, bssid);
1717 }
1718 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001719 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1721 return;
1722 }
1723 if (wpa_s->current_ssid) {
1724 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001725
1726 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1727 if (!bss) {
1728 wpa_supplicant_update_scan_results(wpa_s);
1729
1730 /* Get the BSS from the new scan results */
1731 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1732 }
1733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 if (bss)
1735 wpa_s->current_bss = bss;
1736 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001737
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001738#ifdef ANDROID
1739 if (wpa_s->conf->ap_scan == 1) {
1740#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001741 if (wpa_s->conf->ap_scan == 1 &&
1742 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001743#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001744 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1745 wpa_msg(wpa_s, MSG_WARNING,
1746 "WPA/RSN IEs not updated");
1747 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 }
1749
1750#ifdef CONFIG_SME
1751 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1752 wpa_s->sme.prev_bssid_set = 1;
1753#endif /* CONFIG_SME */
1754
1755 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1756 if (wpa_s->current_ssid) {
1757 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1758 * initialized before association, but for other modes,
1759 * initialize PC/SC here, if the current configuration needs
1760 * smartcard or SIM/USIM. */
1761 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1762 }
1763 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1764 if (wpa_s->l2)
1765 l2_packet_notify_auth_start(wpa_s->l2);
1766
1767 /*
1768 * Set portEnabled first to FALSE in order to get EAP state machine out
1769 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1770 * state machine may transit to AUTHENTICATING state based on obsolete
1771 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1772 * AUTHENTICATED without ever giving chance to EAP state machine to
1773 * reset the state.
1774 */
1775 if (!ft_completed) {
1776 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1777 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1778 }
1779 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1780 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1781 /* 802.1X::portControl = Auto */
1782 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1783 wpa_s->eapol_received = 0;
1784 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1785 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1786 (wpa_s->current_ssid &&
1787 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1788 wpa_supplicant_cancel_auth_timeout(wpa_s);
1789 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1790 } else if (!ft_completed) {
1791 /* Timeout for receiving the first EAPOL packet */
1792 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1793 }
1794 wpa_supplicant_cancel_scan(wpa_s);
1795
1796 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1797 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1798 /*
1799 * We are done; the driver will take care of RSN 4-way
1800 * handshake.
1801 */
1802 wpa_supplicant_cancel_auth_timeout(wpa_s);
1803 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1804 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1805 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1806 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1807 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1808 /*
1809 * The driver will take care of RSN 4-way handshake, so we need
1810 * to allow EAPOL supplicant to complete its work without
1811 * waiting for WPA supplicant.
1812 */
1813 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1814 } else if (ft_completed) {
1815 /*
1816 * FT protocol completed - make sure EAPOL state machine ends
1817 * up in authenticated.
1818 */
1819 wpa_supplicant_cancel_auth_timeout(wpa_s);
1820 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1821 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1822 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1823 }
1824
Jouni Malinena05074c2012-12-21 21:35:35 +02001825 wpa_s->last_eapol_matches_bssid = 0;
1826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 if (wpa_s->pending_eapol_rx) {
1828 struct os_time now, age;
1829 os_get_time(&now);
1830 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1831 if (age.sec == 0 && age.usec < 100000 &&
1832 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1833 0) {
1834 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1835 "frame that was received just before "
1836 "association notification");
1837 wpa_supplicant_rx_eapol(
1838 wpa_s, wpa_s->pending_eapol_rx_src,
1839 wpabuf_head(wpa_s->pending_eapol_rx),
1840 wpabuf_len(wpa_s->pending_eapol_rx));
1841 }
1842 wpabuf_free(wpa_s->pending_eapol_rx);
1843 wpa_s->pending_eapol_rx = NULL;
1844 }
1845
1846 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1847 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1848 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1849 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1850 /* Set static WEP keys again */
1851 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1852 }
1853
1854#ifdef CONFIG_IBSS_RSN
1855 if (wpa_s->current_ssid &&
1856 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1857 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1858 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1859 wpa_s->ibss_rsn == NULL) {
1860 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1861 if (!wpa_s->ibss_rsn) {
1862 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1863 wpa_supplicant_deauthenticate(
1864 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1865 return;
1866 }
1867
1868 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1869 }
1870#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001871
1872 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873}
1874
1875
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876static int disconnect_reason_recoverable(u16 reason_code)
1877{
1878 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1879 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1880 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1881}
1882
1883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001885 u16 reason_code,
1886 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887{
1888 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001889
1890 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1891 /*
1892 * At least Host AP driver and a Prism3 card seemed to be
1893 * generating streams of disconnected events when configuring
1894 * IBSS for WPA-None. Ignore them for now.
1895 */
1896 return;
1897 }
1898
1899 bssid = wpa_s->bssid;
1900 if (is_zero_ether_addr(bssid))
1901 bssid = wpa_s->pending_bssid;
1902
1903 if (!is_zero_ether_addr(bssid) ||
1904 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1905 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1906 " reason=%d%s",
1907 MAC2STR(bssid), reason_code,
1908 locally_generated ? " locally_generated=1" : "");
1909 }
1910}
1911
1912
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001913static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1914 int locally_generated)
1915{
1916 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1917 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1918 return 0; /* Not in 4-way handshake with PSK */
1919
1920 /*
1921 * It looks like connection was lost while trying to go through PSK
1922 * 4-way handshake. Filter out known disconnection cases that are caused
1923 * by something else than PSK mismatch to avoid confusing reports.
1924 */
1925
1926 if (locally_generated) {
1927 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1928 return 0;
1929 }
1930
1931 return 1;
1932}
1933
1934
Jouni Malinen2b89da82012-08-31 22:04:41 +03001935static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1936 u16 reason_code,
1937 int locally_generated)
1938{
1939 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940 int authenticating;
1941 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001942 struct wpa_bss *fast_reconnect = NULL;
1943 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001944 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945
1946 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1947 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1948
1949 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1950 /*
1951 * At least Host AP driver and a Prism3 card seemed to be
1952 * generating streams of disconnected events when configuring
1953 * IBSS for WPA-None. Ignore them for now.
1954 */
1955 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1956 "IBSS/WPA-None mode");
1957 return;
1958 }
1959
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001960 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1962 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001963 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 }
1965 if (!wpa_s->auto_reconnect_disabled ||
1966 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001967 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1968 "reconnect (wps=%d wpa_state=%d)",
1969 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1970 wpa_s->wpa_state);
1971 if (wpa_s->wpa_state == WPA_COMPLETED &&
1972 wpa_s->current_ssid &&
1973 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001974 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001975 disconnect_reason_recoverable(reason_code)) {
1976 /*
1977 * It looks like the AP has dropped association with
1978 * us, but could allow us to get back in. Try to
1979 * reconnect to the same BSS without full scan to save
1980 * time for some common cases.
1981 */
1982 fast_reconnect = wpa_s->current_bss;
1983 fast_reconnect_ssid = wpa_s->current_ssid;
1984 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001985#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001986 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001987#else
1988 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1989#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001990 else
1991 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1992 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001994 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 "try to re-connect");
1996 wpa_s->reassociate = 0;
1997 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001998 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 }
2000 bssid = wpa_s->bssid;
2001 if (is_zero_ether_addr(bssid))
2002 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002003 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2004 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002006 if (locally_generated)
2007 wpa_s->disconnect_reason = -reason_code;
2008 else
2009 wpa_s->disconnect_reason = reason_code;
2010 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2012 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2013 wpa_s->keys_cleared = 0;
2014 wpa_clear_keys(wpa_s, wpa_s->bssid);
2015 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002016 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 wpa_supplicant_mark_disassoc(wpa_s);
2018
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002019 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002021 wpa_s->current_ssid = last_ssid;
2022 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002023
2024 if (fast_reconnect) {
2025#ifndef CONFIG_NO_SCAN_PROCESSING
2026 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2027 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2028 fast_reconnect_ssid) < 0) {
2029 /* Recover through full scan */
2030 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2031 }
2032#endif /* CONFIG_NO_SCAN_PROCESSING */
2033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034}
2035
2036
2037#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002038void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039{
2040 struct wpa_supplicant *wpa_s = eloop_ctx;
2041
2042 if (!wpa_s->pending_mic_error_report)
2043 return;
2044
2045 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2046 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2047 wpa_s->pending_mic_error_report = 0;
2048}
2049#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2050
2051
2052static void
2053wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2054 union wpa_event_data *data)
2055{
2056 int pairwise;
2057 struct os_time t;
2058
2059 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2060 pairwise = (data && data->michael_mic_failure.unicast);
2061 os_get_time(&t);
2062 if ((wpa_s->last_michael_mic_error &&
2063 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2064 wpa_s->pending_mic_error_report) {
2065 if (wpa_s->pending_mic_error_report) {
2066 /*
2067 * Send the pending MIC error report immediately since
2068 * we are going to start countermeasures and AP better
2069 * do the same.
2070 */
2071 wpa_sm_key_request(wpa_s->wpa, 1,
2072 wpa_s->pending_mic_error_pairwise);
2073 }
2074
2075 /* Send the new MIC error report immediately since we are going
2076 * to start countermeasures and AP better do the same.
2077 */
2078 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2079
2080 /* initialize countermeasures */
2081 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002082
2083 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2086
2087 /*
2088 * Need to wait for completion of request frame. We do not get
2089 * any callback for the message completion, so just wait a
2090 * short while and hope for the best. */
2091 os_sleep(0, 10000);
2092
2093 wpa_drv_set_countermeasures(wpa_s, 1);
2094 wpa_supplicant_deauthenticate(wpa_s,
2095 WLAN_REASON_MICHAEL_MIC_FAILURE);
2096 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2097 wpa_s, NULL);
2098 eloop_register_timeout(60, 0,
2099 wpa_supplicant_stop_countermeasures,
2100 wpa_s, NULL);
2101 /* TODO: mark the AP rejected for 60 second. STA is
2102 * allowed to associate with another AP.. */
2103 } else {
2104#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2105 if (wpa_s->mic_errors_seen) {
2106 /*
2107 * Reduce the effectiveness of Michael MIC error
2108 * reports as a means for attacking against TKIP if
2109 * more than one MIC failure is noticed with the same
2110 * PTK. We delay the transmission of the reports by a
2111 * random time between 0 and 60 seconds in order to
2112 * force the attacker wait 60 seconds before getting
2113 * the information on whether a frame resulted in a MIC
2114 * failure.
2115 */
2116 u8 rval[4];
2117 int sec;
2118
2119 if (os_get_random(rval, sizeof(rval)) < 0)
2120 sec = os_random() % 60;
2121 else
2122 sec = WPA_GET_BE32(rval) % 60;
2123 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2124 "report %d seconds", sec);
2125 wpa_s->pending_mic_error_report = 1;
2126 wpa_s->pending_mic_error_pairwise = pairwise;
2127 eloop_cancel_timeout(
2128 wpa_supplicant_delayed_mic_error_report,
2129 wpa_s, NULL);
2130 eloop_register_timeout(
2131 sec, os_random() % 1000000,
2132 wpa_supplicant_delayed_mic_error_report,
2133 wpa_s, NULL);
2134 } else {
2135 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2136 }
2137#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2138 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2139#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2140 }
2141 wpa_s->last_michael_mic_error = t.sec;
2142 wpa_s->mic_errors_seen++;
2143}
2144
2145
2146#ifdef CONFIG_TERMINATE_ONLASTIF
2147static int any_interfaces(struct wpa_supplicant *head)
2148{
2149 struct wpa_supplicant *wpa_s;
2150
2151 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2152 if (!wpa_s->interface_removed)
2153 return 1;
2154 return 0;
2155}
2156#endif /* CONFIG_TERMINATE_ONLASTIF */
2157
2158
2159static void
2160wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2161 union wpa_event_data *data)
2162{
2163 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2164 return;
2165
2166 switch (data->interface_status.ievent) {
2167 case EVENT_INTERFACE_ADDED:
2168 if (!wpa_s->interface_removed)
2169 break;
2170 wpa_s->interface_removed = 0;
2171 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2172 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2173 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2174 "driver after interface was added");
2175 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002176 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 break;
2178 case EVENT_INTERFACE_REMOVED:
2179 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2180 wpa_s->interface_removed = 1;
2181 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002182 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 l2_packet_deinit(wpa_s->l2);
2184 wpa_s->l2 = NULL;
2185#ifdef CONFIG_IBSS_RSN
2186 ibss_rsn_deinit(wpa_s->ibss_rsn);
2187 wpa_s->ibss_rsn = NULL;
2188#endif /* CONFIG_IBSS_RSN */
2189#ifdef CONFIG_TERMINATE_ONLASTIF
2190 /* check if last interface */
2191 if (!any_interfaces(wpa_s->global->ifaces))
2192 eloop_terminate();
2193#endif /* CONFIG_TERMINATE_ONLASTIF */
2194 break;
2195 }
2196}
2197
2198
2199#ifdef CONFIG_PEERKEY
2200static void
2201wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2202 union wpa_event_data *data)
2203{
2204 if (data == NULL)
2205 return;
2206 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2207}
2208#endif /* CONFIG_PEERKEY */
2209
2210
2211#ifdef CONFIG_TDLS
2212static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2213 union wpa_event_data *data)
2214{
2215 if (data == NULL)
2216 return;
2217 switch (data->tdls.oper) {
2218 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002219 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2220 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2221 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2222 else
2223 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 break;
2225 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002226 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 break;
2229 }
2230}
2231#endif /* CONFIG_TDLS */
2232
2233
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002234#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002235static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2236 union wpa_event_data *data)
2237{
2238 if (data == NULL)
2239 return;
2240 switch (data->wnm.oper) {
2241 case WNM_OPER_SLEEP:
2242 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2243 "(action=%d, intval=%d)",
2244 data->wnm.sleep_action, data->wnm.sleep_intval);
2245 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002246 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002247 break;
2248 }
2249}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002250#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002251
2252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253#ifdef CONFIG_IEEE80211R
2254static void
2255wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2256 union wpa_event_data *data)
2257{
2258 if (data == NULL)
2259 return;
2260
2261 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2262 data->ft_ies.ies_len,
2263 data->ft_ies.ft_action,
2264 data->ft_ies.target_ap,
2265 data->ft_ies.ric_ies,
2266 data->ft_ies.ric_ies_len) < 0) {
2267 /* TODO: prevent MLME/driver from trying to associate? */
2268 }
2269}
2270#endif /* CONFIG_IEEE80211R */
2271
2272
2273#ifdef CONFIG_IBSS_RSN
2274static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2275 union wpa_event_data *data)
2276{
2277 struct wpa_ssid *ssid;
2278 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2279 return;
2280 if (data == NULL)
2281 return;
2282 ssid = wpa_s->current_ssid;
2283 if (ssid == NULL)
2284 return;
2285 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2286 return;
2287
2288 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2289}
2290#endif /* CONFIG_IBSS_RSN */
2291
2292
2293#ifdef CONFIG_IEEE80211R
2294static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2295 size_t len)
2296{
2297 const u8 *sta_addr, *target_ap_addr;
2298 u16 status;
2299
2300 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2301 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2302 return; /* only SME case supported for now */
2303 if (len < 1 + 2 * ETH_ALEN + 2)
2304 return;
2305 if (data[0] != 2)
2306 return; /* Only FT Action Response is supported for now */
2307 sta_addr = data + 1;
2308 target_ap_addr = data + 1 + ETH_ALEN;
2309 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2310 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2311 MACSTR " TargetAP " MACSTR " status %u",
2312 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2313
2314 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2315 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2316 " in FT Action Response", MAC2STR(sta_addr));
2317 return;
2318 }
2319
2320 if (status) {
2321 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2322 "failure (status code %d)", status);
2323 /* TODO: report error to FT code(?) */
2324 return;
2325 }
2326
2327 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2328 len - (1 + 2 * ETH_ALEN + 2), 1,
2329 target_ap_addr, NULL, 0) < 0)
2330 return;
2331
2332#ifdef CONFIG_SME
2333 {
2334 struct wpa_bss *bss;
2335 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2336 if (bss)
2337 wpa_s->sme.freq = bss->freq;
2338 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2339 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2340 WLAN_AUTH_FT);
2341 }
2342#endif /* CONFIG_SME */
2343}
2344#endif /* CONFIG_IEEE80211R */
2345
2346
2347static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2348 struct unprot_deauth *e)
2349{
2350#ifdef CONFIG_IEEE80211W
2351 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2352 "dropped: " MACSTR " -> " MACSTR
2353 " (reason code %u)",
2354 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2355 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2356#endif /* CONFIG_IEEE80211W */
2357}
2358
2359
2360static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2361 struct unprot_disassoc *e)
2362{
2363#ifdef CONFIG_IEEE80211W
2364 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2365 "dropped: " MACSTR " -> " MACSTR
2366 " (reason code %u)",
2367 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2368 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2369#endif /* CONFIG_IEEE80211W */
2370}
2371
2372
2373void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2374 union wpa_event_data *data)
2375{
2376 struct wpa_supplicant *wpa_s = ctx;
2377 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002378 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
2380 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2381 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002382 event != EVENT_INTERFACE_STATUS &&
2383 event != EVENT_SCHED_SCAN_STOPPED) {
2384 wpa_dbg(wpa_s, MSG_DEBUG,
2385 "Ignore event %s (%d) while interface is disabled",
2386 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 return;
2388 }
2389
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002390#ifndef CONFIG_NO_STDOUT_DEBUG
2391{
2392 int level = MSG_DEBUG;
2393
Dmitry Shmidt04949592012-07-19 12:16:46 -07002394 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395 const struct ieee80211_hdr *hdr;
2396 u16 fc;
2397 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2398 fc = le_to_host16(hdr->frame_control);
2399 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2400 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2401 level = MSG_EXCESSIVE;
2402 }
2403
2404 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2405 event_to_string(event), event);
2406}
2407#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408
2409 switch (event) {
2410 case EVENT_AUTH:
2411 sme_event_auth(wpa_s, data);
2412 break;
2413 case EVENT_ASSOC:
2414 wpa_supplicant_event_assoc(wpa_s, data);
2415 break;
2416 case EVENT_DISASSOC:
2417 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2418 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002419 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2420 data->disassoc_info.reason_code,
2421 data->disassoc_info.locally_generated ?
2422 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 if (data->disassoc_info.addr)
2424 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2425 MAC2STR(data->disassoc_info.addr));
2426 }
2427#ifdef CONFIG_AP
2428 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2429 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2430 data->disassoc_info.addr);
2431 break;
2432 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002433 if (wpa_s->ap_iface) {
2434 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2435 "AP mode");
2436 break;
2437 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438#endif /* CONFIG_AP */
2439 if (data) {
2440 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002441 locally_generated =
2442 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2444 data->disassoc_info.ie,
2445 data->disassoc_info.ie_len);
2446#ifdef CONFIG_P2P
2447 wpas_p2p_disassoc_notif(
2448 wpa_s, data->disassoc_info.addr, reason_code,
2449 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002450 data->disassoc_info.ie_len,
2451 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452#endif /* CONFIG_P2P */
2453 }
2454 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2455 sme_event_disassoc(wpa_s, data);
2456 /* fall through */
2457 case EVENT_DEAUTH:
2458 if (event == EVENT_DEAUTH) {
2459 wpa_dbg(wpa_s, MSG_DEBUG,
2460 "Deauthentication notification");
2461 if (data) {
2462 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002463 locally_generated =
2464 data->deauth_info.locally_generated;
2465 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2466 data->deauth_info.reason_code,
2467 data->deauth_info.locally_generated ?
2468 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 if (data->deauth_info.addr) {
2470 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2471 MACSTR,
2472 MAC2STR(data->deauth_info.
2473 addr));
2474 }
2475 wpa_hexdump(MSG_DEBUG,
2476 "Deauthentication frame IE(s)",
2477 data->deauth_info.ie,
2478 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 }
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002480 wpa_reset_ft_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 }
2482#ifdef CONFIG_AP
2483 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2484 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2485 data->deauth_info.addr);
2486 break;
2487 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002488 if (wpa_s->ap_iface) {
2489 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2490 "AP mode");
2491 break;
2492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002494 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2495 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002496 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2497 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2498 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2499 eapol_sm_failed(wpa_s->eapol)))
2500 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002501#ifdef CONFIG_P2P
2502 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002503 if (wpas_p2p_deauth_notif(wpa_s,
2504 data->deauth_info.addr,
2505 reason_code,
2506 data->deauth_info.ie,
2507 data->deauth_info.ie_len,
2508 locally_generated) > 0) {
2509 /*
2510 * The interface was removed, so cannot
2511 * continue processing any additional
2512 * operations after this.
2513 */
2514 break;
2515 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002516 }
2517#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002518 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2519 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 break;
2521 case EVENT_MICHAEL_MIC_FAILURE:
2522 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2523 break;
2524#ifndef CONFIG_NO_SCAN_PROCESSING
2525 case EVENT_SCAN_RESULTS:
2526 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002527 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2528 wpa_s->wpa_state != WPA_ASSOCIATING)
2529 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 break;
2531#endif /* CONFIG_NO_SCAN_PROCESSING */
2532 case EVENT_ASSOCINFO:
2533 wpa_supplicant_event_associnfo(wpa_s, data);
2534 break;
2535 case EVENT_INTERFACE_STATUS:
2536 wpa_supplicant_event_interface_status(wpa_s, data);
2537 break;
2538 case EVENT_PMKID_CANDIDATE:
2539 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2540 break;
2541#ifdef CONFIG_PEERKEY
2542 case EVENT_STKSTART:
2543 wpa_supplicant_event_stkstart(wpa_s, data);
2544 break;
2545#endif /* CONFIG_PEERKEY */
2546#ifdef CONFIG_TDLS
2547 case EVENT_TDLS:
2548 wpa_supplicant_event_tdls(wpa_s, data);
2549 break;
2550#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002551#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002552 case EVENT_WNM:
2553 wpa_supplicant_event_wnm(wpa_s, data);
2554 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002555#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556#ifdef CONFIG_IEEE80211R
2557 case EVENT_FT_RESPONSE:
2558 wpa_supplicant_event_ft_response(wpa_s, data);
2559 break;
2560#endif /* CONFIG_IEEE80211R */
2561#ifdef CONFIG_IBSS_RSN
2562 case EVENT_IBSS_RSN_START:
2563 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2564 break;
2565#endif /* CONFIG_IBSS_RSN */
2566 case EVENT_ASSOC_REJECT:
2567 if (data->assoc_reject.bssid)
2568 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2569 "bssid=" MACSTR " status_code=%u",
2570 MAC2STR(data->assoc_reject.bssid),
2571 data->assoc_reject.status_code);
2572 else
2573 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2574 "status_code=%u",
2575 data->assoc_reject.status_code);
2576 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2577 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002578 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002579#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002580 if(!wpa_s->current_ssid) {
2581 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2582 break;
2583 }
2584 /* If assoc reject is reported by the driver, then avoid
2585 * waiting for the authentication timeout. Cancel the
2586 * authentication timeout and retry the assoc.
2587 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002588 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002589 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2590 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002591
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002592 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002593
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002594 /* Clear the states */
2595 wpa_sm_notify_disassoc(wpa_s->wpa);
2596 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2597
2598 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002599 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002600 const u8 *bl_bssid = data->assoc_reject.bssid;
2601 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2602 bl_bssid = wpa_s->pending_bssid;
2603 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002604 wpa_supplicant_req_scan(wpa_s, 0, 0);
2605 } else {
2606 wpa_supplicant_req_scan(wpa_s, 1, 0);
2607 }
2608 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002609 /* If we ASSOC_REJECT's hits threshold, disable the
2610 * network
2611 */
2612 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2613 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002614 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002615 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002616 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002617 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002618#else
2619 const u8 *bssid = data->assoc_reject.bssid;
2620 if (bssid == NULL || is_zero_ether_addr(bssid))
2621 bssid = wpa_s->pending_bssid;
2622 wpas_connection_failed(wpa_s, bssid);
2623 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002624#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002625 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626 break;
2627 case EVENT_AUTH_TIMED_OUT:
2628 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2629 sme_event_auth_timed_out(wpa_s, data);
2630 break;
2631 case EVENT_ASSOC_TIMED_OUT:
2632 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2633 sme_event_assoc_timed_out(wpa_s, data);
2634 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 case EVENT_TX_STATUS:
2636 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2637 " type=%d stype=%d",
2638 MAC2STR(data->tx_status.dst),
2639 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2644 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002645 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 wpa_s, data->tx_status.dst,
2647 data->tx_status.data,
2648 data->tx_status.data_len,
2649 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002650 OFFCHANNEL_SEND_ACTION_SUCCESS :
2651 OFFCHANNEL_SEND_ACTION_NO_ACK);
2652#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 break;
2654 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655#endif /* CONFIG_AP */
2656#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2658 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2659 /*
2660 * Catch TX status events for Action frames we sent via group
2661 * interface in GO mode.
2662 */
2663 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2664 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2665 os_memcmp(wpa_s->parent->pending_action_dst,
2666 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668 wpa_s->parent, data->tx_status.dst,
2669 data->tx_status.data,
2670 data->tx_status.data_len,
2671 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002672 OFFCHANNEL_SEND_ACTION_SUCCESS :
2673 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 break;
2675 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676#endif /* CONFIG_OFFCHANNEL */
2677#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 switch (data->tx_status.type) {
2679 case WLAN_FC_TYPE_MGMT:
2680 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2681 data->tx_status.data_len,
2682 data->tx_status.stype,
2683 data->tx_status.ack);
2684 break;
2685 case WLAN_FC_TYPE_DATA:
2686 ap_tx_status(wpa_s, data->tx_status.dst,
2687 data->tx_status.data,
2688 data->tx_status.data_len,
2689 data->tx_status.ack);
2690 break;
2691 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002692#endif /* CONFIG_AP */
2693 break;
2694#ifdef CONFIG_AP
2695 case EVENT_EAPOL_TX_STATUS:
2696 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2697 data->eapol_tx_status.data,
2698 data->eapol_tx_status.data_len,
2699 data->eapol_tx_status.ack);
2700 break;
2701 case EVENT_DRIVER_CLIENT_POLL_OK:
2702 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 break;
2704 case EVENT_RX_FROM_UNKNOWN:
2705 if (wpa_s->ap_iface == NULL)
2706 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002707 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2708 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002710 case EVENT_CH_SWITCH:
2711 if (!data)
2712 break;
2713 if (!wpa_s->ap_iface) {
2714 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2715 "event in non-AP mode");
2716 break;
2717 }
2718
2719#ifdef CONFIG_AP
2720 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2721 data->ch_switch.ht_enabled,
2722 data->ch_switch.ch_offset);
2723#endif /* CONFIG_AP */
2724 break;
2725 case EVENT_RX_MGMT: {
2726 u16 fc, stype;
2727 const struct ieee80211_mgmt *mgmt;
2728
2729 mgmt = (const struct ieee80211_mgmt *)
2730 data->rx_mgmt.frame;
2731 fc = le_to_host16(mgmt->frame_control);
2732 stype = WLAN_FC_GET_STYPE(fc);
2733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 if (wpa_s->ap_iface == NULL) {
2735#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002736 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2737 data->rx_mgmt.frame_len > 24) {
2738 const u8 *src = mgmt->sa;
2739 const u8 *ie = mgmt->u.probe_req.variable;
2740 size_t ie_len = data->rx_mgmt.frame_len -
2741 (mgmt->u.probe_req.variable -
2742 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002743 wpas_p2p_probe_req_rx(
2744 wpa_s, src, mgmt->da,
2745 mgmt->bssid, ie, ie_len,
2746 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747 break;
2748 }
2749#endif /* CONFIG_P2P */
2750 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2751 "management frame in non-AP mode");
2752 break;
2753 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002754
2755 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2756 data->rx_mgmt.frame_len > 24) {
2757 const u8 *ie = mgmt->u.probe_req.variable;
2758 size_t ie_len = data->rx_mgmt.frame_len -
2759 (mgmt->u.probe_req.variable -
2760 data->rx_mgmt.frame);
2761
2762 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2763 mgmt->bssid, ie, ie_len,
2764 data->rx_mgmt.ssi_signal);
2765 }
2766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2768 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770#endif /* CONFIG_AP */
2771 case EVENT_RX_ACTION:
2772 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2773 " Category=%u DataLen=%d freq=%d MHz",
2774 MAC2STR(data->rx_action.sa),
2775 data->rx_action.category, (int) data->rx_action.len,
2776 data->rx_action.freq);
2777#ifdef CONFIG_IEEE80211R
2778 if (data->rx_action.category == WLAN_ACTION_FT) {
2779 ft_rx_action(wpa_s, data->rx_action.data,
2780 data->rx_action.len);
2781 break;
2782 }
2783#endif /* CONFIG_IEEE80211R */
2784#ifdef CONFIG_IEEE80211W
2785#ifdef CONFIG_SME
2786 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2787 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2788 data->rx_action.data,
2789 data->rx_action.len);
2790 break;
2791 }
2792#endif /* CONFIG_SME */
2793#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002794#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002795 if (data->rx_action.category == WLAN_ACTION_WNM) {
2796 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2797 break;
2798 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002799#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002800#ifdef CONFIG_GAS
2801 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2802 gas_query_rx(wpa_s->gas, data->rx_action.da,
2803 data->rx_action.sa, data->rx_action.bssid,
2804 data->rx_action.data, data->rx_action.len,
2805 data->rx_action.freq) == 0)
2806 break;
2807#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002808#ifdef CONFIG_TDLS
2809 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2810 data->rx_action.len >= 4 &&
2811 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2812 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2813 "Response from " MACSTR,
2814 MAC2STR(data->rx_action.sa));
2815 break;
2816 }
2817#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818#ifdef CONFIG_P2P
2819 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2820 data->rx_action.sa,
2821 data->rx_action.bssid,
2822 data->rx_action.category,
2823 data->rx_action.data,
2824 data->rx_action.len, data->rx_action.freq);
2825#endif /* CONFIG_P2P */
2826 break;
2827 case EVENT_RX_PROBE_REQ:
2828 if (data->rx_probe_req.sa == NULL ||
2829 data->rx_probe_req.ie == NULL)
2830 break;
2831#ifdef CONFIG_AP
2832 if (wpa_s->ap_iface) {
2833 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2834 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002835 data->rx_probe_req.da,
2836 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002838 data->rx_probe_req.ie_len,
2839 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840 break;
2841 }
2842#endif /* CONFIG_AP */
2843#ifdef CONFIG_P2P
2844 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002845 data->rx_probe_req.da,
2846 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002848 data->rx_probe_req.ie_len,
2849 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850#endif /* CONFIG_P2P */
2851 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002853#ifdef CONFIG_OFFCHANNEL
2854 offchannel_remain_on_channel_cb(
2855 wpa_s, data->remain_on_channel.freq,
2856 data->remain_on_channel.duration);
2857#endif /* CONFIG_OFFCHANNEL */
2858#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 wpas_p2p_remain_on_channel_cb(
2860 wpa_s, data->remain_on_channel.freq,
2861 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002862#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 break;
2864 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002865#ifdef CONFIG_OFFCHANNEL
2866 offchannel_cancel_remain_on_channel_cb(
2867 wpa_s, data->remain_on_channel.freq);
2868#endif /* CONFIG_OFFCHANNEL */
2869#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 wpas_p2p_cancel_remain_on_channel_cb(
2871 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002872#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002874#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 case EVENT_P2P_DEV_FOUND: {
2876 struct p2p_peer_info peer_info;
2877
2878 os_memset(&peer_info, 0, sizeof(peer_info));
2879 if (data->p2p_dev_found.dev_addr)
2880 os_memcpy(peer_info.p2p_device_addr,
2881 data->p2p_dev_found.dev_addr, ETH_ALEN);
2882 if (data->p2p_dev_found.pri_dev_type)
2883 os_memcpy(peer_info.pri_dev_type,
2884 data->p2p_dev_found.pri_dev_type,
2885 sizeof(peer_info.pri_dev_type));
2886 if (data->p2p_dev_found.dev_name)
2887 os_strlcpy(peer_info.device_name,
2888 data->p2p_dev_found.dev_name,
2889 sizeof(peer_info.device_name));
2890 peer_info.config_methods = data->p2p_dev_found.config_methods;
2891 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2892 peer_info.group_capab = data->p2p_dev_found.group_capab;
2893
2894 /*
2895 * FIX: new_device=1 is not necessarily correct. We should
2896 * maintain a P2P peer database in wpa_supplicant and update
2897 * this information based on whether the peer is truly new.
2898 */
2899 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2900 break;
2901 }
2902 case EVENT_P2P_GO_NEG_REQ_RX:
2903 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2904 data->p2p_go_neg_req_rx.dev_passwd_id);
2905 break;
2906 case EVENT_P2P_GO_NEG_COMPLETED:
2907 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2908 break;
2909 case EVENT_P2P_PROV_DISC_REQUEST:
2910 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2911 data->p2p_prov_disc_req.config_methods,
2912 data->p2p_prov_disc_req.dev_addr,
2913 data->p2p_prov_disc_req.pri_dev_type,
2914 data->p2p_prov_disc_req.dev_name,
2915 data->p2p_prov_disc_req.supp_config_methods,
2916 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002917 data->p2p_prov_disc_req.group_capab,
2918 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919 break;
2920 case EVENT_P2P_PROV_DISC_RESPONSE:
2921 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2922 data->p2p_prov_disc_resp.config_methods);
2923 break;
2924 case EVENT_P2P_SD_REQUEST:
2925 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2926 data->p2p_sd_req.sa,
2927 data->p2p_sd_req.dialog_token,
2928 data->p2p_sd_req.update_indic,
2929 data->p2p_sd_req.tlvs,
2930 data->p2p_sd_req.tlvs_len);
2931 break;
2932 case EVENT_P2P_SD_RESPONSE:
2933 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2934 data->p2p_sd_resp.update_indic,
2935 data->p2p_sd_resp.tlvs,
2936 data->p2p_sd_resp.tlvs_len);
2937 break;
2938#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 case EVENT_EAPOL_RX:
2940 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2941 data->eapol_rx.data,
2942 data->eapol_rx.data_len);
2943 break;
2944 case EVENT_SIGNAL_CHANGE:
2945 bgscan_notify_signal_change(
2946 wpa_s, data->signal_change.above_threshold,
2947 data->signal_change.current_signal,
2948 data->signal_change.current_noise,
2949 data->signal_change.current_txrate);
2950 break;
2951 case EVENT_INTERFACE_ENABLED:
2952 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2953 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955#ifdef CONFIG_AP
2956 if (!wpa_s->ap_iface) {
2957 wpa_supplicant_set_state(wpa_s,
2958 WPA_DISCONNECTED);
2959 wpa_supplicant_req_scan(wpa_s, 0, 0);
2960 } else
2961 wpa_supplicant_set_state(wpa_s,
2962 WPA_COMPLETED);
2963#else /* CONFIG_AP */
2964 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2965 wpa_supplicant_req_scan(wpa_s, 0, 0);
2966#endif /* CONFIG_AP */
2967 }
2968 break;
2969 case EVENT_INTERFACE_DISABLED:
2970 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2971 wpa_supplicant_mark_disassoc(wpa_s);
2972 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2973 break;
2974 case EVENT_CHANNEL_LIST_CHANGED:
2975 if (wpa_s->drv_priv == NULL)
2976 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002977
2978 free_hw_features(wpa_s);
2979 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2980 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982#ifdef CONFIG_P2P
2983 wpas_p2p_update_channel_list(wpa_s);
2984#endif /* CONFIG_P2P */
2985 break;
2986 case EVENT_INTERFACE_UNAVAILABLE:
2987#ifdef CONFIG_P2P
2988 wpas_p2p_interface_unavailable(wpa_s);
2989#endif /* CONFIG_P2P */
2990 break;
2991 case EVENT_BEST_CHANNEL:
2992 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2993 "(%d %d %d)",
2994 data->best_chan.freq_24, data->best_chan.freq_5,
2995 data->best_chan.freq_overall);
2996 wpa_s->best_24_freq = data->best_chan.freq_24;
2997 wpa_s->best_5_freq = data->best_chan.freq_5;
2998 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2999#ifdef CONFIG_P2P
3000 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3001 data->best_chan.freq_5,
3002 data->best_chan.freq_overall);
3003#endif /* CONFIG_P2P */
3004 break;
3005 case EVENT_UNPROT_DEAUTH:
3006 wpa_supplicant_event_unprot_deauth(wpa_s,
3007 &data->unprot_deauth);
3008 break;
3009 case EVENT_UNPROT_DISASSOC:
3010 wpa_supplicant_event_unprot_disassoc(wpa_s,
3011 &data->unprot_disassoc);
3012 break;
3013 case EVENT_STATION_LOW_ACK:
3014#ifdef CONFIG_AP
3015 if (wpa_s->ap_iface && data)
3016 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3017 data->low_ack.addr);
3018#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003019#ifdef CONFIG_TDLS
3020 if (data)
3021 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3022#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 break;
3024 case EVENT_IBSS_PEER_LOST:
3025#ifdef CONFIG_IBSS_RSN
3026 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3027#endif /* CONFIG_IBSS_RSN */
3028 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029 case EVENT_DRIVER_GTK_REKEY:
3030 if (os_memcmp(data->driver_gtk_rekey.bssid,
3031 wpa_s->bssid, ETH_ALEN))
3032 break;
3033 if (!wpa_s->wpa)
3034 break;
3035 wpa_sm_update_replay_ctr(wpa_s->wpa,
3036 data->driver_gtk_rekey.replay_ctr);
3037 break;
3038 case EVENT_SCHED_SCAN_STOPPED:
3039 wpa_s->sched_scanning = 0;
3040 wpa_supplicant_notify_scanning(wpa_s, 0);
3041
3042 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3043 break;
3044
3045 /*
3046 * If we timed out, start a new sched scan to continue
3047 * searching for more SSIDs.
3048 */
3049 if (wpa_s->sched_scan_timed_out)
3050 wpa_supplicant_req_sched_scan(wpa_s);
3051 break;
3052 case EVENT_WPS_BUTTON_PUSHED:
3053#ifdef CONFIG_WPS
3054 wpas_wps_start_pbc(wpa_s, NULL, 0);
3055#endif /* CONFIG_WPS */
3056 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003057 case EVENT_CONNECT_FAILED_REASON:
3058#ifdef CONFIG_AP
3059 if (!wpa_s->ap_iface || !data)
3060 break;
3061 hostapd_event_connect_failed_reason(
3062 wpa_s->ap_iface->bss[0],
3063 data->connect_failed_reason.addr,
3064 data->connect_failed_reason.code);
3065#endif /* CONFIG_AP */
3066 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 default:
3068 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3069 break;
3070 }
3071}