blob: d77e96b7342e5670c37862197ce1056f464d9959 [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;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700193 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700194 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195}
196
197
198static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
199{
200 struct wpa_ie_data ie;
201 int pmksa_set = -1;
202 size_t i;
203
204 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
205 ie.pmkid == NULL)
206 return;
207
208 for (i = 0; i < ie.num_pmkid; i++) {
209 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
210 ie.pmkid + i * PMKID_LEN,
211 NULL, NULL, 0);
212 if (pmksa_set == 0) {
213 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
214 break;
215 }
216 }
217
218 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
219 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
220}
221
222
223static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
224 union wpa_event_data *data)
225{
226 if (data == NULL) {
227 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
228 "event");
229 return;
230 }
231 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
232 " index=%d preauth=%d",
233 MAC2STR(data->pmkid_candidate.bssid),
234 data->pmkid_candidate.index,
235 data->pmkid_candidate.preauth);
236
237 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
238 data->pmkid_candidate.index,
239 data->pmkid_candidate.preauth);
240}
241
242
243static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
244{
245 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
246 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
247 return 0;
248
249#ifdef IEEE8021X_EAPOL
250 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
251 wpa_s->current_ssid &&
252 !(wpa_s->current_ssid->eapol_flags &
253 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
254 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
255 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
256 * plaintext or static WEP keys). */
257 return 0;
258 }
259#endif /* IEEE8021X_EAPOL */
260
261 return 1;
262}
263
264
265/**
266 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
267 * @wpa_s: pointer to wpa_supplicant data
268 * @ssid: Configuration data for the network
269 * Returns: 0 on success, -1 on failure
270 *
271 * This function is called when starting authentication with a network that is
272 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
273 */
274int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
275 struct wpa_ssid *ssid)
276{
277#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279 int aka = 0, sim = 0, type;
280
281 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
282 return 0;
283
284 if (ssid->eap.eap_methods == NULL) {
285 sim = 1;
286 aka = 1;
287 } else {
288 struct eap_method_type *eap = ssid->eap.eap_methods;
289 while (eap->vendor != EAP_VENDOR_IETF ||
290 eap->method != EAP_TYPE_NONE) {
291 if (eap->vendor == EAP_VENDOR_IETF) {
292 if (eap->method == EAP_TYPE_SIM)
293 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700294 else if (eap->method == EAP_TYPE_AKA ||
295 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 aka = 1;
297 }
298 eap++;
299 }
300 }
301
302 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
303 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700304 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
305 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
306 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 aka = 0;
308
309 if (!sim && !aka) {
310 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
311 "use SIM, but neither EAP-SIM nor EAP-AKA are "
312 "enabled");
313 return 0;
314 }
315
316 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
317 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
318 if (sim && aka)
319 type = SCARD_TRY_BOTH;
320 else if (aka)
321 type = SCARD_USIM_ONLY;
322 else
323 type = SCARD_GSM_SIM_ONLY;
324
Dmitry Shmidt04949592012-07-19 12:16:46 -0700325 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 if (wpa_s->scard == NULL) {
327 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
328 "(pcsc-lite)");
329 return -1;
330 }
331 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
332 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334#endif /* IEEE8021X_EAPOL */
335
336 return 0;
337}
338
339
340#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700341
342static int has_wep_key(struct wpa_ssid *ssid)
343{
344 int i;
345
346 for (i = 0; i < NUM_WEP_KEYS; i++) {
347 if (ssid->wep_key_len[i])
348 return 1;
349 }
350
351 return 0;
352}
353
354
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700355static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 struct wpa_ssid *ssid)
357{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700358 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
360 if (ssid->mixed_cell)
361 return 1;
362
363#ifdef CONFIG_WPS
364 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
365 return 1;
366#endif /* CONFIG_WPS */
367
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700368 if (has_wep_key(ssid))
369 privacy = 1;
370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371#ifdef IEEE8021X_EAPOL
372 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
373 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
374 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
375 privacy = 1;
376#endif /* IEEE8021X_EAPOL */
377
Jouni Malinen75ecf522011-06-27 15:19:46 -0700378 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
379 privacy = 1;
380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 if (bss->caps & IEEE80211_CAP_PRIVACY)
382 return privacy;
383 return !privacy;
384}
385
386
387static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
388 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700389 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390{
391 struct wpa_ie_data ie;
392 int proto_match = 0;
393 const u8 *rsn_ie, *wpa_ie;
394 int ret;
395 int wep_ok;
396
397 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
398 if (ret >= 0)
399 return ret;
400
401 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
402 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
403 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
404 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
405 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
406
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700407 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
409 proto_match++;
410
411 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
412 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
413 "failed");
414 break;
415 }
416
417 if (wep_ok &&
418 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
419 {
420 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
421 "in RSN IE");
422 return 1;
423 }
424
425 if (!(ie.proto & ssid->proto)) {
426 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
427 "mismatch");
428 break;
429 }
430
431 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
432 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
433 "cipher mismatch");
434 break;
435 }
436
437 if (!(ie.group_cipher & ssid->group_cipher)) {
438 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
439 "cipher mismatch");
440 break;
441 }
442
443 if (!(ie.key_mgmt & ssid->key_mgmt)) {
444 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
445 "mismatch");
446 break;
447 }
448
449#ifdef CONFIG_IEEE80211W
450 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800451 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
452 wpa_s->conf->pmf : ssid->ieee80211w) ==
453 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
455 "frame protection");
456 break;
457 }
458#endif /* CONFIG_IEEE80211W */
459
460 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
461 return 1;
462 }
463
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700464 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
466 proto_match++;
467
468 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
469 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
470 "failed");
471 break;
472 }
473
474 if (wep_ok &&
475 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
476 {
477 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
478 "in WPA IE");
479 return 1;
480 }
481
482 if (!(ie.proto & ssid->proto)) {
483 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
484 "mismatch");
485 break;
486 }
487
488 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
489 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
490 "cipher mismatch");
491 break;
492 }
493
494 if (!(ie.group_cipher & ssid->group_cipher)) {
495 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
496 "cipher mismatch");
497 break;
498 }
499
500 if (!(ie.key_mgmt & ssid->key_mgmt)) {
501 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
502 "mismatch");
503 break;
504 }
505
506 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
507 return 1;
508 }
509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
511 !rsn_ie) {
512 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
513 return 1;
514 }
515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700516 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
517 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
518 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
519 return 0;
520 }
521
522 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
523 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
524 return 1;
525 }
526
527 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
528 "WPA/WPA2");
529
530 return 0;
531}
532
533
534static int freq_allowed(int *freqs, int freq)
535{
536 int i;
537
538 if (freqs == NULL)
539 return 1;
540
541 for (i = 0; freqs[i]; i++)
542 if (freqs[i] == freq)
543 return 1;
544 return 0;
545}
546
547
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800548static int ht_supported(const struct hostapd_hw_modes *mode)
549{
550 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
551 /*
552 * The driver did not indicate whether it supports HT. Assume
553 * it does to avoid connection issues.
554 */
555 return 1;
556 }
557
558 /*
559 * IEEE Std 802.11n-2009 20.1.1:
560 * An HT non-AP STA shall support all EQM rates for one spatial stream.
561 */
562 return mode->mcs_set[0] == 0xff;
563}
564
565
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700566static int vht_supported(const struct hostapd_hw_modes *mode)
567{
568 if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
569 /*
570 * The driver did not indicate whether it supports VHT. Assume
571 * it does to avoid connection issues.
572 */
573 return 1;
574 }
575
576 /*
577 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
578 * TODO: Verify if this complies with the standard
579 */
580 return (mode->vht_mcs_set[0] & 0x3) != 3;
581}
582
583
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700584static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585{
586 const struct hostapd_hw_modes *mode = NULL, *modes;
587 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
588 const u8 *rate_ie;
589 int i, j, k;
590
591 if (bss->freq == 0)
592 return 1; /* Cannot do matching without knowing band */
593
594 modes = wpa_s->hw.modes;
595 if (modes == NULL) {
596 /*
597 * The driver does not provide any additional information
598 * about the utilized hardware, so allow the connection attempt
599 * to continue.
600 */
601 return 1;
602 }
603
604 for (i = 0; i < wpa_s->hw.num_modes; i++) {
605 for (j = 0; j < modes[i].num_channels; j++) {
606 int freq = modes[i].channels[j].freq;
607 if (freq == bss->freq) {
608 if (mode &&
609 mode->mode == HOSTAPD_MODE_IEEE80211G)
610 break; /* do not allow 802.11b replace
611 * 802.11g */
612 mode = &modes[i];
613 break;
614 }
615 }
616 }
617
618 if (mode == NULL)
619 return 0;
620
621 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700622 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800623 if (rate_ie == NULL)
624 continue;
625
626 for (j = 2; j < rate_ie[1] + 2; j++) {
627 int flagged = !!(rate_ie[j] & 0x80);
628 int r = (rate_ie[j] & 0x7f) * 5;
629
630 /*
631 * IEEE Std 802.11n-2009 7.3.2.2:
632 * The new BSS Membership selector value is encoded
633 * like a legacy basic rate, but it is not a rate and
634 * only indicates if the BSS members are required to
635 * support the mandatory features of Clause 20 [HT PHY]
636 * in order to join the BSS.
637 */
638 if (flagged && ((rate_ie[j] & 0x7f) ==
639 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
640 if (!ht_supported(mode)) {
641 wpa_dbg(wpa_s, MSG_DEBUG,
642 " hardware does not support "
643 "HT PHY");
644 return 0;
645 }
646 continue;
647 }
648
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700649 /* There's also a VHT selector for 802.11ac */
650 if (flagged && ((rate_ie[j] & 0x7f) ==
651 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
652 if (!vht_supported(mode)) {
653 wpa_dbg(wpa_s, MSG_DEBUG,
654 " hardware does not support "
655 "VHT PHY");
656 return 0;
657 }
658 continue;
659 }
660
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800661 if (!flagged)
662 continue;
663
664 /* check for legacy basic rates */
665 for (k = 0; k < mode->num_rates; k++) {
666 if (mode->rates[k] == r)
667 break;
668 }
669 if (k == mode->num_rates) {
670 /*
671 * IEEE Std 802.11-2007 7.3.2.2 demands that in
672 * order to join a BSS all required rates
673 * have to be supported by the hardware.
674 */
675 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
676 "not support required rate %d.%d Mbps",
677 r / 10, r % 10);
678 return 0;
679 }
680 }
681 }
682
683 return 1;
684}
685
686
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800687static int bss_is_dmg(struct wpa_bss *bss)
688{
689 return bss->freq > 45000;
690}
691
692
693/*
694 * Test whether BSS is in an ESS.
695 * This is done differently in DMG (60 GHz) and non-DMG bands
696 */
697static int bss_is_ess(struct wpa_bss *bss)
698{
699 if (bss_is_dmg(bss)) {
700 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
701 IEEE80211_CAP_DMG_AP;
702 }
703
704 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
705 IEEE80211_CAP_ESS);
706}
707
708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700710 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 struct wpa_ssid *group)
712{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700713 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 int wpa;
715 struct wpa_blacklist *e;
716 const u8 *ie;
717 struct wpa_ssid *ssid;
718
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700719 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 wpa_ie_len = ie ? ie[1] : 0;
721
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700722 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 rsn_ie_len = ie ? ie[1] : 0;
724
725 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
726 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700727 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700729 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730
731 e = wpa_blacklist_get(wpa_s, bss->bssid);
732 if (e) {
733 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700734 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 /*
736 * When only a single network is enabled, we can
737 * trigger blacklisting on the first failure. This
738 * should not be done with multiple enabled networks to
739 * avoid getting forced to move into a worse ESS on
740 * single error if there are no other BSSes of the
741 * current ESS.
742 */
743 limit = 0;
744 }
745 if (e->count > limit) {
746 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
747 "(count=%d limit=%d)", e->count, limit);
748 return NULL;
749 }
750 }
751
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700752 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
754 return NULL;
755 }
756
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800757 if (disallowed_bssid(wpa_s, bss->bssid)) {
758 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
759 return NULL;
760 }
761
762 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
763 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
764 return NULL;
765 }
766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
768
769 for (ssid = group; ssid; ssid = ssid->pnext) {
770 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700771 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772
Dmitry Shmidt04949592012-07-19 12:16:46 -0700773 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
775 continue;
776 }
777
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 res = wpas_temp_disabled(wpa_s, ssid);
779 if (res > 0) {
780 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
781 "temporarily for %d second(s)", res);
782 continue;
783 }
784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785#ifdef CONFIG_WPS
786 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
787 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
788 "(WPS)");
789 continue;
790 }
791
792 if (wpa && ssid->ssid_len == 0 &&
793 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
794 check_ssid = 0;
795
796 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
797 /* Only allow wildcard SSID match if an AP
798 * advertises active WPS operation that matches
799 * with our mode. */
800 check_ssid = 1;
801 if (ssid->ssid_len == 0 &&
802 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
803 check_ssid = 0;
804 }
805#endif /* CONFIG_WPS */
806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 if (ssid->bssid_set && ssid->ssid_len == 0 &&
808 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
809 check_ssid = 0;
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700812 (bss->ssid_len != ssid->ssid_len ||
813 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
815 continue;
816 }
817
818 if (ssid->bssid_set &&
819 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
820 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
821 continue;
822 }
823
824 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
825 continue;
826
827 if (!wpa &&
828 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
829 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
830 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
831 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
832 "not allowed");
833 continue;
834 }
835
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700836 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
837 has_wep_key(ssid)) {
838 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
839 continue;
840 }
841
Jouni Malinen75ecf522011-06-27 15:19:46 -0700842 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
844 "mismatch");
845 continue;
846 }
847
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800848 if (!bss_is_ess(bss)) {
849 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 continue;
851 }
852
853 if (!freq_allowed(ssid->freq_list, bss->freq)) {
854 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
855 "allowed");
856 continue;
857 }
858
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800859 if (!rate_match(wpa_s, bss)) {
860 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
861 "not match");
862 continue;
863 }
864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865#ifdef CONFIG_P2P
866 /*
867 * TODO: skip the AP if its P2P IE has Group Formation
868 * bit set in the P2P Group Capability Bitmap and we
869 * are not in Group Formation with that device.
870 */
871#endif /* CONFIG_P2P */
872
873 /* Matching configuration found */
874 return ssid;
875 }
876
877 /* No matching configuration found */
878 return NULL;
879}
880
881
882static struct wpa_bss *
883wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 struct wpa_ssid *group,
885 struct wpa_ssid **selected_ssid)
886{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700887 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888
889 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
890 group->priority);
891
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700892 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
893 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
895 if (!*selected_ssid)
896 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
898 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700899 MAC2STR(bss->bssid),
900 wpa_ssid_txt(bss->ssid, bss->ssid_len));
901 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 }
903
904 return NULL;
905}
906
907
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700908struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
909 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910{
911 struct wpa_bss *selected = NULL;
912 int prio;
913
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700914 if (wpa_s->last_scan_res == NULL ||
915 wpa_s->last_scan_res_used == 0)
916 return NULL; /* no scan results from last update */
917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 while (selected == NULL) {
919 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
920 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700921 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 selected_ssid);
923 if (selected)
924 break;
925 }
926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800927 if (selected == NULL && wpa_s->blacklist &&
928 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
930 "blacklist and try again");
931 wpa_blacklist_clear(wpa_s);
932 wpa_s->blacklist_cleared++;
933 } else if (selected == NULL)
934 break;
935 }
936
937 return selected;
938}
939
940
941static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
942 int timeout_sec, int timeout_usec)
943{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700944 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 /*
946 * No networks are enabled; short-circuit request so
947 * we don't wait timeout seconds before transitioning
948 * to INACTIVE state.
949 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700950 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
951 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700953#ifdef CONFIG_P2P
954 wpa_s->sta_scan_pending = 0;
955#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 return;
957 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800958
959 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
961}
962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800963
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700964int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800965 struct wpa_bss *selected,
966 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967{
968 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
969 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
970 "PBC session overlap");
971#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800972 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700973 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974#endif /* CONFIG_P2P */
975
976#ifdef CONFIG_WPS
977 wpas_wps_cancel(wpa_s);
978#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700979 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 }
981
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -0700982 wpa_msg(wpa_s, MSG_DEBUG,
983 "Considering connect request: reassociate: %d selected: "
984 MACSTR " bssid: " MACSTR " pending: " MACSTR
985 " wpa_state: %s ssid=%p current_ssid=%p",
986 wpa_s->reassociate, MAC2STR(selected->bssid),
987 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
988 wpa_supplicant_state_txt(wpa_s->wpa_state),
989 ssid, wpa_s->current_ssid);
990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 /*
992 * Do not trigger new association unless the BSSID has changed or if
993 * reassociation is requested. If we are in process of associating with
994 * the selected BSSID, do not trigger new attempt.
995 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800996 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
998 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
999 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001000 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1001 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1002 0) ||
1003 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1004 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1006 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001007 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001009 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1010 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011 wpa_supplicant_associate(wpa_s, selected, ssid);
1012 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001013 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1014 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001016
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001017 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018}
1019
1020
1021static struct wpa_ssid *
1022wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1023{
1024 int prio;
1025 struct wpa_ssid *ssid;
1026
1027 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1028 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1029 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001030 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001031 continue;
1032 if (ssid->mode == IEEE80211_MODE_IBSS ||
1033 ssid->mode == IEEE80211_MODE_AP)
1034 return ssid;
1035 }
1036 }
1037 return NULL;
1038}
1039
1040
1041/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1042 * on BSS added and BSS changed events */
1043static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001044 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001046 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047
1048 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1049 return;
1050
Jouni Malinen87fd2792011-05-16 18:35:42 +03001051 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053
Jouni Malinen87fd2792011-05-16 18:35:42 +03001054 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 if (ssid == NULL)
1056 continue;
1057
Jouni Malinen87fd2792011-05-16 18:35:42 +03001058 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 if (rsn == NULL)
1060 continue;
1061
Jouni Malinen87fd2792011-05-16 18:35:42 +03001062 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 }
1064
1065}
1066
1067
1068static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1069 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001070 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001072 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073 int min_diff;
1074
1075 if (wpa_s->reassociate)
1076 return 1; /* explicit request to reassociate */
1077 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1078 return 1; /* we are not associated; continue */
1079 if (wpa_s->current_ssid == NULL)
1080 return 1; /* unknown current SSID */
1081 if (wpa_s->current_ssid != ssid)
1082 return 1; /* different network block */
1083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 if (wpas_driver_bss_selection(wpa_s))
1085 return 0; /* Driver-based roaming */
1086
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001087 if (wpa_s->current_ssid->ssid)
1088 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1089 wpa_s->current_ssid->ssid,
1090 wpa_s->current_ssid->ssid_len);
1091 if (!current_bss)
1092 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093
1094 if (!current_bss)
1095 return 1; /* current BSS not seen in scan results */
1096
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001097 if (current_bss == selected)
1098 return 0;
1099
1100 if (selected->last_update_idx > current_bss->last_update_idx)
1101 return 1; /* current BSS not seen in the last scan */
1102
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001103#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1105 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1106 MAC2STR(current_bss->bssid), current_bss->level);
1107 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1108 MAC2STR(selected->bssid), selected->level);
1109
1110 if (wpa_s->current_ssid->bssid_set &&
1111 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1112 0) {
1113 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1114 "has preferred BSSID");
1115 return 1;
1116 }
1117
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001118 if (current_bss->level < 0 && current_bss->level > selected->level) {
1119 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1120 "signal level");
1121 return 0;
1122 }
1123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 min_diff = 2;
1125 if (current_bss->level < 0) {
1126 if (current_bss->level < -85)
1127 min_diff = 1;
1128 else if (current_bss->level < -80)
1129 min_diff = 2;
1130 else if (current_bss->level < -75)
1131 min_diff = 3;
1132 else if (current_bss->level < -70)
1133 min_diff = 4;
1134 else
1135 min_diff = 5;
1136 }
1137 if (abs(current_bss->level - selected->level) < min_diff) {
1138 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1139 "in signal level");
1140 return 0;
1141 }
1142
1143 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001144#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001145 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001146#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147}
1148
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001149
Jouni Malinen89ca7022012-09-14 13:03:12 -07001150/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001151 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001152static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001153 union wpa_event_data *data,
1154 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 struct wpa_scan_results *scan_res;
1157 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001158#ifndef CONFIG_NO_RANDOM_POOL
1159 size_t i, num;
1160#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161
1162#ifdef CONFIG_AP
1163 if (wpa_s->ap_iface)
1164 ap = 1;
1165#endif /* CONFIG_AP */
1166
1167 wpa_supplicant_notify_scanning(wpa_s, 0);
1168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169#ifdef CONFIG_P2P
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001170 if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
Jouni Malinendc7b7132012-09-14 12:53:47 -07001171 !wpa_s->global->p2p_disabled &&
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001172 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1173 !wpa_s->scan_res_handler) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001174 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1177 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001178 wpa_s->sta_scan_pending = 1;
1179 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001180 return -1;
1181 }
1182 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001183 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184#endif /* CONFIG_P2P */
1185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1187 data ? &data->scan_info :
1188 NULL, 1);
1189 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001190 if (wpa_s->conf->ap_scan == 2 || ap ||
1191 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001193 if (!own_request)
1194 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1196 "scanning again");
1197 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1198 return -1;
1199 }
1200
1201#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 num = scan_res->num;
1203 if (num > 10)
1204 num = 10;
1205 for (i = 0; i < num; i++) {
1206 u8 buf[5];
1207 struct wpa_scan_res *res = scan_res->res[i];
1208 buf[0] = res->bssid[5];
1209 buf[1] = res->qual & 0xff;
1210 buf[2] = res->noise & 0xff;
1211 buf[3] = res->level & 0xff;
1212 buf[4] = res->tsf & 0xff;
1213 random_add_randomness(buf, sizeof(buf));
1214 }
1215#endif /* CONFIG_NO_RANDOM_POOL */
1216
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001217 if (own_request && wpa_s->scan_res_handler) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1219 struct wpa_scan_results *scan_res);
1220
1221 scan_res_handler = wpa_s->scan_res_handler;
1222 wpa_s->scan_res_handler = NULL;
1223 scan_res_handler(wpa_s, scan_res);
1224
1225 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001226 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 }
1228
1229 if (ap) {
1230 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1231#ifdef CONFIG_AP
1232 if (wpa_s->ap_iface->scan_cb)
1233 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1234#endif /* CONFIG_AP */
1235 wpa_scan_results_free(scan_res);
1236 return 0;
1237 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001238
Dmitry Shmidt04949592012-07-19 12:16:46 -07001239 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1240 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1241 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242
1243 wpas_notify_scan_done(wpa_s, 1);
1244
Dmitry Shmidt04949592012-07-19 12:16:46 -07001245 if (sme_proc_obss_scan(wpa_s) > 0) {
1246 wpa_scan_results_free(scan_res);
1247 return 0;
1248 }
1249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1251 wpa_scan_results_free(scan_res);
1252 return 0;
1253 }
1254
Dmitry Shmidt04949592012-07-19 12:16:46 -07001255 if (autoscan_notify_scan(wpa_s, scan_res)) {
1256 wpa_scan_results_free(scan_res);
1257 return 0;
1258 }
1259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 if (wpa_s->disconnected) {
1261 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1262 wpa_scan_results_free(scan_res);
1263 return 0;
1264 }
1265
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266 if (!wpas_driver_bss_selection(wpa_s) &&
1267 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 wpa_scan_results_free(scan_res);
1269 return 0;
1270 }
1271
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001272 wpas_wps_update_ap_info(wpa_s, scan_res);
1273
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001274 wpa_scan_results_free(scan_res);
1275
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001276 return wpas_select_network_from_last_scan(wpa_s, 1);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001277}
1278
1279
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001280static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1281 int new_scan)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001282{
1283 struct wpa_bss *selected;
1284 struct wpa_ssid *ssid = NULL;
1285
1286 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287
1288 if (selected) {
1289 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001290 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001291 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001292 if (new_scan)
1293 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001295 }
1296
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001297 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001298 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001299 return -1;
1300 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001301 if (new_scan)
1302 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001303 /*
1304 * Do not notify other virtual radios of scan results since we do not
1305 * want them to start other associations at the same time.
1306 */
1307 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1310 ssid = wpa_supplicant_pick_new_network(wpa_s);
1311 if (ssid) {
1312 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1313 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001314 if (new_scan)
1315 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 } else {
1317 int timeout_sec = wpa_s->scan_interval;
1318 int timeout_usec = 0;
1319#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001320 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1321 return 0;
1322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 if (wpa_s->p2p_in_provisioning) {
1324 /*
1325 * Use shorter wait during P2P Provisioning
1326 * state to speed up group formation.
1327 */
1328 timeout_sec = 0;
1329 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001330 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1331 timeout_usec);
1332 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 }
1334#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001335#ifdef CONFIG_INTERWORKING
1336 if (wpa_s->conf->auto_interworking &&
1337 wpa_s->conf->interworking &&
1338 wpa_s->conf->cred) {
1339 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1340 "start ANQP fetch since no matching "
1341 "networks found");
1342 wpa_s->network_select = 1;
1343 wpa_s->auto_network_select = 1;
1344 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001345 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001346 }
1347#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001348 if (wpa_supplicant_req_sched_scan(wpa_s))
1349 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1350 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 }
1352 }
1353 return 0;
1354}
1355
1356
1357static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1358 union wpa_event_data *data)
1359{
1360 const char *rn, *rn2;
1361 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001362
1363 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 /*
1365 * If no scan results could be fetched, then no need to
1366 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001367 * this scan. Similarly, if scan results started a new operation on this
1368 * interface, do not notify other interfaces to avoid concurrent
1369 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 */
1371 return;
1372 }
1373
1374 /*
1375 * Check other interfaces to see if they have the same radio-name. If
1376 * so, they get updated with this same scan info.
1377 */
1378 if (!wpa_s->driver->get_radio_name)
1379 return;
1380
1381 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1382 if (rn == NULL || rn[0] == '\0')
1383 return;
1384
1385 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1386 "sharing same radio (%s) in event_scan_results", rn);
1387
1388 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1389 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1390 continue;
1391
1392 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1393 if (rn2 && os_strcmp(rn, rn2) == 0) {
1394 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1395 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001396 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 }
1398 }
1399}
1400
1401#endif /* CONFIG_NO_SCAN_PROCESSING */
1402
1403
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001404int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1405{
1406#ifdef CONFIG_NO_SCAN_PROCESSING
1407 return -1;
1408#else /* CONFIG_NO_SCAN_PROCESSING */
1409 struct os_time now;
1410
1411 if (wpa_s->last_scan_res_used <= 0)
1412 return -1;
1413
1414 os_get_time(&now);
1415 if (now.sec - wpa_s->last_scan.sec > 5) {
1416 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1417 return -1;
1418 }
1419
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001420 return wpas_select_network_from_last_scan(wpa_s, 0);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001421#endif /* CONFIG_NO_SCAN_PROCESSING */
1422}
1423
Dmitry Shmidt04949592012-07-19 12:16:46 -07001424#ifdef CONFIG_WNM
1425
1426static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1427{
1428 struct wpa_supplicant *wpa_s = eloop_ctx;
1429
1430 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1431 return;
1432
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001433 if (!wpa_s->no_keep_alive) {
1434 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1435 MAC2STR(wpa_s->bssid));
1436 /* TODO: could skip this if normal data traffic has been sent */
1437 /* TODO: Consider using some more appropriate data frame for
1438 * this */
1439 if (wpa_s->l2)
1440 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1441 (u8 *) "", 0);
1442 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001443
1444#ifdef CONFIG_SME
1445 if (wpa_s->sme.bss_max_idle_period) {
1446 unsigned int msec;
1447 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1448 if (msec > 100)
1449 msec -= 100;
1450 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1451 wnm_bss_keep_alive, wpa_s, NULL);
1452 }
1453#endif /* CONFIG_SME */
1454}
1455
1456
1457static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1458 const u8 *ies, size_t ies_len)
1459{
1460 struct ieee802_11_elems elems;
1461
1462 if (ies == NULL)
1463 return;
1464
1465 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1466 return;
1467
1468#ifdef CONFIG_SME
1469 if (elems.bss_max_idle_period) {
1470 unsigned int msec;
1471 wpa_s->sme.bss_max_idle_period =
1472 WPA_GET_LE16(elems.bss_max_idle_period);
1473 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1474 "TU)%s", wpa_s->sme.bss_max_idle_period,
1475 (elems.bss_max_idle_period[2] & 0x01) ?
1476 " (protected keep-live required)" : "");
1477 if (wpa_s->sme.bss_max_idle_period == 0)
1478 wpa_s->sme.bss_max_idle_period = 1;
1479 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1480 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1481 /* msec times 1000 */
1482 msec = wpa_s->sme.bss_max_idle_period * 1024;
1483 if (msec > 100)
1484 msec -= 100;
1485 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1486 wnm_bss_keep_alive, wpa_s,
1487 NULL);
1488 }
1489 }
1490#endif /* CONFIG_SME */
1491}
1492
1493#endif /* CONFIG_WNM */
1494
1495
1496void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1497{
1498#ifdef CONFIG_WNM
1499 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1500#endif /* CONFIG_WNM */
1501}
1502
1503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1505 union wpa_event_data *data)
1506{
1507 int l, len, found = 0, wpa_found, rsn_found;
1508 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001509#ifdef CONFIG_IEEE80211R
1510 u8 bssid[ETH_ALEN];
1511#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512
1513 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1514 if (data->assoc_info.req_ies)
1515 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1516 data->assoc_info.req_ies_len);
1517 if (data->assoc_info.resp_ies) {
1518 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1519 data->assoc_info.resp_ies_len);
1520#ifdef CONFIG_TDLS
1521 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1522 data->assoc_info.resp_ies_len);
1523#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001524#ifdef CONFIG_WNM
1525 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1526 data->assoc_info.resp_ies_len);
1527#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 }
1529 if (data->assoc_info.beacon_ies)
1530 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1531 data->assoc_info.beacon_ies,
1532 data->assoc_info.beacon_ies_len);
1533 if (data->assoc_info.freq)
1534 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1535 data->assoc_info.freq);
1536
1537 p = data->assoc_info.req_ies;
1538 l = data->assoc_info.req_ies_len;
1539
1540 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1541 while (p && l >= 2) {
1542 len = p[1] + 2;
1543 if (len > l) {
1544 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1545 p, l);
1546 break;
1547 }
1548 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1549 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1550 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1551 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1552 break;
1553 found = 1;
1554 wpa_find_assoc_pmkid(wpa_s);
1555 break;
1556 }
1557 l -= len;
1558 p += len;
1559 }
1560 if (!found && data->assoc_info.req_ies)
1561 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1562
1563#ifdef CONFIG_IEEE80211R
1564#ifdef CONFIG_SME
1565 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1567 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1568 data->assoc_info.resp_ies,
1569 data->assoc_info.resp_ies_len,
1570 bssid) < 0) {
1571 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1572 "Reassociation Response failed");
1573 wpa_supplicant_deauthenticate(
1574 wpa_s, WLAN_REASON_INVALID_IE);
1575 return -1;
1576 }
1577 }
1578
1579 p = data->assoc_info.resp_ies;
1580 l = data->assoc_info.resp_ies_len;
1581
1582#ifdef CONFIG_WPS_STRICT
1583 if (p && wpa_s->current_ssid &&
1584 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1585 struct wpabuf *wps;
1586 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1587 if (wps == NULL) {
1588 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1589 "include WPS IE in (Re)Association Response");
1590 return -1;
1591 }
1592
1593 if (wps_validate_assoc_resp(wps) < 0) {
1594 wpabuf_free(wps);
1595 wpa_supplicant_deauthenticate(
1596 wpa_s, WLAN_REASON_INVALID_IE);
1597 return -1;
1598 }
1599 wpabuf_free(wps);
1600 }
1601#endif /* CONFIG_WPS_STRICT */
1602
1603 /* Go through the IEs and make a copy of the MDIE, if present. */
1604 while (p && l >= 2) {
1605 len = p[1] + 2;
1606 if (len > l) {
1607 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1608 p, l);
1609 break;
1610 }
1611 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1612 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1613 wpa_s->sme.ft_used = 1;
1614 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1615 MOBILITY_DOMAIN_ID_LEN);
1616 break;
1617 }
1618 l -= len;
1619 p += len;
1620 }
1621#endif /* CONFIG_SME */
1622
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001623 /* Process FT when SME is in the driver */
1624 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1625 wpa_ft_is_completed(wpa_s->wpa)) {
1626 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1627 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1628 data->assoc_info.resp_ies,
1629 data->assoc_info.resp_ies_len,
1630 bssid) < 0) {
1631 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1632 "Reassociation Response failed");
1633 wpa_supplicant_deauthenticate(
1634 wpa_s, WLAN_REASON_INVALID_IE);
1635 return -1;
1636 }
1637 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1638 }
1639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1641 data->assoc_info.resp_ies_len);
1642#endif /* CONFIG_IEEE80211R */
1643
1644 /* WPA/RSN IE from Beacon/ProbeResp */
1645 p = data->assoc_info.beacon_ies;
1646 l = data->assoc_info.beacon_ies_len;
1647
1648 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1649 */
1650 wpa_found = rsn_found = 0;
1651 while (p && l >= 2) {
1652 len = p[1] + 2;
1653 if (len > l) {
1654 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1655 p, l);
1656 break;
1657 }
1658 if (!wpa_found &&
1659 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1660 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1661 wpa_found = 1;
1662 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1663 }
1664
1665 if (!rsn_found &&
1666 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1667 rsn_found = 1;
1668 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1669 }
1670
1671 l -= len;
1672 p += len;
1673 }
1674
1675 if (!wpa_found && data->assoc_info.beacon_ies)
1676 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1677 if (!rsn_found && data->assoc_info.beacon_ies)
1678 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1679 if (wpa_found || rsn_found)
1680 wpa_s->ap_ies_from_associnfo = 1;
1681
Jouni Malinen87fd2792011-05-16 18:35:42 +03001682 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1683 wpa_s->assoc_freq != data->assoc_info.freq) {
1684 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1685 "%u to %u MHz",
1686 wpa_s->assoc_freq, data->assoc_info.freq);
1687 wpa_supplicant_update_scan_results(wpa_s);
1688 }
1689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 wpa_s->assoc_freq = data->assoc_info.freq;
1691
1692 return 0;
1693}
1694
1695
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001696static struct wpa_bss * wpa_supplicant_get_new_bss(
1697 struct wpa_supplicant *wpa_s, const u8 *bssid)
1698{
1699 struct wpa_bss *bss = NULL;
1700 struct wpa_ssid *ssid = wpa_s->current_ssid;
1701
1702 if (ssid->ssid_len > 0)
1703 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1704 if (!bss)
1705 bss = wpa_bss_get_bssid(wpa_s, bssid);
1706
1707 return bss;
1708}
1709
1710
1711static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1712{
1713 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1714
1715 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1716 return -1;
1717
1718 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1719 return 0;
1720
1721 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1722 WPA_IE_VENDOR_TYPE);
1723 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1724
1725 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1726 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1727 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1728 bss_rsn ? 2 + bss_rsn[1] : 0))
1729 return -1;
1730
1731 return 0;
1732}
1733
1734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1736 union wpa_event_data *data)
1737{
1738 u8 bssid[ETH_ALEN];
1739 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740
1741#ifdef CONFIG_AP
1742 if (wpa_s->ap_iface) {
1743 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1744 data->assoc_info.addr,
1745 data->assoc_info.req_ies,
1746 data->assoc_info.req_ies_len,
1747 data->assoc_info.reassoc);
1748 return;
1749 }
1750#endif /* CONFIG_AP */
1751
1752 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1753 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1754 return;
1755
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001756 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1757 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001758 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001759 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1760 return;
1761 }
1762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001764 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1766 MACSTR, MAC2STR(bssid));
1767 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1769 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001770 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1773 wpa_clear_keys(wpa_s, bssid);
1774 }
1775 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001776 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1778 return;
1779 }
1780 if (wpa_s->current_ssid) {
1781 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001782
1783 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1784 if (!bss) {
1785 wpa_supplicant_update_scan_results(wpa_s);
1786
1787 /* Get the BSS from the new scan results */
1788 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1789 }
1790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 if (bss)
1792 wpa_s->current_bss = bss;
1793 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001794
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001795#ifdef ANDROID
1796 if (wpa_s->conf->ap_scan == 1) {
1797#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001798 if (wpa_s->conf->ap_scan == 1 &&
1799 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001800#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001801 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1802 wpa_msg(wpa_s, MSG_WARNING,
1803 "WPA/RSN IEs not updated");
1804 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 }
1806
1807#ifdef CONFIG_SME
1808 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1809 wpa_s->sme.prev_bssid_set = 1;
1810#endif /* CONFIG_SME */
1811
1812 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1813 if (wpa_s->current_ssid) {
1814 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1815 * initialized before association, but for other modes,
1816 * initialize PC/SC here, if the current configuration needs
1817 * smartcard or SIM/USIM. */
1818 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1819 }
1820 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1821 if (wpa_s->l2)
1822 l2_packet_notify_auth_start(wpa_s->l2);
1823
1824 /*
1825 * Set portEnabled first to FALSE in order to get EAP state machine out
1826 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1827 * state machine may transit to AUTHENTICATING state based on obsolete
1828 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1829 * AUTHENTICATED without ever giving chance to EAP state machine to
1830 * reset the state.
1831 */
1832 if (!ft_completed) {
1833 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1834 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1835 }
1836 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1837 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1838 /* 802.1X::portControl = Auto */
1839 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1840 wpa_s->eapol_received = 0;
1841 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1842 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1843 (wpa_s->current_ssid &&
1844 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001845 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1846 (wpa_s->drv_flags &
1847 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1848 /*
1849 * Set the key after having received joined-IBSS event
1850 * from the driver.
1851 */
1852 wpa_supplicant_set_wpa_none_key(wpa_s,
1853 wpa_s->current_ssid);
1854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 wpa_supplicant_cancel_auth_timeout(wpa_s);
1856 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1857 } else if (!ft_completed) {
1858 /* Timeout for receiving the first EAPOL packet */
1859 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1860 }
1861 wpa_supplicant_cancel_scan(wpa_s);
1862
1863 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1864 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1865 /*
1866 * We are done; the driver will take care of RSN 4-way
1867 * handshake.
1868 */
1869 wpa_supplicant_cancel_auth_timeout(wpa_s);
1870 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1871 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1872 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1873 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1874 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1875 /*
1876 * The driver will take care of RSN 4-way handshake, so we need
1877 * to allow EAPOL supplicant to complete its work without
1878 * waiting for WPA supplicant.
1879 */
1880 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1881 } else if (ft_completed) {
1882 /*
1883 * FT protocol completed - make sure EAPOL state machine ends
1884 * up in authenticated.
1885 */
1886 wpa_supplicant_cancel_auth_timeout(wpa_s);
1887 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1888 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1889 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1890 }
1891
Jouni Malinena05074c2012-12-21 21:35:35 +02001892 wpa_s->last_eapol_matches_bssid = 0;
1893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 if (wpa_s->pending_eapol_rx) {
1895 struct os_time now, age;
1896 os_get_time(&now);
1897 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1898 if (age.sec == 0 && age.usec < 100000 &&
1899 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1900 0) {
1901 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1902 "frame that was received just before "
1903 "association notification");
1904 wpa_supplicant_rx_eapol(
1905 wpa_s, wpa_s->pending_eapol_rx_src,
1906 wpabuf_head(wpa_s->pending_eapol_rx),
1907 wpabuf_len(wpa_s->pending_eapol_rx));
1908 }
1909 wpabuf_free(wpa_s->pending_eapol_rx);
1910 wpa_s->pending_eapol_rx = NULL;
1911 }
1912
1913 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1914 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001915 wpa_s->current_ssid &&
1916 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 /* Set static WEP keys again */
1918 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1919 }
1920
1921#ifdef CONFIG_IBSS_RSN
1922 if (wpa_s->current_ssid &&
1923 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1924 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1925 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1926 wpa_s->ibss_rsn == NULL) {
1927 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1928 if (!wpa_s->ibss_rsn) {
1929 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1930 wpa_supplicant_deauthenticate(
1931 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1932 return;
1933 }
1934
1935 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1936 }
1937#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001938
1939 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940}
1941
1942
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001943static int disconnect_reason_recoverable(u16 reason_code)
1944{
1945 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1946 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1947 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1948}
1949
1950
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001952 u16 reason_code,
1953 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954{
1955 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001956
1957 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1958 /*
1959 * At least Host AP driver and a Prism3 card seemed to be
1960 * generating streams of disconnected events when configuring
1961 * IBSS for WPA-None. Ignore them for now.
1962 */
1963 return;
1964 }
1965
1966 bssid = wpa_s->bssid;
1967 if (is_zero_ether_addr(bssid))
1968 bssid = wpa_s->pending_bssid;
1969
1970 if (!is_zero_ether_addr(bssid) ||
1971 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1972 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1973 " reason=%d%s",
1974 MAC2STR(bssid), reason_code,
1975 locally_generated ? " locally_generated=1" : "");
1976 }
1977}
1978
1979
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001980static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1981 int locally_generated)
1982{
1983 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1984 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1985 return 0; /* Not in 4-way handshake with PSK */
1986
1987 /*
1988 * It looks like connection was lost while trying to go through PSK
1989 * 4-way handshake. Filter out known disconnection cases that are caused
1990 * by something else than PSK mismatch to avoid confusing reports.
1991 */
1992
1993 if (locally_generated) {
1994 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1995 return 0;
1996 }
1997
1998 return 1;
1999}
2000
2001
Jouni Malinen2b89da82012-08-31 22:04:41 +03002002static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2003 u16 reason_code,
2004 int locally_generated)
2005{
2006 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 int authenticating;
2008 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002009 struct wpa_bss *fast_reconnect = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002010#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002011 struct wpa_ssid *fast_reconnect_ssid = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002012#endif /* CONFIG_NO_SCAN_PROCESSING */
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002013 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014
2015 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2016 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2017
2018 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2019 /*
2020 * At least Host AP driver and a Prism3 card seemed to be
2021 * generating streams of disconnected events when configuring
2022 * IBSS for WPA-None. Ignore them for now.
2023 */
2024 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2025 "IBSS/WPA-None mode");
2026 return;
2027 }
2028
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002029 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2031 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002032 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002034 if (!wpa_s->disconnected &&
2035 (!wpa_s->auto_reconnect_disabled ||
2036 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002037 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2038 "reconnect (wps=%d wpa_state=%d)",
2039 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2040 wpa_s->wpa_state);
2041 if (wpa_s->wpa_state == WPA_COMPLETED &&
2042 wpa_s->current_ssid &&
2043 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002044 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002045 disconnect_reason_recoverable(reason_code)) {
2046 /*
2047 * It looks like the AP has dropped association with
2048 * us, but could allow us to get back in. Try to
2049 * reconnect to the same BSS without full scan to save
2050 * time for some common cases.
2051 */
2052 fast_reconnect = wpa_s->current_bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002053#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002054 fast_reconnect_ssid = wpa_s->current_ssid;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002055#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002056 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002057#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07002058 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002059#else
2060 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2061#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062 else
2063 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2064 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002066 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 "try to re-connect");
2068 wpa_s->reassociate = 0;
2069 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002070 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 }
2072 bssid = wpa_s->bssid;
2073 if (is_zero_ether_addr(bssid))
2074 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002075 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2076 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002078 if (locally_generated)
2079 wpa_s->disconnect_reason = -reason_code;
2080 else
2081 wpa_s->disconnect_reason = reason_code;
2082 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2084 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2085 wpa_s->keys_cleared = 0;
2086 wpa_clear_keys(wpa_s, wpa_s->bssid);
2087 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002088 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 wpa_supplicant_mark_disassoc(wpa_s);
2090
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002091 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002093 wpa_s->current_ssid = last_ssid;
2094 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002095
2096 if (fast_reconnect) {
2097#ifndef CONFIG_NO_SCAN_PROCESSING
2098 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2099 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2100 fast_reconnect_ssid) < 0) {
2101 /* Recover through full scan */
2102 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2103 }
2104#endif /* CONFIG_NO_SCAN_PROCESSING */
2105 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106}
2107
2108
2109#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002110void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111{
2112 struct wpa_supplicant *wpa_s = eloop_ctx;
2113
2114 if (!wpa_s->pending_mic_error_report)
2115 return;
2116
2117 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2118 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2119 wpa_s->pending_mic_error_report = 0;
2120}
2121#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2122
2123
2124static void
2125wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2126 union wpa_event_data *data)
2127{
2128 int pairwise;
2129 struct os_time t;
2130
2131 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2132 pairwise = (data && data->michael_mic_failure.unicast);
2133 os_get_time(&t);
2134 if ((wpa_s->last_michael_mic_error &&
2135 t.sec - wpa_s->last_michael_mic_error <= 60) ||
2136 wpa_s->pending_mic_error_report) {
2137 if (wpa_s->pending_mic_error_report) {
2138 /*
2139 * Send the pending MIC error report immediately since
2140 * we are going to start countermeasures and AP better
2141 * do the same.
2142 */
2143 wpa_sm_key_request(wpa_s->wpa, 1,
2144 wpa_s->pending_mic_error_pairwise);
2145 }
2146
2147 /* Send the new MIC error report immediately since we are going
2148 * to start countermeasures and AP better do the same.
2149 */
2150 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2151
2152 /* initialize countermeasures */
2153 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002154
2155 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2158
2159 /*
2160 * Need to wait for completion of request frame. We do not get
2161 * any callback for the message completion, so just wait a
2162 * short while and hope for the best. */
2163 os_sleep(0, 10000);
2164
2165 wpa_drv_set_countermeasures(wpa_s, 1);
2166 wpa_supplicant_deauthenticate(wpa_s,
2167 WLAN_REASON_MICHAEL_MIC_FAILURE);
2168 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2169 wpa_s, NULL);
2170 eloop_register_timeout(60, 0,
2171 wpa_supplicant_stop_countermeasures,
2172 wpa_s, NULL);
2173 /* TODO: mark the AP rejected for 60 second. STA is
2174 * allowed to associate with another AP.. */
2175 } else {
2176#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2177 if (wpa_s->mic_errors_seen) {
2178 /*
2179 * Reduce the effectiveness of Michael MIC error
2180 * reports as a means for attacking against TKIP if
2181 * more than one MIC failure is noticed with the same
2182 * PTK. We delay the transmission of the reports by a
2183 * random time between 0 and 60 seconds in order to
2184 * force the attacker wait 60 seconds before getting
2185 * the information on whether a frame resulted in a MIC
2186 * failure.
2187 */
2188 u8 rval[4];
2189 int sec;
2190
2191 if (os_get_random(rval, sizeof(rval)) < 0)
2192 sec = os_random() % 60;
2193 else
2194 sec = WPA_GET_BE32(rval) % 60;
2195 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2196 "report %d seconds", sec);
2197 wpa_s->pending_mic_error_report = 1;
2198 wpa_s->pending_mic_error_pairwise = pairwise;
2199 eloop_cancel_timeout(
2200 wpa_supplicant_delayed_mic_error_report,
2201 wpa_s, NULL);
2202 eloop_register_timeout(
2203 sec, os_random() % 1000000,
2204 wpa_supplicant_delayed_mic_error_report,
2205 wpa_s, NULL);
2206 } else {
2207 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2208 }
2209#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2210 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2211#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2212 }
2213 wpa_s->last_michael_mic_error = t.sec;
2214 wpa_s->mic_errors_seen++;
2215}
2216
2217
2218#ifdef CONFIG_TERMINATE_ONLASTIF
2219static int any_interfaces(struct wpa_supplicant *head)
2220{
2221 struct wpa_supplicant *wpa_s;
2222
2223 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2224 if (!wpa_s->interface_removed)
2225 return 1;
2226 return 0;
2227}
2228#endif /* CONFIG_TERMINATE_ONLASTIF */
2229
2230
2231static void
2232wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2233 union wpa_event_data *data)
2234{
2235 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2236 return;
2237
2238 switch (data->interface_status.ievent) {
2239 case EVENT_INTERFACE_ADDED:
2240 if (!wpa_s->interface_removed)
2241 break;
2242 wpa_s->interface_removed = 0;
2243 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2244 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2245 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2246 "driver after interface was added");
2247 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002248 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 break;
2250 case EVENT_INTERFACE_REMOVED:
2251 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2252 wpa_s->interface_removed = 1;
2253 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002254 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255 l2_packet_deinit(wpa_s->l2);
2256 wpa_s->l2 = NULL;
2257#ifdef CONFIG_IBSS_RSN
2258 ibss_rsn_deinit(wpa_s->ibss_rsn);
2259 wpa_s->ibss_rsn = NULL;
2260#endif /* CONFIG_IBSS_RSN */
2261#ifdef CONFIG_TERMINATE_ONLASTIF
2262 /* check if last interface */
2263 if (!any_interfaces(wpa_s->global->ifaces))
2264 eloop_terminate();
2265#endif /* CONFIG_TERMINATE_ONLASTIF */
2266 break;
2267 }
2268}
2269
2270
2271#ifdef CONFIG_PEERKEY
2272static void
2273wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2274 union wpa_event_data *data)
2275{
2276 if (data == NULL)
2277 return;
2278 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2279}
2280#endif /* CONFIG_PEERKEY */
2281
2282
2283#ifdef CONFIG_TDLS
2284static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2285 union wpa_event_data *data)
2286{
2287 if (data == NULL)
2288 return;
2289 switch (data->tdls.oper) {
2290 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002291 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2292 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2293 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2294 else
2295 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 break;
2297 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002298 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002299 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 break;
2301 }
2302}
2303#endif /* CONFIG_TDLS */
2304
2305
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002306#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002307static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2308 union wpa_event_data *data)
2309{
2310 if (data == NULL)
2311 return;
2312 switch (data->wnm.oper) {
2313 case WNM_OPER_SLEEP:
2314 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2315 "(action=%d, intval=%d)",
2316 data->wnm.sleep_action, data->wnm.sleep_intval);
2317 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002318 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002319 break;
2320 }
2321}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002322#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002323
2324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325#ifdef CONFIG_IEEE80211R
2326static void
2327wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2328 union wpa_event_data *data)
2329{
2330 if (data == NULL)
2331 return;
2332
2333 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2334 data->ft_ies.ies_len,
2335 data->ft_ies.ft_action,
2336 data->ft_ies.target_ap,
2337 data->ft_ies.ric_ies,
2338 data->ft_ies.ric_ies_len) < 0) {
2339 /* TODO: prevent MLME/driver from trying to associate? */
2340 }
2341}
2342#endif /* CONFIG_IEEE80211R */
2343
2344
2345#ifdef CONFIG_IBSS_RSN
2346static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2347 union wpa_event_data *data)
2348{
2349 struct wpa_ssid *ssid;
2350 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2351 return;
2352 if (data == NULL)
2353 return;
2354 ssid = wpa_s->current_ssid;
2355 if (ssid == NULL)
2356 return;
2357 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2358 return;
2359
2360 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2361}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002362
2363
2364static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2365 union wpa_event_data *data)
2366{
2367 struct wpa_ssid *ssid = wpa_s->current_ssid;
2368
2369 if (ssid == NULL)
2370 return;
2371
2372 /* check if the ssid is correctly configured as IBSS/RSN */
2373 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2374 return;
2375
2376 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2377 data->rx_mgmt.frame_len);
2378}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379#endif /* CONFIG_IBSS_RSN */
2380
2381
2382#ifdef CONFIG_IEEE80211R
2383static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2384 size_t len)
2385{
2386 const u8 *sta_addr, *target_ap_addr;
2387 u16 status;
2388
2389 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2390 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2391 return; /* only SME case supported for now */
2392 if (len < 1 + 2 * ETH_ALEN + 2)
2393 return;
2394 if (data[0] != 2)
2395 return; /* Only FT Action Response is supported for now */
2396 sta_addr = data + 1;
2397 target_ap_addr = data + 1 + ETH_ALEN;
2398 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2399 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2400 MACSTR " TargetAP " MACSTR " status %u",
2401 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2402
2403 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2404 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2405 " in FT Action Response", MAC2STR(sta_addr));
2406 return;
2407 }
2408
2409 if (status) {
2410 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2411 "failure (status code %d)", status);
2412 /* TODO: report error to FT code(?) */
2413 return;
2414 }
2415
2416 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2417 len - (1 + 2 * ETH_ALEN + 2), 1,
2418 target_ap_addr, NULL, 0) < 0)
2419 return;
2420
2421#ifdef CONFIG_SME
2422 {
2423 struct wpa_bss *bss;
2424 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2425 if (bss)
2426 wpa_s->sme.freq = bss->freq;
2427 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2428 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2429 WLAN_AUTH_FT);
2430 }
2431#endif /* CONFIG_SME */
2432}
2433#endif /* CONFIG_IEEE80211R */
2434
2435
2436static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2437 struct unprot_deauth *e)
2438{
2439#ifdef CONFIG_IEEE80211W
2440 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2441 "dropped: " MACSTR " -> " MACSTR
2442 " (reason code %u)",
2443 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2444 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2445#endif /* CONFIG_IEEE80211W */
2446}
2447
2448
2449static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2450 struct unprot_disassoc *e)
2451{
2452#ifdef CONFIG_IEEE80211W
2453 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2454 "dropped: " MACSTR " -> " MACSTR
2455 " (reason code %u)",
2456 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2457 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2458#endif /* CONFIG_IEEE80211W */
2459}
2460
2461
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002462static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2463 u16 reason_code, int locally_generated,
2464 const u8 *ie, size_t ie_len, int deauth)
2465{
2466#ifdef CONFIG_AP
2467 if (wpa_s->ap_iface && addr) {
2468 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2469 return;
2470 }
2471
2472 if (wpa_s->ap_iface) {
2473 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2474 return;
2475 }
2476#endif /* CONFIG_AP */
2477
2478 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2479
2480 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2481 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2482 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2483 eapol_sm_failed(wpa_s->eapol)))
2484 wpas_auth_failed(wpa_s);
2485
2486#ifdef CONFIG_P2P
2487 if (deauth && ie && ie_len > 0) {
2488 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2489 locally_generated) > 0) {
2490 /*
2491 * The interface was removed, so cannot continue
2492 * processing any additional operations after this.
2493 */
2494 return;
2495 }
2496 }
2497#endif /* CONFIG_P2P */
2498
2499 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2500 locally_generated);
2501}
2502
2503
2504static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2505 struct disassoc_info *info)
2506{
2507 u16 reason_code = 0;
2508 int locally_generated = 0;
2509 const u8 *addr = NULL;
2510 const u8 *ie = NULL;
2511 size_t ie_len = 0;
2512
2513 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2514
2515 if (info) {
2516 addr = info->addr;
2517 ie = info->ie;
2518 ie_len = info->ie_len;
2519 reason_code = info->reason_code;
2520 locally_generated = info->locally_generated;
2521 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2522 locally_generated ? " (locally generated)" : "");
2523 if (addr)
2524 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2525 MAC2STR(addr));
2526 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2527 ie, ie_len);
2528 }
2529
2530#ifdef CONFIG_AP
2531 if (wpa_s->ap_iface && info && info->addr) {
2532 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2533 return;
2534 }
2535
2536 if (wpa_s->ap_iface) {
2537 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2538 return;
2539 }
2540#endif /* CONFIG_AP */
2541
2542#ifdef CONFIG_P2P
2543 if (info) {
2544 wpas_p2p_disassoc_notif(
2545 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2546 locally_generated);
2547 }
2548#endif /* CONFIG_P2P */
2549
2550 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2551 sme_event_disassoc(wpa_s, info);
2552
2553 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2554 ie, ie_len, 0);
2555}
2556
2557
2558static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2559 struct deauth_info *info)
2560{
2561 u16 reason_code = 0;
2562 int locally_generated = 0;
2563 const u8 *addr = NULL;
2564 const u8 *ie = NULL;
2565 size_t ie_len = 0;
2566
2567 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2568
2569 if (info) {
2570 addr = info->addr;
2571 ie = info->ie;
2572 ie_len = info->ie_len;
2573 reason_code = info->reason_code;
2574 locally_generated = info->locally_generated;
2575 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2576 reason_code,
2577 locally_generated ? " (locally generated)" : "");
2578 if (addr) {
2579 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2580 MAC2STR(addr));
2581 }
2582 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2583 ie, ie_len);
2584 }
2585
2586 wpa_reset_ft_completed(wpa_s->wpa);
2587
2588 wpas_event_disconnect(wpa_s, addr, reason_code,
2589 locally_generated, ie, ie_len, 1);
2590}
2591
2592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2594 union wpa_event_data *data)
2595{
2596 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597
2598 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2599 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002600 event != EVENT_INTERFACE_STATUS &&
2601 event != EVENT_SCHED_SCAN_STOPPED) {
2602 wpa_dbg(wpa_s, MSG_DEBUG,
2603 "Ignore event %s (%d) while interface is disabled",
2604 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 return;
2606 }
2607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608#ifndef CONFIG_NO_STDOUT_DEBUG
2609{
2610 int level = MSG_DEBUG;
2611
Dmitry Shmidt04949592012-07-19 12:16:46 -07002612 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002613 const struct ieee80211_hdr *hdr;
2614 u16 fc;
2615 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2616 fc = le_to_host16(hdr->frame_control);
2617 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2618 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2619 level = MSG_EXCESSIVE;
2620 }
2621
2622 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2623 event_to_string(event), event);
2624}
2625#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626
2627 switch (event) {
2628 case EVENT_AUTH:
2629 sme_event_auth(wpa_s, data);
2630 break;
2631 case EVENT_ASSOC:
2632 wpa_supplicant_event_assoc(wpa_s, data);
2633 break;
2634 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002635 wpas_event_disassoc(wpa_s,
2636 data ? &data->disassoc_info : NULL);
2637 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002639 wpas_event_deauth(wpa_s,
2640 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641 break;
2642 case EVENT_MICHAEL_MIC_FAILURE:
2643 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2644 break;
2645#ifndef CONFIG_NO_SCAN_PROCESSING
2646 case EVENT_SCAN_RESULTS:
2647 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002648 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2649 wpa_s->wpa_state != WPA_ASSOCIATING)
2650 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 break;
2652#endif /* CONFIG_NO_SCAN_PROCESSING */
2653 case EVENT_ASSOCINFO:
2654 wpa_supplicant_event_associnfo(wpa_s, data);
2655 break;
2656 case EVENT_INTERFACE_STATUS:
2657 wpa_supplicant_event_interface_status(wpa_s, data);
2658 break;
2659 case EVENT_PMKID_CANDIDATE:
2660 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2661 break;
2662#ifdef CONFIG_PEERKEY
2663 case EVENT_STKSTART:
2664 wpa_supplicant_event_stkstart(wpa_s, data);
2665 break;
2666#endif /* CONFIG_PEERKEY */
2667#ifdef CONFIG_TDLS
2668 case EVENT_TDLS:
2669 wpa_supplicant_event_tdls(wpa_s, data);
2670 break;
2671#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002672#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002673 case EVENT_WNM:
2674 wpa_supplicant_event_wnm(wpa_s, data);
2675 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002676#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002677#ifdef CONFIG_IEEE80211R
2678 case EVENT_FT_RESPONSE:
2679 wpa_supplicant_event_ft_response(wpa_s, data);
2680 break;
2681#endif /* CONFIG_IEEE80211R */
2682#ifdef CONFIG_IBSS_RSN
2683 case EVENT_IBSS_RSN_START:
2684 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2685 break;
2686#endif /* CONFIG_IBSS_RSN */
2687 case EVENT_ASSOC_REJECT:
2688 if (data->assoc_reject.bssid)
2689 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2690 "bssid=" MACSTR " status_code=%u",
2691 MAC2STR(data->assoc_reject.bssid),
2692 data->assoc_reject.status_code);
2693 else
2694 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2695 "status_code=%u",
2696 data->assoc_reject.status_code);
2697 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2698 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002699 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002700#ifdef ANDROID_P2P
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002701 if(!wpa_s->current_ssid) {
2702 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2703 break;
2704 }
2705 /* If assoc reject is reported by the driver, then avoid
2706 * waiting for the authentication timeout. Cancel the
2707 * authentication timeout and retry the assoc.
2708 */
Jeff Johnsonb485b182012-10-21 18:19:27 -07002709 if(wpa_s->current_ssid->assoc_retry++ < 10) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002710 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2711 wpa_s->current_ssid->assoc_retry);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002712
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002713 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002714
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002715 /* Clear the states */
2716 wpa_sm_notify_disassoc(wpa_s->wpa);
2717 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2718
2719 wpa_s->reassociate = 1;
Jeff Johnsonb485b182012-10-21 18:19:27 -07002720 if (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE) {
Dmitry Shmidt54cb0f62012-10-29 13:12:24 -07002721 const u8 *bl_bssid = data->assoc_reject.bssid;
2722 if (!bl_bssid || is_zero_ether_addr(bl_bssid))
2723 bl_bssid = wpa_s->pending_bssid;
2724 wpa_blacklist_add(wpa_s, bl_bssid);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002725 wpa_supplicant_req_scan(wpa_s, 0, 0);
2726 } else {
2727 wpa_supplicant_req_scan(wpa_s, 1, 0);
2728 }
2729 } else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002730 /* If we ASSOC_REJECT's hits threshold, disable the
2731 * network
2732 */
2733 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2734 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002735 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002736 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002737 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002738 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002739#else
2740 const u8 *bssid = data->assoc_reject.bssid;
2741 if (bssid == NULL || is_zero_ether_addr(bssid))
2742 bssid = wpa_s->pending_bssid;
2743 wpas_connection_failed(wpa_s, bssid);
2744 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002745#endif /* ANDROID_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002747 break;
2748 case EVENT_AUTH_TIMED_OUT:
2749 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2750 sme_event_auth_timed_out(wpa_s, data);
2751 break;
2752 case EVENT_ASSOC_TIMED_OUT:
2753 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2754 sme_event_assoc_timed_out(wpa_s, data);
2755 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 case EVENT_TX_STATUS:
2757 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2758 " type=%d stype=%d",
2759 MAC2STR(data->tx_status.dst),
2760 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002761#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002763#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2765 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002766 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 wpa_s, data->tx_status.dst,
2768 data->tx_status.data,
2769 data->tx_status.data_len,
2770 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002771 OFFCHANNEL_SEND_ACTION_SUCCESS :
2772 OFFCHANNEL_SEND_ACTION_NO_ACK);
2773#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 break;
2775 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776#endif /* CONFIG_AP */
2777#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2779 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2780 /*
2781 * Catch TX status events for Action frames we sent via group
2782 * interface in GO mode.
2783 */
2784 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2785 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2786 os_memcmp(wpa_s->parent->pending_action_dst,
2787 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002788 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 wpa_s->parent, data->tx_status.dst,
2790 data->tx_status.data,
2791 data->tx_status.data_len,
2792 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002793 OFFCHANNEL_SEND_ACTION_SUCCESS :
2794 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 break;
2796 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797#endif /* CONFIG_OFFCHANNEL */
2798#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 switch (data->tx_status.type) {
2800 case WLAN_FC_TYPE_MGMT:
2801 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2802 data->tx_status.data_len,
2803 data->tx_status.stype,
2804 data->tx_status.ack);
2805 break;
2806 case WLAN_FC_TYPE_DATA:
2807 ap_tx_status(wpa_s, data->tx_status.dst,
2808 data->tx_status.data,
2809 data->tx_status.data_len,
2810 data->tx_status.ack);
2811 break;
2812 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813#endif /* CONFIG_AP */
2814 break;
2815#ifdef CONFIG_AP
2816 case EVENT_EAPOL_TX_STATUS:
2817 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2818 data->eapol_tx_status.data,
2819 data->eapol_tx_status.data_len,
2820 data->eapol_tx_status.ack);
2821 break;
2822 case EVENT_DRIVER_CLIENT_POLL_OK:
2823 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 break;
2825 case EVENT_RX_FROM_UNKNOWN:
2826 if (wpa_s->ap_iface == NULL)
2827 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002828 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2829 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002831 case EVENT_CH_SWITCH:
2832 if (!data)
2833 break;
2834 if (!wpa_s->ap_iface) {
2835 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2836 "event in non-AP mode");
2837 break;
2838 }
2839
Dmitry Shmidt04949592012-07-19 12:16:46 -07002840 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2841 data->ch_switch.ht_enabled,
2842 data->ch_switch.ch_offset);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002844#endif /* CONFIG_AP */
2845#if defined(CONFIG_AP) || defined(CONFIG_IBSS_RSN)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002846 case EVENT_RX_MGMT: {
2847 u16 fc, stype;
2848 const struct ieee80211_mgmt *mgmt;
2849
2850 mgmt = (const struct ieee80211_mgmt *)
2851 data->rx_mgmt.frame;
2852 fc = le_to_host16(mgmt->frame_control);
2853 stype = WLAN_FC_GET_STYPE(fc);
2854
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002855#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002857#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2860 data->rx_mgmt.frame_len > 24) {
2861 const u8 *src = mgmt->sa;
2862 const u8 *ie = mgmt->u.probe_req.variable;
2863 size_t ie_len = data->rx_mgmt.frame_len -
2864 (mgmt->u.probe_req.variable -
2865 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002866 wpas_p2p_probe_req_rx(
2867 wpa_s, src, mgmt->da,
2868 mgmt->bssid, ie, ie_len,
2869 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 break;
2871 }
2872#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002873#ifdef CONFIG_IBSS_RSN
2874 if (stype == WLAN_FC_STYPE_AUTH &&
2875 data->rx_mgmt.frame_len >= 30) {
2876 wpa_supplicant_event_ibss_auth(wpa_s, data);
2877 break;
2878 }
2879#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2881 "management frame in non-AP mode");
2882 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002883#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002885
2886 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2887 data->rx_mgmt.frame_len > 24) {
2888 const u8 *ie = mgmt->u.probe_req.variable;
2889 size_t ie_len = data->rx_mgmt.frame_len -
2890 (mgmt->u.probe_req.variable -
2891 data->rx_mgmt.frame);
2892
2893 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2894 mgmt->bssid, ie, ie_len,
2895 data->rx_mgmt.ssi_signal);
2896 }
2897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002899#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002900 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002901 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002902#endif /* CONFIG_AP || CONFIG_IBSS_RSN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 case EVENT_RX_ACTION:
2904 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2905 " Category=%u DataLen=%d freq=%d MHz",
2906 MAC2STR(data->rx_action.sa),
2907 data->rx_action.category, (int) data->rx_action.len,
2908 data->rx_action.freq);
2909#ifdef CONFIG_IEEE80211R
2910 if (data->rx_action.category == WLAN_ACTION_FT) {
2911 ft_rx_action(wpa_s, data->rx_action.data,
2912 data->rx_action.len);
2913 break;
2914 }
2915#endif /* CONFIG_IEEE80211R */
2916#ifdef CONFIG_IEEE80211W
2917#ifdef CONFIG_SME
2918 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2919 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2920 data->rx_action.data,
2921 data->rx_action.len);
2922 break;
2923 }
2924#endif /* CONFIG_SME */
2925#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002926#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002927 if (data->rx_action.category == WLAN_ACTION_WNM) {
2928 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2929 break;
2930 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002931#endif /* CONFIG_WNM */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002932#ifdef CONFIG_GAS
2933 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2934 gas_query_rx(wpa_s->gas, data->rx_action.da,
2935 data->rx_action.sa, data->rx_action.bssid,
2936 data->rx_action.data, data->rx_action.len,
2937 data->rx_action.freq) == 0)
2938 break;
2939#endif /* CONFIG_GAS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002940#ifdef CONFIG_TDLS
2941 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2942 data->rx_action.len >= 4 &&
2943 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2944 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2945 "Response from " MACSTR,
2946 MAC2STR(data->rx_action.sa));
2947 break;
2948 }
2949#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950#ifdef CONFIG_P2P
2951 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2952 data->rx_action.sa,
2953 data->rx_action.bssid,
2954 data->rx_action.category,
2955 data->rx_action.data,
2956 data->rx_action.len, data->rx_action.freq);
2957#endif /* CONFIG_P2P */
2958 break;
2959 case EVENT_RX_PROBE_REQ:
2960 if (data->rx_probe_req.sa == NULL ||
2961 data->rx_probe_req.ie == NULL)
2962 break;
2963#ifdef CONFIG_AP
2964 if (wpa_s->ap_iface) {
2965 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2966 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002967 data->rx_probe_req.da,
2968 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002970 data->rx_probe_req.ie_len,
2971 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 break;
2973 }
2974#endif /* CONFIG_AP */
2975#ifdef CONFIG_P2P
2976 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002977 data->rx_probe_req.da,
2978 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002980 data->rx_probe_req.ie_len,
2981 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982#endif /* CONFIG_P2P */
2983 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002985#ifdef CONFIG_OFFCHANNEL
2986 offchannel_remain_on_channel_cb(
2987 wpa_s, data->remain_on_channel.freq,
2988 data->remain_on_channel.duration);
2989#endif /* CONFIG_OFFCHANNEL */
2990#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 wpas_p2p_remain_on_channel_cb(
2992 wpa_s, data->remain_on_channel.freq,
2993 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002994#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995 break;
2996 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002997#ifdef CONFIG_OFFCHANNEL
2998 offchannel_cancel_remain_on_channel_cb(
2999 wpa_s, data->remain_on_channel.freq);
3000#endif /* CONFIG_OFFCHANNEL */
3001#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 wpas_p2p_cancel_remain_on_channel_cb(
3003 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003004#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003006#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 case EVENT_P2P_DEV_FOUND: {
3008 struct p2p_peer_info peer_info;
3009
3010 os_memset(&peer_info, 0, sizeof(peer_info));
3011 if (data->p2p_dev_found.dev_addr)
3012 os_memcpy(peer_info.p2p_device_addr,
3013 data->p2p_dev_found.dev_addr, ETH_ALEN);
3014 if (data->p2p_dev_found.pri_dev_type)
3015 os_memcpy(peer_info.pri_dev_type,
3016 data->p2p_dev_found.pri_dev_type,
3017 sizeof(peer_info.pri_dev_type));
3018 if (data->p2p_dev_found.dev_name)
3019 os_strlcpy(peer_info.device_name,
3020 data->p2p_dev_found.dev_name,
3021 sizeof(peer_info.device_name));
3022 peer_info.config_methods = data->p2p_dev_found.config_methods;
3023 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
3024 peer_info.group_capab = data->p2p_dev_found.group_capab;
3025
3026 /*
3027 * FIX: new_device=1 is not necessarily correct. We should
3028 * maintain a P2P peer database in wpa_supplicant and update
3029 * this information based on whether the peer is truly new.
3030 */
3031 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
3032 break;
3033 }
3034 case EVENT_P2P_GO_NEG_REQ_RX:
3035 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
3036 data->p2p_go_neg_req_rx.dev_passwd_id);
3037 break;
3038 case EVENT_P2P_GO_NEG_COMPLETED:
3039 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
3040 break;
3041 case EVENT_P2P_PROV_DISC_REQUEST:
3042 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
3043 data->p2p_prov_disc_req.config_methods,
3044 data->p2p_prov_disc_req.dev_addr,
3045 data->p2p_prov_disc_req.pri_dev_type,
3046 data->p2p_prov_disc_req.dev_name,
3047 data->p2p_prov_disc_req.supp_config_methods,
3048 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003049 data->p2p_prov_disc_req.group_capab,
3050 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 break;
3052 case EVENT_P2P_PROV_DISC_RESPONSE:
3053 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
3054 data->p2p_prov_disc_resp.config_methods);
3055 break;
3056 case EVENT_P2P_SD_REQUEST:
3057 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
3058 data->p2p_sd_req.sa,
3059 data->p2p_sd_req.dialog_token,
3060 data->p2p_sd_req.update_indic,
3061 data->p2p_sd_req.tlvs,
3062 data->p2p_sd_req.tlvs_len);
3063 break;
3064 case EVENT_P2P_SD_RESPONSE:
3065 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
3066 data->p2p_sd_resp.update_indic,
3067 data->p2p_sd_resp.tlvs,
3068 data->p2p_sd_resp.tlvs_len);
3069 break;
3070#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 case EVENT_EAPOL_RX:
3072 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3073 data->eapol_rx.data,
3074 data->eapol_rx.data_len);
3075 break;
3076 case EVENT_SIGNAL_CHANGE:
3077 bgscan_notify_signal_change(
3078 wpa_s, data->signal_change.above_threshold,
3079 data->signal_change.current_signal,
3080 data->signal_change.current_noise,
3081 data->signal_change.current_txrate);
3082 break;
3083 case EVENT_INTERFACE_ENABLED:
3084 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3085 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003086 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087#ifdef CONFIG_AP
3088 if (!wpa_s->ap_iface) {
3089 wpa_supplicant_set_state(wpa_s,
3090 WPA_DISCONNECTED);
3091 wpa_supplicant_req_scan(wpa_s, 0, 0);
3092 } else
3093 wpa_supplicant_set_state(wpa_s,
3094 WPA_COMPLETED);
3095#else /* CONFIG_AP */
3096 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3097 wpa_supplicant_req_scan(wpa_s, 0, 0);
3098#endif /* CONFIG_AP */
3099 }
3100 break;
3101 case EVENT_INTERFACE_DISABLED:
3102 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3103 wpa_supplicant_mark_disassoc(wpa_s);
3104 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3105 break;
3106 case EVENT_CHANNEL_LIST_CHANGED:
3107 if (wpa_s->drv_priv == NULL)
3108 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003109
3110 free_hw_features(wpa_s);
3111 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
3112 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
3113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114#ifdef CONFIG_P2P
3115 wpas_p2p_update_channel_list(wpa_s);
3116#endif /* CONFIG_P2P */
3117 break;
3118 case EVENT_INTERFACE_UNAVAILABLE:
3119#ifdef CONFIG_P2P
3120 wpas_p2p_interface_unavailable(wpa_s);
3121#endif /* CONFIG_P2P */
3122 break;
3123 case EVENT_BEST_CHANNEL:
3124 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3125 "(%d %d %d)",
3126 data->best_chan.freq_24, data->best_chan.freq_5,
3127 data->best_chan.freq_overall);
3128 wpa_s->best_24_freq = data->best_chan.freq_24;
3129 wpa_s->best_5_freq = data->best_chan.freq_5;
3130 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3131#ifdef CONFIG_P2P
3132 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3133 data->best_chan.freq_5,
3134 data->best_chan.freq_overall);
3135#endif /* CONFIG_P2P */
3136 break;
3137 case EVENT_UNPROT_DEAUTH:
3138 wpa_supplicant_event_unprot_deauth(wpa_s,
3139 &data->unprot_deauth);
3140 break;
3141 case EVENT_UNPROT_DISASSOC:
3142 wpa_supplicant_event_unprot_disassoc(wpa_s,
3143 &data->unprot_disassoc);
3144 break;
3145 case EVENT_STATION_LOW_ACK:
3146#ifdef CONFIG_AP
3147 if (wpa_s->ap_iface && data)
3148 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3149 data->low_ack.addr);
3150#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003151#ifdef CONFIG_TDLS
3152 if (data)
3153 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3154#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 break;
3156 case EVENT_IBSS_PEER_LOST:
3157#ifdef CONFIG_IBSS_RSN
3158 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3159#endif /* CONFIG_IBSS_RSN */
3160 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003161 case EVENT_DRIVER_GTK_REKEY:
3162 if (os_memcmp(data->driver_gtk_rekey.bssid,
3163 wpa_s->bssid, ETH_ALEN))
3164 break;
3165 if (!wpa_s->wpa)
3166 break;
3167 wpa_sm_update_replay_ctr(wpa_s->wpa,
3168 data->driver_gtk_rekey.replay_ctr);
3169 break;
3170 case EVENT_SCHED_SCAN_STOPPED:
3171 wpa_s->sched_scanning = 0;
3172 wpa_supplicant_notify_scanning(wpa_s, 0);
3173
3174 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3175 break;
3176
3177 /*
3178 * If we timed out, start a new sched scan to continue
3179 * searching for more SSIDs.
3180 */
3181 if (wpa_s->sched_scan_timed_out)
3182 wpa_supplicant_req_sched_scan(wpa_s);
3183 break;
3184 case EVENT_WPS_BUTTON_PUSHED:
3185#ifdef CONFIG_WPS
3186 wpas_wps_start_pbc(wpa_s, NULL, 0);
3187#endif /* CONFIG_WPS */
3188 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003189 case EVENT_CONNECT_FAILED_REASON:
3190#ifdef CONFIG_AP
3191 if (!wpa_s->ap_iface || !data)
3192 break;
3193 hostapd_event_connect_failed_reason(
3194 wpa_s->ap_iface->bss[0],
3195 data->connect_failed_reason.addr,
3196 data->connect_failed_reason.code);
3197#endif /* CONFIG_AP */
3198 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 default:
3200 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3201 break;
3202 }
3203}