blob: d39216f2d4500980afdb01e43a8a03d129dab05b [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 Shmidt34af3062013-07-11 10:46:32 -070047#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070048static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
49 int new_scan);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070050#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080051
52
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070053static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
54 struct wpa_ssid *ssid)
55{
56 struct os_time now;
57
58 if (ssid == NULL || ssid->disabled_until.sec == 0)
59 return 0;
60
61 os_get_time(&now);
62 if (ssid->disabled_until.sec > now.sec)
63 return ssid->disabled_until.sec - now.sec;
64
65 wpas_clear_temp_disabled(wpa_s, ssid, 0);
66
67 return 0;
68}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70
71static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
72{
73 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070074 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075
76 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
77 return 0;
78
79 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
80 "information");
81 ssid = wpa_supplicant_get_ssid(wpa_s);
82 if (ssid == NULL) {
83 wpa_msg(wpa_s, MSG_INFO,
84 "No network configuration found for the current AP");
85 return -1;
86 }
87
Dmitry Shmidt04949592012-07-19 12:16:46 -070088 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
90 return -1;
91 }
92
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080093 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
94 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
95 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
96 return -1;
97 }
98
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070099 res = wpas_temp_disabled(wpa_s, ssid);
100 if (res > 0) {
101 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
102 "disabled for %d second(s)", res);
103 return -1;
104 }
105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
107 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800108 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 u8 wpa_ie[80];
110 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800111 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
112 wpa_ie, &wpa_ie_len) < 0)
113 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114 } else {
115 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
116 }
117
118 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
119 eapol_sm_invalidate_cached_session(wpa_s->eapol);
120 old_ssid = wpa_s->current_ssid;
121 wpa_s->current_ssid = ssid;
122 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
123 wpa_supplicant_initiate_eapol(wpa_s);
124 if (old_ssid != wpa_s->current_ssid)
125 wpas_notify_network_changed(wpa_s);
126
127 return 0;
128}
129
130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800131void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700132{
133 struct wpa_supplicant *wpa_s = eloop_ctx;
134
135 if (wpa_s->countermeasures) {
136 wpa_s->countermeasures = 0;
137 wpa_drv_set_countermeasures(wpa_s, 0);
138 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
139 wpa_supplicant_req_scan(wpa_s, 0, 0);
140 }
141}
142
143
144void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
145{
146 int bssid_changed;
147
Dmitry Shmidt04949592012-07-19 12:16:46 -0700148 wnm_bss_keep_alive_deinit(wpa_s);
149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150#ifdef CONFIG_IBSS_RSN
151 ibss_rsn_deinit(wpa_s->ibss_rsn);
152 wpa_s->ibss_rsn = NULL;
153#endif /* CONFIG_IBSS_RSN */
154
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700155#ifdef CONFIG_AP
156 wpa_supplicant_ap_deinit(wpa_s);
157#endif /* CONFIG_AP */
158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
160 return;
161
162 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800163#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700164 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800165#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
167 os_memset(wpa_s->bssid, 0, ETH_ALEN);
168 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700169#ifdef CONFIG_SME
170 wpa_s->sme.prev_bssid_set = 0;
171#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800172#ifdef CONFIG_P2P
173 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
174#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 wpa_s->current_bss = NULL;
176 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700177#ifdef CONFIG_IEEE80211R
178#ifdef CONFIG_SME
179 if (wpa_s->sme.ft_ies)
180 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
181#endif /* CONFIG_SME */
182#endif /* CONFIG_IEEE80211R */
183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 if (bssid_changed)
185 wpas_notify_bssid_changed(wpa_s);
186
187 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
188 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
189 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
190 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
191 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700192 wpa_s->current_ssid = NULL;
193 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194}
195
196
197static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
198{
199 struct wpa_ie_data ie;
200 int pmksa_set = -1;
201 size_t i;
202
203 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
204 ie.pmkid == NULL)
205 return;
206
207 for (i = 0; i < ie.num_pmkid; i++) {
208 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
209 ie.pmkid + i * PMKID_LEN,
210 NULL, NULL, 0);
211 if (pmksa_set == 0) {
212 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
213 break;
214 }
215 }
216
217 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
218 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
219}
220
221
222static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
223 union wpa_event_data *data)
224{
225 if (data == NULL) {
226 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
227 "event");
228 return;
229 }
230 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
231 " index=%d preauth=%d",
232 MAC2STR(data->pmkid_candidate.bssid),
233 data->pmkid_candidate.index,
234 data->pmkid_candidate.preauth);
235
236 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
237 data->pmkid_candidate.index,
238 data->pmkid_candidate.preauth);
239}
240
241
242static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
243{
244 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
245 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
246 return 0;
247
248#ifdef IEEE8021X_EAPOL
249 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
250 wpa_s->current_ssid &&
251 !(wpa_s->current_ssid->eapol_flags &
252 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
253 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
254 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
255 * plaintext or static WEP keys). */
256 return 0;
257 }
258#endif /* IEEE8021X_EAPOL */
259
260 return 1;
261}
262
263
264/**
265 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
266 * @wpa_s: pointer to wpa_supplicant data
267 * @ssid: Configuration data for the network
268 * Returns: 0 on success, -1 on failure
269 *
270 * This function is called when starting authentication with a network that is
271 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
272 */
273int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
274 struct wpa_ssid *ssid)
275{
276#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800277#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278 int aka = 0, sim = 0, type;
279
280 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
281 return 0;
282
283 if (ssid->eap.eap_methods == NULL) {
284 sim = 1;
285 aka = 1;
286 } else {
287 struct eap_method_type *eap = ssid->eap.eap_methods;
288 while (eap->vendor != EAP_VENDOR_IETF ||
289 eap->method != EAP_TYPE_NONE) {
290 if (eap->vendor == EAP_VENDOR_IETF) {
291 if (eap->method == EAP_TYPE_SIM)
292 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293 else if (eap->method == EAP_TYPE_AKA ||
294 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 aka = 1;
296 }
297 eap++;
298 }
299 }
300
301 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
302 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
304 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
305 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 aka = 0;
307
308 if (!sim && !aka) {
309 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
310 "use SIM, but neither EAP-SIM nor EAP-AKA are "
311 "enabled");
312 return 0;
313 }
314
315 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
316 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
317 if (sim && aka)
318 type = SCARD_TRY_BOTH;
319 else if (aka)
320 type = SCARD_USIM_ONLY;
321 else
322 type = SCARD_GSM_SIM_ONLY;
323
Dmitry Shmidt04949592012-07-19 12:16:46 -0700324 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 if (wpa_s->scard == NULL) {
326 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
327 "(pcsc-lite)");
328 return -1;
329 }
330 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
331 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333#endif /* IEEE8021X_EAPOL */
334
335 return 0;
336}
337
338
339#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700340static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 struct wpa_ssid *ssid)
342{
343 int i, privacy = 0;
344
345 if (ssid->mixed_cell)
346 return 1;
347
348#ifdef CONFIG_WPS
349 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
350 return 1;
351#endif /* CONFIG_WPS */
352
353 for (i = 0; i < NUM_WEP_KEYS; i++) {
354 if (ssid->wep_key_len[i]) {
355 privacy = 1;
356 break;
357 }
358 }
359#ifdef IEEE8021X_EAPOL
360 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
361 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
362 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
363 privacy = 1;
364#endif /* IEEE8021X_EAPOL */
365
Jouni Malinen75ecf522011-06-27 15:19:46 -0700366 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
367 privacy = 1;
368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 if (bss->caps & IEEE80211_CAP_PRIVACY)
370 return privacy;
371 return !privacy;
372}
373
374
375static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
376 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700377 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378{
379 struct wpa_ie_data ie;
380 int proto_match = 0;
381 const u8 *rsn_ie, *wpa_ie;
382 int ret;
383 int wep_ok;
384
385 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
386 if (ret >= 0)
387 return ret;
388
389 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
390 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
391 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
392 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
393 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
394
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700395 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
397 proto_match++;
398
399 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
400 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
401 "failed");
402 break;
403 }
404
405 if (wep_ok &&
406 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
407 {
408 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
409 "in RSN IE");
410 return 1;
411 }
412
413 if (!(ie.proto & ssid->proto)) {
414 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
415 "mismatch");
416 break;
417 }
418
419 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
420 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
421 "cipher mismatch");
422 break;
423 }
424
425 if (!(ie.group_cipher & ssid->group_cipher)) {
426 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
427 "cipher mismatch");
428 break;
429 }
430
431 if (!(ie.key_mgmt & ssid->key_mgmt)) {
432 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
433 "mismatch");
434 break;
435 }
436
437#ifdef CONFIG_IEEE80211W
438 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800439 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
440 wpa_s->conf->pmf : ssid->ieee80211w) ==
441 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
443 "frame protection");
444 break;
445 }
446#endif /* CONFIG_IEEE80211W */
447
448 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
449 return 1;
450 }
451
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700452 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
454 proto_match++;
455
456 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
457 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
458 "failed");
459 break;
460 }
461
462 if (wep_ok &&
463 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
464 {
465 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
466 "in WPA IE");
467 return 1;
468 }
469
470 if (!(ie.proto & ssid->proto)) {
471 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
472 "mismatch");
473 break;
474 }
475
476 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
477 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
478 "cipher mismatch");
479 break;
480 }
481
482 if (!(ie.group_cipher & ssid->group_cipher)) {
483 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
484 "cipher mismatch");
485 break;
486 }
487
488 if (!(ie.key_mgmt & ssid->key_mgmt)) {
489 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
490 "mismatch");
491 break;
492 }
493
494 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
495 return 1;
496 }
497
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700498 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
499 !rsn_ie) {
500 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
501 return 1;
502 }
503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
505 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
506 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
507 return 0;
508 }
509
510 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
511 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
512 return 1;
513 }
514
515 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
516 "WPA/WPA2");
517
518 return 0;
519}
520
521
522static int freq_allowed(int *freqs, int freq)
523{
524 int i;
525
526 if (freqs == NULL)
527 return 1;
528
529 for (i = 0; freqs[i]; i++)
530 if (freqs[i] == freq)
531 return 1;
532 return 0;
533}
534
535
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800536static int ht_supported(const struct hostapd_hw_modes *mode)
537{
538 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
539 /*
540 * The driver did not indicate whether it supports HT. Assume
541 * it does to avoid connection issues.
542 */
543 return 1;
544 }
545
546 /*
547 * IEEE Std 802.11n-2009 20.1.1:
548 * An HT non-AP STA shall support all EQM rates for one spatial stream.
549 */
550 return mode->mcs_set[0] == 0xff;
551}
552
553
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700554static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800555{
556 const struct hostapd_hw_modes *mode = NULL, *modes;
557 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
558 const u8 *rate_ie;
559 int i, j, k;
560
561 if (bss->freq == 0)
562 return 1; /* Cannot do matching without knowing band */
563
564 modes = wpa_s->hw.modes;
565 if (modes == NULL) {
566 /*
567 * The driver does not provide any additional information
568 * about the utilized hardware, so allow the connection attempt
569 * to continue.
570 */
571 return 1;
572 }
573
574 for (i = 0; i < wpa_s->hw.num_modes; i++) {
575 for (j = 0; j < modes[i].num_channels; j++) {
576 int freq = modes[i].channels[j].freq;
577 if (freq == bss->freq) {
578 if (mode &&
579 mode->mode == HOSTAPD_MODE_IEEE80211G)
580 break; /* do not allow 802.11b replace
581 * 802.11g */
582 mode = &modes[i];
583 break;
584 }
585 }
586 }
587
588 if (mode == NULL)
589 return 0;
590
591 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700592 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800593 if (rate_ie == NULL)
594 continue;
595
596 for (j = 2; j < rate_ie[1] + 2; j++) {
597 int flagged = !!(rate_ie[j] & 0x80);
598 int r = (rate_ie[j] & 0x7f) * 5;
599
600 /*
601 * IEEE Std 802.11n-2009 7.3.2.2:
602 * The new BSS Membership selector value is encoded
603 * like a legacy basic rate, but it is not a rate and
604 * only indicates if the BSS members are required to
605 * support the mandatory features of Clause 20 [HT PHY]
606 * in order to join the BSS.
607 */
608 if (flagged && ((rate_ie[j] & 0x7f) ==
609 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
610 if (!ht_supported(mode)) {
611 wpa_dbg(wpa_s, MSG_DEBUG,
612 " hardware does not support "
613 "HT PHY");
614 return 0;
615 }
616 continue;
617 }
618
619 if (!flagged)
620 continue;
621
622 /* check for legacy basic rates */
623 for (k = 0; k < mode->num_rates; k++) {
624 if (mode->rates[k] == r)
625 break;
626 }
627 if (k == mode->num_rates) {
628 /*
629 * IEEE Std 802.11-2007 7.3.2.2 demands that in
630 * order to join a BSS all required rates
631 * have to be supported by the hardware.
632 */
633 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
634 "not support required rate %d.%d Mbps",
635 r / 10, r % 10);
636 return 0;
637 }
638 }
639 }
640
641 return 1;
642}
643
644
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800645static int bss_is_dmg(struct wpa_bss *bss)
646{
647 return bss->freq > 45000;
648}
649
650
651/*
652 * Test whether BSS is in an ESS.
653 * This is done differently in DMG (60 GHz) and non-DMG bands
654 */
655static int bss_is_ess(struct wpa_bss *bss)
656{
657 if (bss_is_dmg(bss)) {
658 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
659 IEEE80211_CAP_DMG_AP;
660 }
661
662 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
663 IEEE80211_CAP_ESS);
664}
665
666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700668 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 struct wpa_ssid *group)
670{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700671 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 int wpa;
673 struct wpa_blacklist *e;
674 const u8 *ie;
675 struct wpa_ssid *ssid;
676
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700677 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 wpa_ie_len = ie ? ie[1] : 0;
679
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700680 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 rsn_ie_len = ie ? ie[1] : 0;
682
683 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
684 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700685 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700687 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688
689 e = wpa_blacklist_get(wpa_s, bss->bssid);
690 if (e) {
691 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700692 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 /*
694 * When only a single network is enabled, we can
695 * trigger blacklisting on the first failure. This
696 * should not be done with multiple enabled networks to
697 * avoid getting forced to move into a worse ESS on
698 * single error if there are no other BSSes of the
699 * current ESS.
700 */
701 limit = 0;
702 }
703 if (e->count > limit) {
704 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
705 "(count=%d limit=%d)", e->count, limit);
706 return NULL;
707 }
708 }
709
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700710 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
712 return NULL;
713 }
714
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800715 if (disallowed_bssid(wpa_s, bss->bssid)) {
716 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
717 return NULL;
718 }
719
720 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
721 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
722 return NULL;
723 }
724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
726
727 for (ssid = group; ssid; ssid = ssid->pnext) {
728 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730
Dmitry Shmidt04949592012-07-19 12:16:46 -0700731 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
733 continue;
734 }
735
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700736 res = wpas_temp_disabled(wpa_s, ssid);
737 if (res > 0) {
738 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
739 "temporarily for %d second(s)", res);
740 continue;
741 }
742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743#ifdef CONFIG_WPS
744 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
745 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
746 "(WPS)");
747 continue;
748 }
749
750 if (wpa && ssid->ssid_len == 0 &&
751 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
752 check_ssid = 0;
753
754 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
755 /* Only allow wildcard SSID match if an AP
756 * advertises active WPS operation that matches
757 * with our mode. */
758 check_ssid = 1;
759 if (ssid->ssid_len == 0 &&
760 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
761 check_ssid = 0;
762 }
763#endif /* CONFIG_WPS */
764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 if (ssid->bssid_set && ssid->ssid_len == 0 &&
766 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
767 check_ssid = 0;
768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700770 (bss->ssid_len != ssid->ssid_len ||
771 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
773 continue;
774 }
775
776 if (ssid->bssid_set &&
777 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
778 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
779 continue;
780 }
781
782 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
783 continue;
784
785 if (!wpa &&
786 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
787 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
788 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
789 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
790 "not allowed");
791 continue;
792 }
793
Jouni Malinen75ecf522011-06-27 15:19:46 -0700794 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
796 "mismatch");
797 continue;
798 }
799
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800800 if (!bss_is_ess(bss)) {
801 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 continue;
803 }
804
805 if (!freq_allowed(ssid->freq_list, bss->freq)) {
806 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
807 "allowed");
808 continue;
809 }
810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800811 if (!rate_match(wpa_s, bss)) {
812 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
813 "not match");
814 continue;
815 }
816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817#ifdef CONFIG_P2P
818 /*
819 * TODO: skip the AP if its P2P IE has Group Formation
820 * bit set in the P2P Group Capability Bitmap and we
821 * are not in Group Formation with that device.
822 */
823#endif /* CONFIG_P2P */
824
825 /* Matching configuration found */
826 return ssid;
827 }
828
829 /* No matching configuration found */
830 return NULL;
831}
832
833
834static struct wpa_bss *
835wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 struct wpa_ssid *group,
837 struct wpa_ssid **selected_ssid)
838{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700839 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840
841 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
842 group->priority);
843
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700844 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
845 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
847 if (!*selected_ssid)
848 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
850 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700851 MAC2STR(bss->bssid),
852 wpa_ssid_txt(bss->ssid, bss->ssid_len));
853 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 }
855
856 return NULL;
857}
858
859
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700860struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
861 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862{
863 struct wpa_bss *selected = NULL;
864 int prio;
865
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700866 if (wpa_s->last_scan_res == NULL ||
867 wpa_s->last_scan_res_used == 0)
868 return NULL; /* no scan results from last update */
869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 while (selected == NULL) {
871 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
872 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700873 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874 selected_ssid);
875 if (selected)
876 break;
877 }
878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800879 if (selected == NULL && wpa_s->blacklist &&
880 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
882 "blacklist and try again");
883 wpa_blacklist_clear(wpa_s);
884 wpa_s->blacklist_cleared++;
885 } else if (selected == NULL)
886 break;
887 }
888
889 return selected;
890}
891
892
893static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
894 int timeout_sec, int timeout_usec)
895{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700896 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 /*
898 * No networks are enabled; short-circuit request so
899 * we don't wait timeout seconds before transitioning
900 * to INACTIVE state.
901 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700902 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
903 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700905#ifdef CONFIG_P2P
906 wpa_s->sta_scan_pending = 0;
907#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 return;
909 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800910
911 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
913}
914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700916int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800917 struct wpa_bss *selected,
918 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919{
920 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
921 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
922 "PBC session overlap");
923#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700925 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926#endif /* CONFIG_P2P */
927
928#ifdef CONFIG_WPS
929 wpas_wps_cancel(wpa_s);
930#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700931 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 }
933
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700934 wpa_msg(wpa_s, MSG_DEBUG,
935 "Considering connect request: reassociate: %d selected: "
936 MACSTR " bssid: " MACSTR " pending: " MACSTR
937 " wpa_state: %s ssid=%p current_ssid=%p",
938 wpa_s->reassociate, MAC2STR(selected->bssid),
939 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
940 wpa_supplicant_state_txt(wpa_s->wpa_state),
941 ssid, wpa_s->current_ssid);
942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 /*
944 * Do not trigger new association unless the BSSID has changed or if
945 * reassociation is requested. If we are in process of associating with
946 * the selected BSSID, do not trigger new attempt.
947 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
950 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
951 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700952 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
953 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
954 0) ||
955 (is_zero_ether_addr(wpa_s->pending_bssid) &&
956 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
958 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700959 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700961 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
962 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 wpa_supplicant_associate(wpa_s, selected, ssid);
964 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700965 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
966 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800968
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700969 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970}
971
972
973static struct wpa_ssid *
974wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
975{
976 int prio;
977 struct wpa_ssid *ssid;
978
979 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
980 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
981 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700982 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 continue;
984 if (ssid->mode == IEEE80211_MODE_IBSS ||
985 ssid->mode == IEEE80211_MODE_AP)
986 return ssid;
987 }
988 }
989 return NULL;
990}
991
992
993/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
994 * on BSS added and BSS changed events */
995static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300996 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300998 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999
1000 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1001 return;
1002
Jouni Malinen87fd2792011-05-16 18:35:42 +03001003 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005
Jouni Malinen87fd2792011-05-16 18:35:42 +03001006 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 if (ssid == NULL)
1008 continue;
1009
Jouni Malinen87fd2792011-05-16 18:35:42 +03001010 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 if (rsn == NULL)
1012 continue;
1013
Jouni Malinen87fd2792011-05-16 18:35:42 +03001014 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 }
1016
1017}
1018
1019
1020static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1021 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001022 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001024 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 int min_diff;
1026
1027 if (wpa_s->reassociate)
1028 return 1; /* explicit request to reassociate */
1029 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1030 return 1; /* we are not associated; continue */
1031 if (wpa_s->current_ssid == NULL)
1032 return 1; /* unknown current SSID */
1033 if (wpa_s->current_ssid != ssid)
1034 return 1; /* different network block */
1035
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001036 if (wpas_driver_bss_selection(wpa_s))
1037 return 0; /* Driver-based roaming */
1038
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001039 if (wpa_s->current_ssid->ssid)
1040 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1041 wpa_s->current_ssid->ssid,
1042 wpa_s->current_ssid->ssid_len);
1043 if (!current_bss)
1044 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045
1046 if (!current_bss)
1047 return 1; /* current BSS not seen in scan results */
1048
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001049 if (current_bss == selected)
1050 return 0;
1051
1052 if (selected->last_update_idx > current_bss->last_update_idx)
1053 return 1; /* current BSS not seen in the last scan */
1054
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001055#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1057 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1058 MAC2STR(current_bss->bssid), current_bss->level);
1059 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1060 MAC2STR(selected->bssid), selected->level);
1061
1062 if (wpa_s->current_ssid->bssid_set &&
1063 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1064 0) {
1065 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1066 "has preferred BSSID");
1067 return 1;
1068 }
1069
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001070 if (current_bss->level < 0 && current_bss->level > selected->level) {
1071 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1072 "signal level");
1073 return 0;
1074 }
1075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 min_diff = 2;
1077 if (current_bss->level < 0) {
1078 if (current_bss->level < -85)
1079 min_diff = 1;
1080 else if (current_bss->level < -80)
1081 min_diff = 2;
1082 else if (current_bss->level < -75)
1083 min_diff = 3;
1084 else if (current_bss->level < -70)
1085 min_diff = 4;
1086 else
1087 min_diff = 5;
1088 }
1089 if (abs(current_bss->level - selected->level) < min_diff) {
1090 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1091 "in signal level");
1092 return 0;
1093 }
1094
1095 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001097 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001098#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099}
1100
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001101
Jouni Malinen89ca7022012-09-14 13:03:12 -07001102/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001103 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001104static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001105 union wpa_event_data *data,
1106 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 struct wpa_scan_results *scan_res;
1109 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001110#ifndef CONFIG_NO_RANDOM_POOL
1111 size_t i, num;
1112#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113
1114#ifdef CONFIG_AP
1115 if (wpa_s->ap_iface)
1116 ap = 1;
1117#endif /* CONFIG_AP */
1118
1119 wpa_supplicant_notify_scanning(wpa_s, 0);
1120
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001121#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001122 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001123 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001124 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1125 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001126 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001127 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1128 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1129 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001130 wpa_s->sta_scan_pending = 1;
1131 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001132 return -1;
1133 }
1134 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001135 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001136#endif /* CONFIG_P2P */
1137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1139 data ? &data->scan_info :
1140 NULL, 1);
1141 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001142 if (wpa_s->conf->ap_scan == 2 || ap ||
1143 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001145 if (!own_request)
1146 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1148 "scanning again");
1149 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1150 return -1;
1151 }
1152
1153#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 num = scan_res->num;
1155 if (num > 10)
1156 num = 10;
1157 for (i = 0; i < num; i++) {
1158 u8 buf[5];
1159 struct wpa_scan_res *res = scan_res->res[i];
1160 buf[0] = res->bssid[5];
1161 buf[1] = res->qual & 0xff;
1162 buf[2] = res->noise & 0xff;
1163 buf[3] = res->level & 0xff;
1164 buf[4] = res->tsf & 0xff;
1165 random_add_randomness(buf, sizeof(buf));
1166 }
1167#endif /* CONFIG_NO_RANDOM_POOL */
1168
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001169 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1171 struct wpa_scan_results *scan_res);
1172
1173 scan_res_handler = wpa_s->scan_res_handler;
1174 wpa_s->scan_res_handler = NULL;
1175 scan_res_handler(wpa_s, scan_res);
1176
1177 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001178 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 }
1180
1181 if (ap) {
1182 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1183#ifdef CONFIG_AP
1184 if (wpa_s->ap_iface->scan_cb)
1185 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1186#endif /* CONFIG_AP */
1187 wpa_scan_results_free(scan_res);
1188 return 0;
1189 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001190
Dmitry Shmidt04949592012-07-19 12:16:46 -07001191 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1192 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1193 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194
1195 wpas_notify_scan_done(wpa_s, 1);
1196
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197 if (sme_proc_obss_scan(wpa_s) > 0) {
1198 wpa_scan_results_free(scan_res);
1199 return 0;
1200 }
1201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1203 wpa_scan_results_free(scan_res);
1204 return 0;
1205 }
1206
Dmitry Shmidt04949592012-07-19 12:16:46 -07001207 if (autoscan_notify_scan(wpa_s, scan_res)) {
1208 wpa_scan_results_free(scan_res);
1209 return 0;
1210 }
1211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 if (wpa_s->disconnected) {
1213 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1214 wpa_scan_results_free(scan_res);
1215 return 0;
1216 }
1217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001218 if (!wpas_driver_bss_selection(wpa_s) &&
1219 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_scan_results_free(scan_res);
1221 return 0;
1222 }
1223
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001224 wpas_wps_update_ap_info(wpa_s, scan_res);
1225
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001226 wpa_scan_results_free(scan_res);
1227
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001228 return wpas_select_network_from_last_scan(wpa_s, 1);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001229}
1230
1231
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001232static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1233 int new_scan)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001234{
1235 struct wpa_bss *selected;
1236 struct wpa_ssid *ssid = NULL;
1237
1238 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239
1240 if (selected) {
1241 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001242 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001243 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001244 if (new_scan)
1245 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247 }
1248
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001249 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001251 return -1;
1252 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001253 if (new_scan)
1254 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001255 /*
1256 * Do not notify other virtual radios of scan results since we do not
1257 * want them to start other associations at the same time.
1258 */
1259 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1262 ssid = wpa_supplicant_pick_new_network(wpa_s);
1263 if (ssid) {
1264 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1265 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001266 if (new_scan)
1267 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 } else {
1269 int timeout_sec = wpa_s->scan_interval;
1270 int timeout_usec = 0;
1271#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001272 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1273 return 0;
1274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 if (wpa_s->p2p_in_provisioning) {
1276 /*
1277 * Use shorter wait during P2P Provisioning
1278 * state to speed up group formation.
1279 */
1280 timeout_sec = 0;
1281 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001282 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1283 timeout_usec);
1284 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 }
1286#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001287#ifdef CONFIG_INTERWORKING
1288 if (wpa_s->conf->auto_interworking &&
1289 wpa_s->conf->interworking &&
1290 wpa_s->conf->cred) {
1291 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1292 "start ANQP fetch since no matching "
1293 "networks found");
1294 wpa_s->network_select = 1;
1295 wpa_s->auto_network_select = 1;
1296 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001297 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001298 }
1299#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300 if (wpa_supplicant_req_sched_scan(wpa_s))
1301 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1302 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 }
1304 }
1305 return 0;
1306}
1307
1308
1309static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1310 union wpa_event_data *data)
1311{
1312 const char *rn, *rn2;
1313 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001314
1315 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 /*
1317 * If no scan results could be fetched, then no need to
1318 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001319 * this scan. Similarly, if scan results started a new operation on this
1320 * interface, do not notify other interfaces to avoid concurrent
1321 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 */
1323 return;
1324 }
1325
1326 /*
1327 * Check other interfaces to see if they have the same radio-name. If
1328 * so, they get updated with this same scan info.
1329 */
1330 if (!wpa_s->driver->get_radio_name)
1331 return;
1332
1333 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1334 if (rn == NULL || rn[0] == '\0')
1335 return;
1336
1337 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1338 "sharing same radio (%s) in event_scan_results", rn);
1339
1340 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1341 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1342 continue;
1343
1344 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1345 if (rn2 && os_strcmp(rn, rn2) == 0) {
1346 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1347 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001348 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 }
1350 }
1351}
1352
1353#endif /* CONFIG_NO_SCAN_PROCESSING */
1354
1355
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001356int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1357{
1358#ifdef CONFIG_NO_SCAN_PROCESSING
1359 return -1;
1360#else /* CONFIG_NO_SCAN_PROCESSING */
1361 struct os_time now;
1362
1363 if (wpa_s->last_scan_res_used <= 0)
1364 return -1;
1365
1366 os_get_time(&now);
1367 if (now.sec - wpa_s->last_scan.sec > 5) {
1368 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1369 return -1;
1370 }
1371
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001372 return wpas_select_network_from_last_scan(wpa_s, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001373#endif /* CONFIG_NO_SCAN_PROCESSING */
1374}
1375
Dmitry Shmidt04949592012-07-19 12:16:46 -07001376#ifdef CONFIG_WNM
1377
1378static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1379{
1380 struct wpa_supplicant *wpa_s = eloop_ctx;
1381
1382 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1383 return;
1384
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001385 if (!wpa_s->no_keep_alive) {
1386 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1387 MAC2STR(wpa_s->bssid));
1388 /* TODO: could skip this if normal data traffic has been sent */
1389 /* TODO: Consider using some more appropriate data frame for
1390 * this */
1391 if (wpa_s->l2)
1392 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1393 (u8 *) "", 0);
1394 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001395
1396#ifdef CONFIG_SME
1397 if (wpa_s->sme.bss_max_idle_period) {
1398 unsigned int msec;
1399 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1400 if (msec > 100)
1401 msec -= 100;
1402 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1403 wnm_bss_keep_alive, wpa_s, NULL);
1404 }
1405#endif /* CONFIG_SME */
1406}
1407
1408
1409static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1410 const u8 *ies, size_t ies_len)
1411{
1412 struct ieee802_11_elems elems;
1413
1414 if (ies == NULL)
1415 return;
1416
1417 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1418 return;
1419
1420#ifdef CONFIG_SME
1421 if (elems.bss_max_idle_period) {
1422 unsigned int msec;
1423 wpa_s->sme.bss_max_idle_period =
1424 WPA_GET_LE16(elems.bss_max_idle_period);
1425 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1426 "TU)%s", wpa_s->sme.bss_max_idle_period,
1427 (elems.bss_max_idle_period[2] & 0x01) ?
1428 " (protected keep-live required)" : "");
1429 if (wpa_s->sme.bss_max_idle_period == 0)
1430 wpa_s->sme.bss_max_idle_period = 1;
1431 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1432 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1433 /* msec times 1000 */
1434 msec = wpa_s->sme.bss_max_idle_period * 1024;
1435 if (msec > 100)
1436 msec -= 100;
1437 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1438 wnm_bss_keep_alive, wpa_s,
1439 NULL);
1440 }
1441 }
1442#endif /* CONFIG_SME */
1443}
1444
1445#endif /* CONFIG_WNM */
1446
1447
1448void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1449{
1450#ifdef CONFIG_WNM
1451 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1452#endif /* CONFIG_WNM */
1453}
1454
1455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1457 union wpa_event_data *data)
1458{
1459 int l, len, found = 0, wpa_found, rsn_found;
1460 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001461#ifdef CONFIG_IEEE80211R
1462 u8 bssid[ETH_ALEN];
1463#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464
1465 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1466 if (data->assoc_info.req_ies)
1467 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1468 data->assoc_info.req_ies_len);
1469 if (data->assoc_info.resp_ies) {
1470 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1471 data->assoc_info.resp_ies_len);
1472#ifdef CONFIG_TDLS
1473 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1474 data->assoc_info.resp_ies_len);
1475#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001476#ifdef CONFIG_WNM
1477 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1478 data->assoc_info.resp_ies_len);
1479#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 }
1481 if (data->assoc_info.beacon_ies)
1482 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1483 data->assoc_info.beacon_ies,
1484 data->assoc_info.beacon_ies_len);
1485 if (data->assoc_info.freq)
1486 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1487 data->assoc_info.freq);
1488
1489 p = data->assoc_info.req_ies;
1490 l = data->assoc_info.req_ies_len;
1491
1492 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1493 while (p && l >= 2) {
1494 len = p[1] + 2;
1495 if (len > l) {
1496 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1497 p, l);
1498 break;
1499 }
1500 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1501 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1502 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1503 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1504 break;
1505 found = 1;
1506 wpa_find_assoc_pmkid(wpa_s);
1507 break;
1508 }
1509 l -= len;
1510 p += len;
1511 }
1512 if (!found && data->assoc_info.req_ies)
1513 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1514
1515#ifdef CONFIG_IEEE80211R
1516#ifdef CONFIG_SME
1517 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1519 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1520 data->assoc_info.resp_ies,
1521 data->assoc_info.resp_ies_len,
1522 bssid) < 0) {
1523 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1524 "Reassociation Response failed");
1525 wpa_supplicant_deauthenticate(
1526 wpa_s, WLAN_REASON_INVALID_IE);
1527 return -1;
1528 }
1529 }
1530
1531 p = data->assoc_info.resp_ies;
1532 l = data->assoc_info.resp_ies_len;
1533
1534#ifdef CONFIG_WPS_STRICT
1535 if (p && wpa_s->current_ssid &&
1536 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1537 struct wpabuf *wps;
1538 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1539 if (wps == NULL) {
1540 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1541 "include WPS IE in (Re)Association Response");
1542 return -1;
1543 }
1544
1545 if (wps_validate_assoc_resp(wps) < 0) {
1546 wpabuf_free(wps);
1547 wpa_supplicant_deauthenticate(
1548 wpa_s, WLAN_REASON_INVALID_IE);
1549 return -1;
1550 }
1551 wpabuf_free(wps);
1552 }
1553#endif /* CONFIG_WPS_STRICT */
1554
1555 /* Go through the IEs and make a copy of the MDIE, if present. */
1556 while (p && l >= 2) {
1557 len = p[1] + 2;
1558 if (len > l) {
1559 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1560 p, l);
1561 break;
1562 }
1563 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1564 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1565 wpa_s->sme.ft_used = 1;
1566 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1567 MOBILITY_DOMAIN_ID_LEN);
1568 break;
1569 }
1570 l -= len;
1571 p += len;
1572 }
1573#endif /* CONFIG_SME */
1574
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001575 /* Process FT when SME is in the driver */
1576 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1577 wpa_ft_is_completed(wpa_s->wpa)) {
1578 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1579 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1580 data->assoc_info.resp_ies,
1581 data->assoc_info.resp_ies_len,
1582 bssid) < 0) {
1583 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1584 "Reassociation Response failed");
1585 wpa_supplicant_deauthenticate(
1586 wpa_s, WLAN_REASON_INVALID_IE);
1587 return -1;
1588 }
1589 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1590 }
1591
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1593 data->assoc_info.resp_ies_len);
1594#endif /* CONFIG_IEEE80211R */
1595
1596 /* WPA/RSN IE from Beacon/ProbeResp */
1597 p = data->assoc_info.beacon_ies;
1598 l = data->assoc_info.beacon_ies_len;
1599
1600 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1601 */
1602 wpa_found = rsn_found = 0;
1603 while (p && l >= 2) {
1604 len = p[1] + 2;
1605 if (len > l) {
1606 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1607 p, l);
1608 break;
1609 }
1610 if (!wpa_found &&
1611 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1612 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1613 wpa_found = 1;
1614 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1615 }
1616
1617 if (!rsn_found &&
1618 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1619 rsn_found = 1;
1620 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1621 }
1622
1623 l -= len;
1624 p += len;
1625 }
1626
1627 if (!wpa_found && data->assoc_info.beacon_ies)
1628 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1629 if (!rsn_found && data->assoc_info.beacon_ies)
1630 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1631 if (wpa_found || rsn_found)
1632 wpa_s->ap_ies_from_associnfo = 1;
1633
Jouni Malinen87fd2792011-05-16 18:35:42 +03001634 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1635 wpa_s->assoc_freq != data->assoc_info.freq) {
1636 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1637 "%u to %u MHz",
1638 wpa_s->assoc_freq, data->assoc_info.freq);
1639 wpa_supplicant_update_scan_results(wpa_s);
1640 }
1641
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 wpa_s->assoc_freq = data->assoc_info.freq;
1643
1644 return 0;
1645}
1646
1647
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001648static struct wpa_bss * wpa_supplicant_get_new_bss(
1649 struct wpa_supplicant *wpa_s, const u8 *bssid)
1650{
1651 struct wpa_bss *bss = NULL;
1652 struct wpa_ssid *ssid = wpa_s->current_ssid;
1653
1654 if (ssid->ssid_len > 0)
1655 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1656 if (!bss)
1657 bss = wpa_bss_get_bssid(wpa_s, bssid);
1658
1659 return bss;
1660}
1661
1662
1663static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1664{
1665 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1666
1667 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1668 return -1;
1669
1670 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1671 return 0;
1672
1673 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1674 WPA_IE_VENDOR_TYPE);
1675 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1676
1677 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1678 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1679 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1680 bss_rsn ? 2 + bss_rsn[1] : 0))
1681 return -1;
1682
1683 return 0;
1684}
1685
1686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1688 union wpa_event_data *data)
1689{
1690 u8 bssid[ETH_ALEN];
1691 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692
1693#ifdef CONFIG_AP
1694 if (wpa_s->ap_iface) {
1695 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1696 data->assoc_info.addr,
1697 data->assoc_info.req_ies,
1698 data->assoc_info.req_ies_len,
1699 data->assoc_info.reassoc);
1700 return;
1701 }
1702#endif /* CONFIG_AP */
1703
1704 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1705 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1706 return;
1707
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001708 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1709 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001710 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001711 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1712 return;
1713 }
1714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001715 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001716 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1718 MACSTR, MAC2STR(bssid));
1719 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1721 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001722 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723
1724 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1725 wpa_clear_keys(wpa_s, bssid);
1726 }
1727 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001728 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1730 return;
1731 }
1732 if (wpa_s->current_ssid) {
1733 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001734
1735 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1736 if (!bss) {
1737 wpa_supplicant_update_scan_results(wpa_s);
1738
1739 /* Get the BSS from the new scan results */
1740 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1741 }
1742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 if (bss)
1744 wpa_s->current_bss = bss;
1745 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001746
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001747#ifdef ANDROID
1748 if (wpa_s->conf->ap_scan == 1) {
1749#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001750 if (wpa_s->conf->ap_scan == 1 &&
1751 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001752#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001753 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1754 wpa_msg(wpa_s, MSG_WARNING,
1755 "WPA/RSN IEs not updated");
1756 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757 }
1758
1759#ifdef CONFIG_SME
1760 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1761 wpa_s->sme.prev_bssid_set = 1;
1762#endif /* CONFIG_SME */
1763
1764 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1765 if (wpa_s->current_ssid) {
1766 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1767 * initialized before association, but for other modes,
1768 * initialize PC/SC here, if the current configuration needs
1769 * smartcard or SIM/USIM. */
1770 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1771 }
1772 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1773 if (wpa_s->l2)
1774 l2_packet_notify_auth_start(wpa_s->l2);
1775
1776 /*
1777 * Set portEnabled first to FALSE in order to get EAP state machine out
1778 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1779 * state machine may transit to AUTHENTICATING state based on obsolete
1780 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1781 * AUTHENTICATED without ever giving chance to EAP state machine to
1782 * reset the state.
1783 */
1784 if (!ft_completed) {
1785 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1786 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1787 }
1788 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1789 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1790 /* 802.1X::portControl = Auto */
1791 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1792 wpa_s->eapol_received = 0;
1793 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1794 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1795 (wpa_s->current_ssid &&
1796 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001797 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1798 (wpa_s->drv_flags &
1799 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1800 /*
1801 * Set the key after having received joined-IBSS event
1802 * from the driver.
1803 */
1804 wpa_supplicant_set_wpa_none_key(wpa_s,
1805 wpa_s->current_ssid);
1806 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807 wpa_supplicant_cancel_auth_timeout(wpa_s);
1808 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1809 } else if (!ft_completed) {
1810 /* Timeout for receiving the first EAPOL packet */
1811 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1812 }
1813 wpa_supplicant_cancel_scan(wpa_s);
1814
1815 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1816 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1817 /*
1818 * We are done; the driver will take care of RSN 4-way
1819 * handshake.
1820 */
1821 wpa_supplicant_cancel_auth_timeout(wpa_s);
1822 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1823 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1824 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1825 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1826 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1827 /*
1828 * The driver will take care of RSN 4-way handshake, so we need
1829 * to allow EAPOL supplicant to complete its work without
1830 * waiting for WPA supplicant.
1831 */
1832 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1833 } else if (ft_completed) {
1834 /*
1835 * FT protocol completed - make sure EAPOL state machine ends
1836 * up in authenticated.
1837 */
1838 wpa_supplicant_cancel_auth_timeout(wpa_s);
1839 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1840 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1841 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1842 }
1843
Jouni Malinena05074c2012-12-21 21:35:35 +02001844 wpa_s->last_eapol_matches_bssid = 0;
1845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 if (wpa_s->pending_eapol_rx) {
1847 struct os_time now, age;
1848 os_get_time(&now);
1849 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1850 if (age.sec == 0 && age.usec < 100000 &&
1851 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1852 0) {
1853 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1854 "frame that was received just before "
1855 "association notification");
1856 wpa_supplicant_rx_eapol(
1857 wpa_s, wpa_s->pending_eapol_rx_src,
1858 wpabuf_head(wpa_s->pending_eapol_rx),
1859 wpabuf_len(wpa_s->pending_eapol_rx));
1860 }
1861 wpabuf_free(wpa_s->pending_eapol_rx);
1862 wpa_s->pending_eapol_rx = NULL;
1863 }
1864
1865 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1866 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001867 wpa_s->current_ssid &&
1868 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 /* Set static WEP keys again */
1870 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1871 }
1872
1873#ifdef CONFIG_IBSS_RSN
1874 if (wpa_s->current_ssid &&
1875 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1876 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1877 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1878 wpa_s->ibss_rsn == NULL) {
1879 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1880 if (!wpa_s->ibss_rsn) {
1881 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1882 wpa_supplicant_deauthenticate(
1883 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1884 return;
1885 }
1886
1887 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1888 }
1889#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001890
1891 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892}
1893
1894
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895static int disconnect_reason_recoverable(u16 reason_code)
1896{
1897 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1898 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1899 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1900}
1901
1902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001904 u16 reason_code,
1905 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906{
1907 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001908
1909 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1910 /*
1911 * At least Host AP driver and a Prism3 card seemed to be
1912 * generating streams of disconnected events when configuring
1913 * IBSS for WPA-None. Ignore them for now.
1914 */
1915 return;
1916 }
1917
1918 bssid = wpa_s->bssid;
1919 if (is_zero_ether_addr(bssid))
1920 bssid = wpa_s->pending_bssid;
1921
1922 if (!is_zero_ether_addr(bssid) ||
1923 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1924 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1925 " reason=%d%s",
1926 MAC2STR(bssid), reason_code,
1927 locally_generated ? " locally_generated=1" : "");
1928 }
1929}
1930
1931
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001932static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1933 int locally_generated)
1934{
1935 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1936 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1937 return 0; /* Not in 4-way handshake with PSK */
1938
1939 /*
1940 * It looks like connection was lost while trying to go through PSK
1941 * 4-way handshake. Filter out known disconnection cases that are caused
1942 * by something else than PSK mismatch to avoid confusing reports.
1943 */
1944
1945 if (locally_generated) {
1946 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1947 return 0;
1948 }
1949
1950 return 1;
1951}
1952
1953
Jouni Malinen2b89da82012-08-31 22:04:41 +03001954static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1955 u16 reason_code,
1956 int locally_generated)
1957{
1958 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 int authenticating;
1960 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001961 struct wpa_bss *fast_reconnect = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001962#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001963 struct wpa_ssid *fast_reconnect_ssid = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001964#endif /* CONFIG_NO_SCAN_PROCESSING */
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001965 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966
1967 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1968 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1969
1970 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1971 /*
1972 * At least Host AP driver and a Prism3 card seemed to be
1973 * generating streams of disconnected events when configuring
1974 * IBSS for WPA-None. Ignore them for now.
1975 */
1976 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1977 "IBSS/WPA-None mode");
1978 return;
1979 }
1980
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001981 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1983 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001984 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07001986 if (!wpa_s->disconnected &&
1987 (!wpa_s->auto_reconnect_disabled ||
1988 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001989 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1990 "reconnect (wps=%d wpa_state=%d)",
1991 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1992 wpa_s->wpa_state);
1993 if (wpa_s->wpa_state == WPA_COMPLETED &&
1994 wpa_s->current_ssid &&
1995 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001996 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001997 disconnect_reason_recoverable(reason_code)) {
1998 /*
1999 * It looks like the AP has dropped association with
2000 * us, but could allow us to get back in. Try to
2001 * reconnect to the same BSS without full scan to save
2002 * time for some common cases.
2003 */
2004 fast_reconnect = wpa_s->current_bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002005#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002006 fast_reconnect_ssid = wpa_s->current_ssid;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002007#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002008 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002009#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07002010 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002011#else
2012 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2013#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002014 else
2015 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2016 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002018 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 "try to re-connect");
2020 wpa_s->reassociate = 0;
2021 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002022 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 }
2024 bssid = wpa_s->bssid;
2025 if (is_zero_ether_addr(bssid))
2026 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002027 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2028 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002030 if (locally_generated)
2031 wpa_s->disconnect_reason = -reason_code;
2032 else
2033 wpa_s->disconnect_reason = reason_code;
2034 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2036 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2037 wpa_s->keys_cleared = 0;
2038 wpa_clear_keys(wpa_s, wpa_s->bssid);
2039 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002040 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 wpa_supplicant_mark_disassoc(wpa_s);
2042
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002043 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002045 wpa_s->current_ssid = last_ssid;
2046 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002047
2048 if (fast_reconnect) {
2049#ifndef CONFIG_NO_SCAN_PROCESSING
2050 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2051 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2052 fast_reconnect_ssid) < 0) {
2053 /* Recover through full scan */
2054 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2055 }
2056#endif /* CONFIG_NO_SCAN_PROCESSING */
2057 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058}
2059
2060
2061#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063{
2064 struct wpa_supplicant *wpa_s = eloop_ctx;
2065
2066 if (!wpa_s->pending_mic_error_report)
2067 return;
2068
2069 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2070 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2071 wpa_s->pending_mic_error_report = 0;
2072}
2073#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2074
2075
2076static void
2077wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2078 union wpa_event_data *data)
2079{
2080 int pairwise;
2081 struct os_time t;
2082
2083 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2084 pairwise = (data && data->michael_mic_failure.unicast);
2085 os_get_time(&t);
2086 if ((wpa_s->last_michael_mic_error &&
2087 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2088 wpa_s->pending_mic_error_report) {
2089 if (wpa_s->pending_mic_error_report) {
2090 /*
2091 * Send the pending MIC error report immediately since
2092 * we are going to start countermeasures and AP better
2093 * do the same.
2094 */
2095 wpa_sm_key_request(wpa_s->wpa, 1,
2096 wpa_s->pending_mic_error_pairwise);
2097 }
2098
2099 /* Send the new MIC error report immediately since we are going
2100 * to start countermeasures and AP better do the same.
2101 */
2102 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2103
2104 /* initialize countermeasures */
2105 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002106
2107 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2110
2111 /*
2112 * Need to wait for completion of request frame. We do not get
2113 * any callback for the message completion, so just wait a
2114 * short while and hope for the best. */
2115 os_sleep(0, 10000);
2116
2117 wpa_drv_set_countermeasures(wpa_s, 1);
2118 wpa_supplicant_deauthenticate(wpa_s,
2119 WLAN_REASON_MICHAEL_MIC_FAILURE);
2120 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2121 wpa_s, NULL);
2122 eloop_register_timeout(60, 0,
2123 wpa_supplicant_stop_countermeasures,
2124 wpa_s, NULL);
2125 /* TODO: mark the AP rejected for 60 second. STA is
2126 * allowed to associate with another AP.. */
2127 } else {
2128#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2129 if (wpa_s->mic_errors_seen) {
2130 /*
2131 * Reduce the effectiveness of Michael MIC error
2132 * reports as a means for attacking against TKIP if
2133 * more than one MIC failure is noticed with the same
2134 * PTK. We delay the transmission of the reports by a
2135 * random time between 0 and 60 seconds in order to
2136 * force the attacker wait 60 seconds before getting
2137 * the information on whether a frame resulted in a MIC
2138 * failure.
2139 */
2140 u8 rval[4];
2141 int sec;
2142
2143 if (os_get_random(rval, sizeof(rval)) < 0)
2144 sec = os_random() % 60;
2145 else
2146 sec = WPA_GET_BE32(rval) % 60;
2147 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2148 "report %d seconds", sec);
2149 wpa_s->pending_mic_error_report = 1;
2150 wpa_s->pending_mic_error_pairwise = pairwise;
2151 eloop_cancel_timeout(
2152 wpa_supplicant_delayed_mic_error_report,
2153 wpa_s, NULL);
2154 eloop_register_timeout(
2155 sec, os_random() % 1000000,
2156 wpa_supplicant_delayed_mic_error_report,
2157 wpa_s, NULL);
2158 } else {
2159 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2160 }
2161#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2162 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2163#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2164 }
2165 wpa_s->last_michael_mic_error = t.sec;
2166 wpa_s->mic_errors_seen++;
2167}
2168
2169
2170#ifdef CONFIG_TERMINATE_ONLASTIF
2171static int any_interfaces(struct wpa_supplicant *head)
2172{
2173 struct wpa_supplicant *wpa_s;
2174
2175 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2176 if (!wpa_s->interface_removed)
2177 return 1;
2178 return 0;
2179}
2180#endif /* CONFIG_TERMINATE_ONLASTIF */
2181
2182
2183static void
2184wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2185 union wpa_event_data *data)
2186{
2187 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2188 return;
2189
2190 switch (data->interface_status.ievent) {
2191 case EVENT_INTERFACE_ADDED:
2192 if (!wpa_s->interface_removed)
2193 break;
2194 wpa_s->interface_removed = 0;
2195 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2196 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2197 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2198 "driver after interface was added");
2199 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002200 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 break;
2202 case EVENT_INTERFACE_REMOVED:
2203 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2204 wpa_s->interface_removed = 1;
2205 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002206 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 l2_packet_deinit(wpa_s->l2);
2208 wpa_s->l2 = NULL;
2209#ifdef CONFIG_IBSS_RSN
2210 ibss_rsn_deinit(wpa_s->ibss_rsn);
2211 wpa_s->ibss_rsn = NULL;
2212#endif /* CONFIG_IBSS_RSN */
2213#ifdef CONFIG_TERMINATE_ONLASTIF
2214 /* check if last interface */
2215 if (!any_interfaces(wpa_s->global->ifaces))
2216 eloop_terminate();
2217#endif /* CONFIG_TERMINATE_ONLASTIF */
2218 break;
2219 }
2220}
2221
2222
2223#ifdef CONFIG_PEERKEY
2224static void
2225wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2226 union wpa_event_data *data)
2227{
2228 if (data == NULL)
2229 return;
2230 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2231}
2232#endif /* CONFIG_PEERKEY */
2233
2234
2235#ifdef CONFIG_TDLS
2236static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2237 union wpa_event_data *data)
2238{
2239 if (data == NULL)
2240 return;
2241 switch (data->tdls.oper) {
2242 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002243 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2244 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2245 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2246 else
2247 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 break;
2249 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002250 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002251 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 break;
2253 }
2254}
2255#endif /* CONFIG_TDLS */
2256
2257
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002258#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002259static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2260 union wpa_event_data *data)
2261{
2262 if (data == NULL)
2263 return;
2264 switch (data->wnm.oper) {
2265 case WNM_OPER_SLEEP:
2266 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2267 "(action=%d, intval=%d)",
2268 data->wnm.sleep_action, data->wnm.sleep_intval);
2269 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002270 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002271 break;
2272 }
2273}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002274#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002275
2276
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002277#ifdef CONFIG_IEEE80211R
2278static void
2279wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2280 union wpa_event_data *data)
2281{
2282 if (data == NULL)
2283 return;
2284
2285 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2286 data->ft_ies.ies_len,
2287 data->ft_ies.ft_action,
2288 data->ft_ies.target_ap,
2289 data->ft_ies.ric_ies,
2290 data->ft_ies.ric_ies_len) < 0) {
2291 /* TODO: prevent MLME/driver from trying to associate? */
2292 }
2293}
2294#endif /* CONFIG_IEEE80211R */
2295
2296
2297#ifdef CONFIG_IBSS_RSN
2298static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2299 union wpa_event_data *data)
2300{
2301 struct wpa_ssid *ssid;
2302 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2303 return;
2304 if (data == NULL)
2305 return;
2306 ssid = wpa_s->current_ssid;
2307 if (ssid == NULL)
2308 return;
2309 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2310 return;
2311
2312 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2313}
2314#endif /* CONFIG_IBSS_RSN */
2315
2316
2317#ifdef CONFIG_IEEE80211R
2318static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2319 size_t len)
2320{
2321 const u8 *sta_addr, *target_ap_addr;
2322 u16 status;
2323
2324 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2325 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2326 return; /* only SME case supported for now */
2327 if (len < 1 + 2 * ETH_ALEN + 2)
2328 return;
2329 if (data[0] != 2)
2330 return; /* Only FT Action Response is supported for now */
2331 sta_addr = data + 1;
2332 target_ap_addr = data + 1 + ETH_ALEN;
2333 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2334 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2335 MACSTR " TargetAP " MACSTR " status %u",
2336 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2337
2338 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2339 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2340 " in FT Action Response", MAC2STR(sta_addr));
2341 return;
2342 }
2343
2344 if (status) {
2345 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2346 "failure (status code %d)", status);
2347 /* TODO: report error to FT code(?) */
2348 return;
2349 }
2350
2351 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2352 len - (1 + 2 * ETH_ALEN + 2), 1,
2353 target_ap_addr, NULL, 0) < 0)
2354 return;
2355
2356#ifdef CONFIG_SME
2357 {
2358 struct wpa_bss *bss;
2359 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2360 if (bss)
2361 wpa_s->sme.freq = bss->freq;
2362 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2363 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2364 WLAN_AUTH_FT);
2365 }
2366#endif /* CONFIG_SME */
2367}
2368#endif /* CONFIG_IEEE80211R */
2369
2370
2371static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2372 struct unprot_deauth *e)
2373{
2374#ifdef CONFIG_IEEE80211W
2375 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2376 "dropped: " MACSTR " -> " MACSTR
2377 " (reason code %u)",
2378 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2379 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2380#endif /* CONFIG_IEEE80211W */
2381}
2382
2383
2384static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2385 struct unprot_disassoc *e)
2386{
2387#ifdef CONFIG_IEEE80211W
2388 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2389 "dropped: " MACSTR " -> " MACSTR
2390 " (reason code %u)",
2391 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2392 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2393#endif /* CONFIG_IEEE80211W */
2394}
2395
2396
2397void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2398 union wpa_event_data *data)
2399{
2400 struct wpa_supplicant *wpa_s = ctx;
2401 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002402 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403
2404 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2405 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002406 event != EVENT_INTERFACE_STATUS &&
2407 event != EVENT_SCHED_SCAN_STOPPED) {
2408 wpa_dbg(wpa_s, MSG_DEBUG,
2409 "Ignore event %s (%d) while interface is disabled",
2410 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 return;
2412 }
2413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002414#ifndef CONFIG_NO_STDOUT_DEBUG
2415{
2416 int level = MSG_DEBUG;
2417
Dmitry Shmidt04949592012-07-19 12:16:46 -07002418 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002419 const struct ieee80211_hdr *hdr;
2420 u16 fc;
2421 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2422 fc = le_to_host16(hdr->frame_control);
2423 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2424 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2425 level = MSG_EXCESSIVE;
2426 }
2427
2428 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2429 event_to_string(event), event);
2430}
2431#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432
2433 switch (event) {
2434 case EVENT_AUTH:
2435 sme_event_auth(wpa_s, data);
2436 break;
2437 case EVENT_ASSOC:
2438 wpa_supplicant_event_assoc(wpa_s, data);
2439 break;
2440 case EVENT_DISASSOC:
2441 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2442 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002443 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2444 data->disassoc_info.reason_code,
2445 data->disassoc_info.locally_generated ?
2446 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 if (data->disassoc_info.addr)
2448 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2449 MAC2STR(data->disassoc_info.addr));
2450 }
2451#ifdef CONFIG_AP
2452 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2453 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2454 data->disassoc_info.addr);
2455 break;
2456 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 if (wpa_s->ap_iface) {
2458 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2459 "AP mode");
2460 break;
2461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462#endif /* CONFIG_AP */
2463 if (data) {
2464 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002465 locally_generated =
2466 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2468 data->disassoc_info.ie,
2469 data->disassoc_info.ie_len);
2470#ifdef CONFIG_P2P
2471 wpas_p2p_disassoc_notif(
2472 wpa_s, data->disassoc_info.addr, reason_code,
2473 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002474 data->disassoc_info.ie_len,
2475 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476#endif /* CONFIG_P2P */
2477 }
2478 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2479 sme_event_disassoc(wpa_s, data);
2480 /* fall through */
2481 case EVENT_DEAUTH:
2482 if (event == EVENT_DEAUTH) {
2483 wpa_dbg(wpa_s, MSG_DEBUG,
2484 "Deauthentication notification");
2485 if (data) {
2486 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002487 locally_generated =
2488 data->deauth_info.locally_generated;
2489 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2490 data->deauth_info.reason_code,
2491 data->deauth_info.locally_generated ?
2492 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 if (data->deauth_info.addr) {
2494 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2495 MACSTR,
2496 MAC2STR(data->deauth_info.
2497 addr));
2498 }
2499 wpa_hexdump(MSG_DEBUG,
2500 "Deauthentication frame IE(s)",
2501 data->deauth_info.ie,
2502 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 }
Dmitry Shmidt4b060592013-04-29 16:42:49 -07002504 wpa_reset_ft_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 }
2506#ifdef CONFIG_AP
2507 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2508 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2509 data->deauth_info.addr);
2510 break;
2511 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002512 if (wpa_s->ap_iface) {
2513 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2514 "AP mode");
2515 break;
2516 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002518 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2519 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002520 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2521 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2522 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2523 eapol_sm_failed(wpa_s->eapol)))
2524 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002525#ifdef CONFIG_P2P
2526 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002527 if (wpas_p2p_deauth_notif(wpa_s,
2528 data->deauth_info.addr,
2529 reason_code,
2530 data->deauth_info.ie,
2531 data->deauth_info.ie_len,
2532 locally_generated) > 0) {
2533 /*
2534 * The interface was removed, so cannot
2535 * continue processing any additional
2536 * operations after this.
2537 */
2538 break;
2539 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002540 }
2541#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002542 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2543 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 break;
2545 case EVENT_MICHAEL_MIC_FAILURE:
2546 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2547 break;
2548#ifndef CONFIG_NO_SCAN_PROCESSING
2549 case EVENT_SCAN_RESULTS:
2550 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002551 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2552 wpa_s->wpa_state != WPA_ASSOCIATING)
2553 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554 break;
2555#endif /* CONFIG_NO_SCAN_PROCESSING */
2556 case EVENT_ASSOCINFO:
2557 wpa_supplicant_event_associnfo(wpa_s, data);
2558 break;
2559 case EVENT_INTERFACE_STATUS:
2560 wpa_supplicant_event_interface_status(wpa_s, data);
2561 break;
2562 case EVENT_PMKID_CANDIDATE:
2563 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2564 break;
2565#ifdef CONFIG_PEERKEY
2566 case EVENT_STKSTART:
2567 wpa_supplicant_event_stkstart(wpa_s, data);
2568 break;
2569#endif /* CONFIG_PEERKEY */
2570#ifdef CONFIG_TDLS
2571 case EVENT_TDLS:
2572 wpa_supplicant_event_tdls(wpa_s, data);
2573 break;
2574#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002575#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002576 case EVENT_WNM:
2577 wpa_supplicant_event_wnm(wpa_s, data);
2578 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002579#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580#ifdef CONFIG_IEEE80211R
2581 case EVENT_FT_RESPONSE:
2582 wpa_supplicant_event_ft_response(wpa_s, data);
2583 break;
2584#endif /* CONFIG_IEEE80211R */
2585#ifdef CONFIG_IBSS_RSN
2586 case EVENT_IBSS_RSN_START:
2587 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2588 break;
2589#endif /* CONFIG_IBSS_RSN */
2590 case EVENT_ASSOC_REJECT:
2591 if (data->assoc_reject.bssid)
2592 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2593 "bssid=" MACSTR " status_code=%u",
2594 MAC2STR(data->assoc_reject.bssid),
2595 data->assoc_reject.status_code);
2596 else
2597 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2598 "status_code=%u",
2599 data->assoc_reject.status_code);
2600 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2601 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002602 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002603#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002604 if(!wpa_s->current_ssid) {
2605 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2606 break;
2607 }
2608 /* If assoc reject is reported by the driver, then avoid
2609 * waiting for the authentication timeout. Cancel the
2610 * authentication timeout and retry the assoc.
2611 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002612 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002613 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2614 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002615
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002616 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002617
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002618 /* Clear the states */
2619 wpa_sm_notify_disassoc(wpa_s->wpa);
2620 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2621
2622 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002623 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002624 const u8 *bl_bssid = data->assoc_reject.bssid;
2625 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2626 bl_bssid = wpa_s->pending_bssid;
2627 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002628 wpa_supplicant_req_scan(wpa_s, 0, 0);
2629 } else {
2630 wpa_supplicant_req_scan(wpa_s, 1, 0);
2631 }
2632 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002633 /* If we ASSOC_REJECT's hits threshold, disable the
2634 * network
2635 */
2636 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2637 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002638 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002639 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002640 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002641 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002642#else
2643 const u8 *bssid = data->assoc_reject.bssid;
2644 if (bssid == NULL || is_zero_ether_addr(bssid))
2645 bssid = wpa_s->pending_bssid;
2646 wpas_connection_failed(wpa_s, bssid);
2647 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002648#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 break;
2651 case EVENT_AUTH_TIMED_OUT:
2652 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2653 sme_event_auth_timed_out(wpa_s, data);
2654 break;
2655 case EVENT_ASSOC_TIMED_OUT:
2656 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2657 sme_event_assoc_timed_out(wpa_s, data);
2658 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 case EVENT_TX_STATUS:
2660 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2661 " type=%d stype=%d",
2662 MAC2STR(data->tx_status.dst),
2663 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002664#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002666#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2668 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 wpa_s, data->tx_status.dst,
2671 data->tx_status.data,
2672 data->tx_status.data_len,
2673 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002674 OFFCHANNEL_SEND_ACTION_SUCCESS :
2675 OFFCHANNEL_SEND_ACTION_NO_ACK);
2676#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677 break;
2678 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002679#endif /* CONFIG_AP */
2680#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2682 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2683 /*
2684 * Catch TX status events for Action frames we sent via group
2685 * interface in GO mode.
2686 */
2687 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2688 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2689 os_memcmp(wpa_s->parent->pending_action_dst,
2690 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002691 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 wpa_s->parent, data->tx_status.dst,
2693 data->tx_status.data,
2694 data->tx_status.data_len,
2695 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002696 OFFCHANNEL_SEND_ACTION_SUCCESS :
2697 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 break;
2699 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002700#endif /* CONFIG_OFFCHANNEL */
2701#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 switch (data->tx_status.type) {
2703 case WLAN_FC_TYPE_MGMT:
2704 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2705 data->tx_status.data_len,
2706 data->tx_status.stype,
2707 data->tx_status.ack);
2708 break;
2709 case WLAN_FC_TYPE_DATA:
2710 ap_tx_status(wpa_s, data->tx_status.dst,
2711 data->tx_status.data,
2712 data->tx_status.data_len,
2713 data->tx_status.ack);
2714 break;
2715 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002716#endif /* CONFIG_AP */
2717 break;
2718#ifdef CONFIG_AP
2719 case EVENT_EAPOL_TX_STATUS:
2720 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2721 data->eapol_tx_status.data,
2722 data->eapol_tx_status.data_len,
2723 data->eapol_tx_status.ack);
2724 break;
2725 case EVENT_DRIVER_CLIENT_POLL_OK:
2726 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 break;
2728 case EVENT_RX_FROM_UNKNOWN:
2729 if (wpa_s->ap_iface == NULL)
2730 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002731 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2732 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002734 case EVENT_CH_SWITCH:
2735 if (!data)
2736 break;
2737 if (!wpa_s->ap_iface) {
2738 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2739 "event in non-AP mode");
2740 break;
2741 }
2742
2743#ifdef CONFIG_AP
2744 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2745 data->ch_switch.ht_enabled,
2746 data->ch_switch.ch_offset);
2747#endif /* CONFIG_AP */
2748 break;
2749 case EVENT_RX_MGMT: {
2750 u16 fc, stype;
2751 const struct ieee80211_mgmt *mgmt;
2752
2753 mgmt = (const struct ieee80211_mgmt *)
2754 data->rx_mgmt.frame;
2755 fc = le_to_host16(mgmt->frame_control);
2756 stype = WLAN_FC_GET_STYPE(fc);
2757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 if (wpa_s->ap_iface == NULL) {
2759#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2761 data->rx_mgmt.frame_len > 24) {
2762 const u8 *src = mgmt->sa;
2763 const u8 *ie = mgmt->u.probe_req.variable;
2764 size_t ie_len = data->rx_mgmt.frame_len -
2765 (mgmt->u.probe_req.variable -
2766 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002767 wpas_p2p_probe_req_rx(
2768 wpa_s, src, mgmt->da,
2769 mgmt->bssid, ie, ie_len,
2770 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 break;
2772 }
2773#endif /* CONFIG_P2P */
2774 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2775 "management frame in non-AP mode");
2776 break;
2777 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002778
2779 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2780 data->rx_mgmt.frame_len > 24) {
2781 const u8 *ie = mgmt->u.probe_req.variable;
2782 size_t ie_len = data->rx_mgmt.frame_len -
2783 (mgmt->u.probe_req.variable -
2784 data->rx_mgmt.frame);
2785
2786 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2787 mgmt->bssid, ie, ie_len,
2788 data->rx_mgmt.ssi_signal);
2789 }
2790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2792 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002793 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794#endif /* CONFIG_AP */
2795 case EVENT_RX_ACTION:
2796 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2797 " Category=%u DataLen=%d freq=%d MHz",
2798 MAC2STR(data->rx_action.sa),
2799 data->rx_action.category, (int) data->rx_action.len,
2800 data->rx_action.freq);
2801#ifdef CONFIG_IEEE80211R
2802 if (data->rx_action.category == WLAN_ACTION_FT) {
2803 ft_rx_action(wpa_s, data->rx_action.data,
2804 data->rx_action.len);
2805 break;
2806 }
2807#endif /* CONFIG_IEEE80211R */
2808#ifdef CONFIG_IEEE80211W
2809#ifdef CONFIG_SME
2810 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2811 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2812 data->rx_action.data,
2813 data->rx_action.len);
2814 break;
2815 }
2816#endif /* CONFIG_SME */
2817#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002818#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002819 if (data->rx_action.category == WLAN_ACTION_WNM) {
2820 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2821 break;
2822 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002823#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002824#ifdef CONFIG_GAS
2825 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2826 gas_query_rx(wpa_s->gas, data->rx_action.da,
2827 data->rx_action.sa, data->rx_action.bssid,
2828 data->rx_action.data, data->rx_action.len,
2829 data->rx_action.freq) == 0)
2830 break;
2831#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002832#ifdef CONFIG_TDLS
2833 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2834 data->rx_action.len >= 4 &&
2835 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2836 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2837 "Response from " MACSTR,
2838 MAC2STR(data->rx_action.sa));
2839 break;
2840 }
2841#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842#ifdef CONFIG_P2P
2843 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2844 data->rx_action.sa,
2845 data->rx_action.bssid,
2846 data->rx_action.category,
2847 data->rx_action.data,
2848 data->rx_action.len, data->rx_action.freq);
2849#endif /* CONFIG_P2P */
2850 break;
2851 case EVENT_RX_PROBE_REQ:
2852 if (data->rx_probe_req.sa == NULL ||
2853 data->rx_probe_req.ie == NULL)
2854 break;
2855#ifdef CONFIG_AP
2856 if (wpa_s->ap_iface) {
2857 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2858 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002859 data->rx_probe_req.da,
2860 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002862 data->rx_probe_req.ie_len,
2863 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 break;
2865 }
2866#endif /* CONFIG_AP */
2867#ifdef CONFIG_P2P
2868 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002869 data->rx_probe_req.da,
2870 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002872 data->rx_probe_req.ie_len,
2873 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874#endif /* CONFIG_P2P */
2875 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877#ifdef CONFIG_OFFCHANNEL
2878 offchannel_remain_on_channel_cb(
2879 wpa_s, data->remain_on_channel.freq,
2880 data->remain_on_channel.duration);
2881#endif /* CONFIG_OFFCHANNEL */
2882#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 wpas_p2p_remain_on_channel_cb(
2884 wpa_s, data->remain_on_channel.freq,
2885 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002886#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002887 break;
2888 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002889#ifdef CONFIG_OFFCHANNEL
2890 offchannel_cancel_remain_on_channel_cb(
2891 wpa_s, data->remain_on_channel.freq);
2892#endif /* CONFIG_OFFCHANNEL */
2893#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894 wpas_p2p_cancel_remain_on_channel_cb(
2895 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002896#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002898#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 case EVENT_P2P_DEV_FOUND: {
2900 struct p2p_peer_info peer_info;
2901
2902 os_memset(&peer_info, 0, sizeof(peer_info));
2903 if (data->p2p_dev_found.dev_addr)
2904 os_memcpy(peer_info.p2p_device_addr,
2905 data->p2p_dev_found.dev_addr, ETH_ALEN);
2906 if (data->p2p_dev_found.pri_dev_type)
2907 os_memcpy(peer_info.pri_dev_type,
2908 data->p2p_dev_found.pri_dev_type,
2909 sizeof(peer_info.pri_dev_type));
2910 if (data->p2p_dev_found.dev_name)
2911 os_strlcpy(peer_info.device_name,
2912 data->p2p_dev_found.dev_name,
2913 sizeof(peer_info.device_name));
2914 peer_info.config_methods = data->p2p_dev_found.config_methods;
2915 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2916 peer_info.group_capab = data->p2p_dev_found.group_capab;
2917
2918 /*
2919 * FIX: new_device=1 is not necessarily correct. We should
2920 * maintain a P2P peer database in wpa_supplicant and update
2921 * this information based on whether the peer is truly new.
2922 */
2923 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2924 break;
2925 }
2926 case EVENT_P2P_GO_NEG_REQ_RX:
2927 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2928 data->p2p_go_neg_req_rx.dev_passwd_id);
2929 break;
2930 case EVENT_P2P_GO_NEG_COMPLETED:
2931 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2932 break;
2933 case EVENT_P2P_PROV_DISC_REQUEST:
2934 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2935 data->p2p_prov_disc_req.config_methods,
2936 data->p2p_prov_disc_req.dev_addr,
2937 data->p2p_prov_disc_req.pri_dev_type,
2938 data->p2p_prov_disc_req.dev_name,
2939 data->p2p_prov_disc_req.supp_config_methods,
2940 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002941 data->p2p_prov_disc_req.group_capab,
2942 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 break;
2944 case EVENT_P2P_PROV_DISC_RESPONSE:
2945 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2946 data->p2p_prov_disc_resp.config_methods);
2947 break;
2948 case EVENT_P2P_SD_REQUEST:
2949 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2950 data->p2p_sd_req.sa,
2951 data->p2p_sd_req.dialog_token,
2952 data->p2p_sd_req.update_indic,
2953 data->p2p_sd_req.tlvs,
2954 data->p2p_sd_req.tlvs_len);
2955 break;
2956 case EVENT_P2P_SD_RESPONSE:
2957 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2958 data->p2p_sd_resp.update_indic,
2959 data->p2p_sd_resp.tlvs,
2960 data->p2p_sd_resp.tlvs_len);
2961 break;
2962#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 case EVENT_EAPOL_RX:
2964 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2965 data->eapol_rx.data,
2966 data->eapol_rx.data_len);
2967 break;
2968 case EVENT_SIGNAL_CHANGE:
2969 bgscan_notify_signal_change(
2970 wpa_s, data->signal_change.above_threshold,
2971 data->signal_change.current_signal,
2972 data->signal_change.current_noise,
2973 data->signal_change.current_txrate);
2974 break;
2975 case EVENT_INTERFACE_ENABLED:
2976 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2977 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002978 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979#ifdef CONFIG_AP
2980 if (!wpa_s->ap_iface) {
2981 wpa_supplicant_set_state(wpa_s,
2982 WPA_DISCONNECTED);
2983 wpa_supplicant_req_scan(wpa_s, 0, 0);
2984 } else
2985 wpa_supplicant_set_state(wpa_s,
2986 WPA_COMPLETED);
2987#else /* CONFIG_AP */
2988 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2989 wpa_supplicant_req_scan(wpa_s, 0, 0);
2990#endif /* CONFIG_AP */
2991 }
2992 break;
2993 case EVENT_INTERFACE_DISABLED:
2994 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2995 wpa_supplicant_mark_disassoc(wpa_s);
2996 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2997 break;
2998 case EVENT_CHANNEL_LIST_CHANGED:
2999 if (wpa_s->drv_priv == NULL)
3000 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003001
3002 free_hw_features(wpa_s);
3003 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
3004 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
3005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006#ifdef CONFIG_P2P
3007 wpas_p2p_update_channel_list(wpa_s);
3008#endif /* CONFIG_P2P */
3009 break;
3010 case EVENT_INTERFACE_UNAVAILABLE:
3011#ifdef CONFIG_P2P
3012 wpas_p2p_interface_unavailable(wpa_s);
3013#endif /* CONFIG_P2P */
3014 break;
3015 case EVENT_BEST_CHANNEL:
3016 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3017 "(%d %d %d)",
3018 data->best_chan.freq_24, data->best_chan.freq_5,
3019 data->best_chan.freq_overall);
3020 wpa_s->best_24_freq = data->best_chan.freq_24;
3021 wpa_s->best_5_freq = data->best_chan.freq_5;
3022 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3023#ifdef CONFIG_P2P
3024 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3025 data->best_chan.freq_5,
3026 data->best_chan.freq_overall);
3027#endif /* CONFIG_P2P */
3028 break;
3029 case EVENT_UNPROT_DEAUTH:
3030 wpa_supplicant_event_unprot_deauth(wpa_s,
3031 &data->unprot_deauth);
3032 break;
3033 case EVENT_UNPROT_DISASSOC:
3034 wpa_supplicant_event_unprot_disassoc(wpa_s,
3035 &data->unprot_disassoc);
3036 break;
3037 case EVENT_STATION_LOW_ACK:
3038#ifdef CONFIG_AP
3039 if (wpa_s->ap_iface && data)
3040 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3041 data->low_ack.addr);
3042#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003043#ifdef CONFIG_TDLS
3044 if (data)
3045 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3046#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 break;
3048 case EVENT_IBSS_PEER_LOST:
3049#ifdef CONFIG_IBSS_RSN
3050 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3051#endif /* CONFIG_IBSS_RSN */
3052 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053 case EVENT_DRIVER_GTK_REKEY:
3054 if (os_memcmp(data->driver_gtk_rekey.bssid,
3055 wpa_s->bssid, ETH_ALEN))
3056 break;
3057 if (!wpa_s->wpa)
3058 break;
3059 wpa_sm_update_replay_ctr(wpa_s->wpa,
3060 data->driver_gtk_rekey.replay_ctr);
3061 break;
3062 case EVENT_SCHED_SCAN_STOPPED:
3063 wpa_s->sched_scanning = 0;
3064 wpa_supplicant_notify_scanning(wpa_s, 0);
3065
3066 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3067 break;
3068
3069 /*
3070 * If we timed out, start a new sched scan to continue
3071 * searching for more SSIDs.
3072 */
3073 if (wpa_s->sched_scan_timed_out)
3074 wpa_supplicant_req_sched_scan(wpa_s);
3075 break;
3076 case EVENT_WPS_BUTTON_PUSHED:
3077#ifdef CONFIG_WPS
3078 wpas_wps_start_pbc(wpa_s, NULL, 0);
3079#endif /* CONFIG_WPS */
3080 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003081 case EVENT_CONNECT_FAILED_REASON:
3082#ifdef CONFIG_AP
3083 if (!wpa_s->ap_iface || !data)
3084 break;
3085 hostapd_event_connect_failed_reason(
3086 wpa_s->ap_iface->bss[0],
3087 data->connect_failed_reason.addr,
3088 data->connect_failed_reason.code);
3089#endif /* CONFIG_AP */
3090 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 default:
3092 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3093 break;
3094 }
3095}