blob: c5e65efbae587c1ecfc9c564396448bbc5cf8093 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eapol_supp/eapol_supp_sm.h"
13#include "rsn_supp/wpa.h"
14#include "eloop.h"
15#include "config.h"
16#include "l2_packet/l2_packet.h"
17#include "wpa_supplicant_i.h"
18#include "driver_i.h"
19#include "pcsc_funcs.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "common/wpa_ctrl.h"
23#include "eap_peer/eap.h"
24#include "ap/hostapd.h"
25#include "p2p/p2p.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070026#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "notify.h"
28#include "common/ieee802_11_defs.h"
29#include "common/ieee802_11_common.h"
30#include "crypto/random.h"
31#include "blacklist.h"
32#include "wpas_glue.h"
33#include "wps_supplicant.h"
34#include "ibss_rsn.h"
35#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "p2p_supplicant.h"
38#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "ap.h"
41#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070044#include "interworking.h"
45
46
Dmitry Shmidt34af3062013-07-11 10:46:32 -070047#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070048static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080049 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070050#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080051
52
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070053static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
54 struct wpa_ssid *ssid)
55{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080056 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057
58 if (ssid == NULL || ssid->disabled_until.sec == 0)
59 return 0;
60
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080061 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070062 if (ssid->disabled_until.sec > now.sec)
63 return ssid->disabled_until.sec - now.sec;
64
65 wpas_clear_temp_disabled(wpa_s, ssid, 0);
66
67 return 0;
68}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080071static struct wpa_bss * wpa_supplicant_get_new_bss(
72 struct wpa_supplicant *wpa_s, const u8 *bssid)
73{
74 struct wpa_bss *bss = NULL;
75 struct wpa_ssid *ssid = wpa_s->current_ssid;
76
77 if (ssid->ssid_len > 0)
78 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
79 if (!bss)
80 bss = wpa_bss_get_bssid(wpa_s, bssid);
81
82 return bss;
83}
84
85
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
87{
88 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080089 struct wpa_bss *bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070090 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091
92 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
93 return 0;
94
95 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
96 "information");
97 ssid = wpa_supplicant_get_ssid(wpa_s);
98 if (ssid == NULL) {
99 wpa_msg(wpa_s, MSG_INFO,
100 "No network configuration found for the current AP");
101 return -1;
102 }
103
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
106 return -1;
107 }
108
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800109 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
110 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
111 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
112 return -1;
113 }
114
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700115 res = wpas_temp_disabled(wpa_s, ssid);
116 if (res > 0) {
117 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
118 "disabled for %d second(s)", res);
119 return -1;
120 }
121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
123 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 u8 wpa_ie[80];
126 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800127 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
128 wpa_ie, &wpa_ie_len) < 0)
129 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 } else {
131 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
132 }
133
134 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
135 eapol_sm_invalidate_cached_session(wpa_s->eapol);
136 old_ssid = wpa_s->current_ssid;
137 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800138
139 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
140 if (!bss) {
141 wpa_supplicant_update_scan_results(wpa_s);
142
143 /* Get the BSS from the new scan results */
144 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
145 }
146
147 if (bss)
148 wpa_s->current_bss = bss;
149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
151 wpa_supplicant_initiate_eapol(wpa_s);
152 if (old_ssid != wpa_s->current_ssid)
153 wpas_notify_network_changed(wpa_s);
154
155 return 0;
156}
157
158
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800159void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160{
161 struct wpa_supplicant *wpa_s = eloop_ctx;
162
163 if (wpa_s->countermeasures) {
164 wpa_s->countermeasures = 0;
165 wpa_drv_set_countermeasures(wpa_s, 0);
166 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
167 wpa_supplicant_req_scan(wpa_s, 0, 0);
168 }
169}
170
171
172void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
173{
174 int bssid_changed;
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176 wnm_bss_keep_alive_deinit(wpa_s);
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178#ifdef CONFIG_IBSS_RSN
179 ibss_rsn_deinit(wpa_s->ibss_rsn);
180 wpa_s->ibss_rsn = NULL;
181#endif /* CONFIG_IBSS_RSN */
182
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700183#ifdef CONFIG_AP
184 wpa_supplicant_ap_deinit(wpa_s);
185#endif /* CONFIG_AP */
186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
188 return;
189
190 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
191 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
192 os_memset(wpa_s->bssid, 0, ETH_ALEN);
193 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700194#ifdef CONFIG_SME
195 wpa_s->sme.prev_bssid_set = 0;
196#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800197#ifdef CONFIG_P2P
198 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
199#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 wpa_s->current_bss = NULL;
201 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700202#ifdef CONFIG_IEEE80211R
203#ifdef CONFIG_SME
204 if (wpa_s->sme.ft_ies)
205 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
206#endif /* CONFIG_SME */
207#endif /* CONFIG_IEEE80211R */
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 if (bssid_changed)
210 wpas_notify_bssid_changed(wpa_s);
211
212 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
213 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
214 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
215 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
216 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700217 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700218 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700219 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220}
221
222
223static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
224{
225 struct wpa_ie_data ie;
226 int pmksa_set = -1;
227 size_t i;
228
229 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
230 ie.pmkid == NULL)
231 return;
232
233 for (i = 0; i < ie.num_pmkid; i++) {
234 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
235 ie.pmkid + i * PMKID_LEN,
236 NULL, NULL, 0);
237 if (pmksa_set == 0) {
238 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
239 break;
240 }
241 }
242
243 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
244 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
245}
246
247
248static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
249 union wpa_event_data *data)
250{
251 if (data == NULL) {
252 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
253 "event");
254 return;
255 }
256 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
257 " index=%d preauth=%d",
258 MAC2STR(data->pmkid_candidate.bssid),
259 data->pmkid_candidate.index,
260 data->pmkid_candidate.preauth);
261
262 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
263 data->pmkid_candidate.index,
264 data->pmkid_candidate.preauth);
265}
266
267
268static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
269{
270 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
271 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
272 return 0;
273
274#ifdef IEEE8021X_EAPOL
275 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
276 wpa_s->current_ssid &&
277 !(wpa_s->current_ssid->eapol_flags &
278 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
279 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
280 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
281 * plaintext or static WEP keys). */
282 return 0;
283 }
284#endif /* IEEE8021X_EAPOL */
285
286 return 1;
287}
288
289
290/**
291 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
292 * @wpa_s: pointer to wpa_supplicant data
293 * @ssid: Configuration data for the network
294 * Returns: 0 on success, -1 on failure
295 *
296 * This function is called when starting authentication with a network that is
297 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
298 */
299int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
300 struct wpa_ssid *ssid)
301{
302#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700304 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305
Dmitry Shmidt051af732013-10-22 13:52:46 -0700306 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL ||
307 wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 return 0;
309
310 if (ssid->eap.eap_methods == NULL) {
311 sim = 1;
312 aka = 1;
313 } else {
314 struct eap_method_type *eap = ssid->eap.eap_methods;
315 while (eap->vendor != EAP_VENDOR_IETF ||
316 eap->method != EAP_TYPE_NONE) {
317 if (eap->vendor == EAP_VENDOR_IETF) {
318 if (eap->method == EAP_TYPE_SIM)
319 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700320 else if (eap->method == EAP_TYPE_AKA ||
321 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 aka = 1;
323 }
324 eap++;
325 }
326 }
327
328 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
329 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700330 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
331 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
332 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 aka = 0;
334
335 if (!sim && !aka) {
336 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
337 "use SIM, but neither EAP-SIM nor EAP-AKA are "
338 "enabled");
339 return 0;
340 }
341
342 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
343 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800345 wpa_s->scard = scard_init(NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 if (wpa_s->scard == NULL) {
347 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
348 "(pcsc-lite)");
349 return -1;
350 }
351 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
352 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800353#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354#endif /* IEEE8021X_EAPOL */
355
356 return 0;
357}
358
359
360#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700361
362static int has_wep_key(struct wpa_ssid *ssid)
363{
364 int i;
365
366 for (i = 0; i < NUM_WEP_KEYS; i++) {
367 if (ssid->wep_key_len[i])
368 return 1;
369 }
370
371 return 0;
372}
373
374
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700375static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 struct wpa_ssid *ssid)
377{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700378 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379
380 if (ssid->mixed_cell)
381 return 1;
382
383#ifdef CONFIG_WPS
384 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
385 return 1;
386#endif /* CONFIG_WPS */
387
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700388 if (has_wep_key(ssid))
389 privacy = 1;
390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391#ifdef IEEE8021X_EAPOL
392 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
393 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
394 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
395 privacy = 1;
396#endif /* IEEE8021X_EAPOL */
397
Jouni Malinen75ecf522011-06-27 15:19:46 -0700398 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
399 privacy = 1;
400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 if (bss->caps & IEEE80211_CAP_PRIVACY)
402 return privacy;
403 return !privacy;
404}
405
406
407static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
408 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700409 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410{
411 struct wpa_ie_data ie;
412 int proto_match = 0;
413 const u8 *rsn_ie, *wpa_ie;
414 int ret;
415 int wep_ok;
416
417 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
418 if (ret >= 0)
419 return ret;
420
421 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
422 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
423 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
424 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
425 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
426
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700427 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
429 proto_match++;
430
431 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
432 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
433 "failed");
434 break;
435 }
436
437 if (wep_ok &&
438 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
439 {
440 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
441 "in RSN IE");
442 return 1;
443 }
444
445 if (!(ie.proto & ssid->proto)) {
446 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
447 "mismatch");
448 break;
449 }
450
451 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
452 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
453 "cipher mismatch");
454 break;
455 }
456
457 if (!(ie.group_cipher & ssid->group_cipher)) {
458 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
459 "cipher mismatch");
460 break;
461 }
462
463 if (!(ie.key_mgmt & ssid->key_mgmt)) {
464 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
465 "mismatch");
466 break;
467 }
468
469#ifdef CONFIG_IEEE80211W
470 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800471 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
472 wpa_s->conf->pmf : ssid->ieee80211w) ==
473 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
475 "frame protection");
476 break;
477 }
478#endif /* CONFIG_IEEE80211W */
479
480 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
481 return 1;
482 }
483
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700484 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
486 proto_match++;
487
488 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
489 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
490 "failed");
491 break;
492 }
493
494 if (wep_ok &&
495 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
496 {
497 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
498 "in WPA IE");
499 return 1;
500 }
501
502 if (!(ie.proto & ssid->proto)) {
503 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
504 "mismatch");
505 break;
506 }
507
508 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
509 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
510 "cipher mismatch");
511 break;
512 }
513
514 if (!(ie.group_cipher & ssid->group_cipher)) {
515 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
516 "cipher mismatch");
517 break;
518 }
519
520 if (!(ie.key_mgmt & ssid->key_mgmt)) {
521 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
522 "mismatch");
523 break;
524 }
525
526 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
527 return 1;
528 }
529
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700530 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
531 !rsn_ie) {
532 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
533 return 1;
534 }
535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
537 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
538 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
539 return 0;
540 }
541
542 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
543 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
544 return 1;
545 }
546
547 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
548 "WPA/WPA2");
549
550 return 0;
551}
552
553
554static int freq_allowed(int *freqs, int freq)
555{
556 int i;
557
558 if (freqs == NULL)
559 return 1;
560
561 for (i = 0; freqs[i]; i++)
562 if (freqs[i] == freq)
563 return 1;
564 return 0;
565}
566
567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568static int ht_supported(const struct hostapd_hw_modes *mode)
569{
570 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
571 /*
572 * The driver did not indicate whether it supports HT. Assume
573 * it does to avoid connection issues.
574 */
575 return 1;
576 }
577
578 /*
579 * IEEE Std 802.11n-2009 20.1.1:
580 * An HT non-AP STA shall support all EQM rates for one spatial stream.
581 */
582 return mode->mcs_set[0] == 0xff;
583}
584
585
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700586static int vht_supported(const struct hostapd_hw_modes *mode)
587{
588 if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
589 /*
590 * The driver did not indicate whether it supports VHT. Assume
591 * it does to avoid connection issues.
592 */
593 return 1;
594 }
595
596 /*
597 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
598 * TODO: Verify if this complies with the standard
599 */
600 return (mode->vht_mcs_set[0] & 0x3) != 3;
601}
602
603
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700604static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800605{
606 const struct hostapd_hw_modes *mode = NULL, *modes;
607 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
608 const u8 *rate_ie;
609 int i, j, k;
610
611 if (bss->freq == 0)
612 return 1; /* Cannot do matching without knowing band */
613
614 modes = wpa_s->hw.modes;
615 if (modes == NULL) {
616 /*
617 * The driver does not provide any additional information
618 * about the utilized hardware, so allow the connection attempt
619 * to continue.
620 */
621 return 1;
622 }
623
624 for (i = 0; i < wpa_s->hw.num_modes; i++) {
625 for (j = 0; j < modes[i].num_channels; j++) {
626 int freq = modes[i].channels[j].freq;
627 if (freq == bss->freq) {
628 if (mode &&
629 mode->mode == HOSTAPD_MODE_IEEE80211G)
630 break; /* do not allow 802.11b replace
631 * 802.11g */
632 mode = &modes[i];
633 break;
634 }
635 }
636 }
637
638 if (mode == NULL)
639 return 0;
640
641 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700642 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800643 if (rate_ie == NULL)
644 continue;
645
646 for (j = 2; j < rate_ie[1] + 2; j++) {
647 int flagged = !!(rate_ie[j] & 0x80);
648 int r = (rate_ie[j] & 0x7f) * 5;
649
650 /*
651 * IEEE Std 802.11n-2009 7.3.2.2:
652 * The new BSS Membership selector value is encoded
653 * like a legacy basic rate, but it is not a rate and
654 * only indicates if the BSS members are required to
655 * support the mandatory features of Clause 20 [HT PHY]
656 * in order to join the BSS.
657 */
658 if (flagged && ((rate_ie[j] & 0x7f) ==
659 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
660 if (!ht_supported(mode)) {
661 wpa_dbg(wpa_s, MSG_DEBUG,
662 " hardware does not support "
663 "HT PHY");
664 return 0;
665 }
666 continue;
667 }
668
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700669 /* There's also a VHT selector for 802.11ac */
670 if (flagged && ((rate_ie[j] & 0x7f) ==
671 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
672 if (!vht_supported(mode)) {
673 wpa_dbg(wpa_s, MSG_DEBUG,
674 " hardware does not support "
675 "VHT PHY");
676 return 0;
677 }
678 continue;
679 }
680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800681 if (!flagged)
682 continue;
683
684 /* check for legacy basic rates */
685 for (k = 0; k < mode->num_rates; k++) {
686 if (mode->rates[k] == r)
687 break;
688 }
689 if (k == mode->num_rates) {
690 /*
691 * IEEE Std 802.11-2007 7.3.2.2 demands that in
692 * order to join a BSS all required rates
693 * have to be supported by the hardware.
694 */
695 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
696 "not support required rate %d.%d Mbps",
697 r / 10, r % 10);
698 return 0;
699 }
700 }
701 }
702
703 return 1;
704}
705
706
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800707static int bss_is_dmg(struct wpa_bss *bss)
708{
709 return bss->freq > 45000;
710}
711
712
713/*
714 * Test whether BSS is in an ESS.
715 * This is done differently in DMG (60 GHz) and non-DMG bands
716 */
717static int bss_is_ess(struct wpa_bss *bss)
718{
719 if (bss_is_dmg(bss)) {
720 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
721 IEEE80211_CAP_DMG_AP;
722 }
723
724 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
725 IEEE80211_CAP_ESS);
726}
727
728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700730 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 struct wpa_ssid *group)
732{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700733 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 int wpa;
735 struct wpa_blacklist *e;
736 const u8 *ie;
737 struct wpa_ssid *ssid;
738
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700739 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 wpa_ie_len = ie ? ie[1] : 0;
741
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700742 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743 rsn_ie_len = ie ? ie[1] : 0;
744
745 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidt96571392013-10-14 12:54:46 -0700746 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700747 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700749 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
750 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
751 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
752 " p2p" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753
754 e = wpa_blacklist_get(wpa_s, bss->bssid);
755 if (e) {
756 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700757 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 /*
759 * When only a single network is enabled, we can
760 * trigger blacklisting on the first failure. This
761 * should not be done with multiple enabled networks to
762 * avoid getting forced to move into a worse ESS on
763 * single error if there are no other BSSes of the
764 * current ESS.
765 */
766 limit = 0;
767 }
768 if (e->count > limit) {
769 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
770 "(count=%d limit=%d)", e->count, limit);
771 return NULL;
772 }
773 }
774
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700775 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
777 return NULL;
778 }
779
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800780 if (disallowed_bssid(wpa_s, bss->bssid)) {
781 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
782 return NULL;
783 }
784
785 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
786 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
787 return NULL;
788 }
789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
791
792 for (ssid = group; ssid; ssid = ssid->pnext) {
793 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700794 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
798 continue;
799 }
800
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700801 res = wpas_temp_disabled(wpa_s, ssid);
802 if (res > 0) {
803 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
804 "temporarily for %d second(s)", res);
805 continue;
806 }
807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808#ifdef CONFIG_WPS
809 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
810 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
811 "(WPS)");
812 continue;
813 }
814
815 if (wpa && ssid->ssid_len == 0 &&
816 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
817 check_ssid = 0;
818
819 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
820 /* Only allow wildcard SSID match if an AP
821 * advertises active WPS operation that matches
822 * with our mode. */
823 check_ssid = 1;
824 if (ssid->ssid_len == 0 &&
825 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
826 check_ssid = 0;
827 }
828#endif /* CONFIG_WPS */
829
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800830 if (ssid->bssid_set && ssid->ssid_len == 0 &&
831 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
832 check_ssid = 0;
833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700835 (bss->ssid_len != ssid->ssid_len ||
836 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
838 continue;
839 }
840
841 if (ssid->bssid_set &&
842 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
843 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
844 continue;
845 }
846
847 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
848 continue;
849
850 if (!wpa &&
851 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
852 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
853 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
854 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
855 "not allowed");
856 continue;
857 }
858
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700859 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
860 has_wep_key(ssid)) {
861 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
862 continue;
863 }
864
Jouni Malinen75ecf522011-06-27 15:19:46 -0700865 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
867 "mismatch");
868 continue;
869 }
870
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800871 if (!bss_is_ess(bss)) {
872 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873 continue;
874 }
875
876 if (!freq_allowed(ssid->freq_list, bss->freq)) {
877 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
878 "allowed");
879 continue;
880 }
881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 if (!rate_match(wpa_s, bss)) {
883 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
884 "not match");
885 continue;
886 }
887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -0700889 if (ssid->p2p_group &&
890 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
891 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
892 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
893 continue;
894 }
895
Dmitry Shmidt54605472013-11-08 11:10:19 -0800896 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
897 struct wpabuf *p2p_ie;
898 u8 dev_addr[ETH_ALEN];
899
900 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
901 if (ie == NULL) {
902 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
903 continue;
904 }
905 p2p_ie = wpa_bss_get_vendor_ie_multi(
906 bss, P2P_IE_VENDOR_TYPE);
907 if (p2p_ie == NULL) {
908 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
909 continue;
910 }
911
912 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
913 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
914 ETH_ALEN) != 0) {
915 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
916 wpabuf_free(p2p_ie);
917 continue;
918 }
919 wpabuf_free(p2p_ie);
920 }
921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 /*
923 * TODO: skip the AP if its P2P IE has Group Formation
924 * bit set in the P2P Group Capability Bitmap and we
925 * are not in Group Formation with that device.
926 */
927#endif /* CONFIG_P2P */
928
929 /* Matching configuration found */
930 return ssid;
931 }
932
933 /* No matching configuration found */
934 return NULL;
935}
936
937
938static struct wpa_bss *
939wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 struct wpa_ssid *group,
941 struct wpa_ssid **selected_ssid)
942{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700943 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944
945 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
946 group->priority);
947
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700948 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
949 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
951 if (!*selected_ssid)
952 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
954 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700955 MAC2STR(bss->bssid),
956 wpa_ssid_txt(bss->ssid, bss->ssid_len));
957 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 }
959
960 return NULL;
961}
962
963
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700964struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
965 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966{
967 struct wpa_bss *selected = NULL;
968 int prio;
969
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700970 if (wpa_s->last_scan_res == NULL ||
971 wpa_s->last_scan_res_used == 0)
972 return NULL; /* no scan results from last update */
973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 while (selected == NULL) {
975 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
976 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700977 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 selected_ssid);
979 if (selected)
980 break;
981 }
982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800983 if (selected == NULL && wpa_s->blacklist &&
984 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
986 "blacklist and try again");
987 wpa_blacklist_clear(wpa_s);
988 wpa_s->blacklist_cleared++;
989 } else if (selected == NULL)
990 break;
991 }
992
993 return selected;
994}
995
996
997static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
998 int timeout_sec, int timeout_usec)
999{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001000 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 /*
1002 * No networks are enabled; short-circuit request so
1003 * we don't wait timeout seconds before transitioning
1004 * to INACTIVE state.
1005 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001006 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1007 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1009 return;
1010 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001011
1012 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1014}
1015
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001016
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001017int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001018 struct wpa_bss *selected,
1019 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020{
1021 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1022 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1023 "PBC session overlap");
1024#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001026 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027#endif /* CONFIG_P2P */
1028
1029#ifdef CONFIG_WPS
1030 wpas_wps_cancel(wpa_s);
1031#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001032 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 }
1034
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001035 wpa_msg(wpa_s, MSG_DEBUG,
1036 "Considering connect request: reassociate: %d selected: "
1037 MACSTR " bssid: " MACSTR " pending: " MACSTR
1038 " wpa_state: %s ssid=%p current_ssid=%p",
1039 wpa_s->reassociate, MAC2STR(selected->bssid),
1040 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1041 wpa_supplicant_state_txt(wpa_s->wpa_state),
1042 ssid, wpa_s->current_ssid);
1043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 /*
1045 * Do not trigger new association unless the BSSID has changed or if
1046 * reassociation is requested. If we are in process of associating with
1047 * the selected BSSID, do not trigger new attempt.
1048 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1051 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1052 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001053 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1054 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1055 0) ||
1056 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1057 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1059 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001060 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001062 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1063 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 wpa_supplicant_associate(wpa_s, selected, ssid);
1065 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001066 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1067 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001069
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001070 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071}
1072
1073
1074static struct wpa_ssid *
1075wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1076{
1077 int prio;
1078 struct wpa_ssid *ssid;
1079
1080 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1081 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1082 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001083 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084 continue;
1085 if (ssid->mode == IEEE80211_MODE_IBSS ||
1086 ssid->mode == IEEE80211_MODE_AP)
1087 return ssid;
1088 }
1089 }
1090 return NULL;
1091}
1092
1093
1094/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1095 * on BSS added and BSS changed events */
1096static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001097 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001099 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100
1101 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1102 return;
1103
Jouni Malinen87fd2792011-05-16 18:35:42 +03001104 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106
Jouni Malinen87fd2792011-05-16 18:35:42 +03001107 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 if (ssid == NULL)
1109 continue;
1110
Jouni Malinen87fd2792011-05-16 18:35:42 +03001111 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 if (rsn == NULL)
1113 continue;
1114
Jouni Malinen87fd2792011-05-16 18:35:42 +03001115 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 }
1117
1118}
1119
1120
1121static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1122 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001123 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001125 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 int min_diff;
1127
1128 if (wpa_s->reassociate)
1129 return 1; /* explicit request to reassociate */
1130 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1131 return 1; /* we are not associated; continue */
1132 if (wpa_s->current_ssid == NULL)
1133 return 1; /* unknown current SSID */
1134 if (wpa_s->current_ssid != ssid)
1135 return 1; /* different network block */
1136
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001137 if (wpas_driver_bss_selection(wpa_s))
1138 return 0; /* Driver-based roaming */
1139
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001140 if (wpa_s->current_ssid->ssid)
1141 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1142 wpa_s->current_ssid->ssid,
1143 wpa_s->current_ssid->ssid_len);
1144 if (!current_bss)
1145 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146
1147 if (!current_bss)
1148 return 1; /* current BSS not seen in scan results */
1149
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001150 if (current_bss == selected)
1151 return 0;
1152
1153 if (selected->last_update_idx > current_bss->last_update_idx)
1154 return 1; /* current BSS not seen in the last scan */
1155
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001156#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1159 MAC2STR(current_bss->bssid), current_bss->level);
1160 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1161 MAC2STR(selected->bssid), selected->level);
1162
1163 if (wpa_s->current_ssid->bssid_set &&
1164 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1165 0) {
1166 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1167 "has preferred BSSID");
1168 return 1;
1169 }
1170
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001171 if (current_bss->level < 0 && current_bss->level > selected->level) {
1172 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1173 "signal level");
1174 return 0;
1175 }
1176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 min_diff = 2;
1178 if (current_bss->level < 0) {
1179 if (current_bss->level < -85)
1180 min_diff = 1;
1181 else if (current_bss->level < -80)
1182 min_diff = 2;
1183 else if (current_bss->level < -75)
1184 min_diff = 3;
1185 else if (current_bss->level < -70)
1186 min_diff = 4;
1187 else
1188 min_diff = 5;
1189 }
1190 if (abs(current_bss->level - selected->level) < min_diff) {
1191 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1192 "in signal level");
1193 return 0;
1194 }
1195
1196 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001197#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001198 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001199#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200}
1201
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001202
Jouni Malinen89ca7022012-09-14 13:03:12 -07001203/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001204 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001205static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001206 union wpa_event_data *data,
1207 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001209 struct wpa_scan_results *scan_res = NULL;
1210 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001212#ifndef CONFIG_NO_RANDOM_POOL
1213 size_t i, num;
1214#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215
1216#ifdef CONFIG_AP
1217 if (wpa_s->ap_iface)
1218 ap = 1;
1219#endif /* CONFIG_AP */
1220
1221 wpa_supplicant_notify_scanning(wpa_s, 0);
1222
1223 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1224 data ? &data->scan_info :
1225 NULL, 1);
1226 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001227 if (wpa_s->conf->ap_scan == 2 || ap ||
1228 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001230 if (!own_request)
1231 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1233 "scanning again");
1234 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001235 ret = -1;
1236 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 }
1238
1239#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 num = scan_res->num;
1241 if (num > 10)
1242 num = 10;
1243 for (i = 0; i < num; i++) {
1244 u8 buf[5];
1245 struct wpa_scan_res *res = scan_res->res[i];
1246 buf[0] = res->bssid[5];
1247 buf[1] = res->qual & 0xff;
1248 buf[2] = res->noise & 0xff;
1249 buf[3] = res->level & 0xff;
1250 buf[4] = res->tsf & 0xff;
1251 random_add_randomness(buf, sizeof(buf));
1252 }
1253#endif /* CONFIG_NO_RANDOM_POOL */
1254
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001255 if (own_request && wpa_s->scan_res_handler &&
1256 (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1258 struct wpa_scan_results *scan_res);
1259
1260 scan_res_handler = wpa_s->scan_res_handler;
1261 wpa_s->scan_res_handler = NULL;
1262 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001263 ret = -2;
1264 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 }
1266
1267 if (ap) {
1268 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1269#ifdef CONFIG_AP
1270 if (wpa_s->ap_iface->scan_cb)
1271 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1272#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001273 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001275
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001276 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1277 wpa_s->own_scan_running, wpa_s->external_scan_running);
1278 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1279 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1280 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1281 wpa_s->manual_scan_id);
1282 wpa_s->manual_scan_use_id = 0;
1283 } else {
1284 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1285 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001286 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287
1288 wpas_notify_scan_done(wpa_s, 1);
1289
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001290 if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1291 wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
Dmitry Shmidt04949592012-07-19 12:16:46 -07001292 wpa_scan_results_free(scan_res);
1293 return 0;
1294 }
1295
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001296 if (sme_proc_obss_scan(wpa_s) > 0)
1297 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001298
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001299 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1300 goto scan_work_done;
1301
1302 if (autoscan_notify_scan(wpa_s, scan_res))
1303 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 if (wpa_s->disconnected) {
1306 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001307 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 }
1309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001311 bgscan_notify_scan(wpa_s, scan_res) == 1)
1312 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001314 wpas_wps_update_ap_info(wpa_s, scan_res);
1315
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001316 wpa_scan_results_free(scan_res);
1317
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001318 if (wpa_s->scan_work) {
1319 struct wpa_radio_work *work = wpa_s->scan_work;
1320 wpa_s->scan_work = NULL;
1321 radio_work_done(work);
1322 }
1323
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001324 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001325
1326scan_work_done:
1327 wpa_scan_results_free(scan_res);
1328 if (wpa_s->scan_work) {
1329 struct wpa_radio_work *work = wpa_s->scan_work;
1330 wpa_s->scan_work = NULL;
1331 radio_work_done(work);
1332 }
1333 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001334}
1335
1336
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001337static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001338 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001339{
1340 struct wpa_bss *selected;
1341 struct wpa_ssid *ssid = NULL;
1342
1343 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344
1345 if (selected) {
1346 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001347 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001348 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001349 if (new_scan)
1350 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001352 }
1353
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001354 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001356 return -1;
1357 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001358 if (new_scan)
1359 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001360 /*
1361 * Do not notify other virtual radios of scan results since we do not
1362 * want them to start other associations at the same time.
1363 */
1364 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1367 ssid = wpa_supplicant_pick_new_network(wpa_s);
1368 if (ssid) {
1369 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1370 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001371 if (new_scan)
1372 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001373 } else if (own_request) {
1374 /*
1375 * No SSID found. If SCAN results are as a result of
1376 * own scan request and not due to a scan request on
1377 * another shared interface, try another scan.
1378 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 int timeout_sec = wpa_s->scan_interval;
1380 int timeout_usec = 0;
1381#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001382 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1383 return 0;
1384
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001385 if (wpa_s->p2p_in_provisioning ||
1386 wpa_s->show_group_started) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 /*
1388 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001389 * state and during P2P join-a-group operation
1390 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 */
1392 timeout_sec = 0;
1393 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001394 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1395 timeout_usec);
1396 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 }
1398#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001399#ifdef CONFIG_INTERWORKING
1400 if (wpa_s->conf->auto_interworking &&
1401 wpa_s->conf->interworking &&
1402 wpa_s->conf->cred) {
1403 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1404 "start ANQP fetch since no matching "
1405 "networks found");
1406 wpa_s->network_select = 1;
1407 wpa_s->auto_network_select = 1;
1408 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001409 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001410 }
1411#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001412#ifdef CONFIG_WPS
1413 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1414 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1415 timeout_sec = 0;
1416 timeout_usec = 500000;
1417 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1418 timeout_usec);
1419 return 0;
1420 }
1421#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001422 if (wpa_supplicant_req_sched_scan(wpa_s))
1423 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1424 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 }
1426 }
1427 return 0;
1428}
1429
1430
1431static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1432 union wpa_event_data *data)
1433{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001435
1436 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 /*
1438 * If no scan results could be fetched, then no need to
1439 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001440 * this scan. Similarly, if scan results started a new operation on this
1441 * interface, do not notify other interfaces to avoid concurrent
1442 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 */
1444 return;
1445 }
1446
1447 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001448 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 * so, they get updated with this same scan info.
1450 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001451 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1452 radio_list) {
1453 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1455 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001456 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 }
1458 }
1459}
1460
1461#endif /* CONFIG_NO_SCAN_PROCESSING */
1462
1463
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001464int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1465{
1466#ifdef CONFIG_NO_SCAN_PROCESSING
1467 return -1;
1468#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001469 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001470
1471 if (wpa_s->last_scan_res_used <= 0)
1472 return -1;
1473
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001474 os_get_reltime(&now);
1475 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001476 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1477 return -1;
1478 }
1479
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001480 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001481#endif /* CONFIG_NO_SCAN_PROCESSING */
1482}
1483
Dmitry Shmidt04949592012-07-19 12:16:46 -07001484#ifdef CONFIG_WNM
1485
1486static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1487{
1488 struct wpa_supplicant *wpa_s = eloop_ctx;
1489
1490 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1491 return;
1492
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001493 if (!wpa_s->no_keep_alive) {
1494 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1495 MAC2STR(wpa_s->bssid));
1496 /* TODO: could skip this if normal data traffic has been sent */
1497 /* TODO: Consider using some more appropriate data frame for
1498 * this */
1499 if (wpa_s->l2)
1500 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1501 (u8 *) "", 0);
1502 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001503
1504#ifdef CONFIG_SME
1505 if (wpa_s->sme.bss_max_idle_period) {
1506 unsigned int msec;
1507 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1508 if (msec > 100)
1509 msec -= 100;
1510 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1511 wnm_bss_keep_alive, wpa_s, NULL);
1512 }
1513#endif /* CONFIG_SME */
1514}
1515
1516
1517static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1518 const u8 *ies, size_t ies_len)
1519{
1520 struct ieee802_11_elems elems;
1521
1522 if (ies == NULL)
1523 return;
1524
1525 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1526 return;
1527
1528#ifdef CONFIG_SME
1529 if (elems.bss_max_idle_period) {
1530 unsigned int msec;
1531 wpa_s->sme.bss_max_idle_period =
1532 WPA_GET_LE16(elems.bss_max_idle_period);
1533 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1534 "TU)%s", wpa_s->sme.bss_max_idle_period,
1535 (elems.bss_max_idle_period[2] & 0x01) ?
1536 " (protected keep-live required)" : "");
1537 if (wpa_s->sme.bss_max_idle_period == 0)
1538 wpa_s->sme.bss_max_idle_period = 1;
1539 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1540 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1541 /* msec times 1000 */
1542 msec = wpa_s->sme.bss_max_idle_period * 1024;
1543 if (msec > 100)
1544 msec -= 100;
1545 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1546 wnm_bss_keep_alive, wpa_s,
1547 NULL);
1548 }
1549 }
1550#endif /* CONFIG_SME */
1551}
1552
1553#endif /* CONFIG_WNM */
1554
1555
1556void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1557{
1558#ifdef CONFIG_WNM
1559 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1560#endif /* CONFIG_WNM */
1561}
1562
1563
Dmitry Shmidt051af732013-10-22 13:52:46 -07001564#ifdef CONFIG_INTERWORKING
1565
1566static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1567 size_t len)
1568{
1569 int res;
1570
1571 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1572 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1573 if (res) {
1574 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1575 }
1576
1577 return res;
1578}
1579
1580
1581static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1582 const u8 *ies, size_t ies_len)
1583{
1584 struct ieee802_11_elems elems;
1585
1586 if (ies == NULL)
1587 return;
1588
1589 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1590 return;
1591
1592 if (elems.qos_map_set) {
1593 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1594 elems.qos_map_set_len);
1595 }
1596}
1597
1598#endif /* CONFIG_INTERWORKING */
1599
1600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001601static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1602 union wpa_event_data *data)
1603{
1604 int l, len, found = 0, wpa_found, rsn_found;
1605 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001606#ifdef CONFIG_IEEE80211R
1607 u8 bssid[ETH_ALEN];
1608#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609
1610 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1611 if (data->assoc_info.req_ies)
1612 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1613 data->assoc_info.req_ies_len);
1614 if (data->assoc_info.resp_ies) {
1615 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1616 data->assoc_info.resp_ies_len);
1617#ifdef CONFIG_TDLS
1618 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1619 data->assoc_info.resp_ies_len);
1620#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001621#ifdef CONFIG_WNM
1622 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1623 data->assoc_info.resp_ies_len);
1624#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001625#ifdef CONFIG_INTERWORKING
1626 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1627 data->assoc_info.resp_ies_len);
1628#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 }
1630 if (data->assoc_info.beacon_ies)
1631 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1632 data->assoc_info.beacon_ies,
1633 data->assoc_info.beacon_ies_len);
1634 if (data->assoc_info.freq)
1635 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1636 data->assoc_info.freq);
1637
1638 p = data->assoc_info.req_ies;
1639 l = data->assoc_info.req_ies_len;
1640
1641 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1642 while (p && l >= 2) {
1643 len = p[1] + 2;
1644 if (len > l) {
1645 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1646 p, l);
1647 break;
1648 }
1649 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1650 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1651 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1652 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1653 break;
1654 found = 1;
1655 wpa_find_assoc_pmkid(wpa_s);
1656 break;
1657 }
1658 l -= len;
1659 p += len;
1660 }
1661 if (!found && data->assoc_info.req_ies)
1662 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1663
1664#ifdef CONFIG_IEEE80211R
1665#ifdef CONFIG_SME
1666 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1668 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1669 data->assoc_info.resp_ies,
1670 data->assoc_info.resp_ies_len,
1671 bssid) < 0) {
1672 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1673 "Reassociation Response failed");
1674 wpa_supplicant_deauthenticate(
1675 wpa_s, WLAN_REASON_INVALID_IE);
1676 return -1;
1677 }
1678 }
1679
1680 p = data->assoc_info.resp_ies;
1681 l = data->assoc_info.resp_ies_len;
1682
1683#ifdef CONFIG_WPS_STRICT
1684 if (p && wpa_s->current_ssid &&
1685 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1686 struct wpabuf *wps;
1687 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1688 if (wps == NULL) {
1689 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1690 "include WPS IE in (Re)Association Response");
1691 return -1;
1692 }
1693
1694 if (wps_validate_assoc_resp(wps) < 0) {
1695 wpabuf_free(wps);
1696 wpa_supplicant_deauthenticate(
1697 wpa_s, WLAN_REASON_INVALID_IE);
1698 return -1;
1699 }
1700 wpabuf_free(wps);
1701 }
1702#endif /* CONFIG_WPS_STRICT */
1703
1704 /* Go through the IEs and make a copy of the MDIE, if present. */
1705 while (p && l >= 2) {
1706 len = p[1] + 2;
1707 if (len > l) {
1708 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1709 p, l);
1710 break;
1711 }
1712 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1713 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1714 wpa_s->sme.ft_used = 1;
1715 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1716 MOBILITY_DOMAIN_ID_LEN);
1717 break;
1718 }
1719 l -= len;
1720 p += len;
1721 }
1722#endif /* CONFIG_SME */
1723
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001724 /* Process FT when SME is in the driver */
1725 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1726 wpa_ft_is_completed(wpa_s->wpa)) {
1727 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1728 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1729 data->assoc_info.resp_ies,
1730 data->assoc_info.resp_ies_len,
1731 bssid) < 0) {
1732 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1733 "Reassociation Response failed");
1734 wpa_supplicant_deauthenticate(
1735 wpa_s, WLAN_REASON_INVALID_IE);
1736 return -1;
1737 }
1738 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1739 }
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1742 data->assoc_info.resp_ies_len);
1743#endif /* CONFIG_IEEE80211R */
1744
1745 /* WPA/RSN IE from Beacon/ProbeResp */
1746 p = data->assoc_info.beacon_ies;
1747 l = data->assoc_info.beacon_ies_len;
1748
1749 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1750 */
1751 wpa_found = rsn_found = 0;
1752 while (p && l >= 2) {
1753 len = p[1] + 2;
1754 if (len > l) {
1755 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1756 p, l);
1757 break;
1758 }
1759 if (!wpa_found &&
1760 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1761 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1762 wpa_found = 1;
1763 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1764 }
1765
1766 if (!rsn_found &&
1767 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1768 rsn_found = 1;
1769 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1770 }
1771
1772 l -= len;
1773 p += len;
1774 }
1775
1776 if (!wpa_found && data->assoc_info.beacon_ies)
1777 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1778 if (!rsn_found && data->assoc_info.beacon_ies)
1779 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1780 if (wpa_found || rsn_found)
1781 wpa_s->ap_ies_from_associnfo = 1;
1782
Jouni Malinen87fd2792011-05-16 18:35:42 +03001783 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1784 wpa_s->assoc_freq != data->assoc_info.freq) {
1785 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1786 "%u to %u MHz",
1787 wpa_s->assoc_freq, data->assoc_info.freq);
1788 wpa_supplicant_update_scan_results(wpa_s);
1789 }
1790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 wpa_s->assoc_freq = data->assoc_info.freq;
1792
1793 return 0;
1794}
1795
1796
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001797static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1798{
1799 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1800
1801 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1802 return -1;
1803
1804 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1805 return 0;
1806
1807 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1808 WPA_IE_VENDOR_TYPE);
1809 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1810
1811 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1812 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1813 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1814 bss_rsn ? 2 + bss_rsn[1] : 0))
1815 return -1;
1816
1817 return 0;
1818}
1819
1820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1822 union wpa_event_data *data)
1823{
1824 u8 bssid[ETH_ALEN];
1825 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826
1827#ifdef CONFIG_AP
1828 if (wpa_s->ap_iface) {
1829 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1830 data->assoc_info.addr,
1831 data->assoc_info.req_ies,
1832 data->assoc_info.req_ies_len,
1833 data->assoc_info.reassoc);
1834 return;
1835 }
1836#endif /* CONFIG_AP */
1837
1838 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1839 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1840 return;
1841
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001842 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1843 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001844 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001845 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1846 return;
1847 }
1848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001850 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1852 MACSTR, MAC2STR(bssid));
1853 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1855 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001856 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857
1858 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1859 wpa_clear_keys(wpa_s, bssid);
1860 }
1861 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001862 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1864 return;
1865 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001866
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001867#ifdef ANDROID
1868 if (wpa_s->conf->ap_scan == 1) {
1869#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001870 if (wpa_s->conf->ap_scan == 1 &&
1871 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001872#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001873 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1874 wpa_msg(wpa_s, MSG_WARNING,
1875 "WPA/RSN IEs not updated");
1876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 }
1878
1879#ifdef CONFIG_SME
1880 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1881 wpa_s->sme.prev_bssid_set = 1;
1882#endif /* CONFIG_SME */
1883
1884 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1885 if (wpa_s->current_ssid) {
1886 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1887 * initialized before association, but for other modes,
1888 * initialize PC/SC here, if the current configuration needs
1889 * smartcard or SIM/USIM. */
1890 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1891 }
1892 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1893 if (wpa_s->l2)
1894 l2_packet_notify_auth_start(wpa_s->l2);
1895
1896 /*
1897 * Set portEnabled first to FALSE in order to get EAP state machine out
1898 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1899 * state machine may transit to AUTHENTICATING state based on obsolete
1900 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1901 * AUTHENTICATED without ever giving chance to EAP state machine to
1902 * reset the state.
1903 */
1904 if (!ft_completed) {
1905 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1906 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1907 }
1908 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1909 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1910 /* 802.1X::portControl = Auto */
1911 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1912 wpa_s->eapol_received = 0;
1913 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1914 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1915 (wpa_s->current_ssid &&
1916 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001917 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1918 (wpa_s->drv_flags &
1919 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1920 /*
1921 * Set the key after having received joined-IBSS event
1922 * from the driver.
1923 */
1924 wpa_supplicant_set_wpa_none_key(wpa_s,
1925 wpa_s->current_ssid);
1926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 wpa_supplicant_cancel_auth_timeout(wpa_s);
1928 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1929 } else if (!ft_completed) {
1930 /* Timeout for receiving the first EAPOL packet */
1931 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1932 }
1933 wpa_supplicant_cancel_scan(wpa_s);
1934
1935 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1936 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1937 /*
1938 * We are done; the driver will take care of RSN 4-way
1939 * handshake.
1940 */
1941 wpa_supplicant_cancel_auth_timeout(wpa_s);
1942 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1943 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1944 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1945 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1946 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1947 /*
1948 * The driver will take care of RSN 4-way handshake, so we need
1949 * to allow EAPOL supplicant to complete its work without
1950 * waiting for WPA supplicant.
1951 */
1952 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1953 } else if (ft_completed) {
1954 /*
1955 * FT protocol completed - make sure EAPOL state machine ends
1956 * up in authenticated.
1957 */
1958 wpa_supplicant_cancel_auth_timeout(wpa_s);
1959 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1960 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1961 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1962 }
1963
Jouni Malinena05074c2012-12-21 21:35:35 +02001964 wpa_s->last_eapol_matches_bssid = 0;
1965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001967 struct os_reltime now, age;
1968 os_get_reltime(&now);
1969 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 if (age.sec == 0 && age.usec < 100000 &&
1971 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1972 0) {
1973 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1974 "frame that was received just before "
1975 "association notification");
1976 wpa_supplicant_rx_eapol(
1977 wpa_s, wpa_s->pending_eapol_rx_src,
1978 wpabuf_head(wpa_s->pending_eapol_rx),
1979 wpabuf_len(wpa_s->pending_eapol_rx));
1980 }
1981 wpabuf_free(wpa_s->pending_eapol_rx);
1982 wpa_s->pending_eapol_rx = NULL;
1983 }
1984
1985 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1986 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001987 wpa_s->current_ssid &&
1988 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 /* Set static WEP keys again */
1990 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1991 }
1992
1993#ifdef CONFIG_IBSS_RSN
1994 if (wpa_s->current_ssid &&
1995 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1996 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1997 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1998 wpa_s->ibss_rsn == NULL) {
1999 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2000 if (!wpa_s->ibss_rsn) {
2001 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2002 wpa_supplicant_deauthenticate(
2003 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2004 return;
2005 }
2006
2007 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2008 }
2009#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002010
2011 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012}
2013
2014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002015static int disconnect_reason_recoverable(u16 reason_code)
2016{
2017 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2018 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2019 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2020}
2021
2022
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002024 u16 reason_code,
2025 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026{
2027 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002028
2029 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2030 /*
2031 * At least Host AP driver and a Prism3 card seemed to be
2032 * generating streams of disconnected events when configuring
2033 * IBSS for WPA-None. Ignore them for now.
2034 */
2035 return;
2036 }
2037
2038 bssid = wpa_s->bssid;
2039 if (is_zero_ether_addr(bssid))
2040 bssid = wpa_s->pending_bssid;
2041
2042 if (!is_zero_ether_addr(bssid) ||
2043 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2044 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2045 " reason=%d%s",
2046 MAC2STR(bssid), reason_code,
2047 locally_generated ? " locally_generated=1" : "");
2048 }
2049}
2050
2051
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002052static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2053 int locally_generated)
2054{
2055 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2056 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2057 return 0; /* Not in 4-way handshake with PSK */
2058
2059 /*
2060 * It looks like connection was lost while trying to go through PSK
2061 * 4-way handshake. Filter out known disconnection cases that are caused
2062 * by something else than PSK mismatch to avoid confusing reports.
2063 */
2064
2065 if (locally_generated) {
2066 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2067 return 0;
2068 }
2069
2070 return 1;
2071}
2072
2073
Jouni Malinen2b89da82012-08-31 22:04:41 +03002074static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2075 u16 reason_code,
2076 int locally_generated)
2077{
2078 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 int authenticating;
2080 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002081 struct wpa_bss *fast_reconnect = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002082#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002083 struct wpa_ssid *fast_reconnect_ssid = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002084#endif /* CONFIG_NO_SCAN_PROCESSING */
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002085 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086
2087 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2088 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2089
2090 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2091 /*
2092 * At least Host AP driver and a Prism3 card seemed to be
2093 * generating streams of disconnected events when configuring
2094 * IBSS for WPA-None. Ignore them for now.
2095 */
2096 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2097 "IBSS/WPA-None mode");
2098 return;
2099 }
2100
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002101 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2103 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002104 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2105 return; /* P2P group removed */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002106 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002108 if (!wpa_s->disconnected &&
2109 (!wpa_s->auto_reconnect_disabled ||
2110 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002111 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2112 "reconnect (wps=%d wpa_state=%d)",
2113 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2114 wpa_s->wpa_state);
2115 if (wpa_s->wpa_state == WPA_COMPLETED &&
2116 wpa_s->current_ssid &&
2117 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002118 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002119 disconnect_reason_recoverable(reason_code)) {
2120 /*
2121 * It looks like the AP has dropped association with
2122 * us, but could allow us to get back in. Try to
2123 * reconnect to the same BSS without full scan to save
2124 * time for some common cases.
2125 */
2126 fast_reconnect = wpa_s->current_bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002127#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002128 fast_reconnect_ssid = wpa_s->current_ssid;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002129#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002131 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002132 else
2133 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2134 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 "try to re-connect");
2138 wpa_s->reassociate = 0;
2139 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002140 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 }
2142 bssid = wpa_s->bssid;
2143 if (is_zero_ether_addr(bssid))
2144 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002145 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2146 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002148 if (locally_generated)
2149 wpa_s->disconnect_reason = -reason_code;
2150 else
2151 wpa_s->disconnect_reason = reason_code;
2152 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2154 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 wpa_clear_keys(wpa_s, wpa_s->bssid);
2156 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002157 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 wpa_supplicant_mark_disassoc(wpa_s);
2159
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002160 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002162 wpa_s->current_ssid = last_ssid;
2163 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002164
2165 if (fast_reconnect) {
2166#ifndef CONFIG_NO_SCAN_PROCESSING
2167 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2168 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2169 fast_reconnect_ssid) < 0) {
2170 /* Recover through full scan */
2171 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2172 }
2173#endif /* CONFIG_NO_SCAN_PROCESSING */
2174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175}
2176
2177
2178#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002179void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180{
2181 struct wpa_supplicant *wpa_s = eloop_ctx;
2182
2183 if (!wpa_s->pending_mic_error_report)
2184 return;
2185
2186 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2187 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2188 wpa_s->pending_mic_error_report = 0;
2189}
2190#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2191
2192
2193static void
2194wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2195 union wpa_event_data *data)
2196{
2197 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002198 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199
2200 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2201 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002202 os_get_reltime(&t);
2203 if ((wpa_s->last_michael_mic_error.sec &&
2204 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 wpa_s->pending_mic_error_report) {
2206 if (wpa_s->pending_mic_error_report) {
2207 /*
2208 * Send the pending MIC error report immediately since
2209 * we are going to start countermeasures and AP better
2210 * do the same.
2211 */
2212 wpa_sm_key_request(wpa_s->wpa, 1,
2213 wpa_s->pending_mic_error_pairwise);
2214 }
2215
2216 /* Send the new MIC error report immediately since we are going
2217 * to start countermeasures and AP better do the same.
2218 */
2219 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2220
2221 /* initialize countermeasures */
2222 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002223
2224 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2227
2228 /*
2229 * Need to wait for completion of request frame. We do not get
2230 * any callback for the message completion, so just wait a
2231 * short while and hope for the best. */
2232 os_sleep(0, 10000);
2233
2234 wpa_drv_set_countermeasures(wpa_s, 1);
2235 wpa_supplicant_deauthenticate(wpa_s,
2236 WLAN_REASON_MICHAEL_MIC_FAILURE);
2237 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2238 wpa_s, NULL);
2239 eloop_register_timeout(60, 0,
2240 wpa_supplicant_stop_countermeasures,
2241 wpa_s, NULL);
2242 /* TODO: mark the AP rejected for 60 second. STA is
2243 * allowed to associate with another AP.. */
2244 } else {
2245#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2246 if (wpa_s->mic_errors_seen) {
2247 /*
2248 * Reduce the effectiveness of Michael MIC error
2249 * reports as a means for attacking against TKIP if
2250 * more than one MIC failure is noticed with the same
2251 * PTK. We delay the transmission of the reports by a
2252 * random time between 0 and 60 seconds in order to
2253 * force the attacker wait 60 seconds before getting
2254 * the information on whether a frame resulted in a MIC
2255 * failure.
2256 */
2257 u8 rval[4];
2258 int sec;
2259
2260 if (os_get_random(rval, sizeof(rval)) < 0)
2261 sec = os_random() % 60;
2262 else
2263 sec = WPA_GET_BE32(rval) % 60;
2264 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2265 "report %d seconds", sec);
2266 wpa_s->pending_mic_error_report = 1;
2267 wpa_s->pending_mic_error_pairwise = pairwise;
2268 eloop_cancel_timeout(
2269 wpa_supplicant_delayed_mic_error_report,
2270 wpa_s, NULL);
2271 eloop_register_timeout(
2272 sec, os_random() % 1000000,
2273 wpa_supplicant_delayed_mic_error_report,
2274 wpa_s, NULL);
2275 } else {
2276 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2277 }
2278#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2279 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2280#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2281 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002282 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 wpa_s->mic_errors_seen++;
2284}
2285
2286
2287#ifdef CONFIG_TERMINATE_ONLASTIF
2288static int any_interfaces(struct wpa_supplicant *head)
2289{
2290 struct wpa_supplicant *wpa_s;
2291
2292 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2293 if (!wpa_s->interface_removed)
2294 return 1;
2295 return 0;
2296}
2297#endif /* CONFIG_TERMINATE_ONLASTIF */
2298
2299
2300static void
2301wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2302 union wpa_event_data *data)
2303{
2304 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2305 return;
2306
2307 switch (data->interface_status.ievent) {
2308 case EVENT_INTERFACE_ADDED:
2309 if (!wpa_s->interface_removed)
2310 break;
2311 wpa_s->interface_removed = 0;
2312 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2313 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2314 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2315 "driver after interface was added");
2316 }
2317 break;
2318 case EVENT_INTERFACE_REMOVED:
2319 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2320 wpa_s->interface_removed = 1;
2321 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002322 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323 l2_packet_deinit(wpa_s->l2);
2324 wpa_s->l2 = NULL;
2325#ifdef CONFIG_IBSS_RSN
2326 ibss_rsn_deinit(wpa_s->ibss_rsn);
2327 wpa_s->ibss_rsn = NULL;
2328#endif /* CONFIG_IBSS_RSN */
2329#ifdef CONFIG_TERMINATE_ONLASTIF
2330 /* check if last interface */
2331 if (!any_interfaces(wpa_s->global->ifaces))
2332 eloop_terminate();
2333#endif /* CONFIG_TERMINATE_ONLASTIF */
2334 break;
2335 }
2336}
2337
2338
2339#ifdef CONFIG_PEERKEY
2340static void
2341wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2342 union wpa_event_data *data)
2343{
2344 if (data == NULL)
2345 return;
2346 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2347}
2348#endif /* CONFIG_PEERKEY */
2349
2350
2351#ifdef CONFIG_TDLS
2352static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2353 union wpa_event_data *data)
2354{
2355 if (data == NULL)
2356 return;
2357 switch (data->tdls.oper) {
2358 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002359 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2360 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2361 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2362 else
2363 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364 break;
2365 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002366 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2367 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2368 data->tdls.reason_code);
2369 else
2370 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2371 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 break;
2373 }
2374}
2375#endif /* CONFIG_TDLS */
2376
2377
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002378#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002379static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2380 union wpa_event_data *data)
2381{
2382 if (data == NULL)
2383 return;
2384 switch (data->wnm.oper) {
2385 case WNM_OPER_SLEEP:
2386 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2387 "(action=%d, intval=%d)",
2388 data->wnm.sleep_action, data->wnm.sleep_intval);
2389 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002390 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002391 break;
2392 }
2393}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002394#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002395
2396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397#ifdef CONFIG_IEEE80211R
2398static void
2399wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2400 union wpa_event_data *data)
2401{
2402 if (data == NULL)
2403 return;
2404
2405 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2406 data->ft_ies.ies_len,
2407 data->ft_ies.ft_action,
2408 data->ft_ies.target_ap,
2409 data->ft_ies.ric_ies,
2410 data->ft_ies.ric_ies_len) < 0) {
2411 /* TODO: prevent MLME/driver from trying to associate? */
2412 }
2413}
2414#endif /* CONFIG_IEEE80211R */
2415
2416
2417#ifdef CONFIG_IBSS_RSN
2418static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2419 union wpa_event_data *data)
2420{
2421 struct wpa_ssid *ssid;
2422 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2423 return;
2424 if (data == NULL)
2425 return;
2426 ssid = wpa_s->current_ssid;
2427 if (ssid == NULL)
2428 return;
2429 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2430 return;
2431
2432 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2433}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002434
2435
2436static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2437 union wpa_event_data *data)
2438{
2439 struct wpa_ssid *ssid = wpa_s->current_ssid;
2440
2441 if (ssid == NULL)
2442 return;
2443
2444 /* check if the ssid is correctly configured as IBSS/RSN */
2445 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2446 return;
2447
2448 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2449 data->rx_mgmt.frame_len);
2450}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451#endif /* CONFIG_IBSS_RSN */
2452
2453
2454#ifdef CONFIG_IEEE80211R
2455static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2456 size_t len)
2457{
2458 const u8 *sta_addr, *target_ap_addr;
2459 u16 status;
2460
2461 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2462 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2463 return; /* only SME case supported for now */
2464 if (len < 1 + 2 * ETH_ALEN + 2)
2465 return;
2466 if (data[0] != 2)
2467 return; /* Only FT Action Response is supported for now */
2468 sta_addr = data + 1;
2469 target_ap_addr = data + 1 + ETH_ALEN;
2470 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2471 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2472 MACSTR " TargetAP " MACSTR " status %u",
2473 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2474
2475 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2476 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2477 " in FT Action Response", MAC2STR(sta_addr));
2478 return;
2479 }
2480
2481 if (status) {
2482 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2483 "failure (status code %d)", status);
2484 /* TODO: report error to FT code(?) */
2485 return;
2486 }
2487
2488 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2489 len - (1 + 2 * ETH_ALEN + 2), 1,
2490 target_ap_addr, NULL, 0) < 0)
2491 return;
2492
2493#ifdef CONFIG_SME
2494 {
2495 struct wpa_bss *bss;
2496 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2497 if (bss)
2498 wpa_s->sme.freq = bss->freq;
2499 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2500 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2501 WLAN_AUTH_FT);
2502 }
2503#endif /* CONFIG_SME */
2504}
2505#endif /* CONFIG_IEEE80211R */
2506
2507
2508static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2509 struct unprot_deauth *e)
2510{
2511#ifdef CONFIG_IEEE80211W
2512 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2513 "dropped: " MACSTR " -> " MACSTR
2514 " (reason code %u)",
2515 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2516 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2517#endif /* CONFIG_IEEE80211W */
2518}
2519
2520
2521static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2522 struct unprot_disassoc *e)
2523{
2524#ifdef CONFIG_IEEE80211W
2525 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2526 "dropped: " MACSTR " -> " MACSTR
2527 " (reason code %u)",
2528 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2529 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2530#endif /* CONFIG_IEEE80211W */
2531}
2532
2533
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002534static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2535 u16 reason_code, int locally_generated,
2536 const u8 *ie, size_t ie_len, int deauth)
2537{
2538#ifdef CONFIG_AP
2539 if (wpa_s->ap_iface && addr) {
2540 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2541 return;
2542 }
2543
2544 if (wpa_s->ap_iface) {
2545 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2546 return;
2547 }
2548#endif /* CONFIG_AP */
2549
2550 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2551
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002552 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2553 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2554 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2555 eapol_sm_failed(wpa_s->eapol))) &&
2556 !wpa_s->eap_expected_failure))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002557 wpas_auth_failed(wpa_s);
2558
2559#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002560 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002561 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2562 locally_generated) > 0) {
2563 /*
2564 * The interface was removed, so cannot continue
2565 * processing any additional operations after this.
2566 */
2567 return;
2568 }
2569 }
2570#endif /* CONFIG_P2P */
2571
2572 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2573 locally_generated);
2574}
2575
2576
2577static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2578 struct disassoc_info *info)
2579{
2580 u16 reason_code = 0;
2581 int locally_generated = 0;
2582 const u8 *addr = NULL;
2583 const u8 *ie = NULL;
2584 size_t ie_len = 0;
2585
2586 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2587
2588 if (info) {
2589 addr = info->addr;
2590 ie = info->ie;
2591 ie_len = info->ie_len;
2592 reason_code = info->reason_code;
2593 locally_generated = info->locally_generated;
2594 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2595 locally_generated ? " (locally generated)" : "");
2596 if (addr)
2597 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2598 MAC2STR(addr));
2599 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2600 ie, ie_len);
2601 }
2602
2603#ifdef CONFIG_AP
2604 if (wpa_s->ap_iface && info && info->addr) {
2605 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2606 return;
2607 }
2608
2609 if (wpa_s->ap_iface) {
2610 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2611 return;
2612 }
2613#endif /* CONFIG_AP */
2614
2615#ifdef CONFIG_P2P
2616 if (info) {
2617 wpas_p2p_disassoc_notif(
2618 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2619 locally_generated);
2620 }
2621#endif /* CONFIG_P2P */
2622
2623 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2624 sme_event_disassoc(wpa_s, info);
2625
2626 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2627 ie, ie_len, 0);
2628}
2629
2630
2631static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2632 struct deauth_info *info)
2633{
2634 u16 reason_code = 0;
2635 int locally_generated = 0;
2636 const u8 *addr = NULL;
2637 const u8 *ie = NULL;
2638 size_t ie_len = 0;
2639
2640 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2641
2642 if (info) {
2643 addr = info->addr;
2644 ie = info->ie;
2645 ie_len = info->ie_len;
2646 reason_code = info->reason_code;
2647 locally_generated = info->locally_generated;
2648 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2649 reason_code,
2650 locally_generated ? " (locally generated)" : "");
2651 if (addr) {
2652 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2653 MAC2STR(addr));
2654 }
2655 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2656 ie, ie_len);
2657 }
2658
2659 wpa_reset_ft_completed(wpa_s->wpa);
2660
2661 wpas_event_disconnect(wpa_s, addr, reason_code,
2662 locally_generated, ie, ie_len, 1);
2663}
2664
2665
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002666static void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s)
2667{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002668 struct wpa_supplicant *ifs;
2669
2670 if (wpa_s->drv_priv == NULL)
2671 return; /* Ignore event during drv initialization */
2672
2673 free_hw_features(wpa_s);
2674 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2675 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2676
2677#ifdef CONFIG_P2P
2678 wpas_p2p_update_channel_list(wpa_s);
2679#endif /* CONFIG_P2P */
2680
2681 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002682 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002683 * so, they get updated with this same hw mode info.
2684 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002685 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2686 radio_list) {
2687 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002688 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2689 ifs->ifname);
2690 free_hw_features(ifs);
2691 ifs->hw.modes = wpa_drv_get_hw_feature_data(
2692 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2693 }
2694 }
2695}
2696
2697
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002698static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2699 const struct ieee80211_mgmt *mgmt,
2700 size_t len, int freq)
2701{
2702 const u8 *payload;
2703 size_t plen;
2704 u8 category;
2705
2706 if (len < IEEE80211_HDRLEN + 2)
2707 return;
2708
2709 payload = &mgmt->u.action.category;
2710 category = *payload++;
2711 plen = (((const u8 *) mgmt) + len) - payload;
2712
2713 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2714 " Category=%u DataLen=%d freq=%d MHz",
2715 MAC2STR(mgmt->sa), category, (int) plen, freq);
2716
2717#ifdef CONFIG_IEEE80211R
2718 if (category == WLAN_ACTION_FT) {
2719 ft_rx_action(wpa_s, payload, plen);
2720 return;
2721 }
2722#endif /* CONFIG_IEEE80211R */
2723
2724#ifdef CONFIG_IEEE80211W
2725#ifdef CONFIG_SME
2726 if (category == WLAN_ACTION_SA_QUERY) {
2727 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2728 return;
2729 }
2730#endif /* CONFIG_SME */
2731#endif /* CONFIG_IEEE80211W */
2732
2733#ifdef CONFIG_WNM
2734 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2735 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2736 return;
2737 }
2738#endif /* CONFIG_WNM */
2739
2740#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08002741 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2742 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002743 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08002744 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002745 payload, plen, freq) == 0)
2746 return;
2747#endif /* CONFIG_GAS */
2748
2749#ifdef CONFIG_TDLS
2750 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2751 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2752 wpa_dbg(wpa_s, MSG_DEBUG,
2753 "TDLS: Received Discovery Response from " MACSTR,
2754 MAC2STR(mgmt->sa));
2755 return;
2756 }
2757#endif /* CONFIG_TDLS */
2758
2759#ifdef CONFIG_INTERWORKING
2760 if (category == WLAN_ACTION_QOS && plen >= 1 &&
2761 payload[0] == QOS_QOS_MAP_CONFIG) {
2762 const u8 *pos = payload + 1;
2763 size_t qlen = plen - 1;
2764 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2765 MACSTR, MAC2STR(mgmt->sa));
2766 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2767 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2768 pos[1] <= qlen - 2 && pos[1] >= 16)
2769 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2770 return;
2771 }
2772#endif /* CONFIG_INTERWORKING */
2773
2774#ifdef CONFIG_P2P
2775 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2776 category, payload, plen, freq);
2777#endif /* CONFIG_P2P */
2778}
2779
2780
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002781static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2782 union wpa_event_data *event)
2783{
2784#ifdef CONFIG_P2P
2785 struct wpa_supplicant *ifs;
2786#endif /* CONFIG_P2P */
2787 struct wpa_freq_range_list *list;
2788 char *str = NULL;
2789
2790 list = &event->freq_range;
2791
2792 if (list->num)
2793 str = freq_range_list_str(list);
2794 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2795 str ? str : "");
2796
2797#ifdef CONFIG_P2P
2798 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2799 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2800 __func__);
2801 } else {
2802 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2803 wpas_p2p_update_channel_list(wpa_s);
2804 }
2805
2806 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2807 int freq;
2808 if (!ifs->current_ssid ||
2809 !ifs->current_ssid->p2p_group ||
2810 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2811 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2812 continue;
2813
2814 freq = ifs->current_ssid->frequency;
2815 if (!freq_range_list_includes(list, freq)) {
2816 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2817 freq);
2818 continue;
2819 }
2820
2821 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2822 freq);
2823 /* TODO: Consider using CSA or removing the group within
2824 * wpa_supplicant */
2825 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2826 }
2827#endif /* CONFIG_P2P */
2828
2829 os_free(str);
2830}
2831
2832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2834 union wpa_event_data *data)
2835{
2836 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837
2838 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2839 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002840 event != EVENT_INTERFACE_STATUS &&
2841 event != EVENT_SCHED_SCAN_STOPPED) {
2842 wpa_dbg(wpa_s, MSG_DEBUG,
2843 "Ignore event %s (%d) while interface is disabled",
2844 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845 return;
2846 }
2847
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002848#ifndef CONFIG_NO_STDOUT_DEBUG
2849{
2850 int level = MSG_DEBUG;
2851
Dmitry Shmidt04949592012-07-19 12:16:46 -07002852 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002853 const struct ieee80211_hdr *hdr;
2854 u16 fc;
2855 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2856 fc = le_to_host16(hdr->frame_control);
2857 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2858 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2859 level = MSG_EXCESSIVE;
2860 }
2861
2862 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2863 event_to_string(event), event);
2864}
2865#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866
2867 switch (event) {
2868 case EVENT_AUTH:
2869 sme_event_auth(wpa_s, data);
2870 break;
2871 case EVENT_ASSOC:
2872 wpa_supplicant_event_assoc(wpa_s, data);
2873 break;
2874 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002875 wpas_event_disassoc(wpa_s,
2876 data ? &data->disassoc_info : NULL);
2877 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002879 wpas_event_deauth(wpa_s,
2880 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 break;
2882 case EVENT_MICHAEL_MIC_FAILURE:
2883 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2884 break;
2885#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002886 case EVENT_SCAN_STARTED:
2887 os_get_reltime(&wpa_s->scan_start_time);
2888 if (wpa_s->own_scan_requested) {
2889 struct os_reltime diff;
2890
2891 os_reltime_sub(&wpa_s->scan_start_time,
2892 &wpa_s->scan_trigger_time, &diff);
2893 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2894 diff.sec, diff.usec);
2895 wpa_s->own_scan_requested = 0;
2896 wpa_s->own_scan_running = 1;
2897 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2898 wpa_s->manual_scan_use_id) {
2899 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
2900 wpa_s->manual_scan_id);
2901 } else {
2902 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2903 }
2904 } else {
2905 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
2906 wpa_s->external_scan_running = 1;
2907 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2908 }
2909 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002911 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
2912 struct os_reltime now, diff;
2913 os_get_reltime(&now);
2914 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
2915 wpa_s->scan_start_time.sec = 0;
2916 wpa_s->scan_start_time.usec = 0;
2917 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
2918 diff.sec, diff.usec);
2919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002921 wpa_s->own_scan_running = 0;
2922 wpa_s->external_scan_running = 0;
2923 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002924 break;
2925#endif /* CONFIG_NO_SCAN_PROCESSING */
2926 case EVENT_ASSOCINFO:
2927 wpa_supplicant_event_associnfo(wpa_s, data);
2928 break;
2929 case EVENT_INTERFACE_STATUS:
2930 wpa_supplicant_event_interface_status(wpa_s, data);
2931 break;
2932 case EVENT_PMKID_CANDIDATE:
2933 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2934 break;
2935#ifdef CONFIG_PEERKEY
2936 case EVENT_STKSTART:
2937 wpa_supplicant_event_stkstart(wpa_s, data);
2938 break;
2939#endif /* CONFIG_PEERKEY */
2940#ifdef CONFIG_TDLS
2941 case EVENT_TDLS:
2942 wpa_supplicant_event_tdls(wpa_s, data);
2943 break;
2944#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002945#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002946 case EVENT_WNM:
2947 wpa_supplicant_event_wnm(wpa_s, data);
2948 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002949#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950#ifdef CONFIG_IEEE80211R
2951 case EVENT_FT_RESPONSE:
2952 wpa_supplicant_event_ft_response(wpa_s, data);
2953 break;
2954#endif /* CONFIG_IEEE80211R */
2955#ifdef CONFIG_IBSS_RSN
2956 case EVENT_IBSS_RSN_START:
2957 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2958 break;
2959#endif /* CONFIG_IBSS_RSN */
2960 case EVENT_ASSOC_REJECT:
2961 if (data->assoc_reject.bssid)
2962 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2963 "bssid=" MACSTR " status_code=%u",
2964 MAC2STR(data->assoc_reject.bssid),
2965 data->assoc_reject.status_code);
2966 else
2967 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2968 "status_code=%u",
2969 data->assoc_reject.status_code);
2970 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2971 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07002972 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002973 const u8 *bssid = data->assoc_reject.bssid;
2974 if (bssid == NULL || is_zero_ether_addr(bssid))
2975 bssid = wpa_s->pending_bssid;
2976 wpas_connection_failed(wpa_s, bssid);
2977 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002978 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 break;
2980 case EVENT_AUTH_TIMED_OUT:
2981 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2982 sme_event_auth_timed_out(wpa_s, data);
2983 break;
2984 case EVENT_ASSOC_TIMED_OUT:
2985 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2986 sme_event_assoc_timed_out(wpa_s, data);
2987 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 case EVENT_TX_STATUS:
2989 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2990 " type=%d stype=%d",
2991 MAC2STR(data->tx_status.dst),
2992 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002993#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002995#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2997 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002998 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 wpa_s, data->tx_status.dst,
3000 data->tx_status.data,
3001 data->tx_status.data_len,
3002 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003003 OFFCHANNEL_SEND_ACTION_SUCCESS :
3004 OFFCHANNEL_SEND_ACTION_NO_ACK);
3005#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 break;
3007 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003008#endif /* CONFIG_AP */
3009#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3011 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3012 /*
3013 * Catch TX status events for Action frames we sent via group
3014 * interface in GO mode.
3015 */
3016 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3017 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3018 os_memcmp(wpa_s->parent->pending_action_dst,
3019 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003020 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 wpa_s->parent, data->tx_status.dst,
3022 data->tx_status.data,
3023 data->tx_status.data_len,
3024 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003025 OFFCHANNEL_SEND_ACTION_SUCCESS :
3026 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 break;
3028 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003029#endif /* CONFIG_OFFCHANNEL */
3030#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 switch (data->tx_status.type) {
3032 case WLAN_FC_TYPE_MGMT:
3033 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3034 data->tx_status.data_len,
3035 data->tx_status.stype,
3036 data->tx_status.ack);
3037 break;
3038 case WLAN_FC_TYPE_DATA:
3039 ap_tx_status(wpa_s, data->tx_status.dst,
3040 data->tx_status.data,
3041 data->tx_status.data_len,
3042 data->tx_status.ack);
3043 break;
3044 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003045#endif /* CONFIG_AP */
3046 break;
3047#ifdef CONFIG_AP
3048 case EVENT_EAPOL_TX_STATUS:
3049 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3050 data->eapol_tx_status.data,
3051 data->eapol_tx_status.data_len,
3052 data->eapol_tx_status.ack);
3053 break;
3054 case EVENT_DRIVER_CLIENT_POLL_OK:
3055 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 break;
3057 case EVENT_RX_FROM_UNKNOWN:
3058 if (wpa_s->ap_iface == NULL)
3059 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003060 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3061 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063 case EVENT_CH_SWITCH:
3064 if (!data)
3065 break;
3066 if (!wpa_s->ap_iface) {
3067 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3068 "event in non-AP mode");
3069 break;
3070 }
3071
Dmitry Shmidt04949592012-07-19 12:16:46 -07003072 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3073 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003074 data->ch_switch.ch_offset,
3075 data->ch_switch.ch_width,
3076 data->ch_switch.cf1,
3077 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003078 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003079#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003080 case EVENT_RX_MGMT: {
3081 u16 fc, stype;
3082 const struct ieee80211_mgmt *mgmt;
3083
3084 mgmt = (const struct ieee80211_mgmt *)
3085 data->rx_mgmt.frame;
3086 fc = le_to_host16(mgmt->frame_control);
3087 stype = WLAN_FC_GET_STYPE(fc);
3088
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003089#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003091#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3094 data->rx_mgmt.frame_len > 24) {
3095 const u8 *src = mgmt->sa;
3096 const u8 *ie = mgmt->u.probe_req.variable;
3097 size_t ie_len = data->rx_mgmt.frame_len -
3098 (mgmt->u.probe_req.variable -
3099 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003100 wpas_p2p_probe_req_rx(
3101 wpa_s, src, mgmt->da,
3102 mgmt->bssid, ie, ie_len,
3103 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104 break;
3105 }
3106#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003107#ifdef CONFIG_IBSS_RSN
3108 if (stype == WLAN_FC_STYPE_AUTH &&
3109 data->rx_mgmt.frame_len >= 30) {
3110 wpa_supplicant_event_ibss_auth(wpa_s, data);
3111 break;
3112 }
3113#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003114
3115 if (stype == WLAN_FC_STYPE_ACTION) {
3116 wpas_event_rx_mgmt_action(
3117 wpa_s, mgmt, data->rx_mgmt.frame_len,
3118 data->rx_mgmt.freq);
3119 break;
3120 }
3121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3123 "management frame in non-AP mode");
3124 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003125#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003127
3128 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3129 data->rx_mgmt.frame_len > 24) {
3130 const u8 *ie = mgmt->u.probe_req.variable;
3131 size_t ie_len = data->rx_mgmt.frame_len -
3132 (mgmt->u.probe_req.variable -
3133 data->rx_mgmt.frame);
3134
3135 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3136 mgmt->bssid, ie, ie_len,
3137 data->rx_mgmt.ssi_signal);
3138 }
3139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003141#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 case EVENT_RX_PROBE_REQ:
3145 if (data->rx_probe_req.sa == NULL ||
3146 data->rx_probe_req.ie == NULL)
3147 break;
3148#ifdef CONFIG_AP
3149 if (wpa_s->ap_iface) {
3150 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3151 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003152 data->rx_probe_req.da,
3153 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003154 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003155 data->rx_probe_req.ie_len,
3156 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157 break;
3158 }
3159#endif /* CONFIG_AP */
3160#ifdef CONFIG_P2P
3161 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003162 data->rx_probe_req.da,
3163 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003165 data->rx_probe_req.ie_len,
3166 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167#endif /* CONFIG_P2P */
3168 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003170#ifdef CONFIG_OFFCHANNEL
3171 offchannel_remain_on_channel_cb(
3172 wpa_s, data->remain_on_channel.freq,
3173 data->remain_on_channel.duration);
3174#endif /* CONFIG_OFFCHANNEL */
3175#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003176 wpas_p2p_remain_on_channel_cb(
3177 wpa_s, data->remain_on_channel.freq,
3178 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003179#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180 break;
3181 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003182#ifdef CONFIG_OFFCHANNEL
3183 offchannel_cancel_remain_on_channel_cb(
3184 wpa_s, data->remain_on_channel.freq);
3185#endif /* CONFIG_OFFCHANNEL */
3186#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 wpas_p2p_cancel_remain_on_channel_cb(
3188 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003189#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 case EVENT_EAPOL_RX:
3192 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3193 data->eapol_rx.data,
3194 data->eapol_rx.data_len);
3195 break;
3196 case EVENT_SIGNAL_CHANGE:
3197 bgscan_notify_signal_change(
3198 wpa_s, data->signal_change.above_threshold,
3199 data->signal_change.current_signal,
3200 data->signal_change.current_noise,
3201 data->signal_change.current_txrate);
3202 break;
3203 case EVENT_INTERFACE_ENABLED:
3204 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3205 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003206 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207#ifdef CONFIG_AP
3208 if (!wpa_s->ap_iface) {
3209 wpa_supplicant_set_state(wpa_s,
3210 WPA_DISCONNECTED);
3211 wpa_supplicant_req_scan(wpa_s, 0, 0);
3212 } else
3213 wpa_supplicant_set_state(wpa_s,
3214 WPA_COMPLETED);
3215#else /* CONFIG_AP */
3216 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3217 wpa_supplicant_req_scan(wpa_s, 0, 0);
3218#endif /* CONFIG_AP */
3219 }
3220 break;
3221 case EVENT_INTERFACE_DISABLED:
3222 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003223#ifdef CONFIG_P2P
3224 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3225 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3226 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3227 /*
3228 * The interface was externally disabled. Remove
3229 * it assuming an external entity will start a
3230 * new session if needed.
3231 */
3232 wpas_p2p_disconnect(wpa_s);
3233 break;
3234 }
3235#endif /* CONFIG_P2P */
3236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003238 radio_remove_works(wpa_s, NULL, 0);
3239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3241 break;
3242 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003243 wpa_supplicant_update_channel_list(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 break;
3245 case EVENT_INTERFACE_UNAVAILABLE:
3246#ifdef CONFIG_P2P
3247 wpas_p2p_interface_unavailable(wpa_s);
3248#endif /* CONFIG_P2P */
3249 break;
3250 case EVENT_BEST_CHANNEL:
3251 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3252 "(%d %d %d)",
3253 data->best_chan.freq_24, data->best_chan.freq_5,
3254 data->best_chan.freq_overall);
3255 wpa_s->best_24_freq = data->best_chan.freq_24;
3256 wpa_s->best_5_freq = data->best_chan.freq_5;
3257 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3258#ifdef CONFIG_P2P
3259 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3260 data->best_chan.freq_5,
3261 data->best_chan.freq_overall);
3262#endif /* CONFIG_P2P */
3263 break;
3264 case EVENT_UNPROT_DEAUTH:
3265 wpa_supplicant_event_unprot_deauth(wpa_s,
3266 &data->unprot_deauth);
3267 break;
3268 case EVENT_UNPROT_DISASSOC:
3269 wpa_supplicant_event_unprot_disassoc(wpa_s,
3270 &data->unprot_disassoc);
3271 break;
3272 case EVENT_STATION_LOW_ACK:
3273#ifdef CONFIG_AP
3274 if (wpa_s->ap_iface && data)
3275 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3276 data->low_ack.addr);
3277#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003278#ifdef CONFIG_TDLS
3279 if (data)
3280 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3281#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003282 break;
3283 case EVENT_IBSS_PEER_LOST:
3284#ifdef CONFIG_IBSS_RSN
3285 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3286#endif /* CONFIG_IBSS_RSN */
3287 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003288 case EVENT_DRIVER_GTK_REKEY:
3289 if (os_memcmp(data->driver_gtk_rekey.bssid,
3290 wpa_s->bssid, ETH_ALEN))
3291 break;
3292 if (!wpa_s->wpa)
3293 break;
3294 wpa_sm_update_replay_ctr(wpa_s->wpa,
3295 data->driver_gtk_rekey.replay_ctr);
3296 break;
3297 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003298 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003299 wpa_s->sched_scanning = 0;
3300 wpa_supplicant_notify_scanning(wpa_s, 0);
3301
3302 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3303 break;
3304
3305 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003306 * Start a new sched scan to continue searching for more SSIDs
3307 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003308 */
Dmitry Shmidt18463232014-01-24 12:29:41 -08003309 if (wpa_s->sched_scan_timed_out || wpa_s->pno_sched_pending) {
3310
3311 if (wpa_supplicant_req_sched_scan(wpa_s) < 0 &&
3312 wpa_s->pno_sched_pending) {
3313 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3314 } else if (wpa_s->pno_sched_pending) {
3315 wpa_s->pno_sched_pending = 0;
3316 wpa_s->pno = 1;
3317 }
3318 }
3319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003320 break;
3321 case EVENT_WPS_BUTTON_PUSHED:
3322#ifdef CONFIG_WPS
3323 wpas_wps_start_pbc(wpa_s, NULL, 0);
3324#endif /* CONFIG_WPS */
3325 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003326 case EVENT_AVOID_FREQUENCIES:
3327 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3328 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003329 case EVENT_CONNECT_FAILED_REASON:
3330#ifdef CONFIG_AP
3331 if (!wpa_s->ap_iface || !data)
3332 break;
3333 hostapd_event_connect_failed_reason(
3334 wpa_s->ap_iface->bss[0],
3335 data->connect_failed_reason.addr,
3336 data->connect_failed_reason.code);
3337#endif /* CONFIG_AP */
3338 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339 default:
3340 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3341 break;
3342 }
3343}