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