blob: 60e71b6989d60b6a0e72675953ab246d8066b6e2 [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
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700932 wpa_msg(wpa_s, MSG_DEBUG,
933 "Considering connect request: reassociate: %d selected: "
934 MACSTR " bssid: " MACSTR " pending: " MACSTR
935 " wpa_state: %s ssid=%p current_ssid=%p",
936 wpa_s->reassociate, MAC2STR(selected->bssid),
937 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
938 wpa_supplicant_state_txt(wpa_s->wpa_state),
939 ssid, wpa_s->current_ssid);
940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700941 /*
942 * Do not trigger new association unless the BSSID has changed or if
943 * reassociation is requested. If we are in process of associating with
944 * the selected BSSID, do not trigger new attempt.
945 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800946 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
948 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
949 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700950 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
951 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
952 0) ||
953 (is_zero_ether_addr(wpa_s->pending_bssid) &&
954 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
956 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700957 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700959 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
960 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 wpa_supplicant_associate(wpa_s, selected, ssid);
962 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700963 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
964 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800966
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700967 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968}
969
970
971static struct wpa_ssid *
972wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
973{
974 int prio;
975 struct wpa_ssid *ssid;
976
977 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
978 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
979 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700980 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 continue;
982 if (ssid->mode == IEEE80211_MODE_IBSS ||
983 ssid->mode == IEEE80211_MODE_AP)
984 return ssid;
985 }
986 }
987 return NULL;
988}
989
990
991/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
992 * on BSS added and BSS changed events */
993static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300994 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300996 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997
998 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
999 return;
1000
Jouni Malinen87fd2792011-05-16 18:35:42 +03001001 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003
Jouni Malinen87fd2792011-05-16 18:35:42 +03001004 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 if (ssid == NULL)
1006 continue;
1007
Jouni Malinen87fd2792011-05-16 18:35:42 +03001008 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 if (rsn == NULL)
1010 continue;
1011
Jouni Malinen87fd2792011-05-16 18:35:42 +03001012 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 }
1014
1015}
1016
1017
1018static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1019 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001020 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001022 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 int min_diff;
1024
1025 if (wpa_s->reassociate)
1026 return 1; /* explicit request to reassociate */
1027 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1028 return 1; /* we are not associated; continue */
1029 if (wpa_s->current_ssid == NULL)
1030 return 1; /* unknown current SSID */
1031 if (wpa_s->current_ssid != ssid)
1032 return 1; /* different network block */
1033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001034 if (wpas_driver_bss_selection(wpa_s))
1035 return 0; /* Driver-based roaming */
1036
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001037 if (wpa_s->current_ssid->ssid)
1038 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1039 wpa_s->current_ssid->ssid,
1040 wpa_s->current_ssid->ssid_len);
1041 if (!current_bss)
1042 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043
1044 if (!current_bss)
1045 return 1; /* current BSS not seen in scan results */
1046
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001047 if (current_bss == selected)
1048 return 0;
1049
1050 if (selected->last_update_idx > current_bss->last_update_idx)
1051 return 1; /* current BSS not seen in the last scan */
1052
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001053#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1055 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1056 MAC2STR(current_bss->bssid), current_bss->level);
1057 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1058 MAC2STR(selected->bssid), selected->level);
1059
1060 if (wpa_s->current_ssid->bssid_set &&
1061 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1062 0) {
1063 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1064 "has preferred BSSID");
1065 return 1;
1066 }
1067
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001068 if (current_bss->level < 0 && current_bss->level > selected->level) {
1069 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1070 "signal level");
1071 return 0;
1072 }
1073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 min_diff = 2;
1075 if (current_bss->level < 0) {
1076 if (current_bss->level < -85)
1077 min_diff = 1;
1078 else if (current_bss->level < -80)
1079 min_diff = 2;
1080 else if (current_bss->level < -75)
1081 min_diff = 3;
1082 else if (current_bss->level < -70)
1083 min_diff = 4;
1084 else
1085 min_diff = 5;
1086 }
1087 if (abs(current_bss->level - selected->level) < min_diff) {
1088 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1089 "in signal level");
1090 return 0;
1091 }
1092
1093 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001095 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097}
1098
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001099
Jouni Malinen89ca7022012-09-14 13:03:12 -07001100/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001102static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001103 union wpa_event_data *data,
1104 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 struct wpa_scan_results *scan_res;
1107 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001108#ifndef CONFIG_NO_RANDOM_POOL
1109 size_t i, num;
1110#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111
1112#ifdef CONFIG_AP
1113 if (wpa_s->ap_iface)
1114 ap = 1;
1115#endif /* CONFIG_AP */
1116
1117 wpa_supplicant_notify_scanning(wpa_s, 0);
1118
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001119#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001120 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001121 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001122 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1123 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001124 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1126 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1127 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001128 wpa_s->sta_scan_pending = 1;
1129 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001130 return -1;
1131 }
1132 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001133 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001134#endif /* CONFIG_P2P */
1135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1137 data ? &data->scan_info :
1138 NULL, 1);
1139 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001140 if (wpa_s->conf->ap_scan == 2 || ap ||
1141 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001143 if (!own_request)
1144 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1146 "scanning again");
1147 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1148 return -1;
1149 }
1150
1151#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 num = scan_res->num;
1153 if (num > 10)
1154 num = 10;
1155 for (i = 0; i < num; i++) {
1156 u8 buf[5];
1157 struct wpa_scan_res *res = scan_res->res[i];
1158 buf[0] = res->bssid[5];
1159 buf[1] = res->qual & 0xff;
1160 buf[2] = res->noise & 0xff;
1161 buf[3] = res->level & 0xff;
1162 buf[4] = res->tsf & 0xff;
1163 random_add_randomness(buf, sizeof(buf));
1164 }
1165#endif /* CONFIG_NO_RANDOM_POOL */
1166
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001167 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1169 struct wpa_scan_results *scan_res);
1170
1171 scan_res_handler = wpa_s->scan_res_handler;
1172 wpa_s->scan_res_handler = NULL;
1173 scan_res_handler(wpa_s, scan_res);
1174
1175 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001176 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 }
1178
1179 if (ap) {
1180 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1181#ifdef CONFIG_AP
1182 if (wpa_s->ap_iface->scan_cb)
1183 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1184#endif /* CONFIG_AP */
1185 wpa_scan_results_free(scan_res);
1186 return 0;
1187 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001188
Dmitry Shmidt04949592012-07-19 12:16:46 -07001189 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1190 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1191 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192
1193 wpas_notify_scan_done(wpa_s, 1);
1194
Dmitry Shmidt04949592012-07-19 12:16:46 -07001195 if (sme_proc_obss_scan(wpa_s) > 0) {
1196 wpa_scan_results_free(scan_res);
1197 return 0;
1198 }
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1201 wpa_scan_results_free(scan_res);
1202 return 0;
1203 }
1204
Dmitry Shmidt04949592012-07-19 12:16:46 -07001205 if (autoscan_notify_scan(wpa_s, scan_res)) {
1206 wpa_scan_results_free(scan_res);
1207 return 0;
1208 }
1209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 if (wpa_s->disconnected) {
1211 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1212 wpa_scan_results_free(scan_res);
1213 return 0;
1214 }
1215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216 if (!wpas_driver_bss_selection(wpa_s) &&
1217 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_scan_results_free(scan_res);
1219 return 0;
1220 }
1221
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001222 wpas_wps_update_ap_info(wpa_s, scan_res);
1223
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001224 wpa_scan_results_free(scan_res);
1225
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001226 return wpas_select_network_from_last_scan(wpa_s, 1);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001227}
1228
1229
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001230static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1231 int new_scan)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001232{
1233 struct wpa_bss *selected;
1234 struct wpa_ssid *ssid = NULL;
1235
1236 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237
1238 if (selected) {
1239 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001240 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001241 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001242 if (new_scan)
1243 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245 }
1246
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001247 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001248 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001249 return -1;
1250 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001251 if (new_scan)
1252 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001253 /*
1254 * Do not notify other virtual radios of scan results since we do not
1255 * want them to start other associations at the same time.
1256 */
1257 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1260 ssid = wpa_supplicant_pick_new_network(wpa_s);
1261 if (ssid) {
1262 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1263 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001264 if (new_scan)
1265 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 } else {
1267 int timeout_sec = wpa_s->scan_interval;
1268 int timeout_usec = 0;
1269#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001270 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1271 return 0;
1272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 if (wpa_s->p2p_in_provisioning) {
1274 /*
1275 * Use shorter wait during P2P Provisioning
1276 * state to speed up group formation.
1277 */
1278 timeout_sec = 0;
1279 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001280 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1281 timeout_usec);
1282 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 }
1284#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001285#ifdef CONFIG_INTERWORKING
1286 if (wpa_s->conf->auto_interworking &&
1287 wpa_s->conf->interworking &&
1288 wpa_s->conf->cred) {
1289 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1290 "start ANQP fetch since no matching "
1291 "networks found");
1292 wpa_s->network_select = 1;
1293 wpa_s->auto_network_select = 1;
1294 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001295 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001296 }
1297#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001298 if (wpa_supplicant_req_sched_scan(wpa_s))
1299 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1300 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 }
1302 }
1303 return 0;
1304}
1305
1306
1307static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1308 union wpa_event_data *data)
1309{
1310 const char *rn, *rn2;
1311 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001312
1313 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 /*
1315 * If no scan results could be fetched, then no need to
1316 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001317 * this scan. Similarly, if scan results started a new operation on this
1318 * interface, do not notify other interfaces to avoid concurrent
1319 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 */
1321 return;
1322 }
1323
1324 /*
1325 * Check other interfaces to see if they have the same radio-name. If
1326 * so, they get updated with this same scan info.
1327 */
1328 if (!wpa_s->driver->get_radio_name)
1329 return;
1330
1331 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1332 if (rn == NULL || rn[0] == '\0')
1333 return;
1334
1335 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1336 "sharing same radio (%s) in event_scan_results", rn);
1337
1338 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1339 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1340 continue;
1341
1342 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1343 if (rn2 && os_strcmp(rn, rn2) == 0) {
1344 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1345 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001346 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 }
1348 }
1349}
1350
1351#endif /* CONFIG_NO_SCAN_PROCESSING */
1352
1353
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001354int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1355{
1356#ifdef CONFIG_NO_SCAN_PROCESSING
1357 return -1;
1358#else /* CONFIG_NO_SCAN_PROCESSING */
1359 struct os_time now;
1360
1361 if (wpa_s->last_scan_res_used <= 0)
1362 return -1;
1363
1364 os_get_time(&now);
1365 if (now.sec - wpa_s->last_scan.sec > 5) {
1366 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1367 return -1;
1368 }
1369
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001370 return wpas_select_network_from_last_scan(wpa_s, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001371#endif /* CONFIG_NO_SCAN_PROCESSING */
1372}
1373
Dmitry Shmidt04949592012-07-19 12:16:46 -07001374#ifdef CONFIG_WNM
1375
1376static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1377{
1378 struct wpa_supplicant *wpa_s = eloop_ctx;
1379
1380 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1381 return;
1382
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001383 if (!wpa_s->no_keep_alive) {
1384 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1385 MAC2STR(wpa_s->bssid));
1386 /* TODO: could skip this if normal data traffic has been sent */
1387 /* TODO: Consider using some more appropriate data frame for
1388 * this */
1389 if (wpa_s->l2)
1390 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1391 (u8 *) "", 0);
1392 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001393
1394#ifdef CONFIG_SME
1395 if (wpa_s->sme.bss_max_idle_period) {
1396 unsigned int msec;
1397 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1398 if (msec > 100)
1399 msec -= 100;
1400 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1401 wnm_bss_keep_alive, wpa_s, NULL);
1402 }
1403#endif /* CONFIG_SME */
1404}
1405
1406
1407static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1408 const u8 *ies, size_t ies_len)
1409{
1410 struct ieee802_11_elems elems;
1411
1412 if (ies == NULL)
1413 return;
1414
1415 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1416 return;
1417
1418#ifdef CONFIG_SME
1419 if (elems.bss_max_idle_period) {
1420 unsigned int msec;
1421 wpa_s->sme.bss_max_idle_period =
1422 WPA_GET_LE16(elems.bss_max_idle_period);
1423 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1424 "TU)%s", wpa_s->sme.bss_max_idle_period,
1425 (elems.bss_max_idle_period[2] & 0x01) ?
1426 " (protected keep-live required)" : "");
1427 if (wpa_s->sme.bss_max_idle_period == 0)
1428 wpa_s->sme.bss_max_idle_period = 1;
1429 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1430 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1431 /* msec times 1000 */
1432 msec = wpa_s->sme.bss_max_idle_period * 1024;
1433 if (msec > 100)
1434 msec -= 100;
1435 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1436 wnm_bss_keep_alive, wpa_s,
1437 NULL);
1438 }
1439 }
1440#endif /* CONFIG_SME */
1441}
1442
1443#endif /* CONFIG_WNM */
1444
1445
1446void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1447{
1448#ifdef CONFIG_WNM
1449 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1450#endif /* CONFIG_WNM */
1451}
1452
1453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1455 union wpa_event_data *data)
1456{
1457 int l, len, found = 0, wpa_found, rsn_found;
1458 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001459#ifdef CONFIG_IEEE80211R
1460 u8 bssid[ETH_ALEN];
1461#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462
1463 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1464 if (data->assoc_info.req_ies)
1465 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1466 data->assoc_info.req_ies_len);
1467 if (data->assoc_info.resp_ies) {
1468 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1469 data->assoc_info.resp_ies_len);
1470#ifdef CONFIG_TDLS
1471 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1472 data->assoc_info.resp_ies_len);
1473#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001474#ifdef CONFIG_WNM
1475 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1476 data->assoc_info.resp_ies_len);
1477#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 }
1479 if (data->assoc_info.beacon_ies)
1480 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1481 data->assoc_info.beacon_ies,
1482 data->assoc_info.beacon_ies_len);
1483 if (data->assoc_info.freq)
1484 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1485 data->assoc_info.freq);
1486
1487 p = data->assoc_info.req_ies;
1488 l = data->assoc_info.req_ies_len;
1489
1490 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1491 while (p && l >= 2) {
1492 len = p[1] + 2;
1493 if (len > l) {
1494 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1495 p, l);
1496 break;
1497 }
1498 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1499 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1500 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1501 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1502 break;
1503 found = 1;
1504 wpa_find_assoc_pmkid(wpa_s);
1505 break;
1506 }
1507 l -= len;
1508 p += len;
1509 }
1510 if (!found && data->assoc_info.req_ies)
1511 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1512
1513#ifdef CONFIG_IEEE80211R
1514#ifdef CONFIG_SME
1515 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1517 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1518 data->assoc_info.resp_ies,
1519 data->assoc_info.resp_ies_len,
1520 bssid) < 0) {
1521 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1522 "Reassociation Response failed");
1523 wpa_supplicant_deauthenticate(
1524 wpa_s, WLAN_REASON_INVALID_IE);
1525 return -1;
1526 }
1527 }
1528
1529 p = data->assoc_info.resp_ies;
1530 l = data->assoc_info.resp_ies_len;
1531
1532#ifdef CONFIG_WPS_STRICT
1533 if (p && wpa_s->current_ssid &&
1534 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1535 struct wpabuf *wps;
1536 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1537 if (wps == NULL) {
1538 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1539 "include WPS IE in (Re)Association Response");
1540 return -1;
1541 }
1542
1543 if (wps_validate_assoc_resp(wps) < 0) {
1544 wpabuf_free(wps);
1545 wpa_supplicant_deauthenticate(
1546 wpa_s, WLAN_REASON_INVALID_IE);
1547 return -1;
1548 }
1549 wpabuf_free(wps);
1550 }
1551#endif /* CONFIG_WPS_STRICT */
1552
1553 /* Go through the IEs and make a copy of the MDIE, if present. */
1554 while (p && l >= 2) {
1555 len = p[1] + 2;
1556 if (len > l) {
1557 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1558 p, l);
1559 break;
1560 }
1561 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1562 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1563 wpa_s->sme.ft_used = 1;
1564 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1565 MOBILITY_DOMAIN_ID_LEN);
1566 break;
1567 }
1568 l -= len;
1569 p += len;
1570 }
1571#endif /* CONFIG_SME */
1572
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001573 /* Process FT when SME is in the driver */
1574 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1575 wpa_ft_is_completed(wpa_s->wpa)) {
1576 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1577 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1578 data->assoc_info.resp_ies,
1579 data->assoc_info.resp_ies_len,
1580 bssid) < 0) {
1581 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1582 "Reassociation Response failed");
1583 wpa_supplicant_deauthenticate(
1584 wpa_s, WLAN_REASON_INVALID_IE);
1585 return -1;
1586 }
1587 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1588 }
1589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1591 data->assoc_info.resp_ies_len);
1592#endif /* CONFIG_IEEE80211R */
1593
1594 /* WPA/RSN IE from Beacon/ProbeResp */
1595 p = data->assoc_info.beacon_ies;
1596 l = data->assoc_info.beacon_ies_len;
1597
1598 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1599 */
1600 wpa_found = rsn_found = 0;
1601 while (p && l >= 2) {
1602 len = p[1] + 2;
1603 if (len > l) {
1604 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1605 p, l);
1606 break;
1607 }
1608 if (!wpa_found &&
1609 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1610 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1611 wpa_found = 1;
1612 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1613 }
1614
1615 if (!rsn_found &&
1616 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1617 rsn_found = 1;
1618 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1619 }
1620
1621 l -= len;
1622 p += len;
1623 }
1624
1625 if (!wpa_found && data->assoc_info.beacon_ies)
1626 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1627 if (!rsn_found && data->assoc_info.beacon_ies)
1628 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1629 if (wpa_found || rsn_found)
1630 wpa_s->ap_ies_from_associnfo = 1;
1631
Jouni Malinen87fd2792011-05-16 18:35:42 +03001632 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1633 wpa_s->assoc_freq != data->assoc_info.freq) {
1634 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1635 "%u to %u MHz",
1636 wpa_s->assoc_freq, data->assoc_info.freq);
1637 wpa_supplicant_update_scan_results(wpa_s);
1638 }
1639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 wpa_s->assoc_freq = data->assoc_info.freq;
1641
1642 return 0;
1643}
1644
1645
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001646static struct wpa_bss * wpa_supplicant_get_new_bss(
1647 struct wpa_supplicant *wpa_s, const u8 *bssid)
1648{
1649 struct wpa_bss *bss = NULL;
1650 struct wpa_ssid *ssid = wpa_s->current_ssid;
1651
1652 if (ssid->ssid_len > 0)
1653 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1654 if (!bss)
1655 bss = wpa_bss_get_bssid(wpa_s, bssid);
1656
1657 return bss;
1658}
1659
1660
1661static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1662{
1663 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1664
1665 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1666 return -1;
1667
1668 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1669 return 0;
1670
1671 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1672 WPA_IE_VENDOR_TYPE);
1673 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1674
1675 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1676 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1677 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1678 bss_rsn ? 2 + bss_rsn[1] : 0))
1679 return -1;
1680
1681 return 0;
1682}
1683
1684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1686 union wpa_event_data *data)
1687{
1688 u8 bssid[ETH_ALEN];
1689 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690
1691#ifdef CONFIG_AP
1692 if (wpa_s->ap_iface) {
1693 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1694 data->assoc_info.addr,
1695 data->assoc_info.req_ies,
1696 data->assoc_info.req_ies_len,
1697 data->assoc_info.reassoc);
1698 return;
1699 }
1700#endif /* CONFIG_AP */
1701
1702 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1703 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1704 return;
1705
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001706 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1707 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001708 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001709 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1710 return;
1711 }
1712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001714 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1716 MACSTR, MAC2STR(bssid));
1717 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1719 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001720 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721
1722 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1723 wpa_clear_keys(wpa_s, bssid);
1724 }
1725 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001726 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1728 return;
1729 }
1730 if (wpa_s->current_ssid) {
1731 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001732
1733 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1734 if (!bss) {
1735 wpa_supplicant_update_scan_results(wpa_s);
1736
1737 /* Get the BSS from the new scan results */
1738 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1739 }
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 if (bss)
1742 wpa_s->current_bss = bss;
1743 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001744
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001745#ifdef ANDROID
1746 if (wpa_s->conf->ap_scan == 1) {
1747#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001748 if (wpa_s->conf->ap_scan == 1 &&
1749 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001750#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001751 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1752 wpa_msg(wpa_s, MSG_WARNING,
1753 "WPA/RSN IEs not updated");
1754 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 }
1756
1757#ifdef CONFIG_SME
1758 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1759 wpa_s->sme.prev_bssid_set = 1;
1760#endif /* CONFIG_SME */
1761
1762 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1763 if (wpa_s->current_ssid) {
1764 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1765 * initialized before association, but for other modes,
1766 * initialize PC/SC here, if the current configuration needs
1767 * smartcard or SIM/USIM. */
1768 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1769 }
1770 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1771 if (wpa_s->l2)
1772 l2_packet_notify_auth_start(wpa_s->l2);
1773
1774 /*
1775 * Set portEnabled first to FALSE in order to get EAP state machine out
1776 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1777 * state machine may transit to AUTHENTICATING state based on obsolete
1778 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1779 * AUTHENTICATED without ever giving chance to EAP state machine to
1780 * reset the state.
1781 */
1782 if (!ft_completed) {
1783 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1784 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1785 }
1786 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1787 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1788 /* 802.1X::portControl = Auto */
1789 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1790 wpa_s->eapol_received = 0;
1791 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1792 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1793 (wpa_s->current_ssid &&
1794 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001795 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1796 (wpa_s->drv_flags &
1797 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1798 /*
1799 * Set the key after having received joined-IBSS event
1800 * from the driver.
1801 */
1802 wpa_supplicant_set_wpa_none_key(wpa_s,
1803 wpa_s->current_ssid);
1804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 wpa_supplicant_cancel_auth_timeout(wpa_s);
1806 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1807 } else if (!ft_completed) {
1808 /* Timeout for receiving the first EAPOL packet */
1809 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1810 }
1811 wpa_supplicant_cancel_scan(wpa_s);
1812
1813 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1814 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1815 /*
1816 * We are done; the driver will take care of RSN 4-way
1817 * handshake.
1818 */
1819 wpa_supplicant_cancel_auth_timeout(wpa_s);
1820 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1821 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1822 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1823 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1824 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1825 /*
1826 * The driver will take care of RSN 4-way handshake, so we need
1827 * to allow EAPOL supplicant to complete its work without
1828 * waiting for WPA supplicant.
1829 */
1830 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1831 } else if (ft_completed) {
1832 /*
1833 * FT protocol completed - make sure EAPOL state machine ends
1834 * up in authenticated.
1835 */
1836 wpa_supplicant_cancel_auth_timeout(wpa_s);
1837 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1838 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1839 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1840 }
1841
Jouni Malinena05074c2012-12-21 21:35:35 +02001842 wpa_s->last_eapol_matches_bssid = 0;
1843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 if (wpa_s->pending_eapol_rx) {
1845 struct os_time now, age;
1846 os_get_time(&now);
1847 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1848 if (age.sec == 0 && age.usec < 100000 &&
1849 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1850 0) {
1851 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1852 "frame that was received just before "
1853 "association notification");
1854 wpa_supplicant_rx_eapol(
1855 wpa_s, wpa_s->pending_eapol_rx_src,
1856 wpabuf_head(wpa_s->pending_eapol_rx),
1857 wpabuf_len(wpa_s->pending_eapol_rx));
1858 }
1859 wpabuf_free(wpa_s->pending_eapol_rx);
1860 wpa_s->pending_eapol_rx = NULL;
1861 }
1862
1863 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1864 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001865 wpa_s->current_ssid &&
1866 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 /* Set static WEP keys again */
1868 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1869 }
1870
1871#ifdef CONFIG_IBSS_RSN
1872 if (wpa_s->current_ssid &&
1873 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1874 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1875 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1876 wpa_s->ibss_rsn == NULL) {
1877 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1878 if (!wpa_s->ibss_rsn) {
1879 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1880 wpa_supplicant_deauthenticate(
1881 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1882 return;
1883 }
1884
1885 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1886 }
1887#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001888
1889 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890}
1891
1892
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001893static int disconnect_reason_recoverable(u16 reason_code)
1894{
1895 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1896 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1897 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1898}
1899
1900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001902 u16 reason_code,
1903 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904{
1905 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001906
1907 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1908 /*
1909 * At least Host AP driver and a Prism3 card seemed to be
1910 * generating streams of disconnected events when configuring
1911 * IBSS for WPA-None. Ignore them for now.
1912 */
1913 return;
1914 }
1915
1916 bssid = wpa_s->bssid;
1917 if (is_zero_ether_addr(bssid))
1918 bssid = wpa_s->pending_bssid;
1919
1920 if (!is_zero_ether_addr(bssid) ||
1921 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1922 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1923 " reason=%d%s",
1924 MAC2STR(bssid), reason_code,
1925 locally_generated ? " locally_generated=1" : "");
1926 }
1927}
1928
1929
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001930static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1931 int locally_generated)
1932{
1933 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1934 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1935 return 0; /* Not in 4-way handshake with PSK */
1936
1937 /*
1938 * It looks like connection was lost while trying to go through PSK
1939 * 4-way handshake. Filter out known disconnection cases that are caused
1940 * by something else than PSK mismatch to avoid confusing reports.
1941 */
1942
1943 if (locally_generated) {
1944 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1945 return 0;
1946 }
1947
1948 return 1;
1949}
1950
1951
Jouni Malinen2b89da82012-08-31 22:04:41 +03001952static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1953 u16 reason_code,
1954 int locally_generated)
1955{
1956 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 int authenticating;
1958 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001959 struct wpa_bss *fast_reconnect = NULL;
1960 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001961 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962
1963 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1964 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1965
1966 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1967 /*
1968 * At least Host AP driver and a Prism3 card seemed to be
1969 * generating streams of disconnected events when configuring
1970 * IBSS for WPA-None. Ignore them for now.
1971 */
1972 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1973 "IBSS/WPA-None mode");
1974 return;
1975 }
1976
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001977 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1979 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001980 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001981 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001982 if (!wpa_s->disconnected &&
1983 (!wpa_s->auto_reconnect_disabled ||
1984 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001985 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1986 "reconnect (wps=%d wpa_state=%d)",
1987 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1988 wpa_s->wpa_state);
1989 if (wpa_s->wpa_state == WPA_COMPLETED &&
1990 wpa_s->current_ssid &&
1991 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001992 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001993 disconnect_reason_recoverable(reason_code)) {
1994 /*
1995 * It looks like the AP has dropped association with
1996 * us, but could allow us to get back in. Try to
1997 * reconnect to the same BSS without full scan to save
1998 * time for some common cases.
1999 */
2000 fast_reconnect = wpa_s->current_bss;
2001 fast_reconnect_ssid = wpa_s->current_ssid;
2002 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002003#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07002004 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002005#else
2006 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2007#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002008 else
2009 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2010 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002012 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 "try to re-connect");
2014 wpa_s->reassociate = 0;
2015 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002016 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 }
2018 bssid = wpa_s->bssid;
2019 if (is_zero_ether_addr(bssid))
2020 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002021 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2022 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002024 if (locally_generated)
2025 wpa_s->disconnect_reason = -reason_code;
2026 else
2027 wpa_s->disconnect_reason = reason_code;
2028 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2030 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2031 wpa_s->keys_cleared = 0;
2032 wpa_clear_keys(wpa_s, wpa_s->bssid);
2033 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002034 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 wpa_supplicant_mark_disassoc(wpa_s);
2036
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002037 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002039 wpa_s->current_ssid = last_ssid;
2040 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002041
2042 if (fast_reconnect) {
2043#ifndef CONFIG_NO_SCAN_PROCESSING
2044 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2045 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2046 fast_reconnect_ssid) < 0) {
2047 /* Recover through full scan */
2048 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2049 }
2050#endif /* CONFIG_NO_SCAN_PROCESSING */
2051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052}
2053
2054
2055#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002056void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002057{
2058 struct wpa_supplicant *wpa_s = eloop_ctx;
2059
2060 if (!wpa_s->pending_mic_error_report)
2061 return;
2062
2063 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2064 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2065 wpa_s->pending_mic_error_report = 0;
2066}
2067#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2068
2069
2070static void
2071wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2072 union wpa_event_data *data)
2073{
2074 int pairwise;
2075 struct os_time t;
2076
2077 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2078 pairwise = (data && data->michael_mic_failure.unicast);
2079 os_get_time(&t);
2080 if ((wpa_s->last_michael_mic_error &&
2081 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2082 wpa_s->pending_mic_error_report) {
2083 if (wpa_s->pending_mic_error_report) {
2084 /*
2085 * Send the pending MIC error report immediately since
2086 * we are going to start countermeasures and AP better
2087 * do the same.
2088 */
2089 wpa_sm_key_request(wpa_s->wpa, 1,
2090 wpa_s->pending_mic_error_pairwise);
2091 }
2092
2093 /* Send the new MIC error report immediately since we are going
2094 * to start countermeasures and AP better do the same.
2095 */
2096 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2097
2098 /* initialize countermeasures */
2099 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002100
2101 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2104
2105 /*
2106 * Need to wait for completion of request frame. We do not get
2107 * any callback for the message completion, so just wait a
2108 * short while and hope for the best. */
2109 os_sleep(0, 10000);
2110
2111 wpa_drv_set_countermeasures(wpa_s, 1);
2112 wpa_supplicant_deauthenticate(wpa_s,
2113 WLAN_REASON_MICHAEL_MIC_FAILURE);
2114 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2115 wpa_s, NULL);
2116 eloop_register_timeout(60, 0,
2117 wpa_supplicant_stop_countermeasures,
2118 wpa_s, NULL);
2119 /* TODO: mark the AP rejected for 60 second. STA is
2120 * allowed to associate with another AP.. */
2121 } else {
2122#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2123 if (wpa_s->mic_errors_seen) {
2124 /*
2125 * Reduce the effectiveness of Michael MIC error
2126 * reports as a means for attacking against TKIP if
2127 * more than one MIC failure is noticed with the same
2128 * PTK. We delay the transmission of the reports by a
2129 * random time between 0 and 60 seconds in order to
2130 * force the attacker wait 60 seconds before getting
2131 * the information on whether a frame resulted in a MIC
2132 * failure.
2133 */
2134 u8 rval[4];
2135 int sec;
2136
2137 if (os_get_random(rval, sizeof(rval)) < 0)
2138 sec = os_random() % 60;
2139 else
2140 sec = WPA_GET_BE32(rval) % 60;
2141 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2142 "report %d seconds", sec);
2143 wpa_s->pending_mic_error_report = 1;
2144 wpa_s->pending_mic_error_pairwise = pairwise;
2145 eloop_cancel_timeout(
2146 wpa_supplicant_delayed_mic_error_report,
2147 wpa_s, NULL);
2148 eloop_register_timeout(
2149 sec, os_random() % 1000000,
2150 wpa_supplicant_delayed_mic_error_report,
2151 wpa_s, NULL);
2152 } else {
2153 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2154 }
2155#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2156 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2157#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2158 }
2159 wpa_s->last_michael_mic_error = t.sec;
2160 wpa_s->mic_errors_seen++;
2161}
2162
2163
2164#ifdef CONFIG_TERMINATE_ONLASTIF
2165static int any_interfaces(struct wpa_supplicant *head)
2166{
2167 struct wpa_supplicant *wpa_s;
2168
2169 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2170 if (!wpa_s->interface_removed)
2171 return 1;
2172 return 0;
2173}
2174#endif /* CONFIG_TERMINATE_ONLASTIF */
2175
2176
2177static void
2178wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2179 union wpa_event_data *data)
2180{
2181 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2182 return;
2183
2184 switch (data->interface_status.ievent) {
2185 case EVENT_INTERFACE_ADDED:
2186 if (!wpa_s->interface_removed)
2187 break;
2188 wpa_s->interface_removed = 0;
2189 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2190 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2191 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2192 "driver after interface was added");
2193 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002194 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 break;
2196 case EVENT_INTERFACE_REMOVED:
2197 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2198 wpa_s->interface_removed = 1;
2199 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002200 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 l2_packet_deinit(wpa_s->l2);
2202 wpa_s->l2 = NULL;
2203#ifdef CONFIG_IBSS_RSN
2204 ibss_rsn_deinit(wpa_s->ibss_rsn);
2205 wpa_s->ibss_rsn = NULL;
2206#endif /* CONFIG_IBSS_RSN */
2207#ifdef CONFIG_TERMINATE_ONLASTIF
2208 /* check if last interface */
2209 if (!any_interfaces(wpa_s->global->ifaces))
2210 eloop_terminate();
2211#endif /* CONFIG_TERMINATE_ONLASTIF */
2212 break;
2213 }
2214}
2215
2216
2217#ifdef CONFIG_PEERKEY
2218static void
2219wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2220 union wpa_event_data *data)
2221{
2222 if (data == NULL)
2223 return;
2224 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2225}
2226#endif /* CONFIG_PEERKEY */
2227
2228
2229#ifdef CONFIG_TDLS
2230static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2231 union wpa_event_data *data)
2232{
2233 if (data == NULL)
2234 return;
2235 switch (data->tdls.oper) {
2236 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002237 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2238 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2239 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2240 else
2241 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 break;
2243 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002244 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002245 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 break;
2247 }
2248}
2249#endif /* CONFIG_TDLS */
2250
2251
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002252#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002253static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2254 union wpa_event_data *data)
2255{
2256 if (data == NULL)
2257 return;
2258 switch (data->wnm.oper) {
2259 case WNM_OPER_SLEEP:
2260 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2261 "(action=%d, intval=%d)",
2262 data->wnm.sleep_action, data->wnm.sleep_intval);
2263 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002264 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002265 break;
2266 }
2267}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002268#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002269
2270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271#ifdef CONFIG_IEEE80211R
2272static void
2273wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2274 union wpa_event_data *data)
2275{
2276 if (data == NULL)
2277 return;
2278
2279 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2280 data->ft_ies.ies_len,
2281 data->ft_ies.ft_action,
2282 data->ft_ies.target_ap,
2283 data->ft_ies.ric_ies,
2284 data->ft_ies.ric_ies_len) < 0) {
2285 /* TODO: prevent MLME/driver from trying to associate? */
2286 }
2287}
2288#endif /* CONFIG_IEEE80211R */
2289
2290
2291#ifdef CONFIG_IBSS_RSN
2292static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2293 union wpa_event_data *data)
2294{
2295 struct wpa_ssid *ssid;
2296 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2297 return;
2298 if (data == NULL)
2299 return;
2300 ssid = wpa_s->current_ssid;
2301 if (ssid == NULL)
2302 return;
2303 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2304 return;
2305
2306 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2307}
2308#endif /* CONFIG_IBSS_RSN */
2309
2310
2311#ifdef CONFIG_IEEE80211R
2312static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2313 size_t len)
2314{
2315 const u8 *sta_addr, *target_ap_addr;
2316 u16 status;
2317
2318 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2319 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2320 return; /* only SME case supported for now */
2321 if (len < 1 + 2 * ETH_ALEN + 2)
2322 return;
2323 if (data[0] != 2)
2324 return; /* Only FT Action Response is supported for now */
2325 sta_addr = data + 1;
2326 target_ap_addr = data + 1 + ETH_ALEN;
2327 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2328 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2329 MACSTR " TargetAP " MACSTR " status %u",
2330 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2331
2332 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2333 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2334 " in FT Action Response", MAC2STR(sta_addr));
2335 return;
2336 }
2337
2338 if (status) {
2339 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2340 "failure (status code %d)", status);
2341 /* TODO: report error to FT code(?) */
2342 return;
2343 }
2344
2345 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2346 len - (1 + 2 * ETH_ALEN + 2), 1,
2347 target_ap_addr, NULL, 0) < 0)
2348 return;
2349
2350#ifdef CONFIG_SME
2351 {
2352 struct wpa_bss *bss;
2353 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2354 if (bss)
2355 wpa_s->sme.freq = bss->freq;
2356 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2357 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2358 WLAN_AUTH_FT);
2359 }
2360#endif /* CONFIG_SME */
2361}
2362#endif /* CONFIG_IEEE80211R */
2363
2364
2365static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2366 struct unprot_deauth *e)
2367{
2368#ifdef CONFIG_IEEE80211W
2369 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2370 "dropped: " MACSTR " -> " MACSTR
2371 " (reason code %u)",
2372 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2373 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2374#endif /* CONFIG_IEEE80211W */
2375}
2376
2377
2378static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2379 struct unprot_disassoc *e)
2380{
2381#ifdef CONFIG_IEEE80211W
2382 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2383 "dropped: " MACSTR " -> " MACSTR
2384 " (reason code %u)",
2385 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2386 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2387#endif /* CONFIG_IEEE80211W */
2388}
2389
2390
2391void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2392 union wpa_event_data *data)
2393{
2394 struct wpa_supplicant *wpa_s = ctx;
2395 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002396 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397
2398 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2399 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002400 event != EVENT_INTERFACE_STATUS &&
2401 event != EVENT_SCHED_SCAN_STOPPED) {
2402 wpa_dbg(wpa_s, MSG_DEBUG,
2403 "Ignore event %s (%d) while interface is disabled",
2404 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 return;
2406 }
2407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408#ifndef CONFIG_NO_STDOUT_DEBUG
2409{
2410 int level = MSG_DEBUG;
2411
Dmitry Shmidt04949592012-07-19 12:16:46 -07002412 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002413 const struct ieee80211_hdr *hdr;
2414 u16 fc;
2415 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2416 fc = le_to_host16(hdr->frame_control);
2417 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2418 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2419 level = MSG_EXCESSIVE;
2420 }
2421
2422 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2423 event_to_string(event), event);
2424}
2425#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426
2427 switch (event) {
2428 case EVENT_AUTH:
2429 sme_event_auth(wpa_s, data);
2430 break;
2431 case EVENT_ASSOC:
2432 wpa_supplicant_event_assoc(wpa_s, data);
2433 break;
2434 case EVENT_DISASSOC:
2435 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2436 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002437 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2438 data->disassoc_info.reason_code,
2439 data->disassoc_info.locally_generated ?
2440 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002441 if (data->disassoc_info.addr)
2442 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2443 MAC2STR(data->disassoc_info.addr));
2444 }
2445#ifdef CONFIG_AP
2446 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2447 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2448 data->disassoc_info.addr);
2449 break;
2450 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002451 if (wpa_s->ap_iface) {
2452 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2453 "AP mode");
2454 break;
2455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456#endif /* CONFIG_AP */
2457 if (data) {
2458 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002459 locally_generated =
2460 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2462 data->disassoc_info.ie,
2463 data->disassoc_info.ie_len);
2464#ifdef CONFIG_P2P
2465 wpas_p2p_disassoc_notif(
2466 wpa_s, data->disassoc_info.addr, reason_code,
2467 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002468 data->disassoc_info.ie_len,
2469 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470#endif /* CONFIG_P2P */
2471 }
2472 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2473 sme_event_disassoc(wpa_s, data);
2474 /* fall through */
2475 case EVENT_DEAUTH:
2476 if (event == EVENT_DEAUTH) {
2477 wpa_dbg(wpa_s, MSG_DEBUG,
2478 "Deauthentication notification");
2479 if (data) {
2480 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002481 locally_generated =
2482 data->deauth_info.locally_generated;
2483 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2484 data->deauth_info.reason_code,
2485 data->deauth_info.locally_generated ?
2486 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 if (data->deauth_info.addr) {
2488 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2489 MACSTR,
2490 MAC2STR(data->deauth_info.
2491 addr));
2492 }
2493 wpa_hexdump(MSG_DEBUG,
2494 "Deauthentication frame IE(s)",
2495 data->deauth_info.ie,
2496 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 }
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002498 wpa_reset_ft_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 }
2500#ifdef CONFIG_AP
2501 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2502 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2503 data->deauth_info.addr);
2504 break;
2505 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002506 if (wpa_s->ap_iface) {
2507 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2508 "AP mode");
2509 break;
2510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002512 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2513 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002514 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2515 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2516 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2517 eapol_sm_failed(wpa_s->eapol)))
2518 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519#ifdef CONFIG_P2P
2520 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002521 if (wpas_p2p_deauth_notif(wpa_s,
2522 data->deauth_info.addr,
2523 reason_code,
2524 data->deauth_info.ie,
2525 data->deauth_info.ie_len,
2526 locally_generated) > 0) {
2527 /*
2528 * The interface was removed, so cannot
2529 * continue processing any additional
2530 * operations after this.
2531 */
2532 break;
2533 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002534 }
2535#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002536 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2537 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 break;
2539 case EVENT_MICHAEL_MIC_FAILURE:
2540 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2541 break;
2542#ifndef CONFIG_NO_SCAN_PROCESSING
2543 case EVENT_SCAN_RESULTS:
2544 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002545 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2546 wpa_s->wpa_state != WPA_ASSOCIATING)
2547 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 break;
2549#endif /* CONFIG_NO_SCAN_PROCESSING */
2550 case EVENT_ASSOCINFO:
2551 wpa_supplicant_event_associnfo(wpa_s, data);
2552 break;
2553 case EVENT_INTERFACE_STATUS:
2554 wpa_supplicant_event_interface_status(wpa_s, data);
2555 break;
2556 case EVENT_PMKID_CANDIDATE:
2557 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2558 break;
2559#ifdef CONFIG_PEERKEY
2560 case EVENT_STKSTART:
2561 wpa_supplicant_event_stkstart(wpa_s, data);
2562 break;
2563#endif /* CONFIG_PEERKEY */
2564#ifdef CONFIG_TDLS
2565 case EVENT_TDLS:
2566 wpa_supplicant_event_tdls(wpa_s, data);
2567 break;
2568#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002569#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002570 case EVENT_WNM:
2571 wpa_supplicant_event_wnm(wpa_s, data);
2572 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002573#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574#ifdef CONFIG_IEEE80211R
2575 case EVENT_FT_RESPONSE:
2576 wpa_supplicant_event_ft_response(wpa_s, data);
2577 break;
2578#endif /* CONFIG_IEEE80211R */
2579#ifdef CONFIG_IBSS_RSN
2580 case EVENT_IBSS_RSN_START:
2581 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2582 break;
2583#endif /* CONFIG_IBSS_RSN */
2584 case EVENT_ASSOC_REJECT:
2585 if (data->assoc_reject.bssid)
2586 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2587 "bssid=" MACSTR " status_code=%u",
2588 MAC2STR(data->assoc_reject.bssid),
2589 data->assoc_reject.status_code);
2590 else
2591 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2592 "status_code=%u",
2593 data->assoc_reject.status_code);
2594 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2595 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002596 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002597#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002598 if(!wpa_s->current_ssid) {
2599 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2600 break;
2601 }
2602 /* If assoc reject is reported by the driver, then avoid
2603 * waiting for the authentication timeout. Cancel the
2604 * authentication timeout and retry the assoc.
2605 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002606 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002607 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2608 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002609
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002610 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002611
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002612 /* Clear the states */
2613 wpa_sm_notify_disassoc(wpa_s->wpa);
2614 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2615
2616 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002617 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002618 const u8 *bl_bssid = data->assoc_reject.bssid;
2619 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2620 bl_bssid = wpa_s->pending_bssid;
2621 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002622 wpa_supplicant_req_scan(wpa_s, 0, 0);
2623 } else {
2624 wpa_supplicant_req_scan(wpa_s, 1, 0);
2625 }
2626 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002627 /* If we ASSOC_REJECT's hits threshold, disable the
2628 * network
2629 */
2630 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2631 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002632 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002633 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002634 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002635 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002636#else
2637 const u8 *bssid = data->assoc_reject.bssid;
2638 if (bssid == NULL || is_zero_ether_addr(bssid))
2639 bssid = wpa_s->pending_bssid;
2640 wpas_connection_failed(wpa_s, bssid);
2641 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002642#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 break;
2645 case EVENT_AUTH_TIMED_OUT:
2646 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2647 sme_event_auth_timed_out(wpa_s, data);
2648 break;
2649 case EVENT_ASSOC_TIMED_OUT:
2650 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2651 sme_event_assoc_timed_out(wpa_s, data);
2652 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 case EVENT_TX_STATUS:
2654 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2655 " type=%d stype=%d",
2656 MAC2STR(data->tx_status.dst),
2657 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002658#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002660#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2662 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002663 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 wpa_s, data->tx_status.dst,
2665 data->tx_status.data,
2666 data->tx_status.data_len,
2667 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002668 OFFCHANNEL_SEND_ACTION_SUCCESS :
2669 OFFCHANNEL_SEND_ACTION_NO_ACK);
2670#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 break;
2672 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002673#endif /* CONFIG_AP */
2674#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2676 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2677 /*
2678 * Catch TX status events for Action frames we sent via group
2679 * interface in GO mode.
2680 */
2681 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2682 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2683 os_memcmp(wpa_s->parent->pending_action_dst,
2684 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002685 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686 wpa_s->parent, data->tx_status.dst,
2687 data->tx_status.data,
2688 data->tx_status.data_len,
2689 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002690 OFFCHANNEL_SEND_ACTION_SUCCESS :
2691 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 break;
2693 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002694#endif /* CONFIG_OFFCHANNEL */
2695#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 switch (data->tx_status.type) {
2697 case WLAN_FC_TYPE_MGMT:
2698 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2699 data->tx_status.data_len,
2700 data->tx_status.stype,
2701 data->tx_status.ack);
2702 break;
2703 case WLAN_FC_TYPE_DATA:
2704 ap_tx_status(wpa_s, data->tx_status.dst,
2705 data->tx_status.data,
2706 data->tx_status.data_len,
2707 data->tx_status.ack);
2708 break;
2709 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002710#endif /* CONFIG_AP */
2711 break;
2712#ifdef CONFIG_AP
2713 case EVENT_EAPOL_TX_STATUS:
2714 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2715 data->eapol_tx_status.data,
2716 data->eapol_tx_status.data_len,
2717 data->eapol_tx_status.ack);
2718 break;
2719 case EVENT_DRIVER_CLIENT_POLL_OK:
2720 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 break;
2722 case EVENT_RX_FROM_UNKNOWN:
2723 if (wpa_s->ap_iface == NULL)
2724 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2726 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002728 case EVENT_CH_SWITCH:
2729 if (!data)
2730 break;
2731 if (!wpa_s->ap_iface) {
2732 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2733 "event in non-AP mode");
2734 break;
2735 }
2736
2737#ifdef CONFIG_AP
2738 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2739 data->ch_switch.ht_enabled,
2740 data->ch_switch.ch_offset);
2741#endif /* CONFIG_AP */
2742 break;
2743 case EVENT_RX_MGMT: {
2744 u16 fc, stype;
2745 const struct ieee80211_mgmt *mgmt;
2746
2747 mgmt = (const struct ieee80211_mgmt *)
2748 data->rx_mgmt.frame;
2749 fc = le_to_host16(mgmt->frame_control);
2750 stype = WLAN_FC_GET_STYPE(fc);
2751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 if (wpa_s->ap_iface == NULL) {
2753#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2755 data->rx_mgmt.frame_len > 24) {
2756 const u8 *src = mgmt->sa;
2757 const u8 *ie = mgmt->u.probe_req.variable;
2758 size_t ie_len = data->rx_mgmt.frame_len -
2759 (mgmt->u.probe_req.variable -
2760 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002761 wpas_p2p_probe_req_rx(
2762 wpa_s, src, mgmt->da,
2763 mgmt->bssid, ie, ie_len,
2764 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 break;
2766 }
2767#endif /* CONFIG_P2P */
2768 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2769 "management frame in non-AP mode");
2770 break;
2771 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002772
2773 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2774 data->rx_mgmt.frame_len > 24) {
2775 const u8 *ie = mgmt->u.probe_req.variable;
2776 size_t ie_len = data->rx_mgmt.frame_len -
2777 (mgmt->u.probe_req.variable -
2778 data->rx_mgmt.frame);
2779
2780 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2781 mgmt->bssid, ie, ie_len,
2782 data->rx_mgmt.ssi_signal);
2783 }
2784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2786 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002787 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788#endif /* CONFIG_AP */
2789 case EVENT_RX_ACTION:
2790 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2791 " Category=%u DataLen=%d freq=%d MHz",
2792 MAC2STR(data->rx_action.sa),
2793 data->rx_action.category, (int) data->rx_action.len,
2794 data->rx_action.freq);
2795#ifdef CONFIG_IEEE80211R
2796 if (data->rx_action.category == WLAN_ACTION_FT) {
2797 ft_rx_action(wpa_s, data->rx_action.data,
2798 data->rx_action.len);
2799 break;
2800 }
2801#endif /* CONFIG_IEEE80211R */
2802#ifdef CONFIG_IEEE80211W
2803#ifdef CONFIG_SME
2804 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2805 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2806 data->rx_action.data,
2807 data->rx_action.len);
2808 break;
2809 }
2810#endif /* CONFIG_SME */
2811#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002812#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813 if (data->rx_action.category == WLAN_ACTION_WNM) {
2814 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2815 break;
2816 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002817#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818#ifdef CONFIG_GAS
2819 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2820 gas_query_rx(wpa_s->gas, data->rx_action.da,
2821 data->rx_action.sa, data->rx_action.bssid,
2822 data->rx_action.data, data->rx_action.len,
2823 data->rx_action.freq) == 0)
2824 break;
2825#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826#ifdef CONFIG_TDLS
2827 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2828 data->rx_action.len >= 4 &&
2829 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2830 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2831 "Response from " MACSTR,
2832 MAC2STR(data->rx_action.sa));
2833 break;
2834 }
2835#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836#ifdef CONFIG_P2P
2837 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2838 data->rx_action.sa,
2839 data->rx_action.bssid,
2840 data->rx_action.category,
2841 data->rx_action.data,
2842 data->rx_action.len, data->rx_action.freq);
2843#endif /* CONFIG_P2P */
2844 break;
2845 case EVENT_RX_PROBE_REQ:
2846 if (data->rx_probe_req.sa == NULL ||
2847 data->rx_probe_req.ie == NULL)
2848 break;
2849#ifdef CONFIG_AP
2850 if (wpa_s->ap_iface) {
2851 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2852 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002853 data->rx_probe_req.da,
2854 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002856 data->rx_probe_req.ie_len,
2857 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 break;
2859 }
2860#endif /* CONFIG_AP */
2861#ifdef CONFIG_P2P
2862 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002863 data->rx_probe_req.da,
2864 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 data->rx_probe_req.ie_len,
2867 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868#endif /* CONFIG_P2P */
2869 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002871#ifdef CONFIG_OFFCHANNEL
2872 offchannel_remain_on_channel_cb(
2873 wpa_s, data->remain_on_channel.freq,
2874 data->remain_on_channel.duration);
2875#endif /* CONFIG_OFFCHANNEL */
2876#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877 wpas_p2p_remain_on_channel_cb(
2878 wpa_s, data->remain_on_channel.freq,
2879 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 break;
2882 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002883#ifdef CONFIG_OFFCHANNEL
2884 offchannel_cancel_remain_on_channel_cb(
2885 wpa_s, data->remain_on_channel.freq);
2886#endif /* CONFIG_OFFCHANNEL */
2887#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 wpas_p2p_cancel_remain_on_channel_cb(
2889 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 case EVENT_P2P_DEV_FOUND: {
2894 struct p2p_peer_info peer_info;
2895
2896 os_memset(&peer_info, 0, sizeof(peer_info));
2897 if (data->p2p_dev_found.dev_addr)
2898 os_memcpy(peer_info.p2p_device_addr,
2899 data->p2p_dev_found.dev_addr, ETH_ALEN);
2900 if (data->p2p_dev_found.pri_dev_type)
2901 os_memcpy(peer_info.pri_dev_type,
2902 data->p2p_dev_found.pri_dev_type,
2903 sizeof(peer_info.pri_dev_type));
2904 if (data->p2p_dev_found.dev_name)
2905 os_strlcpy(peer_info.device_name,
2906 data->p2p_dev_found.dev_name,
2907 sizeof(peer_info.device_name));
2908 peer_info.config_methods = data->p2p_dev_found.config_methods;
2909 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2910 peer_info.group_capab = data->p2p_dev_found.group_capab;
2911
2912 /*
2913 * FIX: new_device=1 is not necessarily correct. We should
2914 * maintain a P2P peer database in wpa_supplicant and update
2915 * this information based on whether the peer is truly new.
2916 */
2917 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2918 break;
2919 }
2920 case EVENT_P2P_GO_NEG_REQ_RX:
2921 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2922 data->p2p_go_neg_req_rx.dev_passwd_id);
2923 break;
2924 case EVENT_P2P_GO_NEG_COMPLETED:
2925 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2926 break;
2927 case EVENT_P2P_PROV_DISC_REQUEST:
2928 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2929 data->p2p_prov_disc_req.config_methods,
2930 data->p2p_prov_disc_req.dev_addr,
2931 data->p2p_prov_disc_req.pri_dev_type,
2932 data->p2p_prov_disc_req.dev_name,
2933 data->p2p_prov_disc_req.supp_config_methods,
2934 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002935 data->p2p_prov_disc_req.group_capab,
2936 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 break;
2938 case EVENT_P2P_PROV_DISC_RESPONSE:
2939 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2940 data->p2p_prov_disc_resp.config_methods);
2941 break;
2942 case EVENT_P2P_SD_REQUEST:
2943 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2944 data->p2p_sd_req.sa,
2945 data->p2p_sd_req.dialog_token,
2946 data->p2p_sd_req.update_indic,
2947 data->p2p_sd_req.tlvs,
2948 data->p2p_sd_req.tlvs_len);
2949 break;
2950 case EVENT_P2P_SD_RESPONSE:
2951 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2952 data->p2p_sd_resp.update_indic,
2953 data->p2p_sd_resp.tlvs,
2954 data->p2p_sd_resp.tlvs_len);
2955 break;
2956#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957 case EVENT_EAPOL_RX:
2958 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2959 data->eapol_rx.data,
2960 data->eapol_rx.data_len);
2961 break;
2962 case EVENT_SIGNAL_CHANGE:
2963 bgscan_notify_signal_change(
2964 wpa_s, data->signal_change.above_threshold,
2965 data->signal_change.current_signal,
2966 data->signal_change.current_noise,
2967 data->signal_change.current_txrate);
2968 break;
2969 case EVENT_INTERFACE_ENABLED:
2970 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2971 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002972 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973#ifdef CONFIG_AP
2974 if (!wpa_s->ap_iface) {
2975 wpa_supplicant_set_state(wpa_s,
2976 WPA_DISCONNECTED);
2977 wpa_supplicant_req_scan(wpa_s, 0, 0);
2978 } else
2979 wpa_supplicant_set_state(wpa_s,
2980 WPA_COMPLETED);
2981#else /* CONFIG_AP */
2982 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2983 wpa_supplicant_req_scan(wpa_s, 0, 0);
2984#endif /* CONFIG_AP */
2985 }
2986 break;
2987 case EVENT_INTERFACE_DISABLED:
2988 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2989 wpa_supplicant_mark_disassoc(wpa_s);
2990 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2991 break;
2992 case EVENT_CHANNEL_LIST_CHANGED:
2993 if (wpa_s->drv_priv == NULL)
2994 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002995
2996 free_hw_features(wpa_s);
2997 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2998 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000#ifdef CONFIG_P2P
3001 wpas_p2p_update_channel_list(wpa_s);
3002#endif /* CONFIG_P2P */
3003 break;
3004 case EVENT_INTERFACE_UNAVAILABLE:
3005#ifdef CONFIG_P2P
3006 wpas_p2p_interface_unavailable(wpa_s);
3007#endif /* CONFIG_P2P */
3008 break;
3009 case EVENT_BEST_CHANNEL:
3010 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3011 "(%d %d %d)",
3012 data->best_chan.freq_24, data->best_chan.freq_5,
3013 data->best_chan.freq_overall);
3014 wpa_s->best_24_freq = data->best_chan.freq_24;
3015 wpa_s->best_5_freq = data->best_chan.freq_5;
3016 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3017#ifdef CONFIG_P2P
3018 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3019 data->best_chan.freq_5,
3020 data->best_chan.freq_overall);
3021#endif /* CONFIG_P2P */
3022 break;
3023 case EVENT_UNPROT_DEAUTH:
3024 wpa_supplicant_event_unprot_deauth(wpa_s,
3025 &data->unprot_deauth);
3026 break;
3027 case EVENT_UNPROT_DISASSOC:
3028 wpa_supplicant_event_unprot_disassoc(wpa_s,
3029 &data->unprot_disassoc);
3030 break;
3031 case EVENT_STATION_LOW_ACK:
3032#ifdef CONFIG_AP
3033 if (wpa_s->ap_iface && data)
3034 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3035 data->low_ack.addr);
3036#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003037#ifdef CONFIG_TDLS
3038 if (data)
3039 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3040#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 break;
3042 case EVENT_IBSS_PEER_LOST:
3043#ifdef CONFIG_IBSS_RSN
3044 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3045#endif /* CONFIG_IBSS_RSN */
3046 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003047 case EVENT_DRIVER_GTK_REKEY:
3048 if (os_memcmp(data->driver_gtk_rekey.bssid,
3049 wpa_s->bssid, ETH_ALEN))
3050 break;
3051 if (!wpa_s->wpa)
3052 break;
3053 wpa_sm_update_replay_ctr(wpa_s->wpa,
3054 data->driver_gtk_rekey.replay_ctr);
3055 break;
3056 case EVENT_SCHED_SCAN_STOPPED:
3057 wpa_s->sched_scanning = 0;
3058 wpa_supplicant_notify_scanning(wpa_s, 0);
3059
3060 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3061 break;
3062
3063 /*
3064 * If we timed out, start a new sched scan to continue
3065 * searching for more SSIDs.
3066 */
3067 if (wpa_s->sched_scan_timed_out)
3068 wpa_supplicant_req_sched_scan(wpa_s);
3069 break;
3070 case EVENT_WPS_BUTTON_PUSHED:
3071#ifdef CONFIG_WPS
3072 wpas_wps_start_pbc(wpa_s, NULL, 0);
3073#endif /* CONFIG_WPS */
3074 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003075 case EVENT_CONNECT_FAILED_REASON:
3076#ifdef CONFIG_AP
3077 if (!wpa_s->ap_iface || !data)
3078 break;
3079 hostapd_event_connect_failed_reason(
3080 wpa_s->ap_iface->bss[0],
3081 data->connect_failed_reason.addr,
3082 data->connect_failed_reason.code);
3083#endif /* CONFIG_AP */
3084 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 default:
3086 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3087 break;
3088 }
3089}