blob: 6d9b587f6399f864655cf3165443ad069b9c2cf6 [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
1683#ifdef CONFIG_AP
1684 if (wpa_s->ap_iface) {
1685 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1686 data->assoc_info.addr,
1687 data->assoc_info.req_ies,
1688 data->assoc_info.req_ies_len,
1689 data->assoc_info.reassoc);
1690 return;
1691 }
1692#endif /* CONFIG_AP */
1693
1694 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1695 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1696 return;
1697
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001698 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1699 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001700 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001701 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1702 return;
1703 }
1704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001706 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1708 MACSTR, MAC2STR(bssid));
1709 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1711 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001712 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713
1714 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1715 wpa_clear_keys(wpa_s, bssid);
1716 }
1717 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001718 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1720 return;
1721 }
1722 if (wpa_s->current_ssid) {
1723 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001724
1725 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1726 if (!bss) {
1727 wpa_supplicant_update_scan_results(wpa_s);
1728
1729 /* Get the BSS from the new scan results */
1730 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1731 }
1732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 if (bss)
1734 wpa_s->current_bss = bss;
1735 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001736
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001737#ifdef ANDROID
1738 if (wpa_s->conf->ap_scan == 1) {
1739#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001740 if (wpa_s->conf->ap_scan == 1 &&
1741 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001742#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001743 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1744 wpa_msg(wpa_s, MSG_WARNING,
1745 "WPA/RSN IEs not updated");
1746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 }
1748
1749#ifdef CONFIG_SME
1750 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1751 wpa_s->sme.prev_bssid_set = 1;
1752#endif /* CONFIG_SME */
1753
1754 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1755 if (wpa_s->current_ssid) {
1756 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1757 * initialized before association, but for other modes,
1758 * initialize PC/SC here, if the current configuration needs
1759 * smartcard or SIM/USIM. */
1760 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1761 }
1762 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1763 if (wpa_s->l2)
1764 l2_packet_notify_auth_start(wpa_s->l2);
1765
1766 /*
1767 * Set portEnabled first to FALSE in order to get EAP state machine out
1768 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1769 * state machine may transit to AUTHENTICATING state based on obsolete
1770 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1771 * AUTHENTICATED without ever giving chance to EAP state machine to
1772 * reset the state.
1773 */
1774 if (!ft_completed) {
1775 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1776 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1777 }
1778 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1779 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1780 /* 802.1X::portControl = Auto */
1781 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1782 wpa_s->eapol_received = 0;
1783 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1784 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1785 (wpa_s->current_ssid &&
1786 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001787 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1788 (wpa_s->drv_flags &
1789 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1790 /*
1791 * Set the key after having received joined-IBSS event
1792 * from the driver.
1793 */
1794 wpa_supplicant_set_wpa_none_key(wpa_s,
1795 wpa_s->current_ssid);
1796 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 wpa_supplicant_cancel_auth_timeout(wpa_s);
1798 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1799 } else if (!ft_completed) {
1800 /* Timeout for receiving the first EAPOL packet */
1801 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1802 }
1803 wpa_supplicant_cancel_scan(wpa_s);
1804
1805 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1806 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1807 /*
1808 * We are done; the driver will take care of RSN 4-way
1809 * handshake.
1810 */
1811 wpa_supplicant_cancel_auth_timeout(wpa_s);
1812 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1813 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1814 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1815 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1816 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1817 /*
1818 * The driver will take care of RSN 4-way handshake, so we need
1819 * to allow EAPOL supplicant to complete its work without
1820 * waiting for WPA supplicant.
1821 */
1822 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1823 } else if (ft_completed) {
1824 /*
1825 * FT protocol completed - make sure EAPOL state machine ends
1826 * up in authenticated.
1827 */
1828 wpa_supplicant_cancel_auth_timeout(wpa_s);
1829 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1830 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1831 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1832 }
1833
Jouni Malinena05074c2012-12-21 21:35:35 +02001834 wpa_s->last_eapol_matches_bssid = 0;
1835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 if (wpa_s->pending_eapol_rx) {
1837 struct os_time now, age;
1838 os_get_time(&now);
1839 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1840 if (age.sec == 0 && age.usec < 100000 &&
1841 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1842 0) {
1843 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1844 "frame that was received just before "
1845 "association notification");
1846 wpa_supplicant_rx_eapol(
1847 wpa_s, wpa_s->pending_eapol_rx_src,
1848 wpabuf_head(wpa_s->pending_eapol_rx),
1849 wpabuf_len(wpa_s->pending_eapol_rx));
1850 }
1851 wpabuf_free(wpa_s->pending_eapol_rx);
1852 wpa_s->pending_eapol_rx = NULL;
1853 }
1854
1855 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1856 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001857 wpa_s->current_ssid &&
1858 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 /* Set static WEP keys again */
1860 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1861 }
1862
1863#ifdef CONFIG_IBSS_RSN
1864 if (wpa_s->current_ssid &&
1865 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1866 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1867 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1868 wpa_s->ibss_rsn == NULL) {
1869 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1870 if (!wpa_s->ibss_rsn) {
1871 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1872 wpa_supplicant_deauthenticate(
1873 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1874 return;
1875 }
1876
1877 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1878 }
1879#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001880
1881 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882}
1883
1884
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001885static int disconnect_reason_recoverable(u16 reason_code)
1886{
1887 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1888 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1889 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1890}
1891
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001894 u16 reason_code,
1895 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896{
1897 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001898
1899 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1900 /*
1901 * At least Host AP driver and a Prism3 card seemed to be
1902 * generating streams of disconnected events when configuring
1903 * IBSS for WPA-None. Ignore them for now.
1904 */
1905 return;
1906 }
1907
1908 bssid = wpa_s->bssid;
1909 if (is_zero_ether_addr(bssid))
1910 bssid = wpa_s->pending_bssid;
1911
1912 if (!is_zero_ether_addr(bssid) ||
1913 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1914 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1915 " reason=%d%s",
1916 MAC2STR(bssid), reason_code,
1917 locally_generated ? " locally_generated=1" : "");
1918 }
1919}
1920
1921
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001922static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1923 int locally_generated)
1924{
1925 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1926 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1927 return 0; /* Not in 4-way handshake with PSK */
1928
1929 /*
1930 * It looks like connection was lost while trying to go through PSK
1931 * 4-way handshake. Filter out known disconnection cases that are caused
1932 * by something else than PSK mismatch to avoid confusing reports.
1933 */
1934
1935 if (locally_generated) {
1936 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1937 return 0;
1938 }
1939
1940 return 1;
1941}
1942
1943
Jouni Malinen2b89da82012-08-31 22:04:41 +03001944static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1945 u16 reason_code,
1946 int locally_generated)
1947{
1948 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 int authenticating;
1950 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001951 struct wpa_bss *fast_reconnect = NULL;
1952 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001953 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954
1955 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1956 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1957
1958 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1959 /*
1960 * At least Host AP driver and a Prism3 card seemed to be
1961 * generating streams of disconnected events when configuring
1962 * IBSS for WPA-None. Ignore them for now.
1963 */
1964 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1965 "IBSS/WPA-None mode");
1966 return;
1967 }
1968
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001969 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1971 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001972 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001974 if (!wpa_s->disconnected &&
1975 (!wpa_s->auto_reconnect_disabled ||
1976 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001977 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1978 "reconnect (wps=%d wpa_state=%d)",
1979 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1980 wpa_s->wpa_state);
1981 if (wpa_s->wpa_state == WPA_COMPLETED &&
1982 wpa_s->current_ssid &&
1983 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001984 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985 disconnect_reason_recoverable(reason_code)) {
1986 /*
1987 * It looks like the AP has dropped association with
1988 * us, but could allow us to get back in. Try to
1989 * reconnect to the same BSS without full scan to save
1990 * time for some common cases.
1991 */
1992 fast_reconnect = wpa_s->current_bss;
1993 fast_reconnect_ssid = wpa_s->current_ssid;
1994 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001995#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001996 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001997#else
1998 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1999#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002000 else
2001 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2002 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002004 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 "try to re-connect");
2006 wpa_s->reassociate = 0;
2007 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002008 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 }
2010 bssid = wpa_s->bssid;
2011 if (is_zero_ether_addr(bssid))
2012 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002013 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2014 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002016 if (locally_generated)
2017 wpa_s->disconnect_reason = -reason_code;
2018 else
2019 wpa_s->disconnect_reason = reason_code;
2020 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2022 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2023 wpa_s->keys_cleared = 0;
2024 wpa_clear_keys(wpa_s, wpa_s->bssid);
2025 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002026 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002027 wpa_supplicant_mark_disassoc(wpa_s);
2028
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002029 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002031 wpa_s->current_ssid = last_ssid;
2032 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002033
2034 if (fast_reconnect) {
2035#ifndef CONFIG_NO_SCAN_PROCESSING
2036 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2037 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2038 fast_reconnect_ssid) < 0) {
2039 /* Recover through full scan */
2040 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2041 }
2042#endif /* CONFIG_NO_SCAN_PROCESSING */
2043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044}
2045
2046
2047#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002048void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049{
2050 struct wpa_supplicant *wpa_s = eloop_ctx;
2051
2052 if (!wpa_s->pending_mic_error_report)
2053 return;
2054
2055 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2056 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2057 wpa_s->pending_mic_error_report = 0;
2058}
2059#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2060
2061
2062static void
2063wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2064 union wpa_event_data *data)
2065{
2066 int pairwise;
2067 struct os_time t;
2068
2069 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2070 pairwise = (data && data->michael_mic_failure.unicast);
2071 os_get_time(&t);
2072 if ((wpa_s->last_michael_mic_error &&
2073 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2074 wpa_s->pending_mic_error_report) {
2075 if (wpa_s->pending_mic_error_report) {
2076 /*
2077 * Send the pending MIC error report immediately since
2078 * we are going to start countermeasures and AP better
2079 * do the same.
2080 */
2081 wpa_sm_key_request(wpa_s->wpa, 1,
2082 wpa_s->pending_mic_error_pairwise);
2083 }
2084
2085 /* Send the new MIC error report immediately since we are going
2086 * to start countermeasures and AP better do the same.
2087 */
2088 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2089
2090 /* initialize countermeasures */
2091 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002092
2093 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2096
2097 /*
2098 * Need to wait for completion of request frame. We do not get
2099 * any callback for the message completion, so just wait a
2100 * short while and hope for the best. */
2101 os_sleep(0, 10000);
2102
2103 wpa_drv_set_countermeasures(wpa_s, 1);
2104 wpa_supplicant_deauthenticate(wpa_s,
2105 WLAN_REASON_MICHAEL_MIC_FAILURE);
2106 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2107 wpa_s, NULL);
2108 eloop_register_timeout(60, 0,
2109 wpa_supplicant_stop_countermeasures,
2110 wpa_s, NULL);
2111 /* TODO: mark the AP rejected for 60 second. STA is
2112 * allowed to associate with another AP.. */
2113 } else {
2114#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2115 if (wpa_s->mic_errors_seen) {
2116 /*
2117 * Reduce the effectiveness of Michael MIC error
2118 * reports as a means for attacking against TKIP if
2119 * more than one MIC failure is noticed with the same
2120 * PTK. We delay the transmission of the reports by a
2121 * random time between 0 and 60 seconds in order to
2122 * force the attacker wait 60 seconds before getting
2123 * the information on whether a frame resulted in a MIC
2124 * failure.
2125 */
2126 u8 rval[4];
2127 int sec;
2128
2129 if (os_get_random(rval, sizeof(rval)) < 0)
2130 sec = os_random() % 60;
2131 else
2132 sec = WPA_GET_BE32(rval) % 60;
2133 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2134 "report %d seconds", sec);
2135 wpa_s->pending_mic_error_report = 1;
2136 wpa_s->pending_mic_error_pairwise = pairwise;
2137 eloop_cancel_timeout(
2138 wpa_supplicant_delayed_mic_error_report,
2139 wpa_s, NULL);
2140 eloop_register_timeout(
2141 sec, os_random() % 1000000,
2142 wpa_supplicant_delayed_mic_error_report,
2143 wpa_s, NULL);
2144 } else {
2145 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2146 }
2147#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2148 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2149#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2150 }
2151 wpa_s->last_michael_mic_error = t.sec;
2152 wpa_s->mic_errors_seen++;
2153}
2154
2155
2156#ifdef CONFIG_TERMINATE_ONLASTIF
2157static int any_interfaces(struct wpa_supplicant *head)
2158{
2159 struct wpa_supplicant *wpa_s;
2160
2161 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2162 if (!wpa_s->interface_removed)
2163 return 1;
2164 return 0;
2165}
2166#endif /* CONFIG_TERMINATE_ONLASTIF */
2167
2168
2169static void
2170wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2171 union wpa_event_data *data)
2172{
2173 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2174 return;
2175
2176 switch (data->interface_status.ievent) {
2177 case EVENT_INTERFACE_ADDED:
2178 if (!wpa_s->interface_removed)
2179 break;
2180 wpa_s->interface_removed = 0;
2181 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2182 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2183 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2184 "driver after interface was added");
2185 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002186 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 break;
2188 case EVENT_INTERFACE_REMOVED:
2189 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2190 wpa_s->interface_removed = 1;
2191 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 l2_packet_deinit(wpa_s->l2);
2194 wpa_s->l2 = NULL;
2195#ifdef CONFIG_IBSS_RSN
2196 ibss_rsn_deinit(wpa_s->ibss_rsn);
2197 wpa_s->ibss_rsn = NULL;
2198#endif /* CONFIG_IBSS_RSN */
2199#ifdef CONFIG_TERMINATE_ONLASTIF
2200 /* check if last interface */
2201 if (!any_interfaces(wpa_s->global->ifaces))
2202 eloop_terminate();
2203#endif /* CONFIG_TERMINATE_ONLASTIF */
2204 break;
2205 }
2206}
2207
2208
2209#ifdef CONFIG_PEERKEY
2210static void
2211wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2212 union wpa_event_data *data)
2213{
2214 if (data == NULL)
2215 return;
2216 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2217}
2218#endif /* CONFIG_PEERKEY */
2219
2220
2221#ifdef CONFIG_TDLS
2222static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2223 union wpa_event_data *data)
2224{
2225 if (data == NULL)
2226 return;
2227 switch (data->tdls.oper) {
2228 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002229 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2230 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2231 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2232 else
2233 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234 break;
2235 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002236 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002237 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 break;
2239 }
2240}
2241#endif /* CONFIG_TDLS */
2242
2243
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002244#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002245static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2246 union wpa_event_data *data)
2247{
2248 if (data == NULL)
2249 return;
2250 switch (data->wnm.oper) {
2251 case WNM_OPER_SLEEP:
2252 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2253 "(action=%d, intval=%d)",
2254 data->wnm.sleep_action, data->wnm.sleep_intval);
2255 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002256 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002257 break;
2258 }
2259}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002260#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002261
2262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263#ifdef CONFIG_IEEE80211R
2264static void
2265wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2266 union wpa_event_data *data)
2267{
2268 if (data == NULL)
2269 return;
2270
2271 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2272 data->ft_ies.ies_len,
2273 data->ft_ies.ft_action,
2274 data->ft_ies.target_ap,
2275 data->ft_ies.ric_ies,
2276 data->ft_ies.ric_ies_len) < 0) {
2277 /* TODO: prevent MLME/driver from trying to associate? */
2278 }
2279}
2280#endif /* CONFIG_IEEE80211R */
2281
2282
2283#ifdef CONFIG_IBSS_RSN
2284static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2285 union wpa_event_data *data)
2286{
2287 struct wpa_ssid *ssid;
2288 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2289 return;
2290 if (data == NULL)
2291 return;
2292 ssid = wpa_s->current_ssid;
2293 if (ssid == NULL)
2294 return;
2295 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2296 return;
2297
2298 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2299}
2300#endif /* CONFIG_IBSS_RSN */
2301
2302
2303#ifdef CONFIG_IEEE80211R
2304static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2305 size_t len)
2306{
2307 const u8 *sta_addr, *target_ap_addr;
2308 u16 status;
2309
2310 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2311 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2312 return; /* only SME case supported for now */
2313 if (len < 1 + 2 * ETH_ALEN + 2)
2314 return;
2315 if (data[0] != 2)
2316 return; /* Only FT Action Response is supported for now */
2317 sta_addr = data + 1;
2318 target_ap_addr = data + 1 + ETH_ALEN;
2319 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2320 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2321 MACSTR " TargetAP " MACSTR " status %u",
2322 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2323
2324 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2325 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2326 " in FT Action Response", MAC2STR(sta_addr));
2327 return;
2328 }
2329
2330 if (status) {
2331 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2332 "failure (status code %d)", status);
2333 /* TODO: report error to FT code(?) */
2334 return;
2335 }
2336
2337 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2338 len - (1 + 2 * ETH_ALEN + 2), 1,
2339 target_ap_addr, NULL, 0) < 0)
2340 return;
2341
2342#ifdef CONFIG_SME
2343 {
2344 struct wpa_bss *bss;
2345 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2346 if (bss)
2347 wpa_s->sme.freq = bss->freq;
2348 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2349 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2350 WLAN_AUTH_FT);
2351 }
2352#endif /* CONFIG_SME */
2353}
2354#endif /* CONFIG_IEEE80211R */
2355
2356
2357static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2358 struct unprot_deauth *e)
2359{
2360#ifdef CONFIG_IEEE80211W
2361 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2362 "dropped: " MACSTR " -> " MACSTR
2363 " (reason code %u)",
2364 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2365 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2366#endif /* CONFIG_IEEE80211W */
2367}
2368
2369
2370static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2371 struct unprot_disassoc *e)
2372{
2373#ifdef CONFIG_IEEE80211W
2374 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2375 "dropped: " MACSTR " -> " MACSTR
2376 " (reason code %u)",
2377 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2378 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2379#endif /* CONFIG_IEEE80211W */
2380}
2381
2382
2383void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2384 union wpa_event_data *data)
2385{
2386 struct wpa_supplicant *wpa_s = ctx;
2387 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002388 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389
2390 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2391 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 event != EVENT_INTERFACE_STATUS &&
2393 event != EVENT_SCHED_SCAN_STOPPED) {
2394 wpa_dbg(wpa_s, MSG_DEBUG,
2395 "Ignore event %s (%d) while interface is disabled",
2396 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397 return;
2398 }
2399
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002400#ifndef CONFIG_NO_STDOUT_DEBUG
2401{
2402 int level = MSG_DEBUG;
2403
Dmitry Shmidt04949592012-07-19 12:16:46 -07002404 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002405 const struct ieee80211_hdr *hdr;
2406 u16 fc;
2407 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2408 fc = le_to_host16(hdr->frame_control);
2409 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2410 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2411 level = MSG_EXCESSIVE;
2412 }
2413
2414 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2415 event_to_string(event), event);
2416}
2417#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418
2419 switch (event) {
2420 case EVENT_AUTH:
2421 sme_event_auth(wpa_s, data);
2422 break;
2423 case EVENT_ASSOC:
2424 wpa_supplicant_event_assoc(wpa_s, data);
2425 break;
2426 case EVENT_DISASSOC:
2427 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2428 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002429 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2430 data->disassoc_info.reason_code,
2431 data->disassoc_info.locally_generated ?
2432 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 if (data->disassoc_info.addr)
2434 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2435 MAC2STR(data->disassoc_info.addr));
2436 }
2437#ifdef CONFIG_AP
2438 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2439 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2440 data->disassoc_info.addr);
2441 break;
2442 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002443 if (wpa_s->ap_iface) {
2444 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2445 "AP mode");
2446 break;
2447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002448#endif /* CONFIG_AP */
2449 if (data) {
2450 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002451 locally_generated =
2452 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002453 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2454 data->disassoc_info.ie,
2455 data->disassoc_info.ie_len);
2456#ifdef CONFIG_P2P
2457 wpas_p2p_disassoc_notif(
2458 wpa_s, data->disassoc_info.addr, reason_code,
2459 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002460 data->disassoc_info.ie_len,
2461 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462#endif /* CONFIG_P2P */
2463 }
2464 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2465 sme_event_disassoc(wpa_s, data);
2466 /* fall through */
2467 case EVENT_DEAUTH:
2468 if (event == EVENT_DEAUTH) {
2469 wpa_dbg(wpa_s, MSG_DEBUG,
2470 "Deauthentication notification");
2471 if (data) {
2472 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002473 locally_generated =
2474 data->deauth_info.locally_generated;
2475 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2476 data->deauth_info.reason_code,
2477 data->deauth_info.locally_generated ?
2478 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 if (data->deauth_info.addr) {
2480 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2481 MACSTR,
2482 MAC2STR(data->deauth_info.
2483 addr));
2484 }
2485 wpa_hexdump(MSG_DEBUG,
2486 "Deauthentication frame IE(s)",
2487 data->deauth_info.ie,
2488 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002489 }
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002490 wpa_reset_ft_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491 }
2492#ifdef CONFIG_AP
2493 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2494 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2495 data->deauth_info.addr);
2496 break;
2497 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002498 if (wpa_s->ap_iface) {
2499 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2500 "AP mode");
2501 break;
2502 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002504 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2505 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002506 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2507 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2508 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2509 eapol_sm_failed(wpa_s->eapol)))
2510 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002511#ifdef CONFIG_P2P
2512 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002513 if (wpas_p2p_deauth_notif(wpa_s,
2514 data->deauth_info.addr,
2515 reason_code,
2516 data->deauth_info.ie,
2517 data->deauth_info.ie_len,
2518 locally_generated) > 0) {
2519 /*
2520 * The interface was removed, so cannot
2521 * continue processing any additional
2522 * operations after this.
2523 */
2524 break;
2525 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002526 }
2527#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002528 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2529 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 break;
2531 case EVENT_MICHAEL_MIC_FAILURE:
2532 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2533 break;
2534#ifndef CONFIG_NO_SCAN_PROCESSING
2535 case EVENT_SCAN_RESULTS:
2536 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002537 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2538 wpa_s->wpa_state != WPA_ASSOCIATING)
2539 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540 break;
2541#endif /* CONFIG_NO_SCAN_PROCESSING */
2542 case EVENT_ASSOCINFO:
2543 wpa_supplicant_event_associnfo(wpa_s, data);
2544 break;
2545 case EVENT_INTERFACE_STATUS:
2546 wpa_supplicant_event_interface_status(wpa_s, data);
2547 break;
2548 case EVENT_PMKID_CANDIDATE:
2549 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2550 break;
2551#ifdef CONFIG_PEERKEY
2552 case EVENT_STKSTART:
2553 wpa_supplicant_event_stkstart(wpa_s, data);
2554 break;
2555#endif /* CONFIG_PEERKEY */
2556#ifdef CONFIG_TDLS
2557 case EVENT_TDLS:
2558 wpa_supplicant_event_tdls(wpa_s, data);
2559 break;
2560#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002561#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002562 case EVENT_WNM:
2563 wpa_supplicant_event_wnm(wpa_s, data);
2564 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002565#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566#ifdef CONFIG_IEEE80211R
2567 case EVENT_FT_RESPONSE:
2568 wpa_supplicant_event_ft_response(wpa_s, data);
2569 break;
2570#endif /* CONFIG_IEEE80211R */
2571#ifdef CONFIG_IBSS_RSN
2572 case EVENT_IBSS_RSN_START:
2573 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2574 break;
2575#endif /* CONFIG_IBSS_RSN */
2576 case EVENT_ASSOC_REJECT:
2577 if (data->assoc_reject.bssid)
2578 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2579 "bssid=" MACSTR " status_code=%u",
2580 MAC2STR(data->assoc_reject.bssid),
2581 data->assoc_reject.status_code);
2582 else
2583 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2584 "status_code=%u",
2585 data->assoc_reject.status_code);
2586 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2587 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002588 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002589#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002590 if(!wpa_s->current_ssid) {
2591 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2592 break;
2593 }
2594 /* If assoc reject is reported by the driver, then avoid
2595 * waiting for the authentication timeout. Cancel the
2596 * authentication timeout and retry the assoc.
2597 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002598 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002599 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2600 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002601
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002602 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002603
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002604 /* Clear the states */
2605 wpa_sm_notify_disassoc(wpa_s->wpa);
2606 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2607
2608 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002609 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002610 const u8 *bl_bssid = data->assoc_reject.bssid;
2611 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2612 bl_bssid = wpa_s->pending_bssid;
2613 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002614 wpa_supplicant_req_scan(wpa_s, 0, 0);
2615 } else {
2616 wpa_supplicant_req_scan(wpa_s, 1, 0);
2617 }
2618 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002619 /* If we ASSOC_REJECT's hits threshold, disable the
2620 * network
2621 */
2622 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2623 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002624 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002625 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002626 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002627 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002628#else
2629 const u8 *bssid = data->assoc_reject.bssid;
2630 if (bssid == NULL || is_zero_ether_addr(bssid))
2631 bssid = wpa_s->pending_bssid;
2632 wpas_connection_failed(wpa_s, bssid);
2633 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002634#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002635 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 break;
2637 case EVENT_AUTH_TIMED_OUT:
2638 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2639 sme_event_auth_timed_out(wpa_s, data);
2640 break;
2641 case EVENT_ASSOC_TIMED_OUT:
2642 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2643 sme_event_assoc_timed_out(wpa_s, data);
2644 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 case EVENT_TX_STATUS:
2646 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2647 " type=%d stype=%d",
2648 MAC2STR(data->tx_status.dst),
2649 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002650#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002652#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2654 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002655 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 wpa_s, data->tx_status.dst,
2657 data->tx_status.data,
2658 data->tx_status.data_len,
2659 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002660 OFFCHANNEL_SEND_ACTION_SUCCESS :
2661 OFFCHANNEL_SEND_ACTION_NO_ACK);
2662#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 break;
2664 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002665#endif /* CONFIG_AP */
2666#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2668 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2669 /*
2670 * Catch TX status events for Action frames we sent via group
2671 * interface in GO mode.
2672 */
2673 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2674 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2675 os_memcmp(wpa_s->parent->pending_action_dst,
2676 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002677 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678 wpa_s->parent, data->tx_status.dst,
2679 data->tx_status.data,
2680 data->tx_status.data_len,
2681 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002682 OFFCHANNEL_SEND_ACTION_SUCCESS :
2683 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 break;
2685 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002686#endif /* CONFIG_OFFCHANNEL */
2687#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 switch (data->tx_status.type) {
2689 case WLAN_FC_TYPE_MGMT:
2690 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2691 data->tx_status.data_len,
2692 data->tx_status.stype,
2693 data->tx_status.ack);
2694 break;
2695 case WLAN_FC_TYPE_DATA:
2696 ap_tx_status(wpa_s, data->tx_status.dst,
2697 data->tx_status.data,
2698 data->tx_status.data_len,
2699 data->tx_status.ack);
2700 break;
2701 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002702#endif /* CONFIG_AP */
2703 break;
2704#ifdef CONFIG_AP
2705 case EVENT_EAPOL_TX_STATUS:
2706 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2707 data->eapol_tx_status.data,
2708 data->eapol_tx_status.data_len,
2709 data->eapol_tx_status.ack);
2710 break;
2711 case EVENT_DRIVER_CLIENT_POLL_OK:
2712 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713 break;
2714 case EVENT_RX_FROM_UNKNOWN:
2715 if (wpa_s->ap_iface == NULL)
2716 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002717 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2718 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002720 case EVENT_CH_SWITCH:
2721 if (!data)
2722 break;
2723 if (!wpa_s->ap_iface) {
2724 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2725 "event in non-AP mode");
2726 break;
2727 }
2728
2729#ifdef CONFIG_AP
2730 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2731 data->ch_switch.ht_enabled,
2732 data->ch_switch.ch_offset);
2733#endif /* CONFIG_AP */
2734 break;
2735 case EVENT_RX_MGMT: {
2736 u16 fc, stype;
2737 const struct ieee80211_mgmt *mgmt;
2738
2739 mgmt = (const struct ieee80211_mgmt *)
2740 data->rx_mgmt.frame;
2741 fc = le_to_host16(mgmt->frame_control);
2742 stype = WLAN_FC_GET_STYPE(fc);
2743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 if (wpa_s->ap_iface == NULL) {
2745#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2747 data->rx_mgmt.frame_len > 24) {
2748 const u8 *src = mgmt->sa;
2749 const u8 *ie = mgmt->u.probe_req.variable;
2750 size_t ie_len = data->rx_mgmt.frame_len -
2751 (mgmt->u.probe_req.variable -
2752 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002753 wpas_p2p_probe_req_rx(
2754 wpa_s, src, mgmt->da,
2755 mgmt->bssid, ie, ie_len,
2756 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 break;
2758 }
2759#endif /* CONFIG_P2P */
2760 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2761 "management frame in non-AP mode");
2762 break;
2763 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002764
2765 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2766 data->rx_mgmt.frame_len > 24) {
2767 const u8 *ie = mgmt->u.probe_req.variable;
2768 size_t ie_len = data->rx_mgmt.frame_len -
2769 (mgmt->u.probe_req.variable -
2770 data->rx_mgmt.frame);
2771
2772 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2773 mgmt->bssid, ie, ie_len,
2774 data->rx_mgmt.ssi_signal);
2775 }
2776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2778 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002779 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780#endif /* CONFIG_AP */
2781 case EVENT_RX_ACTION:
2782 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2783 " Category=%u DataLen=%d freq=%d MHz",
2784 MAC2STR(data->rx_action.sa),
2785 data->rx_action.category, (int) data->rx_action.len,
2786 data->rx_action.freq);
2787#ifdef CONFIG_IEEE80211R
2788 if (data->rx_action.category == WLAN_ACTION_FT) {
2789 ft_rx_action(wpa_s, data->rx_action.data,
2790 data->rx_action.len);
2791 break;
2792 }
2793#endif /* CONFIG_IEEE80211R */
2794#ifdef CONFIG_IEEE80211W
2795#ifdef CONFIG_SME
2796 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2797 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2798 data->rx_action.data,
2799 data->rx_action.len);
2800 break;
2801 }
2802#endif /* CONFIG_SME */
2803#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002804#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002805 if (data->rx_action.category == WLAN_ACTION_WNM) {
2806 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2807 break;
2808 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002809#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002810#ifdef CONFIG_GAS
2811 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2812 gas_query_rx(wpa_s->gas, data->rx_action.da,
2813 data->rx_action.sa, data->rx_action.bssid,
2814 data->rx_action.data, data->rx_action.len,
2815 data->rx_action.freq) == 0)
2816 break;
2817#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818#ifdef CONFIG_TDLS
2819 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2820 data->rx_action.len >= 4 &&
2821 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2822 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2823 "Response from " MACSTR,
2824 MAC2STR(data->rx_action.sa));
2825 break;
2826 }
2827#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828#ifdef CONFIG_P2P
2829 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2830 data->rx_action.sa,
2831 data->rx_action.bssid,
2832 data->rx_action.category,
2833 data->rx_action.data,
2834 data->rx_action.len, data->rx_action.freq);
2835#endif /* CONFIG_P2P */
2836 break;
2837 case EVENT_RX_PROBE_REQ:
2838 if (data->rx_probe_req.sa == NULL ||
2839 data->rx_probe_req.ie == NULL)
2840 break;
2841#ifdef CONFIG_AP
2842 if (wpa_s->ap_iface) {
2843 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2844 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 break;
2851 }
2852#endif /* CONFIG_AP */
2853#ifdef CONFIG_P2P
2854 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002855 data->rx_probe_req.da,
2856 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002858 data->rx_probe_req.ie_len,
2859 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860#endif /* CONFIG_P2P */
2861 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002863#ifdef CONFIG_OFFCHANNEL
2864 offchannel_remain_on_channel_cb(
2865 wpa_s, data->remain_on_channel.freq,
2866 data->remain_on_channel.duration);
2867#endif /* CONFIG_OFFCHANNEL */
2868#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 wpas_p2p_remain_on_channel_cb(
2870 wpa_s, data->remain_on_channel.freq,
2871 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002872#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 break;
2874 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002875#ifdef CONFIG_OFFCHANNEL
2876 offchannel_cancel_remain_on_channel_cb(
2877 wpa_s, data->remain_on_channel.freq);
2878#endif /* CONFIG_OFFCHANNEL */
2879#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 wpas_p2p_cancel_remain_on_channel_cb(
2881 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002882#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002884#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 case EVENT_P2P_DEV_FOUND: {
2886 struct p2p_peer_info peer_info;
2887
2888 os_memset(&peer_info, 0, sizeof(peer_info));
2889 if (data->p2p_dev_found.dev_addr)
2890 os_memcpy(peer_info.p2p_device_addr,
2891 data->p2p_dev_found.dev_addr, ETH_ALEN);
2892 if (data->p2p_dev_found.pri_dev_type)
2893 os_memcpy(peer_info.pri_dev_type,
2894 data->p2p_dev_found.pri_dev_type,
2895 sizeof(peer_info.pri_dev_type));
2896 if (data->p2p_dev_found.dev_name)
2897 os_strlcpy(peer_info.device_name,
2898 data->p2p_dev_found.dev_name,
2899 sizeof(peer_info.device_name));
2900 peer_info.config_methods = data->p2p_dev_found.config_methods;
2901 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2902 peer_info.group_capab = data->p2p_dev_found.group_capab;
2903
2904 /*
2905 * FIX: new_device=1 is not necessarily correct. We should
2906 * maintain a P2P peer database in wpa_supplicant and update
2907 * this information based on whether the peer is truly new.
2908 */
2909 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2910 break;
2911 }
2912 case EVENT_P2P_GO_NEG_REQ_RX:
2913 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2914 data->p2p_go_neg_req_rx.dev_passwd_id);
2915 break;
2916 case EVENT_P2P_GO_NEG_COMPLETED:
2917 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2918 break;
2919 case EVENT_P2P_PROV_DISC_REQUEST:
2920 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2921 data->p2p_prov_disc_req.config_methods,
2922 data->p2p_prov_disc_req.dev_addr,
2923 data->p2p_prov_disc_req.pri_dev_type,
2924 data->p2p_prov_disc_req.dev_name,
2925 data->p2p_prov_disc_req.supp_config_methods,
2926 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002927 data->p2p_prov_disc_req.group_capab,
2928 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 break;
2930 case EVENT_P2P_PROV_DISC_RESPONSE:
2931 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2932 data->p2p_prov_disc_resp.config_methods);
2933 break;
2934 case EVENT_P2P_SD_REQUEST:
2935 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2936 data->p2p_sd_req.sa,
2937 data->p2p_sd_req.dialog_token,
2938 data->p2p_sd_req.update_indic,
2939 data->p2p_sd_req.tlvs,
2940 data->p2p_sd_req.tlvs_len);
2941 break;
2942 case EVENT_P2P_SD_RESPONSE:
2943 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2944 data->p2p_sd_resp.update_indic,
2945 data->p2p_sd_resp.tlvs,
2946 data->p2p_sd_resp.tlvs_len);
2947 break;
2948#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949 case EVENT_EAPOL_RX:
2950 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2951 data->eapol_rx.data,
2952 data->eapol_rx.data_len);
2953 break;
2954 case EVENT_SIGNAL_CHANGE:
2955 bgscan_notify_signal_change(
2956 wpa_s, data->signal_change.above_threshold,
2957 data->signal_change.current_signal,
2958 data->signal_change.current_noise,
2959 data->signal_change.current_txrate);
2960 break;
2961 case EVENT_INTERFACE_ENABLED:
2962 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2963 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002964 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965#ifdef CONFIG_AP
2966 if (!wpa_s->ap_iface) {
2967 wpa_supplicant_set_state(wpa_s,
2968 WPA_DISCONNECTED);
2969 wpa_supplicant_req_scan(wpa_s, 0, 0);
2970 } else
2971 wpa_supplicant_set_state(wpa_s,
2972 WPA_COMPLETED);
2973#else /* CONFIG_AP */
2974 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2975 wpa_supplicant_req_scan(wpa_s, 0, 0);
2976#endif /* CONFIG_AP */
2977 }
2978 break;
2979 case EVENT_INTERFACE_DISABLED:
2980 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2981 wpa_supplicant_mark_disassoc(wpa_s);
2982 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2983 break;
2984 case EVENT_CHANNEL_LIST_CHANGED:
2985 if (wpa_s->drv_priv == NULL)
2986 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002987
2988 free_hw_features(wpa_s);
2989 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2990 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992#ifdef CONFIG_P2P
2993 wpas_p2p_update_channel_list(wpa_s);
2994#endif /* CONFIG_P2P */
2995 break;
2996 case EVENT_INTERFACE_UNAVAILABLE:
2997#ifdef CONFIG_P2P
2998 wpas_p2p_interface_unavailable(wpa_s);
2999#endif /* CONFIG_P2P */
3000 break;
3001 case EVENT_BEST_CHANNEL:
3002 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3003 "(%d %d %d)",
3004 data->best_chan.freq_24, data->best_chan.freq_5,
3005 data->best_chan.freq_overall);
3006 wpa_s->best_24_freq = data->best_chan.freq_24;
3007 wpa_s->best_5_freq = data->best_chan.freq_5;
3008 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3009#ifdef CONFIG_P2P
3010 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3011 data->best_chan.freq_5,
3012 data->best_chan.freq_overall);
3013#endif /* CONFIG_P2P */
3014 break;
3015 case EVENT_UNPROT_DEAUTH:
3016 wpa_supplicant_event_unprot_deauth(wpa_s,
3017 &data->unprot_deauth);
3018 break;
3019 case EVENT_UNPROT_DISASSOC:
3020 wpa_supplicant_event_unprot_disassoc(wpa_s,
3021 &data->unprot_disassoc);
3022 break;
3023 case EVENT_STATION_LOW_ACK:
3024#ifdef CONFIG_AP
3025 if (wpa_s->ap_iface && data)
3026 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3027 data->low_ack.addr);
3028#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029#ifdef CONFIG_TDLS
3030 if (data)
3031 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3032#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 break;
3034 case EVENT_IBSS_PEER_LOST:
3035#ifdef CONFIG_IBSS_RSN
3036 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3037#endif /* CONFIG_IBSS_RSN */
3038 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003039 case EVENT_DRIVER_GTK_REKEY:
3040 if (os_memcmp(data->driver_gtk_rekey.bssid,
3041 wpa_s->bssid, ETH_ALEN))
3042 break;
3043 if (!wpa_s->wpa)
3044 break;
3045 wpa_sm_update_replay_ctr(wpa_s->wpa,
3046 data->driver_gtk_rekey.replay_ctr);
3047 break;
3048 case EVENT_SCHED_SCAN_STOPPED:
3049 wpa_s->sched_scanning = 0;
3050 wpa_supplicant_notify_scanning(wpa_s, 0);
3051
3052 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3053 break;
3054
3055 /*
3056 * If we timed out, start a new sched scan to continue
3057 * searching for more SSIDs.
3058 */
3059 if (wpa_s->sched_scan_timed_out)
3060 wpa_supplicant_req_sched_scan(wpa_s);
3061 break;
3062 case EVENT_WPS_BUTTON_PUSHED:
3063#ifdef CONFIG_WPS
3064 wpas_wps_start_pbc(wpa_s, NULL, 0);
3065#endif /* CONFIG_WPS */
3066 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003067 case EVENT_CONNECT_FAILED_REASON:
3068#ifdef CONFIG_AP
3069 if (!wpa_s->ap_iface || !data)
3070 break;
3071 hostapd_event_connect_failed_reason(
3072 wpa_s->ap_iface->bss[0],
3073 data->connect_failed_reason.addr,
3074 data->connect_failed_reason.code);
3075#endif /* CONFIG_AP */
3076 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 default:
3078 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3079 break;
3080 }
3081}