blob: c0f55eea890e6cc9573b9bcd8e6f43abc8a70389 [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 Shmidtf21452a2014-02-26 10:55:25 -0800401 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
402 privacy = 1;
403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 if (bss->caps & IEEE80211_CAP_PRIVACY)
405 return privacy;
406 return !privacy;
407}
408
409
410static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
411 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700412 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413{
414 struct wpa_ie_data ie;
415 int proto_match = 0;
416 const u8 *rsn_ie, *wpa_ie;
417 int ret;
418 int wep_ok;
419
420 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
421 if (ret >= 0)
422 return ret;
423
424 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
425 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
426 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
427 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
428 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
429
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700430 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
432 proto_match++;
433
434 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
435 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
436 "failed");
437 break;
438 }
439
440 if (wep_ok &&
441 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
442 {
443 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
444 "in RSN IE");
445 return 1;
446 }
447
448 if (!(ie.proto & ssid->proto)) {
449 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
450 "mismatch");
451 break;
452 }
453
454 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
455 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
456 "cipher mismatch");
457 break;
458 }
459
460 if (!(ie.group_cipher & ssid->group_cipher)) {
461 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
462 "cipher mismatch");
463 break;
464 }
465
466 if (!(ie.key_mgmt & ssid->key_mgmt)) {
467 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
468 "mismatch");
469 break;
470 }
471
472#ifdef CONFIG_IEEE80211W
473 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800474 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
475 wpa_s->conf->pmf : ssid->ieee80211w) ==
476 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
478 "frame protection");
479 break;
480 }
481#endif /* CONFIG_IEEE80211W */
482
483 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
484 return 1;
485 }
486
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700487 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
489 proto_match++;
490
491 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
492 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
493 "failed");
494 break;
495 }
496
497 if (wep_ok &&
498 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
499 {
500 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
501 "in WPA IE");
502 return 1;
503 }
504
505 if (!(ie.proto & ssid->proto)) {
506 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
507 "mismatch");
508 break;
509 }
510
511 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
512 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
513 "cipher mismatch");
514 break;
515 }
516
517 if (!(ie.group_cipher & ssid->group_cipher)) {
518 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
519 "cipher mismatch");
520 break;
521 }
522
523 if (!(ie.key_mgmt & ssid->key_mgmt)) {
524 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
525 "mismatch");
526 break;
527 }
528
529 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
530 return 1;
531 }
532
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700533 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
534 !rsn_ie) {
535 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
536 return 1;
537 }
538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
540 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
541 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
542 return 0;
543 }
544
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800545 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
546 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
547 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
548 return 1;
549 }
550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
552 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
553 return 1;
554 }
555
556 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
557 "WPA/WPA2");
558
559 return 0;
560}
561
562
563static int freq_allowed(int *freqs, int freq)
564{
565 int i;
566
567 if (freqs == NULL)
568 return 1;
569
570 for (i = 0; freqs[i]; i++)
571 if (freqs[i] == freq)
572 return 1;
573 return 0;
574}
575
576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800577static int ht_supported(const struct hostapd_hw_modes *mode)
578{
579 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
580 /*
581 * The driver did not indicate whether it supports HT. Assume
582 * it does to avoid connection issues.
583 */
584 return 1;
585 }
586
587 /*
588 * IEEE Std 802.11n-2009 20.1.1:
589 * An HT non-AP STA shall support all EQM rates for one spatial stream.
590 */
591 return mode->mcs_set[0] == 0xff;
592}
593
594
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700595static int vht_supported(const struct hostapd_hw_modes *mode)
596{
597 if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
598 /*
599 * The driver did not indicate whether it supports VHT. Assume
600 * it does to avoid connection issues.
601 */
602 return 1;
603 }
604
605 /*
606 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
607 * TODO: Verify if this complies with the standard
608 */
609 return (mode->vht_mcs_set[0] & 0x3) != 3;
610}
611
612
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700613static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614{
615 const struct hostapd_hw_modes *mode = NULL, *modes;
616 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
617 const u8 *rate_ie;
618 int i, j, k;
619
620 if (bss->freq == 0)
621 return 1; /* Cannot do matching without knowing band */
622
623 modes = wpa_s->hw.modes;
624 if (modes == NULL) {
625 /*
626 * The driver does not provide any additional information
627 * about the utilized hardware, so allow the connection attempt
628 * to continue.
629 */
630 return 1;
631 }
632
633 for (i = 0; i < wpa_s->hw.num_modes; i++) {
634 for (j = 0; j < modes[i].num_channels; j++) {
635 int freq = modes[i].channels[j].freq;
636 if (freq == bss->freq) {
637 if (mode &&
638 mode->mode == HOSTAPD_MODE_IEEE80211G)
639 break; /* do not allow 802.11b replace
640 * 802.11g */
641 mode = &modes[i];
642 break;
643 }
644 }
645 }
646
647 if (mode == NULL)
648 return 0;
649
650 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700651 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 if (rate_ie == NULL)
653 continue;
654
655 for (j = 2; j < rate_ie[1] + 2; j++) {
656 int flagged = !!(rate_ie[j] & 0x80);
657 int r = (rate_ie[j] & 0x7f) * 5;
658
659 /*
660 * IEEE Std 802.11n-2009 7.3.2.2:
661 * The new BSS Membership selector value is encoded
662 * like a legacy basic rate, but it is not a rate and
663 * only indicates if the BSS members are required to
664 * support the mandatory features of Clause 20 [HT PHY]
665 * in order to join the BSS.
666 */
667 if (flagged && ((rate_ie[j] & 0x7f) ==
668 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
669 if (!ht_supported(mode)) {
670 wpa_dbg(wpa_s, MSG_DEBUG,
671 " hardware does not support "
672 "HT PHY");
673 return 0;
674 }
675 continue;
676 }
677
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700678 /* There's also a VHT selector for 802.11ac */
679 if (flagged && ((rate_ie[j] & 0x7f) ==
680 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
681 if (!vht_supported(mode)) {
682 wpa_dbg(wpa_s, MSG_DEBUG,
683 " hardware does not support "
684 "VHT PHY");
685 return 0;
686 }
687 continue;
688 }
689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 if (!flagged)
691 continue;
692
693 /* check for legacy basic rates */
694 for (k = 0; k < mode->num_rates; k++) {
695 if (mode->rates[k] == r)
696 break;
697 }
698 if (k == mode->num_rates) {
699 /*
700 * IEEE Std 802.11-2007 7.3.2.2 demands that in
701 * order to join a BSS all required rates
702 * have to be supported by the hardware.
703 */
704 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
705 "not support required rate %d.%d Mbps",
706 r / 10, r % 10);
707 return 0;
708 }
709 }
710 }
711
712 return 1;
713}
714
715
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800716static int bss_is_dmg(struct wpa_bss *bss)
717{
718 return bss->freq > 45000;
719}
720
721
722/*
723 * Test whether BSS is in an ESS.
724 * This is done differently in DMG (60 GHz) and non-DMG bands
725 */
726static int bss_is_ess(struct wpa_bss *bss)
727{
728 if (bss_is_dmg(bss)) {
729 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
730 IEEE80211_CAP_DMG_AP;
731 }
732
733 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
734 IEEE80211_CAP_ESS);
735}
736
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700739 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800740 struct wpa_ssid *group,
741 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700743 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 int wpa;
745 struct wpa_blacklist *e;
746 const u8 *ie;
747 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800748 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700750 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 wpa_ie_len = ie ? ie[1] : 0;
752
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700753 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 rsn_ie_len = ie ? ie[1] : 0;
755
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800756 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
757 osen = ie != NULL;
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800760 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700761 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700763 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
764 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
765 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800766 " p2p" : "",
767 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768
769 e = wpa_blacklist_get(wpa_s, bss->bssid);
770 if (e) {
771 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700772 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 /*
774 * When only a single network is enabled, we can
775 * trigger blacklisting on the first failure. This
776 * should not be done with multiple enabled networks to
777 * avoid getting forced to move into a worse ESS on
778 * single error if there are no other BSSes of the
779 * current ESS.
780 */
781 limit = 0;
782 }
783 if (e->count > limit) {
784 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
785 "(count=%d limit=%d)", e->count, limit);
786 return NULL;
787 }
788 }
789
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700790 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
792 return NULL;
793 }
794
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800795 if (disallowed_bssid(wpa_s, bss->bssid)) {
796 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
797 return NULL;
798 }
799
800 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
801 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
802 return NULL;
803 }
804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
806
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800807 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700809 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
813 continue;
814 }
815
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700816 res = wpas_temp_disabled(wpa_s, ssid);
817 if (res > 0) {
818 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
819 "temporarily for %d second(s)", res);
820 continue;
821 }
822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823#ifdef CONFIG_WPS
824 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
825 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
826 "(WPS)");
827 continue;
828 }
829
830 if (wpa && ssid->ssid_len == 0 &&
831 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
832 check_ssid = 0;
833
834 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
835 /* Only allow wildcard SSID match if an AP
836 * advertises active WPS operation that matches
837 * with our mode. */
838 check_ssid = 1;
839 if (ssid->ssid_len == 0 &&
840 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
841 check_ssid = 0;
842 }
843#endif /* CONFIG_WPS */
844
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845 if (ssid->bssid_set && ssid->ssid_len == 0 &&
846 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
847 check_ssid = 0;
848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700850 (bss->ssid_len != ssid->ssid_len ||
851 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
853 continue;
854 }
855
856 if (ssid->bssid_set &&
857 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
858 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
859 continue;
860 }
861
862 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
863 continue;
864
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800865 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
867 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
868 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
869 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
870 "not allowed");
871 continue;
872 }
873
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700874 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
875 has_wep_key(ssid)) {
876 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
877 continue;
878 }
879
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800880 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
881 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
882 "not allowed");
883 continue;
884 }
885
Jouni Malinen75ecf522011-06-27 15:19:46 -0700886 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
888 "mismatch");
889 continue;
890 }
891
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800892 if (!bss_is_ess(bss)) {
893 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 continue;
895 }
896
897 if (!freq_allowed(ssid->freq_list, bss->freq)) {
898 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
899 "allowed");
900 continue;
901 }
902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 if (!rate_match(wpa_s, bss)) {
904 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
905 "not match");
906 continue;
907 }
908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -0700910 if (ssid->p2p_group &&
911 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
912 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
913 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
914 continue;
915 }
916
Dmitry Shmidt54605472013-11-08 11:10:19 -0800917 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
918 struct wpabuf *p2p_ie;
919 u8 dev_addr[ETH_ALEN];
920
921 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
922 if (ie == NULL) {
923 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
924 continue;
925 }
926 p2p_ie = wpa_bss_get_vendor_ie_multi(
927 bss, P2P_IE_VENDOR_TYPE);
928 if (p2p_ie == NULL) {
929 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
930 continue;
931 }
932
933 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
934 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
935 ETH_ALEN) != 0) {
936 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
937 wpabuf_free(p2p_ie);
938 continue;
939 }
940 wpabuf_free(p2p_ie);
941 }
942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 /*
944 * TODO: skip the AP if its P2P IE has Group Formation
945 * bit set in the P2P Group Capability Bitmap and we
946 * are not in Group Formation with that device.
947 */
948#endif /* CONFIG_P2P */
949
950 /* Matching configuration found */
951 return ssid;
952 }
953
954 /* No matching configuration found */
955 return NULL;
956}
957
958
959static struct wpa_bss *
960wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800962 struct wpa_ssid **selected_ssid,
963 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700965 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 if (only_first_ssid)
968 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
969 group->id);
970 else
971 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
972 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700974 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
975 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800976 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
977 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 if (!*selected_ssid)
979 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
981 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700982 MAC2STR(bss->bssid),
983 wpa_ssid_txt(bss->ssid, bss->ssid_len));
984 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 }
986
987 return NULL;
988}
989
990
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700991struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
992 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993{
994 struct wpa_bss *selected = NULL;
995 int prio;
996
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700997 if (wpa_s->last_scan_res == NULL ||
998 wpa_s->last_scan_res_used == 0)
999 return NULL; /* no scan results from last update */
1000
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 while (selected == NULL) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001002 if (wpa_s->next_ssid) {
1003 struct wpa_ssid *ssid;
1004
1005 /* check that next_ssid is still valid */
1006 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
1007 if (ssid == wpa_s->next_ssid)
1008 break;
1009 wpa_s->next_ssid = NULL;
1010
1011 if (ssid) {
1012 selected = wpa_supplicant_select_bss(
1013 wpa_s, ssid, selected_ssid, 1);
1014 if (selected)
1015 break;
1016 }
1017 }
1018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1020 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001021 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001022 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 if (selected)
1024 break;
1025 }
1026
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001027 if (selected == NULL && wpa_s->blacklist &&
1028 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1030 "blacklist and try again");
1031 wpa_blacklist_clear(wpa_s);
1032 wpa_s->blacklist_cleared++;
1033 } else if (selected == NULL)
1034 break;
1035 }
1036
1037 return selected;
1038}
1039
1040
1041static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1042 int timeout_sec, int timeout_usec)
1043{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 /*
1046 * No networks are enabled; short-circuit request so
1047 * we don't wait timeout seconds before transitioning
1048 * to INACTIVE state.
1049 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001050 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1051 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1053 return;
1054 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001055
1056 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1058}
1059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001060
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001061int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001062 struct wpa_bss *selected,
1063 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064{
1065 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1066 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1067 "PBC session overlap");
1068#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001069 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001070 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071#endif /* CONFIG_P2P */
1072
1073#ifdef CONFIG_WPS
1074 wpas_wps_cancel(wpa_s);
1075#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001076 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 }
1078
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001079 wpa_msg(wpa_s, MSG_DEBUG,
1080 "Considering connect request: reassociate: %d selected: "
1081 MACSTR " bssid: " MACSTR " pending: " MACSTR
1082 " wpa_state: %s ssid=%p current_ssid=%p",
1083 wpa_s->reassociate, MAC2STR(selected->bssid),
1084 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1085 wpa_supplicant_state_txt(wpa_s->wpa_state),
1086 ssid, wpa_s->current_ssid);
1087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 /*
1089 * Do not trigger new association unless the BSSID has changed or if
1090 * reassociation is requested. If we are in process of associating with
1091 * the selected BSSID, do not trigger new attempt.
1092 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001093 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1095 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1096 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001097 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1098 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1099 0) ||
1100 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1101 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1103 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001104 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001106 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1107 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_supplicant_associate(wpa_s, selected, ssid);
1109 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001110 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1111 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001112 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001114 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115}
1116
1117
1118static struct wpa_ssid *
1119wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1120{
1121 int prio;
1122 struct wpa_ssid *ssid;
1123
1124 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1125 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1126 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001127 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 continue;
1129 if (ssid->mode == IEEE80211_MODE_IBSS ||
1130 ssid->mode == IEEE80211_MODE_AP)
1131 return ssid;
1132 }
1133 }
1134 return NULL;
1135}
1136
1137
1138/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1139 * on BSS added and BSS changed events */
1140static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001141 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001143 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144
1145 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1146 return;
1147
Jouni Malinen87fd2792011-05-16 18:35:42 +03001148 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150
Jouni Malinen87fd2792011-05-16 18:35:42 +03001151 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 if (ssid == NULL)
1153 continue;
1154
Jouni Malinen87fd2792011-05-16 18:35:42 +03001155 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 if (rsn == NULL)
1157 continue;
1158
Jouni Malinen87fd2792011-05-16 18:35:42 +03001159 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 }
1161
1162}
1163
1164
1165static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1166 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001167 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001169 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 int min_diff;
1171
1172 if (wpa_s->reassociate)
1173 return 1; /* explicit request to reassociate */
1174 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1175 return 1; /* we are not associated; continue */
1176 if (wpa_s->current_ssid == NULL)
1177 return 1; /* unknown current SSID */
1178 if (wpa_s->current_ssid != ssid)
1179 return 1; /* different network block */
1180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001181 if (wpas_driver_bss_selection(wpa_s))
1182 return 0; /* Driver-based roaming */
1183
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001184 if (wpa_s->current_ssid->ssid)
1185 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1186 wpa_s->current_ssid->ssid,
1187 wpa_s->current_ssid->ssid_len);
1188 if (!current_bss)
1189 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190
1191 if (!current_bss)
1192 return 1; /* current BSS not seen in scan results */
1193
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001194 if (current_bss == selected)
1195 return 0;
1196
1197 if (selected->last_update_idx > current_bss->last_update_idx)
1198 return 1; /* current BSS not seen in the last scan */
1199
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001200#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1202 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1203 MAC2STR(current_bss->bssid), current_bss->level);
1204 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1205 MAC2STR(selected->bssid), selected->level);
1206
1207 if (wpa_s->current_ssid->bssid_set &&
1208 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1209 0) {
1210 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1211 "has preferred BSSID");
1212 return 1;
1213 }
1214
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001215 if (current_bss->level < 0 && current_bss->level > selected->level) {
1216 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1217 "signal level");
1218 return 0;
1219 }
1220
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 min_diff = 2;
1222 if (current_bss->level < 0) {
1223 if (current_bss->level < -85)
1224 min_diff = 1;
1225 else if (current_bss->level < -80)
1226 min_diff = 2;
1227 else if (current_bss->level < -75)
1228 min_diff = 3;
1229 else if (current_bss->level < -70)
1230 min_diff = 4;
1231 else
1232 min_diff = 5;
1233 }
1234 if (abs(current_bss->level - selected->level) < min_diff) {
1235 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1236 "in signal level");
1237 return 0;
1238 }
1239
1240 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001241#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001242 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001243#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244}
1245
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001246
Jouni Malinen89ca7022012-09-14 13:03:12 -07001247/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001248 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001249static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001250 union wpa_event_data *data,
1251 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001253 struct wpa_scan_results *scan_res = NULL;
1254 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001256#ifndef CONFIG_NO_RANDOM_POOL
1257 size_t i, num;
1258#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
1260#ifdef CONFIG_AP
1261 if (wpa_s->ap_iface)
1262 ap = 1;
1263#endif /* CONFIG_AP */
1264
1265 wpa_supplicant_notify_scanning(wpa_s, 0);
1266
1267 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1268 data ? &data->scan_info :
1269 NULL, 1);
1270 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001271 if (wpa_s->conf->ap_scan == 2 || ap ||
1272 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001274 if (!own_request)
1275 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1277 "scanning again");
1278 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001279 ret = -1;
1280 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 }
1282
1283#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 num = scan_res->num;
1285 if (num > 10)
1286 num = 10;
1287 for (i = 0; i < num; i++) {
1288 u8 buf[5];
1289 struct wpa_scan_res *res = scan_res->res[i];
1290 buf[0] = res->bssid[5];
1291 buf[1] = res->qual & 0xff;
1292 buf[2] = res->noise & 0xff;
1293 buf[3] = res->level & 0xff;
1294 buf[4] = res->tsf & 0xff;
1295 random_add_randomness(buf, sizeof(buf));
1296 }
1297#endif /* CONFIG_NO_RANDOM_POOL */
1298
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001299 if (own_request && wpa_s->scan_res_handler &&
1300 (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1302 struct wpa_scan_results *scan_res);
1303
1304 scan_res_handler = wpa_s->scan_res_handler;
1305 wpa_s->scan_res_handler = NULL;
1306 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001307 ret = -2;
1308 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 }
1310
1311 if (ap) {
1312 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1313#ifdef CONFIG_AP
1314 if (wpa_s->ap_iface->scan_cb)
1315 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1316#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001317 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001319
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001320 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1321 wpa_s->own_scan_running, wpa_s->external_scan_running);
1322 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1323 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1324 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1325 wpa_s->manual_scan_id);
1326 wpa_s->manual_scan_use_id = 0;
1327 } else {
1328 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1329 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001330 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
1332 wpas_notify_scan_done(wpa_s, 1);
1333
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001334 if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1335 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 -07001336 wpa_scan_results_free(scan_res);
1337 return 0;
1338 }
1339
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001340 if (sme_proc_obss_scan(wpa_s) > 0)
1341 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001343 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1344 goto scan_work_done;
1345
1346 if (autoscan_notify_scan(wpa_s, scan_res))
1347 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 if (wpa_s->disconnected) {
1350 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001351 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 }
1353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001354 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001355 bgscan_notify_scan(wpa_s, scan_res) == 1)
1356 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001358 wpas_wps_update_ap_info(wpa_s, scan_res);
1359
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001360 wpa_scan_results_free(scan_res);
1361
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001362 if (wpa_s->scan_work) {
1363 struct wpa_radio_work *work = wpa_s->scan_work;
1364 wpa_s->scan_work = NULL;
1365 radio_work_done(work);
1366 }
1367
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001368 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001369
1370scan_work_done:
1371 wpa_scan_results_free(scan_res);
1372 if (wpa_s->scan_work) {
1373 struct wpa_radio_work *work = wpa_s->scan_work;
1374 wpa_s->scan_work = NULL;
1375 radio_work_done(work);
1376 }
1377 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001378}
1379
1380
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001381static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001382 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001383{
1384 struct wpa_bss *selected;
1385 struct wpa_ssid *ssid = NULL;
1386
1387 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388
1389 if (selected) {
1390 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001391 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001392 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001393 if (new_scan)
1394 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001396 }
1397
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001398 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001400 return -1;
1401 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001402 if (new_scan)
1403 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001404 /*
1405 * Do not notify other virtual radios of scan results since we do not
1406 * want them to start other associations at the same time.
1407 */
1408 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1411 ssid = wpa_supplicant_pick_new_network(wpa_s);
1412 if (ssid) {
1413 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1414 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001415 if (new_scan)
1416 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001417 } else if (own_request) {
1418 /*
1419 * No SSID found. If SCAN results are as a result of
1420 * own scan request and not due to a scan request on
1421 * another shared interface, try another scan.
1422 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 int timeout_sec = wpa_s->scan_interval;
1424 int timeout_usec = 0;
1425#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001426 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1427 return 0;
1428
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001429 if (wpa_s->p2p_in_provisioning ||
1430 wpa_s->show_group_started) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 /*
1432 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001433 * state and during P2P join-a-group operation
1434 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 */
1436 timeout_sec = 0;
1437 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001438 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1439 timeout_usec);
1440 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 }
1442#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001443#ifdef CONFIG_INTERWORKING
1444 if (wpa_s->conf->auto_interworking &&
1445 wpa_s->conf->interworking &&
1446 wpa_s->conf->cred) {
1447 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1448 "start ANQP fetch since no matching "
1449 "networks found");
1450 wpa_s->network_select = 1;
1451 wpa_s->auto_network_select = 1;
1452 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001453 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001454 }
1455#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001456#ifdef CONFIG_WPS
1457 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1458 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1459 timeout_sec = 0;
1460 timeout_usec = 500000;
1461 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1462 timeout_usec);
1463 return 0;
1464 }
1465#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001466 if (wpa_supplicant_req_sched_scan(wpa_s))
1467 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1468 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469 }
1470 }
1471 return 0;
1472}
1473
1474
1475static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1476 union wpa_event_data *data)
1477{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001479
1480 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 /*
1482 * If no scan results could be fetched, then no need to
1483 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001484 * this scan. Similarly, if scan results started a new operation on this
1485 * interface, do not notify other interfaces to avoid concurrent
1486 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 */
1488 return;
1489 }
1490
1491 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001492 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493 * so, they get updated with this same scan info.
1494 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001495 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1496 radio_list) {
1497 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1499 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001500 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 }
1502 }
1503}
1504
1505#endif /* CONFIG_NO_SCAN_PROCESSING */
1506
1507
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001508int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1509{
1510#ifdef CONFIG_NO_SCAN_PROCESSING
1511 return -1;
1512#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001513 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001514
1515 if (wpa_s->last_scan_res_used <= 0)
1516 return -1;
1517
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001518 os_get_reltime(&now);
1519 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001520 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1521 return -1;
1522 }
1523
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001524 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001525#endif /* CONFIG_NO_SCAN_PROCESSING */
1526}
1527
Dmitry Shmidt04949592012-07-19 12:16:46 -07001528#ifdef CONFIG_WNM
1529
1530static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1531{
1532 struct wpa_supplicant *wpa_s = eloop_ctx;
1533
1534 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1535 return;
1536
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001537 if (!wpa_s->no_keep_alive) {
1538 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1539 MAC2STR(wpa_s->bssid));
1540 /* TODO: could skip this if normal data traffic has been sent */
1541 /* TODO: Consider using some more appropriate data frame for
1542 * this */
1543 if (wpa_s->l2)
1544 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1545 (u8 *) "", 0);
1546 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001547
1548#ifdef CONFIG_SME
1549 if (wpa_s->sme.bss_max_idle_period) {
1550 unsigned int msec;
1551 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1552 if (msec > 100)
1553 msec -= 100;
1554 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1555 wnm_bss_keep_alive, wpa_s, NULL);
1556 }
1557#endif /* CONFIG_SME */
1558}
1559
1560
1561static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1562 const u8 *ies, size_t ies_len)
1563{
1564 struct ieee802_11_elems elems;
1565
1566 if (ies == NULL)
1567 return;
1568
1569 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1570 return;
1571
1572#ifdef CONFIG_SME
1573 if (elems.bss_max_idle_period) {
1574 unsigned int msec;
1575 wpa_s->sme.bss_max_idle_period =
1576 WPA_GET_LE16(elems.bss_max_idle_period);
1577 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1578 "TU)%s", wpa_s->sme.bss_max_idle_period,
1579 (elems.bss_max_idle_period[2] & 0x01) ?
1580 " (protected keep-live required)" : "");
1581 if (wpa_s->sme.bss_max_idle_period == 0)
1582 wpa_s->sme.bss_max_idle_period = 1;
1583 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1584 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1585 /* msec times 1000 */
1586 msec = wpa_s->sme.bss_max_idle_period * 1024;
1587 if (msec > 100)
1588 msec -= 100;
1589 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1590 wnm_bss_keep_alive, wpa_s,
1591 NULL);
1592 }
1593 }
1594#endif /* CONFIG_SME */
1595}
1596
1597#endif /* CONFIG_WNM */
1598
1599
1600void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1601{
1602#ifdef CONFIG_WNM
1603 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1604#endif /* CONFIG_WNM */
1605}
1606
1607
Dmitry Shmidt051af732013-10-22 13:52:46 -07001608#ifdef CONFIG_INTERWORKING
1609
1610static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1611 size_t len)
1612{
1613 int res;
1614
1615 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1616 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1617 if (res) {
1618 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1619 }
1620
1621 return res;
1622}
1623
1624
1625static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1626 const u8 *ies, size_t ies_len)
1627{
1628 struct ieee802_11_elems elems;
1629
1630 if (ies == NULL)
1631 return;
1632
1633 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1634 return;
1635
1636 if (elems.qos_map_set) {
1637 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1638 elems.qos_map_set_len);
1639 }
1640}
1641
1642#endif /* CONFIG_INTERWORKING */
1643
1644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1646 union wpa_event_data *data)
1647{
1648 int l, len, found = 0, wpa_found, rsn_found;
1649 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001650#ifdef CONFIG_IEEE80211R
1651 u8 bssid[ETH_ALEN];
1652#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653
1654 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1655 if (data->assoc_info.req_ies)
1656 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1657 data->assoc_info.req_ies_len);
1658 if (data->assoc_info.resp_ies) {
1659 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1660 data->assoc_info.resp_ies_len);
1661#ifdef CONFIG_TDLS
1662 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1663 data->assoc_info.resp_ies_len);
1664#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001665#ifdef CONFIG_WNM
1666 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1667 data->assoc_info.resp_ies_len);
1668#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001669#ifdef CONFIG_INTERWORKING
1670 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1671 data->assoc_info.resp_ies_len);
1672#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001673 }
1674 if (data->assoc_info.beacon_ies)
1675 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1676 data->assoc_info.beacon_ies,
1677 data->assoc_info.beacon_ies_len);
1678 if (data->assoc_info.freq)
1679 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1680 data->assoc_info.freq);
1681
1682 p = data->assoc_info.req_ies;
1683 l = data->assoc_info.req_ies_len;
1684
1685 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1686 while (p && l >= 2) {
1687 len = p[1] + 2;
1688 if (len > l) {
1689 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1690 p, l);
1691 break;
1692 }
1693 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1694 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1695 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1696 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1697 break;
1698 found = 1;
1699 wpa_find_assoc_pmkid(wpa_s);
1700 break;
1701 }
1702 l -= len;
1703 p += len;
1704 }
1705 if (!found && data->assoc_info.req_ies)
1706 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1707
1708#ifdef CONFIG_IEEE80211R
1709#ifdef CONFIG_SME
1710 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1712 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1713 data->assoc_info.resp_ies,
1714 data->assoc_info.resp_ies_len,
1715 bssid) < 0) {
1716 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1717 "Reassociation Response failed");
1718 wpa_supplicant_deauthenticate(
1719 wpa_s, WLAN_REASON_INVALID_IE);
1720 return -1;
1721 }
1722 }
1723
1724 p = data->assoc_info.resp_ies;
1725 l = data->assoc_info.resp_ies_len;
1726
1727#ifdef CONFIG_WPS_STRICT
1728 if (p && wpa_s->current_ssid &&
1729 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1730 struct wpabuf *wps;
1731 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1732 if (wps == NULL) {
1733 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1734 "include WPS IE in (Re)Association Response");
1735 return -1;
1736 }
1737
1738 if (wps_validate_assoc_resp(wps) < 0) {
1739 wpabuf_free(wps);
1740 wpa_supplicant_deauthenticate(
1741 wpa_s, WLAN_REASON_INVALID_IE);
1742 return -1;
1743 }
1744 wpabuf_free(wps);
1745 }
1746#endif /* CONFIG_WPS_STRICT */
1747
1748 /* Go through the IEs and make a copy of the MDIE, if present. */
1749 while (p && l >= 2) {
1750 len = p[1] + 2;
1751 if (len > l) {
1752 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1753 p, l);
1754 break;
1755 }
1756 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1757 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1758 wpa_s->sme.ft_used = 1;
1759 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1760 MOBILITY_DOMAIN_ID_LEN);
1761 break;
1762 }
1763 l -= len;
1764 p += len;
1765 }
1766#endif /* CONFIG_SME */
1767
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001768 /* Process FT when SME is in the driver */
1769 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1770 wpa_ft_is_completed(wpa_s->wpa)) {
1771 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1772 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1773 data->assoc_info.resp_ies,
1774 data->assoc_info.resp_ies_len,
1775 bssid) < 0) {
1776 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1777 "Reassociation Response failed");
1778 wpa_supplicant_deauthenticate(
1779 wpa_s, WLAN_REASON_INVALID_IE);
1780 return -1;
1781 }
1782 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1783 }
1784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1786 data->assoc_info.resp_ies_len);
1787#endif /* CONFIG_IEEE80211R */
1788
1789 /* WPA/RSN IE from Beacon/ProbeResp */
1790 p = data->assoc_info.beacon_ies;
1791 l = data->assoc_info.beacon_ies_len;
1792
1793 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1794 */
1795 wpa_found = rsn_found = 0;
1796 while (p && l >= 2) {
1797 len = p[1] + 2;
1798 if (len > l) {
1799 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1800 p, l);
1801 break;
1802 }
1803 if (!wpa_found &&
1804 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1805 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1806 wpa_found = 1;
1807 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1808 }
1809
1810 if (!rsn_found &&
1811 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1812 rsn_found = 1;
1813 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1814 }
1815
1816 l -= len;
1817 p += len;
1818 }
1819
1820 if (!wpa_found && data->assoc_info.beacon_ies)
1821 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1822 if (!rsn_found && data->assoc_info.beacon_ies)
1823 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1824 if (wpa_found || rsn_found)
1825 wpa_s->ap_ies_from_associnfo = 1;
1826
Jouni Malinen87fd2792011-05-16 18:35:42 +03001827 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1828 wpa_s->assoc_freq != data->assoc_info.freq) {
1829 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1830 "%u to %u MHz",
1831 wpa_s->assoc_freq, data->assoc_info.freq);
1832 wpa_supplicant_update_scan_results(wpa_s);
1833 }
1834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 wpa_s->assoc_freq = data->assoc_info.freq;
1836
1837 return 0;
1838}
1839
1840
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001841static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1842{
1843 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1844
1845 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1846 return -1;
1847
1848 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1849 return 0;
1850
1851 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1852 WPA_IE_VENDOR_TYPE);
1853 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1854
1855 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1856 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1857 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1858 bss_rsn ? 2 + bss_rsn[1] : 0))
1859 return -1;
1860
1861 return 0;
1862}
1863
1864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1866 union wpa_event_data *data)
1867{
1868 u8 bssid[ETH_ALEN];
1869 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870
1871#ifdef CONFIG_AP
1872 if (wpa_s->ap_iface) {
1873 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1874 data->assoc_info.addr,
1875 data->assoc_info.req_ies,
1876 data->assoc_info.req_ies_len,
1877 data->assoc_info.reassoc);
1878 return;
1879 }
1880#endif /* CONFIG_AP */
1881
1882 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1883 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1884 return;
1885
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001886 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1887 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001888 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001889 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1890 return;
1891 }
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001894 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1896 MACSTR, MAC2STR(bssid));
1897 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1899 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001900 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901
1902 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1903 wpa_clear_keys(wpa_s, bssid);
1904 }
1905 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001906 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1908 return;
1909 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001910
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001911#ifdef ANDROID
1912 if (wpa_s->conf->ap_scan == 1) {
1913#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001914 if (wpa_s->conf->ap_scan == 1 &&
1915 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001916#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001917 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1918 wpa_msg(wpa_s, MSG_WARNING,
1919 "WPA/RSN IEs not updated");
1920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 }
1922
1923#ifdef CONFIG_SME
1924 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1925 wpa_s->sme.prev_bssid_set = 1;
1926#endif /* CONFIG_SME */
1927
1928 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1929 if (wpa_s->current_ssid) {
1930 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1931 * initialized before association, but for other modes,
1932 * initialize PC/SC here, if the current configuration needs
1933 * smartcard or SIM/USIM. */
1934 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1935 }
1936 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1937 if (wpa_s->l2)
1938 l2_packet_notify_auth_start(wpa_s->l2);
1939
1940 /*
1941 * Set portEnabled first to FALSE in order to get EAP state machine out
1942 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1943 * state machine may transit to AUTHENTICATING state based on obsolete
1944 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1945 * AUTHENTICATED without ever giving chance to EAP state machine to
1946 * reset the state.
1947 */
1948 if (!ft_completed) {
1949 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1950 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1951 }
1952 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1953 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1954 /* 802.1X::portControl = Auto */
1955 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1956 wpa_s->eapol_received = 0;
1957 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1958 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1959 (wpa_s->current_ssid &&
1960 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001961 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1962 (wpa_s->drv_flags &
1963 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1964 /*
1965 * Set the key after having received joined-IBSS event
1966 * from the driver.
1967 */
1968 wpa_supplicant_set_wpa_none_key(wpa_s,
1969 wpa_s->current_ssid);
1970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 wpa_supplicant_cancel_auth_timeout(wpa_s);
1972 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1973 } else if (!ft_completed) {
1974 /* Timeout for receiving the first EAPOL packet */
1975 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1976 }
1977 wpa_supplicant_cancel_scan(wpa_s);
1978
1979 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1980 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1981 /*
1982 * We are done; the driver will take care of RSN 4-way
1983 * handshake.
1984 */
1985 wpa_supplicant_cancel_auth_timeout(wpa_s);
1986 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1987 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1988 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1989 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1990 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1991 /*
1992 * The driver will take care of RSN 4-way handshake, so we need
1993 * to allow EAPOL supplicant to complete its work without
1994 * waiting for WPA supplicant.
1995 */
1996 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1997 } else if (ft_completed) {
1998 /*
1999 * FT protocol completed - make sure EAPOL state machine ends
2000 * up in authenticated.
2001 */
2002 wpa_supplicant_cancel_auth_timeout(wpa_s);
2003 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2004 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2005 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2006 }
2007
Jouni Malinena05074c2012-12-21 21:35:35 +02002008 wpa_s->last_eapol_matches_bssid = 0;
2009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002011 struct os_reltime now, age;
2012 os_get_reltime(&now);
2013 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 if (age.sec == 0 && age.usec < 100000 &&
2015 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2016 0) {
2017 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2018 "frame that was received just before "
2019 "association notification");
2020 wpa_supplicant_rx_eapol(
2021 wpa_s, wpa_s->pending_eapol_rx_src,
2022 wpabuf_head(wpa_s->pending_eapol_rx),
2023 wpabuf_len(wpa_s->pending_eapol_rx));
2024 }
2025 wpabuf_free(wpa_s->pending_eapol_rx);
2026 wpa_s->pending_eapol_rx = NULL;
2027 }
2028
2029 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2030 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002031 wpa_s->current_ssid &&
2032 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 /* Set static WEP keys again */
2034 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2035 }
2036
2037#ifdef CONFIG_IBSS_RSN
2038 if (wpa_s->current_ssid &&
2039 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2040 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2041 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2042 wpa_s->ibss_rsn == NULL) {
2043 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2044 if (!wpa_s->ibss_rsn) {
2045 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2046 wpa_supplicant_deauthenticate(
2047 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2048 return;
2049 }
2050
2051 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2052 }
2053#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002054
2055 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056}
2057
2058
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002059static int disconnect_reason_recoverable(u16 reason_code)
2060{
2061 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2062 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2063 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2064}
2065
2066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002068 u16 reason_code,
2069 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070{
2071 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002072
2073 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2074 /*
2075 * At least Host AP driver and a Prism3 card seemed to be
2076 * generating streams of disconnected events when configuring
2077 * IBSS for WPA-None. Ignore them for now.
2078 */
2079 return;
2080 }
2081
2082 bssid = wpa_s->bssid;
2083 if (is_zero_ether_addr(bssid))
2084 bssid = wpa_s->pending_bssid;
2085
2086 if (!is_zero_ether_addr(bssid) ||
2087 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2088 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2089 " reason=%d%s",
2090 MAC2STR(bssid), reason_code,
2091 locally_generated ? " locally_generated=1" : "");
2092 }
2093}
2094
2095
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002096static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2097 int locally_generated)
2098{
2099 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2100 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2101 return 0; /* Not in 4-way handshake with PSK */
2102
2103 /*
2104 * It looks like connection was lost while trying to go through PSK
2105 * 4-way handshake. Filter out known disconnection cases that are caused
2106 * by something else than PSK mismatch to avoid confusing reports.
2107 */
2108
2109 if (locally_generated) {
2110 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2111 return 0;
2112 }
2113
2114 return 1;
2115}
2116
2117
Jouni Malinen2b89da82012-08-31 22:04:41 +03002118static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2119 u16 reason_code,
2120 int locally_generated)
2121{
2122 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 int authenticating;
2124 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002125 struct wpa_bss *fast_reconnect = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002126#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002127 struct wpa_ssid *fast_reconnect_ssid = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002128#endif /* CONFIG_NO_SCAN_PROCESSING */
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002129 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130
2131 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2132 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2133
2134 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2135 /*
2136 * At least Host AP driver and a Prism3 card seemed to be
2137 * generating streams of disconnected events when configuring
2138 * IBSS for WPA-None. Ignore them for now.
2139 */
2140 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2141 "IBSS/WPA-None mode");
2142 return;
2143 }
2144
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002145 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2147 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002148 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2149 return; /* P2P group removed */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002150 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002152 if (!wpa_s->disconnected &&
2153 (!wpa_s->auto_reconnect_disabled ||
2154 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002155 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2156 "reconnect (wps=%d wpa_state=%d)",
2157 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2158 wpa_s->wpa_state);
2159 if (wpa_s->wpa_state == WPA_COMPLETED &&
2160 wpa_s->current_ssid &&
2161 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002162 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002163 disconnect_reason_recoverable(reason_code)) {
2164 /*
2165 * It looks like the AP has dropped association with
2166 * us, but could allow us to get back in. Try to
2167 * reconnect to the same BSS without full scan to save
2168 * time for some common cases.
2169 */
2170 fast_reconnect = wpa_s->current_bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002171#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002172 fast_reconnect_ssid = wpa_s->current_ssid;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002173#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002174 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002175 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002176 else
2177 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2178 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002180 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 "try to re-connect");
2182 wpa_s->reassociate = 0;
2183 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002184 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 }
2186 bssid = wpa_s->bssid;
2187 if (is_zero_ether_addr(bssid))
2188 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002189 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2190 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002192 if (locally_generated)
2193 wpa_s->disconnect_reason = -reason_code;
2194 else
2195 wpa_s->disconnect_reason = reason_code;
2196 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2198 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 wpa_clear_keys(wpa_s, wpa_s->bssid);
2200 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002201 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 wpa_supplicant_mark_disassoc(wpa_s);
2203
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002204 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002206 wpa_s->current_ssid = last_ssid;
2207 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002209 if (fast_reconnect &&
2210 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2211 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2212 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2213 fast_reconnect->ssid_len) &&
2214 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002215#ifndef CONFIG_NO_SCAN_PROCESSING
2216 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2217 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2218 fast_reconnect_ssid) < 0) {
2219 /* Recover through full scan */
2220 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2221 }
2222#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002223 } else if (fast_reconnect) {
2224 /*
2225 * Could not reconnect to the same BSS due to network being
2226 * disabled. Use a new scan to match the alternative behavior
2227 * above, i.e., to continue automatic reconnection attempt in a
2228 * way that enforces disabled network rules.
2229 */
2230 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232}
2233
2234
2235#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237{
2238 struct wpa_supplicant *wpa_s = eloop_ctx;
2239
2240 if (!wpa_s->pending_mic_error_report)
2241 return;
2242
2243 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2244 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2245 wpa_s->pending_mic_error_report = 0;
2246}
2247#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2248
2249
2250static void
2251wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2252 union wpa_event_data *data)
2253{
2254 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002255 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256
2257 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2258 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002259 os_get_reltime(&t);
2260 if ((wpa_s->last_michael_mic_error.sec &&
2261 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 wpa_s->pending_mic_error_report) {
2263 if (wpa_s->pending_mic_error_report) {
2264 /*
2265 * Send the pending MIC error report immediately since
2266 * we are going to start countermeasures and AP better
2267 * do the same.
2268 */
2269 wpa_sm_key_request(wpa_s->wpa, 1,
2270 wpa_s->pending_mic_error_pairwise);
2271 }
2272
2273 /* Send the new MIC error report immediately since we are going
2274 * to start countermeasures and AP better do the same.
2275 */
2276 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2277
2278 /* initialize countermeasures */
2279 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002280
2281 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2284
2285 /*
2286 * Need to wait for completion of request frame. We do not get
2287 * any callback for the message completion, so just wait a
2288 * short while and hope for the best. */
2289 os_sleep(0, 10000);
2290
2291 wpa_drv_set_countermeasures(wpa_s, 1);
2292 wpa_supplicant_deauthenticate(wpa_s,
2293 WLAN_REASON_MICHAEL_MIC_FAILURE);
2294 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2295 wpa_s, NULL);
2296 eloop_register_timeout(60, 0,
2297 wpa_supplicant_stop_countermeasures,
2298 wpa_s, NULL);
2299 /* TODO: mark the AP rejected for 60 second. STA is
2300 * allowed to associate with another AP.. */
2301 } else {
2302#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2303 if (wpa_s->mic_errors_seen) {
2304 /*
2305 * Reduce the effectiveness of Michael MIC error
2306 * reports as a means for attacking against TKIP if
2307 * more than one MIC failure is noticed with the same
2308 * PTK. We delay the transmission of the reports by a
2309 * random time between 0 and 60 seconds in order to
2310 * force the attacker wait 60 seconds before getting
2311 * the information on whether a frame resulted in a MIC
2312 * failure.
2313 */
2314 u8 rval[4];
2315 int sec;
2316
2317 if (os_get_random(rval, sizeof(rval)) < 0)
2318 sec = os_random() % 60;
2319 else
2320 sec = WPA_GET_BE32(rval) % 60;
2321 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2322 "report %d seconds", sec);
2323 wpa_s->pending_mic_error_report = 1;
2324 wpa_s->pending_mic_error_pairwise = pairwise;
2325 eloop_cancel_timeout(
2326 wpa_supplicant_delayed_mic_error_report,
2327 wpa_s, NULL);
2328 eloop_register_timeout(
2329 sec, os_random() % 1000000,
2330 wpa_supplicant_delayed_mic_error_report,
2331 wpa_s, NULL);
2332 } else {
2333 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2334 }
2335#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2336 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2337#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2338 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002339 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 wpa_s->mic_errors_seen++;
2341}
2342
2343
2344#ifdef CONFIG_TERMINATE_ONLASTIF
2345static int any_interfaces(struct wpa_supplicant *head)
2346{
2347 struct wpa_supplicant *wpa_s;
2348
2349 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2350 if (!wpa_s->interface_removed)
2351 return 1;
2352 return 0;
2353}
2354#endif /* CONFIG_TERMINATE_ONLASTIF */
2355
2356
2357static void
2358wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2359 union wpa_event_data *data)
2360{
2361 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2362 return;
2363
2364 switch (data->interface_status.ievent) {
2365 case EVENT_INTERFACE_ADDED:
2366 if (!wpa_s->interface_removed)
2367 break;
2368 wpa_s->interface_removed = 0;
2369 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2370 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2371 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2372 "driver after interface was added");
2373 }
2374 break;
2375 case EVENT_INTERFACE_REMOVED:
2376 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2377 wpa_s->interface_removed = 1;
2378 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002379 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 l2_packet_deinit(wpa_s->l2);
2381 wpa_s->l2 = NULL;
2382#ifdef CONFIG_IBSS_RSN
2383 ibss_rsn_deinit(wpa_s->ibss_rsn);
2384 wpa_s->ibss_rsn = NULL;
2385#endif /* CONFIG_IBSS_RSN */
2386#ifdef CONFIG_TERMINATE_ONLASTIF
2387 /* check if last interface */
2388 if (!any_interfaces(wpa_s->global->ifaces))
2389 eloop_terminate();
2390#endif /* CONFIG_TERMINATE_ONLASTIF */
2391 break;
2392 }
2393}
2394
2395
2396#ifdef CONFIG_PEERKEY
2397static void
2398wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2399 union wpa_event_data *data)
2400{
2401 if (data == NULL)
2402 return;
2403 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2404}
2405#endif /* CONFIG_PEERKEY */
2406
2407
2408#ifdef CONFIG_TDLS
2409static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2410 union wpa_event_data *data)
2411{
2412 if (data == NULL)
2413 return;
2414 switch (data->tdls.oper) {
2415 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002416 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2417 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2418 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2419 else
2420 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 break;
2422 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002423 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2424 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2425 data->tdls.reason_code);
2426 else
2427 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2428 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 break;
2430 }
2431}
2432#endif /* CONFIG_TDLS */
2433
2434
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002435#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002436static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2437 union wpa_event_data *data)
2438{
2439 if (data == NULL)
2440 return;
2441 switch (data->wnm.oper) {
2442 case WNM_OPER_SLEEP:
2443 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2444 "(action=%d, intval=%d)",
2445 data->wnm.sleep_action, data->wnm.sleep_intval);
2446 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002447 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002448 break;
2449 }
2450}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002451#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002452
2453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002454#ifdef CONFIG_IEEE80211R
2455static void
2456wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2457 union wpa_event_data *data)
2458{
2459 if (data == NULL)
2460 return;
2461
2462 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2463 data->ft_ies.ies_len,
2464 data->ft_ies.ft_action,
2465 data->ft_ies.target_ap,
2466 data->ft_ies.ric_ies,
2467 data->ft_ies.ric_ies_len) < 0) {
2468 /* TODO: prevent MLME/driver from trying to associate? */
2469 }
2470}
2471#endif /* CONFIG_IEEE80211R */
2472
2473
2474#ifdef CONFIG_IBSS_RSN
2475static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2476 union wpa_event_data *data)
2477{
2478 struct wpa_ssid *ssid;
2479 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2480 return;
2481 if (data == NULL)
2482 return;
2483 ssid = wpa_s->current_ssid;
2484 if (ssid == NULL)
2485 return;
2486 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2487 return;
2488
2489 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2490}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002491
2492
2493static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2494 union wpa_event_data *data)
2495{
2496 struct wpa_ssid *ssid = wpa_s->current_ssid;
2497
2498 if (ssid == NULL)
2499 return;
2500
2501 /* check if the ssid is correctly configured as IBSS/RSN */
2502 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2503 return;
2504
2505 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2506 data->rx_mgmt.frame_len);
2507}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508#endif /* CONFIG_IBSS_RSN */
2509
2510
2511#ifdef CONFIG_IEEE80211R
2512static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2513 size_t len)
2514{
2515 const u8 *sta_addr, *target_ap_addr;
2516 u16 status;
2517
2518 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2519 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2520 return; /* only SME case supported for now */
2521 if (len < 1 + 2 * ETH_ALEN + 2)
2522 return;
2523 if (data[0] != 2)
2524 return; /* Only FT Action Response is supported for now */
2525 sta_addr = data + 1;
2526 target_ap_addr = data + 1 + ETH_ALEN;
2527 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2528 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2529 MACSTR " TargetAP " MACSTR " status %u",
2530 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2531
2532 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2533 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2534 " in FT Action Response", MAC2STR(sta_addr));
2535 return;
2536 }
2537
2538 if (status) {
2539 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2540 "failure (status code %d)", status);
2541 /* TODO: report error to FT code(?) */
2542 return;
2543 }
2544
2545 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2546 len - (1 + 2 * ETH_ALEN + 2), 1,
2547 target_ap_addr, NULL, 0) < 0)
2548 return;
2549
2550#ifdef CONFIG_SME
2551 {
2552 struct wpa_bss *bss;
2553 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2554 if (bss)
2555 wpa_s->sme.freq = bss->freq;
2556 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2557 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2558 WLAN_AUTH_FT);
2559 }
2560#endif /* CONFIG_SME */
2561}
2562#endif /* CONFIG_IEEE80211R */
2563
2564
2565static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2566 struct unprot_deauth *e)
2567{
2568#ifdef CONFIG_IEEE80211W
2569 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2570 "dropped: " MACSTR " -> " MACSTR
2571 " (reason code %u)",
2572 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2573 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2574#endif /* CONFIG_IEEE80211W */
2575}
2576
2577
2578static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2579 struct unprot_disassoc *e)
2580{
2581#ifdef CONFIG_IEEE80211W
2582 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2583 "dropped: " MACSTR " -> " MACSTR
2584 " (reason code %u)",
2585 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2586 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2587#endif /* CONFIG_IEEE80211W */
2588}
2589
2590
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002591static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2592 u16 reason_code, int locally_generated,
2593 const u8 *ie, size_t ie_len, int deauth)
2594{
2595#ifdef CONFIG_AP
2596 if (wpa_s->ap_iface && addr) {
2597 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2598 return;
2599 }
2600
2601 if (wpa_s->ap_iface) {
2602 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2603 return;
2604 }
2605#endif /* CONFIG_AP */
2606
2607 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2608
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002609 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2610 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2611 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2612 eapol_sm_failed(wpa_s->eapol))) &&
2613 !wpa_s->eap_expected_failure))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002614 wpas_auth_failed(wpa_s);
2615
2616#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002617 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002618 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2619 locally_generated) > 0) {
2620 /*
2621 * The interface was removed, so cannot continue
2622 * processing any additional operations after this.
2623 */
2624 return;
2625 }
2626 }
2627#endif /* CONFIG_P2P */
2628
2629 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2630 locally_generated);
2631}
2632
2633
2634static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2635 struct disassoc_info *info)
2636{
2637 u16 reason_code = 0;
2638 int locally_generated = 0;
2639 const u8 *addr = NULL;
2640 const u8 *ie = NULL;
2641 size_t ie_len = 0;
2642
2643 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2644
2645 if (info) {
2646 addr = info->addr;
2647 ie = info->ie;
2648 ie_len = info->ie_len;
2649 reason_code = info->reason_code;
2650 locally_generated = info->locally_generated;
2651 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2652 locally_generated ? " (locally generated)" : "");
2653 if (addr)
2654 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2655 MAC2STR(addr));
2656 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2657 ie, ie_len);
2658 }
2659
2660#ifdef CONFIG_AP
2661 if (wpa_s->ap_iface && info && info->addr) {
2662 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2663 return;
2664 }
2665
2666 if (wpa_s->ap_iface) {
2667 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2668 return;
2669 }
2670#endif /* CONFIG_AP */
2671
2672#ifdef CONFIG_P2P
2673 if (info) {
2674 wpas_p2p_disassoc_notif(
2675 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2676 locally_generated);
2677 }
2678#endif /* CONFIG_P2P */
2679
2680 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2681 sme_event_disassoc(wpa_s, info);
2682
2683 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2684 ie, ie_len, 0);
2685}
2686
2687
2688static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2689 struct deauth_info *info)
2690{
2691 u16 reason_code = 0;
2692 int locally_generated = 0;
2693 const u8 *addr = NULL;
2694 const u8 *ie = NULL;
2695 size_t ie_len = 0;
2696
2697 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2698
2699 if (info) {
2700 addr = info->addr;
2701 ie = info->ie;
2702 ie_len = info->ie_len;
2703 reason_code = info->reason_code;
2704 locally_generated = info->locally_generated;
2705 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2706 reason_code,
2707 locally_generated ? " (locally generated)" : "");
2708 if (addr) {
2709 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2710 MAC2STR(addr));
2711 }
2712 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2713 ie, ie_len);
2714 }
2715
2716 wpa_reset_ft_completed(wpa_s->wpa);
2717
2718 wpas_event_disconnect(wpa_s, addr, reason_code,
2719 locally_generated, ie, ie_len, 1);
2720}
2721
2722
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002723static void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s)
2724{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002725 struct wpa_supplicant *ifs;
2726
2727 if (wpa_s->drv_priv == NULL)
2728 return; /* Ignore event during drv initialization */
2729
2730 free_hw_features(wpa_s);
2731 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2732 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2733
2734#ifdef CONFIG_P2P
2735 wpas_p2p_update_channel_list(wpa_s);
2736#endif /* CONFIG_P2P */
2737
2738 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002739 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002740 * so, they get updated with this same hw mode info.
2741 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002742 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2743 radio_list) {
2744 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002745 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2746 ifs->ifname);
2747 free_hw_features(ifs);
2748 ifs->hw.modes = wpa_drv_get_hw_feature_data(
2749 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2750 }
2751 }
2752}
2753
2754
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002755static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2756 const struct ieee80211_mgmt *mgmt,
2757 size_t len, int freq)
2758{
2759 const u8 *payload;
2760 size_t plen;
2761 u8 category;
2762
2763 if (len < IEEE80211_HDRLEN + 2)
2764 return;
2765
2766 payload = &mgmt->u.action.category;
2767 category = *payload++;
2768 plen = (((const u8 *) mgmt) + len) - payload;
2769
2770 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2771 " Category=%u DataLen=%d freq=%d MHz",
2772 MAC2STR(mgmt->sa), category, (int) plen, freq);
2773
2774#ifdef CONFIG_IEEE80211R
2775 if (category == WLAN_ACTION_FT) {
2776 ft_rx_action(wpa_s, payload, plen);
2777 return;
2778 }
2779#endif /* CONFIG_IEEE80211R */
2780
2781#ifdef CONFIG_IEEE80211W
2782#ifdef CONFIG_SME
2783 if (category == WLAN_ACTION_SA_QUERY) {
2784 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2785 return;
2786 }
2787#endif /* CONFIG_SME */
2788#endif /* CONFIG_IEEE80211W */
2789
2790#ifdef CONFIG_WNM
2791 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2792 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2793 return;
2794 }
2795#endif /* CONFIG_WNM */
2796
2797#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08002798 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2799 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002800 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08002801 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002802 payload, plen, freq) == 0)
2803 return;
2804#endif /* CONFIG_GAS */
2805
2806#ifdef CONFIG_TDLS
2807 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2808 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2809 wpa_dbg(wpa_s, MSG_DEBUG,
2810 "TDLS: Received Discovery Response from " MACSTR,
2811 MAC2STR(mgmt->sa));
2812 return;
2813 }
2814#endif /* CONFIG_TDLS */
2815
2816#ifdef CONFIG_INTERWORKING
2817 if (category == WLAN_ACTION_QOS && plen >= 1 &&
2818 payload[0] == QOS_QOS_MAP_CONFIG) {
2819 const u8 *pos = payload + 1;
2820 size_t qlen = plen - 1;
2821 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2822 MACSTR, MAC2STR(mgmt->sa));
2823 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2824 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2825 pos[1] <= qlen - 2 && pos[1] >= 16)
2826 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2827 return;
2828 }
2829#endif /* CONFIG_INTERWORKING */
2830
2831#ifdef CONFIG_P2P
2832 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2833 category, payload, plen, freq);
2834#endif /* CONFIG_P2P */
2835}
2836
2837
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002838static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2839 union wpa_event_data *event)
2840{
2841#ifdef CONFIG_P2P
2842 struct wpa_supplicant *ifs;
2843#endif /* CONFIG_P2P */
2844 struct wpa_freq_range_list *list;
2845 char *str = NULL;
2846
2847 list = &event->freq_range;
2848
2849 if (list->num)
2850 str = freq_range_list_str(list);
2851 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2852 str ? str : "");
2853
2854#ifdef CONFIG_P2P
2855 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2856 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2857 __func__);
2858 } else {
2859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2860 wpas_p2p_update_channel_list(wpa_s);
2861 }
2862
2863 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2864 int freq;
2865 if (!ifs->current_ssid ||
2866 !ifs->current_ssid->p2p_group ||
2867 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2868 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2869 continue;
2870
2871 freq = ifs->current_ssid->frequency;
2872 if (!freq_range_list_includes(list, freq)) {
2873 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2874 freq);
2875 continue;
2876 }
2877
2878 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2879 freq);
2880 /* TODO: Consider using CSA or removing the group within
2881 * wpa_supplicant */
2882 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2883 }
2884#endif /* CONFIG_P2P */
2885
2886 os_free(str);
2887}
2888
2889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2891 union wpa_event_data *data)
2892{
2893 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894
2895 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2896 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002897 event != EVENT_INTERFACE_STATUS &&
2898 event != EVENT_SCHED_SCAN_STOPPED) {
2899 wpa_dbg(wpa_s, MSG_DEBUG,
2900 "Ignore event %s (%d) while interface is disabled",
2901 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 return;
2903 }
2904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002905#ifndef CONFIG_NO_STDOUT_DEBUG
2906{
2907 int level = MSG_DEBUG;
2908
Dmitry Shmidt04949592012-07-19 12:16:46 -07002909 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 const struct ieee80211_hdr *hdr;
2911 u16 fc;
2912 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2913 fc = le_to_host16(hdr->frame_control);
2914 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2915 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2916 level = MSG_EXCESSIVE;
2917 }
2918
2919 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2920 event_to_string(event), event);
2921}
2922#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923
2924 switch (event) {
2925 case EVENT_AUTH:
2926 sme_event_auth(wpa_s, data);
2927 break;
2928 case EVENT_ASSOC:
2929 wpa_supplicant_event_assoc(wpa_s, data);
2930 break;
2931 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002932 wpas_event_disassoc(wpa_s,
2933 data ? &data->disassoc_info : NULL);
2934 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002936 wpas_event_deauth(wpa_s,
2937 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 break;
2939 case EVENT_MICHAEL_MIC_FAILURE:
2940 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2941 break;
2942#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002943 case EVENT_SCAN_STARTED:
2944 os_get_reltime(&wpa_s->scan_start_time);
2945 if (wpa_s->own_scan_requested) {
2946 struct os_reltime diff;
2947
2948 os_reltime_sub(&wpa_s->scan_start_time,
2949 &wpa_s->scan_trigger_time, &diff);
2950 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2951 diff.sec, diff.usec);
2952 wpa_s->own_scan_requested = 0;
2953 wpa_s->own_scan_running = 1;
2954 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2955 wpa_s->manual_scan_use_id) {
2956 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
2957 wpa_s->manual_scan_id);
2958 } else {
2959 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2960 }
2961 } else {
2962 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
2963 wpa_s->external_scan_running = 1;
2964 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2965 }
2966 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002967 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002968 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
2969 struct os_reltime now, diff;
2970 os_get_reltime(&now);
2971 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
2972 wpa_s->scan_start_time.sec = 0;
2973 wpa_s->scan_start_time.usec = 0;
2974 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
2975 diff.sec, diff.usec);
2976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002978 wpa_s->own_scan_running = 0;
2979 wpa_s->external_scan_running = 0;
2980 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 break;
2982#endif /* CONFIG_NO_SCAN_PROCESSING */
2983 case EVENT_ASSOCINFO:
2984 wpa_supplicant_event_associnfo(wpa_s, data);
2985 break;
2986 case EVENT_INTERFACE_STATUS:
2987 wpa_supplicant_event_interface_status(wpa_s, data);
2988 break;
2989 case EVENT_PMKID_CANDIDATE:
2990 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2991 break;
2992#ifdef CONFIG_PEERKEY
2993 case EVENT_STKSTART:
2994 wpa_supplicant_event_stkstart(wpa_s, data);
2995 break;
2996#endif /* CONFIG_PEERKEY */
2997#ifdef CONFIG_TDLS
2998 case EVENT_TDLS:
2999 wpa_supplicant_event_tdls(wpa_s, data);
3000 break;
3001#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003002#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003003 case EVENT_WNM:
3004 wpa_supplicant_event_wnm(wpa_s, data);
3005 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003006#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007#ifdef CONFIG_IEEE80211R
3008 case EVENT_FT_RESPONSE:
3009 wpa_supplicant_event_ft_response(wpa_s, data);
3010 break;
3011#endif /* CONFIG_IEEE80211R */
3012#ifdef CONFIG_IBSS_RSN
3013 case EVENT_IBSS_RSN_START:
3014 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3015 break;
3016#endif /* CONFIG_IBSS_RSN */
3017 case EVENT_ASSOC_REJECT:
3018 if (data->assoc_reject.bssid)
3019 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3020 "bssid=" MACSTR " status_code=%u",
3021 MAC2STR(data->assoc_reject.bssid),
3022 data->assoc_reject.status_code);
3023 else
3024 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3025 "status_code=%u",
3026 data->assoc_reject.status_code);
3027 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3028 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003029 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030 const u8 *bssid = data->assoc_reject.bssid;
3031 if (bssid == NULL || is_zero_ether_addr(bssid))
3032 bssid = wpa_s->pending_bssid;
3033 wpas_connection_failed(wpa_s, bssid);
3034 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003035 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 break;
3037 case EVENT_AUTH_TIMED_OUT:
3038 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3039 sme_event_auth_timed_out(wpa_s, data);
3040 break;
3041 case EVENT_ASSOC_TIMED_OUT:
3042 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3043 sme_event_assoc_timed_out(wpa_s, data);
3044 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 case EVENT_TX_STATUS:
3046 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3047 " type=%d stype=%d",
3048 MAC2STR(data->tx_status.dst),
3049 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003050#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003052#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3054 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003055 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 wpa_s, data->tx_status.dst,
3057 data->tx_status.data,
3058 data->tx_status.data_len,
3059 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003060 OFFCHANNEL_SEND_ACTION_SUCCESS :
3061 OFFCHANNEL_SEND_ACTION_NO_ACK);
3062#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 break;
3064 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003065#endif /* CONFIG_AP */
3066#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3068 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3069 /*
3070 * Catch TX status events for Action frames we sent via group
3071 * interface in GO mode.
3072 */
3073 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3074 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3075 os_memcmp(wpa_s->parent->pending_action_dst,
3076 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003077 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 wpa_s->parent, data->tx_status.dst,
3079 data->tx_status.data,
3080 data->tx_status.data_len,
3081 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003082 OFFCHANNEL_SEND_ACTION_SUCCESS :
3083 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 break;
3085 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003086#endif /* CONFIG_OFFCHANNEL */
3087#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088 switch (data->tx_status.type) {
3089 case WLAN_FC_TYPE_MGMT:
3090 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3091 data->tx_status.data_len,
3092 data->tx_status.stype,
3093 data->tx_status.ack);
3094 break;
3095 case WLAN_FC_TYPE_DATA:
3096 ap_tx_status(wpa_s, data->tx_status.dst,
3097 data->tx_status.data,
3098 data->tx_status.data_len,
3099 data->tx_status.ack);
3100 break;
3101 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003102#endif /* CONFIG_AP */
3103 break;
3104#ifdef CONFIG_AP
3105 case EVENT_EAPOL_TX_STATUS:
3106 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3107 data->eapol_tx_status.data,
3108 data->eapol_tx_status.data_len,
3109 data->eapol_tx_status.ack);
3110 break;
3111 case EVENT_DRIVER_CLIENT_POLL_OK:
3112 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003113 break;
3114 case EVENT_RX_FROM_UNKNOWN:
3115 if (wpa_s->ap_iface == NULL)
3116 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003117 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3118 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003119 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003120 case EVENT_CH_SWITCH:
3121 if (!data)
3122 break;
3123 if (!wpa_s->ap_iface) {
3124 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3125 "event in non-AP mode");
3126 break;
3127 }
3128
Dmitry Shmidt04949592012-07-19 12:16:46 -07003129 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3130 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003131 data->ch_switch.ch_offset,
3132 data->ch_switch.ch_width,
3133 data->ch_switch.cf1,
3134 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003135 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003136#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003137 case EVENT_RX_MGMT: {
3138 u16 fc, stype;
3139 const struct ieee80211_mgmt *mgmt;
3140
3141 mgmt = (const struct ieee80211_mgmt *)
3142 data->rx_mgmt.frame;
3143 fc = le_to_host16(mgmt->frame_control);
3144 stype = WLAN_FC_GET_STYPE(fc);
3145
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003146#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003148#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3151 data->rx_mgmt.frame_len > 24) {
3152 const u8 *src = mgmt->sa;
3153 const u8 *ie = mgmt->u.probe_req.variable;
3154 size_t ie_len = data->rx_mgmt.frame_len -
3155 (mgmt->u.probe_req.variable -
3156 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003157 wpas_p2p_probe_req_rx(
3158 wpa_s, src, mgmt->da,
3159 mgmt->bssid, ie, ie_len,
3160 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 break;
3162 }
3163#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003164#ifdef CONFIG_IBSS_RSN
3165 if (stype == WLAN_FC_STYPE_AUTH &&
3166 data->rx_mgmt.frame_len >= 30) {
3167 wpa_supplicant_event_ibss_auth(wpa_s, data);
3168 break;
3169 }
3170#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003171
3172 if (stype == WLAN_FC_STYPE_ACTION) {
3173 wpas_event_rx_mgmt_action(
3174 wpa_s, mgmt, data->rx_mgmt.frame_len,
3175 data->rx_mgmt.freq);
3176 break;
3177 }
3178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3180 "management frame in non-AP mode");
3181 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003182#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003184
3185 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3186 data->rx_mgmt.frame_len > 24) {
3187 const u8 *ie = mgmt->u.probe_req.variable;
3188 size_t ie_len = data->rx_mgmt.frame_len -
3189 (mgmt->u.probe_req.variable -
3190 data->rx_mgmt.frame);
3191
3192 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3193 mgmt->bssid, ie, ie_len,
3194 data->rx_mgmt.ssi_signal);
3195 }
3196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003198#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 case EVENT_RX_PROBE_REQ:
3202 if (data->rx_probe_req.sa == NULL ||
3203 data->rx_probe_req.ie == NULL)
3204 break;
3205#ifdef CONFIG_AP
3206 if (wpa_s->ap_iface) {
3207 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3208 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003209 data->rx_probe_req.da,
3210 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003212 data->rx_probe_req.ie_len,
3213 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 break;
3215 }
3216#endif /* CONFIG_AP */
3217#ifdef CONFIG_P2P
3218 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003219 data->rx_probe_req.da,
3220 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003222 data->rx_probe_req.ie_len,
3223 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224#endif /* CONFIG_P2P */
3225 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003227#ifdef CONFIG_OFFCHANNEL
3228 offchannel_remain_on_channel_cb(
3229 wpa_s, data->remain_on_channel.freq,
3230 data->remain_on_channel.duration);
3231#endif /* CONFIG_OFFCHANNEL */
3232#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233 wpas_p2p_remain_on_channel_cb(
3234 wpa_s, data->remain_on_channel.freq,
3235 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003236#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 break;
3238 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003239#ifdef CONFIG_OFFCHANNEL
3240 offchannel_cancel_remain_on_channel_cb(
3241 wpa_s, data->remain_on_channel.freq);
3242#endif /* CONFIG_OFFCHANNEL */
3243#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 wpas_p2p_cancel_remain_on_channel_cb(
3245 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 case EVENT_EAPOL_RX:
3249 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3250 data->eapol_rx.data,
3251 data->eapol_rx.data_len);
3252 break;
3253 case EVENT_SIGNAL_CHANGE:
3254 bgscan_notify_signal_change(
3255 wpa_s, data->signal_change.above_threshold,
3256 data->signal_change.current_signal,
3257 data->signal_change.current_noise,
3258 data->signal_change.current_txrate);
3259 break;
3260 case EVENT_INTERFACE_ENABLED:
3261 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3262 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003263 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264#ifdef CONFIG_AP
3265 if (!wpa_s->ap_iface) {
3266 wpa_supplicant_set_state(wpa_s,
3267 WPA_DISCONNECTED);
3268 wpa_supplicant_req_scan(wpa_s, 0, 0);
3269 } else
3270 wpa_supplicant_set_state(wpa_s,
3271 WPA_COMPLETED);
3272#else /* CONFIG_AP */
3273 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3274 wpa_supplicant_req_scan(wpa_s, 0, 0);
3275#endif /* CONFIG_AP */
3276 }
3277 break;
3278 case EVENT_INTERFACE_DISABLED:
3279 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003280#ifdef CONFIG_P2P
3281 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3282 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3283 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3284 /*
3285 * The interface was externally disabled. Remove
3286 * it assuming an external entity will start a
3287 * new session if needed.
3288 */
3289 wpas_p2p_disconnect(wpa_s);
3290 break;
3291 }
3292#endif /* CONFIG_P2P */
3293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003295 radio_remove_works(wpa_s, NULL, 0);
3296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3298 break;
3299 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003300 wpa_supplicant_update_channel_list(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 break;
3302 case EVENT_INTERFACE_UNAVAILABLE:
3303#ifdef CONFIG_P2P
3304 wpas_p2p_interface_unavailable(wpa_s);
3305#endif /* CONFIG_P2P */
3306 break;
3307 case EVENT_BEST_CHANNEL:
3308 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3309 "(%d %d %d)",
3310 data->best_chan.freq_24, data->best_chan.freq_5,
3311 data->best_chan.freq_overall);
3312 wpa_s->best_24_freq = data->best_chan.freq_24;
3313 wpa_s->best_5_freq = data->best_chan.freq_5;
3314 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3315#ifdef CONFIG_P2P
3316 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3317 data->best_chan.freq_5,
3318 data->best_chan.freq_overall);
3319#endif /* CONFIG_P2P */
3320 break;
3321 case EVENT_UNPROT_DEAUTH:
3322 wpa_supplicant_event_unprot_deauth(wpa_s,
3323 &data->unprot_deauth);
3324 break;
3325 case EVENT_UNPROT_DISASSOC:
3326 wpa_supplicant_event_unprot_disassoc(wpa_s,
3327 &data->unprot_disassoc);
3328 break;
3329 case EVENT_STATION_LOW_ACK:
3330#ifdef CONFIG_AP
3331 if (wpa_s->ap_iface && data)
3332 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3333 data->low_ack.addr);
3334#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003335#ifdef CONFIG_TDLS
3336 if (data)
3337 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3338#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339 break;
3340 case EVENT_IBSS_PEER_LOST:
3341#ifdef CONFIG_IBSS_RSN
3342 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3343#endif /* CONFIG_IBSS_RSN */
3344 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003345 case EVENT_DRIVER_GTK_REKEY:
3346 if (os_memcmp(data->driver_gtk_rekey.bssid,
3347 wpa_s->bssid, ETH_ALEN))
3348 break;
3349 if (!wpa_s->wpa)
3350 break;
3351 wpa_sm_update_replay_ctr(wpa_s->wpa,
3352 data->driver_gtk_rekey.replay_ctr);
3353 break;
3354 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003355 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003356 wpa_s->sched_scanning = 0;
3357 wpa_supplicant_notify_scanning(wpa_s, 0);
3358
3359 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3360 break;
3361
3362 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003363 * Start a new sched scan to continue searching for more SSIDs
3364 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003365 */
Dmitry Shmidt18463232014-01-24 12:29:41 -08003366 if (wpa_s->sched_scan_timed_out || wpa_s->pno_sched_pending) {
3367
3368 if (wpa_supplicant_req_sched_scan(wpa_s) < 0 &&
3369 wpa_s->pno_sched_pending) {
3370 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3371 } else if (wpa_s->pno_sched_pending) {
3372 wpa_s->pno_sched_pending = 0;
3373 wpa_s->pno = 1;
3374 }
3375 }
3376
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003377 break;
3378 case EVENT_WPS_BUTTON_PUSHED:
3379#ifdef CONFIG_WPS
3380 wpas_wps_start_pbc(wpa_s, NULL, 0);
3381#endif /* CONFIG_WPS */
3382 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003383 case EVENT_AVOID_FREQUENCIES:
3384 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3385 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003386 case EVENT_CONNECT_FAILED_REASON:
3387#ifdef CONFIG_AP
3388 if (!wpa_s->ap_iface || !data)
3389 break;
3390 hostapd_event_connect_failed_reason(
3391 wpa_s->ap_iface->bss[0],
3392 data->connect_failed_reason.addr,
3393 data->connect_failed_reason.code);
3394#endif /* CONFIG_AP */
3395 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396 default:
3397 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3398 break;
3399 }
3400}