blob: 081b5856acfcb02dd0caf37c6f007ba80929d104 [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
Jouni Malinen5c879ee2014-08-18 11:04:56 -070086static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
87{
88 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
89
90 if (!bss) {
91 wpa_supplicant_update_scan_results(wpa_s);
92
93 /* Get the BSS from the new scan results */
94 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
95 }
96
97 if (bss)
98 wpa_s->current_bss = bss;
99}
100
101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
103{
104 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700105 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700107 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
108 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 return 0;
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111
112 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
113 "information");
114 ssid = wpa_supplicant_get_ssid(wpa_s);
115 if (ssid == NULL) {
116 wpa_msg(wpa_s, MSG_INFO,
117 "No network configuration found for the current AP");
118 return -1;
119 }
120
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
123 return -1;
124 }
125
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800126 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
127 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
128 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
129 return -1;
130 }
131
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700132 res = wpas_temp_disabled(wpa_s, ssid);
133 if (res > 0) {
134 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
135 "disabled for %d second(s)", res);
136 return -1;
137 }
138
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
140 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 u8 wpa_ie[80];
143 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800144 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
145 wpa_ie, &wpa_ie_len) < 0)
146 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147 } else {
148 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
149 }
150
151 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
152 eapol_sm_invalidate_cached_session(wpa_s->eapol);
153 old_ssid = wpa_s->current_ssid;
154 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800155
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700156 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
159 wpa_supplicant_initiate_eapol(wpa_s);
160 if (old_ssid != wpa_s->current_ssid)
161 wpas_notify_network_changed(wpa_s);
162
163 return 0;
164}
165
166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800167void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168{
169 struct wpa_supplicant *wpa_s = eloop_ctx;
170
171 if (wpa_s->countermeasures) {
172 wpa_s->countermeasures = 0;
173 wpa_drv_set_countermeasures(wpa_s, 0);
174 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
175 wpa_supplicant_req_scan(wpa_s, 0, 0);
176 }
177}
178
179
180void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
181{
182 int bssid_changed;
183
Dmitry Shmidt04949592012-07-19 12:16:46 -0700184 wnm_bss_keep_alive_deinit(wpa_s);
185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186#ifdef CONFIG_IBSS_RSN
187 ibss_rsn_deinit(wpa_s->ibss_rsn);
188 wpa_s->ibss_rsn = NULL;
189#endif /* CONFIG_IBSS_RSN */
190
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700191#ifdef CONFIG_AP
192 wpa_supplicant_ap_deinit(wpa_s);
193#endif /* CONFIG_AP */
194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
196 return;
197
198 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
199 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
200 os_memset(wpa_s->bssid, 0, ETH_ALEN);
201 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700202#ifdef CONFIG_SME
203 wpa_s->sme.prev_bssid_set = 0;
204#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800205#ifdef CONFIG_P2P
206 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
207#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208 wpa_s->current_bss = NULL;
209 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700210#ifdef CONFIG_IEEE80211R
211#ifdef CONFIG_SME
212 if (wpa_s->sme.ft_ies)
213 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
214#endif /* CONFIG_SME */
215#endif /* CONFIG_IEEE80211R */
216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (bssid_changed)
218 wpas_notify_bssid_changed(wpa_s);
219
220 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
221 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
222 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
223 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
224 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700225 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700226 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700227 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228}
229
230
231static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
232{
233 struct wpa_ie_data ie;
234 int pmksa_set = -1;
235 size_t i;
236
237 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
238 ie.pmkid == NULL)
239 return;
240
241 for (i = 0; i < ie.num_pmkid; i++) {
242 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
243 ie.pmkid + i * PMKID_LEN,
244 NULL, NULL, 0);
245 if (pmksa_set == 0) {
246 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
247 break;
248 }
249 }
250
251 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
252 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
253}
254
255
256static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
257 union wpa_event_data *data)
258{
259 if (data == NULL) {
260 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
261 "event");
262 return;
263 }
264 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
265 " index=%d preauth=%d",
266 MAC2STR(data->pmkid_candidate.bssid),
267 data->pmkid_candidate.index,
268 data->pmkid_candidate.preauth);
269
270 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
271 data->pmkid_candidate.index,
272 data->pmkid_candidate.preauth);
273}
274
275
276static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
277{
278 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
279 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
280 return 0;
281
282#ifdef IEEE8021X_EAPOL
283 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
284 wpa_s->current_ssid &&
285 !(wpa_s->current_ssid->eapol_flags &
286 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
287 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
288 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
289 * plaintext or static WEP keys). */
290 return 0;
291 }
292#endif /* IEEE8021X_EAPOL */
293
294 return 1;
295}
296
297
298/**
299 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
300 * @wpa_s: pointer to wpa_supplicant data
301 * @ssid: Configuration data for the network
302 * Returns: 0 on success, -1 on failure
303 *
304 * This function is called when starting authentication with a network that is
305 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
306 */
307int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
308 struct wpa_ssid *ssid)
309{
310#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700312 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700314 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
315 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 return 0;
317
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700318 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 sim = 1;
320 aka = 1;
321 } else {
322 struct eap_method_type *eap = ssid->eap.eap_methods;
323 while (eap->vendor != EAP_VENDOR_IETF ||
324 eap->method != EAP_TYPE_NONE) {
325 if (eap->vendor == EAP_VENDOR_IETF) {
326 if (eap->method == EAP_TYPE_SIM)
327 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700328 else if (eap->method == EAP_TYPE_AKA ||
329 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 aka = 1;
331 }
332 eap++;
333 }
334 }
335
336 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
337 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
339 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
340 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 aka = 0;
342
343 if (!sim && !aka) {
344 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
345 "use SIM, but neither EAP-SIM nor EAP-AKA are "
346 "enabled");
347 return 0;
348 }
349
350 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
351 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700353 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 if (wpa_s->scard == NULL) {
355 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
356 "(pcsc-lite)");
357 return -1;
358 }
359 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
360 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362#endif /* IEEE8021X_EAPOL */
363
364 return 0;
365}
366
367
368#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700369
370static int has_wep_key(struct wpa_ssid *ssid)
371{
372 int i;
373
374 for (i = 0; i < NUM_WEP_KEYS; i++) {
375 if (ssid->wep_key_len[i])
376 return 1;
377 }
378
379 return 0;
380}
381
382
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700383static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 struct wpa_ssid *ssid)
385{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700386 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387
388 if (ssid->mixed_cell)
389 return 1;
390
391#ifdef CONFIG_WPS
392 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
393 return 1;
394#endif /* CONFIG_WPS */
395
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700396 if (has_wep_key(ssid))
397 privacy = 1;
398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399#ifdef IEEE8021X_EAPOL
400 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
401 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
402 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
403 privacy = 1;
404#endif /* IEEE8021X_EAPOL */
405
Jouni Malinen75ecf522011-06-27 15:19:46 -0700406 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
407 privacy = 1;
408
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800409 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
410 privacy = 1;
411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 if (bss->caps & IEEE80211_CAP_PRIVACY)
413 return privacy;
414 return !privacy;
415}
416
417
418static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
419 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700420 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421{
422 struct wpa_ie_data ie;
423 int proto_match = 0;
424 const u8 *rsn_ie, *wpa_ie;
425 int ret;
426 int wep_ok;
427
428 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
429 if (ret >= 0)
430 return ret;
431
432 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
433 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
434 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
435 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
436 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
437
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700438 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
440 proto_match++;
441
442 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
443 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
444 "failed");
445 break;
446 }
447
448 if (wep_ok &&
449 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
450 {
451 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
452 "in RSN IE");
453 return 1;
454 }
455
456 if (!(ie.proto & ssid->proto)) {
457 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
458 "mismatch");
459 break;
460 }
461
462 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
463 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
464 "cipher mismatch");
465 break;
466 }
467
468 if (!(ie.group_cipher & ssid->group_cipher)) {
469 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
470 "cipher mismatch");
471 break;
472 }
473
474 if (!(ie.key_mgmt & ssid->key_mgmt)) {
475 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
476 "mismatch");
477 break;
478 }
479
480#ifdef CONFIG_IEEE80211W
481 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800482 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
483 wpa_s->conf->pmf : ssid->ieee80211w) ==
484 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
486 "frame protection");
487 break;
488 }
489#endif /* CONFIG_IEEE80211W */
490
491 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
492 return 1;
493 }
494
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700495 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
497 proto_match++;
498
499 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
500 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
501 "failed");
502 break;
503 }
504
505 if (wep_ok &&
506 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
507 {
508 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
509 "in WPA IE");
510 return 1;
511 }
512
513 if (!(ie.proto & ssid->proto)) {
514 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
515 "mismatch");
516 break;
517 }
518
519 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
520 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
521 "cipher mismatch");
522 break;
523 }
524
525 if (!(ie.group_cipher & ssid->group_cipher)) {
526 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
527 "cipher mismatch");
528 break;
529 }
530
531 if (!(ie.key_mgmt & ssid->key_mgmt)) {
532 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
533 "mismatch");
534 break;
535 }
536
537 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
538 return 1;
539 }
540
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700541 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
542 !rsn_ie) {
543 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
544 return 1;
545 }
546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
548 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
549 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
550 return 0;
551 }
552
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800553 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
554 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
555 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
556 return 1;
557 }
558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
560 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
561 return 1;
562 }
563
564 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
565 "WPA/WPA2");
566
567 return 0;
568}
569
570
571static int freq_allowed(int *freqs, int freq)
572{
573 int i;
574
575 if (freqs == NULL)
576 return 1;
577
578 for (i = 0; freqs[i]; i++)
579 if (freqs[i] == freq)
580 return 1;
581 return 0;
582}
583
584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585static int ht_supported(const struct hostapd_hw_modes *mode)
586{
587 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
588 /*
589 * The driver did not indicate whether it supports HT. Assume
590 * it does to avoid connection issues.
591 */
592 return 1;
593 }
594
595 /*
596 * IEEE Std 802.11n-2009 20.1.1:
597 * An HT non-AP STA shall support all EQM rates for one spatial stream.
598 */
599 return mode->mcs_set[0] == 0xff;
600}
601
602
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700603static int vht_supported(const struct hostapd_hw_modes *mode)
604{
605 if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
606 /*
607 * The driver did not indicate whether it supports VHT. Assume
608 * it does to avoid connection issues.
609 */
610 return 1;
611 }
612
613 /*
614 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
615 * TODO: Verify if this complies with the standard
616 */
617 return (mode->vht_mcs_set[0] & 0x3) != 3;
618}
619
620
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700621static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800622{
623 const struct hostapd_hw_modes *mode = NULL, *modes;
624 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
625 const u8 *rate_ie;
626 int i, j, k;
627
628 if (bss->freq == 0)
629 return 1; /* Cannot do matching without knowing band */
630
631 modes = wpa_s->hw.modes;
632 if (modes == NULL) {
633 /*
634 * The driver does not provide any additional information
635 * about the utilized hardware, so allow the connection attempt
636 * to continue.
637 */
638 return 1;
639 }
640
641 for (i = 0; i < wpa_s->hw.num_modes; i++) {
642 for (j = 0; j < modes[i].num_channels; j++) {
643 int freq = modes[i].channels[j].freq;
644 if (freq == bss->freq) {
645 if (mode &&
646 mode->mode == HOSTAPD_MODE_IEEE80211G)
647 break; /* do not allow 802.11b replace
648 * 802.11g */
649 mode = &modes[i];
650 break;
651 }
652 }
653 }
654
655 if (mode == NULL)
656 return 0;
657
658 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700659 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800660 if (rate_ie == NULL)
661 continue;
662
663 for (j = 2; j < rate_ie[1] + 2; j++) {
664 int flagged = !!(rate_ie[j] & 0x80);
665 int r = (rate_ie[j] & 0x7f) * 5;
666
667 /*
668 * IEEE Std 802.11n-2009 7.3.2.2:
669 * The new BSS Membership selector value is encoded
670 * like a legacy basic rate, but it is not a rate and
671 * only indicates if the BSS members are required to
672 * support the mandatory features of Clause 20 [HT PHY]
673 * in order to join the BSS.
674 */
675 if (flagged && ((rate_ie[j] & 0x7f) ==
676 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
677 if (!ht_supported(mode)) {
678 wpa_dbg(wpa_s, MSG_DEBUG,
679 " hardware does not support "
680 "HT PHY");
681 return 0;
682 }
683 continue;
684 }
685
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700686 /* There's also a VHT selector for 802.11ac */
687 if (flagged && ((rate_ie[j] & 0x7f) ==
688 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
689 if (!vht_supported(mode)) {
690 wpa_dbg(wpa_s, MSG_DEBUG,
691 " hardware does not support "
692 "VHT PHY");
693 return 0;
694 }
695 continue;
696 }
697
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800698 if (!flagged)
699 continue;
700
701 /* check for legacy basic rates */
702 for (k = 0; k < mode->num_rates; k++) {
703 if (mode->rates[k] == r)
704 break;
705 }
706 if (k == mode->num_rates) {
707 /*
708 * IEEE Std 802.11-2007 7.3.2.2 demands that in
709 * order to join a BSS all required rates
710 * have to be supported by the hardware.
711 */
712 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
713 "not support required rate %d.%d Mbps",
714 r / 10, r % 10);
715 return 0;
716 }
717 }
718 }
719
720 return 1;
721}
722
723
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800724/*
725 * Test whether BSS is in an ESS.
726 * This is done differently in DMG (60 GHz) and non-DMG bands
727 */
728static int bss_is_ess(struct wpa_bss *bss)
729{
730 if (bss_is_dmg(bss)) {
731 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
732 IEEE80211_CAP_DMG_AP;
733 }
734
735 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
736 IEEE80211_CAP_ESS);
737}
738
739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700741 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800742 struct wpa_ssid *group,
743 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700745 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746 int wpa;
747 struct wpa_blacklist *e;
748 const u8 *ie;
749 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800750 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700752 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 wpa_ie_len = ie ? ie[1] : 0;
754
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700755 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 rsn_ie_len = ie ? ie[1] : 0;
757
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800758 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
759 osen = ie != NULL;
760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800762 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700763 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700765 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
766 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
767 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800768 " p2p" : "",
769 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770
771 e = wpa_blacklist_get(wpa_s, bss->bssid);
772 if (e) {
773 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700774 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 /*
776 * When only a single network is enabled, we can
777 * trigger blacklisting on the first failure. This
778 * should not be done with multiple enabled networks to
779 * avoid getting forced to move into a worse ESS on
780 * single error if there are no other BSSes of the
781 * current ESS.
782 */
783 limit = 0;
784 }
785 if (e->count > limit) {
786 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
787 "(count=%d limit=%d)", e->count, limit);
788 return NULL;
789 }
790 }
791
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700792 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
794 return NULL;
795 }
796
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800797 if (disallowed_bssid(wpa_s, bss->bssid)) {
798 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
799 return NULL;
800 }
801
802 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
803 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
804 return NULL;
805 }
806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
808
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800809 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700811 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812
Dmitry Shmidt04949592012-07-19 12:16:46 -0700813 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
815 continue;
816 }
817
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700818 res = wpas_temp_disabled(wpa_s, ssid);
819 if (res > 0) {
820 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
821 "temporarily for %d second(s)", res);
822 continue;
823 }
824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825#ifdef CONFIG_WPS
826 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
827 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
828 "(WPS)");
829 continue;
830 }
831
832 if (wpa && ssid->ssid_len == 0 &&
833 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
834 check_ssid = 0;
835
836 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
837 /* Only allow wildcard SSID match if an AP
838 * advertises active WPS operation that matches
839 * with our mode. */
840 check_ssid = 1;
841 if (ssid->ssid_len == 0 &&
842 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
843 check_ssid = 0;
844 }
845#endif /* CONFIG_WPS */
846
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847 if (ssid->bssid_set && ssid->ssid_len == 0 &&
848 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
849 check_ssid = 0;
850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700852 (bss->ssid_len != ssid->ssid_len ||
853 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
855 continue;
856 }
857
858 if (ssid->bssid_set &&
859 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
860 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
861 continue;
862 }
863
864 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
865 continue;
866
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800867 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
869 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
870 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
871 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
872 "not allowed");
873 continue;
874 }
875
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700876 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
877 has_wep_key(ssid)) {
878 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
879 continue;
880 }
881
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800882 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
883 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
884 "not allowed");
885 continue;
886 }
887
Jouni Malinen75ecf522011-06-27 15:19:46 -0700888 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
890 "mismatch");
891 continue;
892 }
893
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800894 if (!bss_is_ess(bss)) {
895 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 continue;
897 }
898
899 if (!freq_allowed(ssid->freq_list, bss->freq)) {
900 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
901 "allowed");
902 continue;
903 }
904
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 if (!rate_match(wpa_s, bss)) {
906 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
907 "not match");
908 continue;
909 }
910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -0700912 if (ssid->p2p_group &&
913 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
914 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
915 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
916 continue;
917 }
918
Dmitry Shmidt54605472013-11-08 11:10:19 -0800919 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
920 struct wpabuf *p2p_ie;
921 u8 dev_addr[ETH_ALEN];
922
923 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
924 if (ie == NULL) {
925 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
926 continue;
927 }
928 p2p_ie = wpa_bss_get_vendor_ie_multi(
929 bss, P2P_IE_VENDOR_TYPE);
930 if (p2p_ie == NULL) {
931 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
932 continue;
933 }
934
935 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
936 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
937 ETH_ALEN) != 0) {
938 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
939 wpabuf_free(p2p_ie);
940 continue;
941 }
942 wpabuf_free(p2p_ie);
943 }
944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 /*
946 * TODO: skip the AP if its P2P IE has Group Formation
947 * bit set in the P2P Group Capability Bitmap and we
948 * are not in Group Formation with that device.
949 */
950#endif /* CONFIG_P2P */
951
952 /* Matching configuration found */
953 return ssid;
954 }
955
956 /* No matching configuration found */
957 return NULL;
958}
959
960
961static struct wpa_bss *
962wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800964 struct wpa_ssid **selected_ssid,
965 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700967 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800969 if (only_first_ssid)
970 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
971 group->id);
972 else
973 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
974 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700976 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
977 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800978 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
979 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 if (!*selected_ssid)
981 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
983 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700984 MAC2STR(bss->bssid),
985 wpa_ssid_txt(bss->ssid, bss->ssid_len));
986 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 }
988
989 return NULL;
990}
991
992
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700993struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
994 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995{
996 struct wpa_bss *selected = NULL;
997 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800998 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001000 if (wpa_s->last_scan_res == NULL ||
1001 wpa_s->last_scan_res_used == 0)
1002 return NULL; /* no scan results from last update */
1003
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001004 if (wpa_s->next_ssid) {
1005 struct wpa_ssid *ssid;
1006
1007 /* check that next_ssid is still valid */
1008 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1009 if (ssid == wpa_s->next_ssid)
1010 break;
1011 }
1012 next_ssid = ssid;
1013 wpa_s->next_ssid = NULL;
1014 }
1015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001017 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1018 if (next_ssid && next_ssid->priority ==
1019 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001020 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001021 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001022 if (selected)
1023 break;
1024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001026 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001027 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001028 if (selected)
1029 break;
1030 }
1031
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001032 if (selected == NULL && wpa_s->blacklist &&
1033 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1035 "blacklist and try again");
1036 wpa_blacklist_clear(wpa_s);
1037 wpa_s->blacklist_cleared++;
1038 } else if (selected == NULL)
1039 break;
1040 }
1041
1042 return selected;
1043}
1044
1045
1046static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1047 int timeout_sec, int timeout_usec)
1048{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001049 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 /*
1051 * No networks are enabled; short-circuit request so
1052 * we don't wait timeout seconds before transitioning
1053 * to INACTIVE state.
1054 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001055 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1056 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1058 return;
1059 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001060
1061 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1063}
1064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001066int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001067 struct wpa_bss *selected,
1068 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069{
1070 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1071 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1072 "PBC session overlap");
1073#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001074 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1075 wpa_s->p2p_in_provisioning) {
1076 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1077 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001078 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080#endif /* CONFIG_P2P */
1081
1082#ifdef CONFIG_WPS
1083 wpas_wps_cancel(wpa_s);
1084#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001085 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 }
1087
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001088 wpa_msg(wpa_s, MSG_DEBUG,
1089 "Considering connect request: reassociate: %d selected: "
1090 MACSTR " bssid: " MACSTR " pending: " MACSTR
1091 " wpa_state: %s ssid=%p current_ssid=%p",
1092 wpa_s->reassociate, MAC2STR(selected->bssid),
1093 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1094 wpa_supplicant_state_txt(wpa_s->wpa_state),
1095 ssid, wpa_s->current_ssid);
1096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 /*
1098 * Do not trigger new association unless the BSSID has changed or if
1099 * reassociation is requested. If we are in process of associating with
1100 * the selected BSSID, do not trigger new attempt.
1101 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001102 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1104 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1105 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001106 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1107 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1108 0) ||
1109 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1110 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1112 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001113 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001115 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1116 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 wpa_supplicant_associate(wpa_s, selected, ssid);
1118 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001119 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1120 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001121 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001122
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001123 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124}
1125
1126
1127static struct wpa_ssid *
1128wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1129{
1130 int prio;
1131 struct wpa_ssid *ssid;
1132
1133 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1134 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1135 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001136 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 continue;
1138 if (ssid->mode == IEEE80211_MODE_IBSS ||
1139 ssid->mode == IEEE80211_MODE_AP)
1140 return ssid;
1141 }
1142 }
1143 return NULL;
1144}
1145
1146
1147/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1148 * on BSS added and BSS changed events */
1149static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001150 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001152 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
1154 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1155 return;
1156
Jouni Malinen87fd2792011-05-16 18:35:42 +03001157 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159
Jouni Malinen87fd2792011-05-16 18:35:42 +03001160 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 if (ssid == NULL)
1162 continue;
1163
Jouni Malinen87fd2792011-05-16 18:35:42 +03001164 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 if (rsn == NULL)
1166 continue;
1167
Jouni Malinen87fd2792011-05-16 18:35:42 +03001168 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 }
1170
1171}
1172
1173
1174static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1175 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001176 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001178 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 int min_diff;
1180
1181 if (wpa_s->reassociate)
1182 return 1; /* explicit request to reassociate */
1183 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1184 return 1; /* we are not associated; continue */
1185 if (wpa_s->current_ssid == NULL)
1186 return 1; /* unknown current SSID */
1187 if (wpa_s->current_ssid != ssid)
1188 return 1; /* different network block */
1189
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 if (wpas_driver_bss_selection(wpa_s))
1191 return 0; /* Driver-based roaming */
1192
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001193 if (wpa_s->current_ssid->ssid)
1194 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1195 wpa_s->current_ssid->ssid,
1196 wpa_s->current_ssid->ssid_len);
1197 if (!current_bss)
1198 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 if (!current_bss)
1201 return 1; /* current BSS not seen in scan results */
1202
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001203 if (current_bss == selected)
1204 return 0;
1205
1206 if (selected->last_update_idx > current_bss->last_update_idx)
1207 return 1; /* current BSS not seen in the last scan */
1208
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001209#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001210 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1211 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1212 MAC2STR(current_bss->bssid), current_bss->level);
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1214 MAC2STR(selected->bssid), selected->level);
1215
1216 if (wpa_s->current_ssid->bssid_set &&
1217 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1218 0) {
1219 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1220 "has preferred BSSID");
1221 return 1;
1222 }
1223
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001224 if (current_bss->level < 0 && current_bss->level > selected->level) {
1225 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1226 "signal level");
1227 return 0;
1228 }
1229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 min_diff = 2;
1231 if (current_bss->level < 0) {
1232 if (current_bss->level < -85)
1233 min_diff = 1;
1234 else if (current_bss->level < -80)
1235 min_diff = 2;
1236 else if (current_bss->level < -75)
1237 min_diff = 3;
1238 else if (current_bss->level < -70)
1239 min_diff = 4;
1240 else
1241 min_diff = 5;
1242 }
1243 if (abs(current_bss->level - selected->level) < min_diff) {
1244 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1245 "in signal level");
1246 return 0;
1247 }
1248
1249 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001250#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001251 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001252#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253}
1254
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001255
Jouni Malinen89ca7022012-09-14 13:03:12 -07001256/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001257 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001258static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001259 union wpa_event_data *data,
1260 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001262 struct wpa_scan_results *scan_res = NULL;
1263 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001265#ifndef CONFIG_NO_RANDOM_POOL
1266 size_t i, num;
1267#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268
1269#ifdef CONFIG_AP
1270 if (wpa_s->ap_iface)
1271 ap = 1;
1272#endif /* CONFIG_AP */
1273
1274 wpa_supplicant_notify_scanning(wpa_s, 0);
1275
1276 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1277 data ? &data->scan_info :
1278 NULL, 1);
1279 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001280 if (wpa_s->conf->ap_scan == 2 || ap ||
1281 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001283 if (!own_request)
1284 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1286 "scanning again");
1287 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001288 ret = -1;
1289 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 }
1291
1292#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 num = scan_res->num;
1294 if (num > 10)
1295 num = 10;
1296 for (i = 0; i < num; i++) {
1297 u8 buf[5];
1298 struct wpa_scan_res *res = scan_res->res[i];
1299 buf[0] = res->bssid[5];
1300 buf[1] = res->qual & 0xff;
1301 buf[2] = res->noise & 0xff;
1302 buf[3] = res->level & 0xff;
1303 buf[4] = res->tsf & 0xff;
1304 random_add_randomness(buf, sizeof(buf));
1305 }
1306#endif /* CONFIG_NO_RANDOM_POOL */
1307
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001308 if (own_request && wpa_s->scan_res_handler &&
1309 (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1311 struct wpa_scan_results *scan_res);
1312
1313 scan_res_handler = wpa_s->scan_res_handler;
1314 wpa_s->scan_res_handler = NULL;
1315 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001316 ret = -2;
1317 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 }
1319
1320 if (ap) {
1321 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1322#ifdef CONFIG_AP
1323 if (wpa_s->ap_iface->scan_cb)
1324 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1325#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001326 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001327 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001329 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1330 wpa_s->own_scan_running, wpa_s->external_scan_running);
1331 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1332 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1333 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1334 wpa_s->manual_scan_id);
1335 wpa_s->manual_scan_use_id = 0;
1336 } else {
1337 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1338 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001339 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340
1341 wpas_notify_scan_done(wpa_s, 1);
1342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001343 if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1344 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 -07001345 wpa_scan_results_free(scan_res);
1346 return 0;
1347 }
1348
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001349 if (sme_proc_obss_scan(wpa_s) > 0)
1350 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001352 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1353 goto scan_work_done;
1354
1355 if (autoscan_notify_scan(wpa_s, scan_res))
1356 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 if (wpa_s->disconnected) {
1359 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001360 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 }
1362
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001363 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001364 bgscan_notify_scan(wpa_s, scan_res) == 1)
1365 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001367 wpas_wps_update_ap_info(wpa_s, scan_res);
1368
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001369 wpa_scan_results_free(scan_res);
1370
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001371 if (wpa_s->scan_work) {
1372 struct wpa_radio_work *work = wpa_s->scan_work;
1373 wpa_s->scan_work = NULL;
1374 radio_work_done(work);
1375 }
1376
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001377 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001378
1379scan_work_done:
1380 wpa_scan_results_free(scan_res);
1381 if (wpa_s->scan_work) {
1382 struct wpa_radio_work *work = wpa_s->scan_work;
1383 wpa_s->scan_work = NULL;
1384 radio_work_done(work);
1385 }
1386 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001387}
1388
1389
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001390static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001391 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001392{
1393 struct wpa_bss *selected;
1394 struct wpa_ssid *ssid = NULL;
1395
1396 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397
1398 if (selected) {
1399 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001400 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001401 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001402 if (new_scan)
1403 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405 }
1406
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001407 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001408 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001409 return -1;
1410 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001411 if (new_scan)
1412 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001413 /*
1414 * Do not notify other virtual radios of scan results since we do not
1415 * want them to start other associations at the same time.
1416 */
1417 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1420 ssid = wpa_supplicant_pick_new_network(wpa_s);
1421 if (ssid) {
1422 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1423 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001424 if (new_scan)
1425 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001426 } else if (own_request) {
1427 /*
1428 * No SSID found. If SCAN results are as a result of
1429 * own scan request and not due to a scan request on
1430 * another shared interface, try another scan.
1431 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 int timeout_sec = wpa_s->scan_interval;
1433 int timeout_usec = 0;
1434#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001435 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1436 return 0;
1437
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001438 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001439 wpa_s->show_group_started ||
1440 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 /*
1442 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001443 * state and during P2P join-a-group operation
1444 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 */
1446 timeout_sec = 0;
1447 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001448 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1449 timeout_usec);
1450 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 }
1452#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001453#ifdef CONFIG_INTERWORKING
1454 if (wpa_s->conf->auto_interworking &&
1455 wpa_s->conf->interworking &&
1456 wpa_s->conf->cred) {
1457 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1458 "start ANQP fetch since no matching "
1459 "networks found");
1460 wpa_s->network_select = 1;
1461 wpa_s->auto_network_select = 1;
1462 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001463 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001464 }
1465#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001466#ifdef CONFIG_WPS
1467 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1468 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1469 timeout_sec = 0;
1470 timeout_usec = 500000;
1471 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1472 timeout_usec);
1473 return 0;
1474 }
1475#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001476 if (wpa_supplicant_req_sched_scan(wpa_s))
1477 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1478 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 }
1480 }
1481 return 0;
1482}
1483
1484
1485static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1486 union wpa_event_data *data)
1487{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001489
1490 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 /*
1492 * If no scan results could be fetched, then no need to
1493 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001494 * this scan. Similarly, if scan results started a new operation on this
1495 * interface, do not notify other interfaces to avoid concurrent
1496 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 */
1498 return;
1499 }
1500
1501 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001502 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 * so, they get updated with this same scan info.
1504 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001505 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1506 radio_list) {
1507 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1509 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001510 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 }
1512 }
1513}
1514
1515#endif /* CONFIG_NO_SCAN_PROCESSING */
1516
1517
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001518int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1519{
1520#ifdef CONFIG_NO_SCAN_PROCESSING
1521 return -1;
1522#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001524
1525 if (wpa_s->last_scan_res_used <= 0)
1526 return -1;
1527
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528 os_get_reltime(&now);
1529 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001530 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1531 return -1;
1532 }
1533
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001534 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001535#endif /* CONFIG_NO_SCAN_PROCESSING */
1536}
1537
Dmitry Shmidt04949592012-07-19 12:16:46 -07001538#ifdef CONFIG_WNM
1539
1540static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1541{
1542 struct wpa_supplicant *wpa_s = eloop_ctx;
1543
1544 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1545 return;
1546
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001547 if (!wpa_s->no_keep_alive) {
1548 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1549 MAC2STR(wpa_s->bssid));
1550 /* TODO: could skip this if normal data traffic has been sent */
1551 /* TODO: Consider using some more appropriate data frame for
1552 * this */
1553 if (wpa_s->l2)
1554 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1555 (u8 *) "", 0);
1556 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001557
1558#ifdef CONFIG_SME
1559 if (wpa_s->sme.bss_max_idle_period) {
1560 unsigned int msec;
1561 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1562 if (msec > 100)
1563 msec -= 100;
1564 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1565 wnm_bss_keep_alive, wpa_s, NULL);
1566 }
1567#endif /* CONFIG_SME */
1568}
1569
1570
1571static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1572 const u8 *ies, size_t ies_len)
1573{
1574 struct ieee802_11_elems elems;
1575
1576 if (ies == NULL)
1577 return;
1578
1579 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1580 return;
1581
1582#ifdef CONFIG_SME
1583 if (elems.bss_max_idle_period) {
1584 unsigned int msec;
1585 wpa_s->sme.bss_max_idle_period =
1586 WPA_GET_LE16(elems.bss_max_idle_period);
1587 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1588 "TU)%s", wpa_s->sme.bss_max_idle_period,
1589 (elems.bss_max_idle_period[2] & 0x01) ?
1590 " (protected keep-live required)" : "");
1591 if (wpa_s->sme.bss_max_idle_period == 0)
1592 wpa_s->sme.bss_max_idle_period = 1;
1593 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1594 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1595 /* msec times 1000 */
1596 msec = wpa_s->sme.bss_max_idle_period * 1024;
1597 if (msec > 100)
1598 msec -= 100;
1599 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1600 wnm_bss_keep_alive, wpa_s,
1601 NULL);
1602 }
1603 }
1604#endif /* CONFIG_SME */
1605}
1606
1607#endif /* CONFIG_WNM */
1608
1609
1610void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1611{
1612#ifdef CONFIG_WNM
1613 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1614#endif /* CONFIG_WNM */
1615}
1616
1617
Dmitry Shmidt051af732013-10-22 13:52:46 -07001618#ifdef CONFIG_INTERWORKING
1619
1620static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1621 size_t len)
1622{
1623 int res;
1624
1625 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1626 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1627 if (res) {
1628 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1629 }
1630
1631 return res;
1632}
1633
1634
1635static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1636 const u8 *ies, size_t ies_len)
1637{
1638 struct ieee802_11_elems elems;
1639
1640 if (ies == NULL)
1641 return;
1642
1643 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1644 return;
1645
1646 if (elems.qos_map_set) {
1647 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1648 elems.qos_map_set_len);
1649 }
1650}
1651
1652#endif /* CONFIG_INTERWORKING */
1653
1654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1656 union wpa_event_data *data)
1657{
1658 int l, len, found = 0, wpa_found, rsn_found;
1659 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001660#ifdef CONFIG_IEEE80211R
1661 u8 bssid[ETH_ALEN];
1662#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663
1664 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1665 if (data->assoc_info.req_ies)
1666 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1667 data->assoc_info.req_ies_len);
1668 if (data->assoc_info.resp_ies) {
1669 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1670 data->assoc_info.resp_ies_len);
1671#ifdef CONFIG_TDLS
1672 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1673 data->assoc_info.resp_ies_len);
1674#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001675#ifdef CONFIG_WNM
1676 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1677 data->assoc_info.resp_ies_len);
1678#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001679#ifdef CONFIG_INTERWORKING
1680 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1681 data->assoc_info.resp_ies_len);
1682#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 }
1684 if (data->assoc_info.beacon_ies)
1685 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1686 data->assoc_info.beacon_ies,
1687 data->assoc_info.beacon_ies_len);
1688 if (data->assoc_info.freq)
1689 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1690 data->assoc_info.freq);
1691
1692 p = data->assoc_info.req_ies;
1693 l = data->assoc_info.req_ies_len;
1694
1695 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1696 while (p && l >= 2) {
1697 len = p[1] + 2;
1698 if (len > l) {
1699 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1700 p, l);
1701 break;
1702 }
1703 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1704 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1705 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1706 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1707 break;
1708 found = 1;
1709 wpa_find_assoc_pmkid(wpa_s);
1710 break;
1711 }
1712 l -= len;
1713 p += len;
1714 }
1715 if (!found && data->assoc_info.req_ies)
1716 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1717
1718#ifdef CONFIG_IEEE80211R
1719#ifdef CONFIG_SME
1720 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1722 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1723 data->assoc_info.resp_ies,
1724 data->assoc_info.resp_ies_len,
1725 bssid) < 0) {
1726 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1727 "Reassociation Response failed");
1728 wpa_supplicant_deauthenticate(
1729 wpa_s, WLAN_REASON_INVALID_IE);
1730 return -1;
1731 }
1732 }
1733
1734 p = data->assoc_info.resp_ies;
1735 l = data->assoc_info.resp_ies_len;
1736
1737#ifdef CONFIG_WPS_STRICT
1738 if (p && wpa_s->current_ssid &&
1739 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1740 struct wpabuf *wps;
1741 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1742 if (wps == NULL) {
1743 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1744 "include WPS IE in (Re)Association Response");
1745 return -1;
1746 }
1747
1748 if (wps_validate_assoc_resp(wps) < 0) {
1749 wpabuf_free(wps);
1750 wpa_supplicant_deauthenticate(
1751 wpa_s, WLAN_REASON_INVALID_IE);
1752 return -1;
1753 }
1754 wpabuf_free(wps);
1755 }
1756#endif /* CONFIG_WPS_STRICT */
1757
1758 /* Go through the IEs and make a copy of the MDIE, if present. */
1759 while (p && l >= 2) {
1760 len = p[1] + 2;
1761 if (len > l) {
1762 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1763 p, l);
1764 break;
1765 }
1766 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1767 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1768 wpa_s->sme.ft_used = 1;
1769 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1770 MOBILITY_DOMAIN_ID_LEN);
1771 break;
1772 }
1773 l -= len;
1774 p += len;
1775 }
1776#endif /* CONFIG_SME */
1777
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001778 /* Process FT when SME is in the driver */
1779 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1780 wpa_ft_is_completed(wpa_s->wpa)) {
1781 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1782 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1783 data->assoc_info.resp_ies,
1784 data->assoc_info.resp_ies_len,
1785 bssid) < 0) {
1786 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1787 "Reassociation Response failed");
1788 wpa_supplicant_deauthenticate(
1789 wpa_s, WLAN_REASON_INVALID_IE);
1790 return -1;
1791 }
1792 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1793 }
1794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1796 data->assoc_info.resp_ies_len);
1797#endif /* CONFIG_IEEE80211R */
1798
1799 /* WPA/RSN IE from Beacon/ProbeResp */
1800 p = data->assoc_info.beacon_ies;
1801 l = data->assoc_info.beacon_ies_len;
1802
1803 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1804 */
1805 wpa_found = rsn_found = 0;
1806 while (p && l >= 2) {
1807 len = p[1] + 2;
1808 if (len > l) {
1809 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1810 p, l);
1811 break;
1812 }
1813 if (!wpa_found &&
1814 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1815 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1816 wpa_found = 1;
1817 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1818 }
1819
1820 if (!rsn_found &&
1821 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1822 rsn_found = 1;
1823 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1824 }
1825
1826 l -= len;
1827 p += len;
1828 }
1829
1830 if (!wpa_found && data->assoc_info.beacon_ies)
1831 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1832 if (!rsn_found && data->assoc_info.beacon_ies)
1833 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1834 if (wpa_found || rsn_found)
1835 wpa_s->ap_ies_from_associnfo = 1;
1836
Jouni Malinen87fd2792011-05-16 18:35:42 +03001837 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1838 wpa_s->assoc_freq != data->assoc_info.freq) {
1839 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1840 "%u to %u MHz",
1841 wpa_s->assoc_freq, data->assoc_info.freq);
1842 wpa_supplicant_update_scan_results(wpa_s);
1843 }
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 wpa_s->assoc_freq = data->assoc_info.freq;
1846
1847 return 0;
1848}
1849
1850
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001851static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1852{
1853 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1854
1855 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1856 return -1;
1857
1858 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1859 return 0;
1860
1861 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1862 WPA_IE_VENDOR_TYPE);
1863 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1864
1865 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1866 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1867 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1868 bss_rsn ? 2 + bss_rsn[1] : 0))
1869 return -1;
1870
1871 return 0;
1872}
1873
1874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1876 union wpa_event_data *data)
1877{
1878 u8 bssid[ETH_ALEN];
1879 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880
1881#ifdef CONFIG_AP
1882 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001883 if (!data)
1884 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1886 data->assoc_info.addr,
1887 data->assoc_info.req_ies,
1888 data->assoc_info.req_ies_len,
1889 data->assoc_info.reassoc);
1890 return;
1891 }
1892#endif /* CONFIG_AP */
1893
1894 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1895 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1896 return;
1897
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001898 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1899 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001900 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001901 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1902 return;
1903 }
1904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001906 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1908 MACSTR, MAC2STR(bssid));
1909 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1911 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001912 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913
1914 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1915 wpa_clear_keys(wpa_s, bssid);
1916 }
1917 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001918 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1920 return;
1921 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001922
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001923#ifdef ANDROID
1924 if (wpa_s->conf->ap_scan == 1) {
1925#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001926 if (wpa_s->conf->ap_scan == 1 &&
1927 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001928#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001929 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1930 wpa_msg(wpa_s, MSG_WARNING,
1931 "WPA/RSN IEs not updated");
1932 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 }
1934
1935#ifdef CONFIG_SME
1936 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1937 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001938 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939#endif /* CONFIG_SME */
1940
1941 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1942 if (wpa_s->current_ssid) {
1943 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1944 * initialized before association, but for other modes,
1945 * initialize PC/SC here, if the current configuration needs
1946 * smartcard or SIM/USIM. */
1947 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1948 }
1949 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1950 if (wpa_s->l2)
1951 l2_packet_notify_auth_start(wpa_s->l2);
1952
1953 /*
1954 * Set portEnabled first to FALSE in order to get EAP state machine out
1955 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1956 * state machine may transit to AUTHENTICATING state based on obsolete
1957 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1958 * AUTHENTICATED without ever giving chance to EAP state machine to
1959 * reset the state.
1960 */
1961 if (!ft_completed) {
1962 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1963 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1964 }
1965 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1966 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1967 /* 802.1X::portControl = Auto */
1968 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1969 wpa_s->eapol_received = 0;
1970 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1971 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1972 (wpa_s->current_ssid &&
1973 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001974 if (wpa_s->current_ssid &&
1975 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001976 (wpa_s->drv_flags &
1977 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1978 /*
1979 * Set the key after having received joined-IBSS event
1980 * from the driver.
1981 */
1982 wpa_supplicant_set_wpa_none_key(wpa_s,
1983 wpa_s->current_ssid);
1984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 wpa_supplicant_cancel_auth_timeout(wpa_s);
1986 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1987 } else if (!ft_completed) {
1988 /* Timeout for receiving the first EAPOL packet */
1989 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1990 }
1991 wpa_supplicant_cancel_scan(wpa_s);
1992
1993 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1994 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1995 /*
1996 * We are done; the driver will take care of RSN 4-way
1997 * handshake.
1998 */
1999 wpa_supplicant_cancel_auth_timeout(wpa_s);
2000 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2001 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2002 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2003 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2004 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2005 /*
2006 * The driver will take care of RSN 4-way handshake, so we need
2007 * to allow EAPOL supplicant to complete its work without
2008 * waiting for WPA supplicant.
2009 */
2010 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2011 } else if (ft_completed) {
2012 /*
2013 * FT protocol completed - make sure EAPOL state machine ends
2014 * up in authenticated.
2015 */
2016 wpa_supplicant_cancel_auth_timeout(wpa_s);
2017 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2018 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2019 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2020 }
2021
Jouni Malinena05074c2012-12-21 21:35:35 +02002022 wpa_s->last_eapol_matches_bssid = 0;
2023
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002025 struct os_reltime now, age;
2026 os_get_reltime(&now);
2027 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 if (age.sec == 0 && age.usec < 100000 &&
2029 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2030 0) {
2031 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2032 "frame that was received just before "
2033 "association notification");
2034 wpa_supplicant_rx_eapol(
2035 wpa_s, wpa_s->pending_eapol_rx_src,
2036 wpabuf_head(wpa_s->pending_eapol_rx),
2037 wpabuf_len(wpa_s->pending_eapol_rx));
2038 }
2039 wpabuf_free(wpa_s->pending_eapol_rx);
2040 wpa_s->pending_eapol_rx = NULL;
2041 }
2042
2043 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2044 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002045 wpa_s->current_ssid &&
2046 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 /* Set static WEP keys again */
2048 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2049 }
2050
2051#ifdef CONFIG_IBSS_RSN
2052 if (wpa_s->current_ssid &&
2053 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2054 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2055 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2056 wpa_s->ibss_rsn == NULL) {
2057 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2058 if (!wpa_s->ibss_rsn) {
2059 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2060 wpa_supplicant_deauthenticate(
2061 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2062 return;
2063 }
2064
2065 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2066 }
2067#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002068
2069 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070}
2071
2072
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073static int disconnect_reason_recoverable(u16 reason_code)
2074{
2075 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2076 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2077 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2078}
2079
2080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002082 u16 reason_code,
2083 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084{
2085 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002086
2087 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2088 /*
2089 * At least Host AP driver and a Prism3 card seemed to be
2090 * generating streams of disconnected events when configuring
2091 * IBSS for WPA-None. Ignore them for now.
2092 */
2093 return;
2094 }
2095
2096 bssid = wpa_s->bssid;
2097 if (is_zero_ether_addr(bssid))
2098 bssid = wpa_s->pending_bssid;
2099
2100 if (!is_zero_ether_addr(bssid) ||
2101 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2102 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2103 " reason=%d%s",
2104 MAC2STR(bssid), reason_code,
2105 locally_generated ? " locally_generated=1" : "");
2106 }
2107}
2108
2109
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002110static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2111 int locally_generated)
2112{
2113 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2114 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2115 return 0; /* Not in 4-way handshake with PSK */
2116
2117 /*
2118 * It looks like connection was lost while trying to go through PSK
2119 * 4-way handshake. Filter out known disconnection cases that are caused
2120 * by something else than PSK mismatch to avoid confusing reports.
2121 */
2122
2123 if (locally_generated) {
2124 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2125 return 0;
2126 }
2127
2128 return 1;
2129}
2130
2131
Jouni Malinen2b89da82012-08-31 22:04:41 +03002132static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2133 u16 reason_code,
2134 int locally_generated)
2135{
2136 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137 int authenticating;
2138 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002139 struct wpa_bss *fast_reconnect = NULL;
2140 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002141 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142
2143 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2144 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2145
2146 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2147 /*
2148 * At least Host AP driver and a Prism3 card seemed to be
2149 * generating streams of disconnected events when configuring
2150 * IBSS for WPA-None. Ignore them for now.
2151 */
2152 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2153 "IBSS/WPA-None mode");
2154 return;
2155 }
2156
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002157 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2159 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002160 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2161 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002162 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002164 if (!wpa_s->disconnected &&
2165 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002166 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2167 wpas_wps_searching(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002168 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002169 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002170 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002171 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002172 wpa_s->wpa_state);
2173 if (wpa_s->wpa_state == WPA_COMPLETED &&
2174 wpa_s->current_ssid &&
2175 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002176 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002177 disconnect_reason_recoverable(reason_code)) {
2178 /*
2179 * It looks like the AP has dropped association with
2180 * us, but could allow us to get back in. Try to
2181 * reconnect to the same BSS without full scan to save
2182 * time for some common cases.
2183 */
2184 fast_reconnect = wpa_s->current_bss;
2185 fast_reconnect_ssid = wpa_s->current_ssid;
2186 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002187 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002188 else
2189 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2190 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002192 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 "try to re-connect");
2194 wpa_s->reassociate = 0;
2195 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002196 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197 }
2198 bssid = wpa_s->bssid;
2199 if (is_zero_ether_addr(bssid))
2200 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002201 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2202 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002204 if (locally_generated)
2205 wpa_s->disconnect_reason = -reason_code;
2206 else
2207 wpa_s->disconnect_reason = reason_code;
2208 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2210 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 wpa_clear_keys(wpa_s, wpa_s->bssid);
2212 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002213 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 wpa_supplicant_mark_disassoc(wpa_s);
2215
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002216 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002218 wpa_s->current_ssid = last_ssid;
2219 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002220
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002221 if (fast_reconnect &&
2222 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2223 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2224 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2225 fast_reconnect->ssid_len) &&
2226 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227#ifndef CONFIG_NO_SCAN_PROCESSING
2228 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2229 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2230 fast_reconnect_ssid) < 0) {
2231 /* Recover through full scan */
2232 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2233 }
2234#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002235 } else if (fast_reconnect) {
2236 /*
2237 * Could not reconnect to the same BSS due to network being
2238 * disabled. Use a new scan to match the alternative behavior
2239 * above, i.e., to continue automatic reconnection attempt in a
2240 * way that enforces disabled network rules.
2241 */
2242 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002243 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244}
2245
2246
2247#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002248void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249{
2250 struct wpa_supplicant *wpa_s = eloop_ctx;
2251
2252 if (!wpa_s->pending_mic_error_report)
2253 return;
2254
2255 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2256 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2257 wpa_s->pending_mic_error_report = 0;
2258}
2259#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2260
2261
2262static void
2263wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2264 union wpa_event_data *data)
2265{
2266 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002267 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268
2269 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2270 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002271 os_get_reltime(&t);
2272 if ((wpa_s->last_michael_mic_error.sec &&
2273 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274 wpa_s->pending_mic_error_report) {
2275 if (wpa_s->pending_mic_error_report) {
2276 /*
2277 * Send the pending MIC error report immediately since
2278 * we are going to start countermeasures and AP better
2279 * do the same.
2280 */
2281 wpa_sm_key_request(wpa_s->wpa, 1,
2282 wpa_s->pending_mic_error_pairwise);
2283 }
2284
2285 /* Send the new MIC error report immediately since we are going
2286 * to start countermeasures and AP better do the same.
2287 */
2288 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2289
2290 /* initialize countermeasures */
2291 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292
2293 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2296
2297 /*
2298 * Need to wait for completion of request frame. We do not get
2299 * any callback for the message completion, so just wait a
2300 * short while and hope for the best. */
2301 os_sleep(0, 10000);
2302
2303 wpa_drv_set_countermeasures(wpa_s, 1);
2304 wpa_supplicant_deauthenticate(wpa_s,
2305 WLAN_REASON_MICHAEL_MIC_FAILURE);
2306 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2307 wpa_s, NULL);
2308 eloop_register_timeout(60, 0,
2309 wpa_supplicant_stop_countermeasures,
2310 wpa_s, NULL);
2311 /* TODO: mark the AP rejected for 60 second. STA is
2312 * allowed to associate with another AP.. */
2313 } else {
2314#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2315 if (wpa_s->mic_errors_seen) {
2316 /*
2317 * Reduce the effectiveness of Michael MIC error
2318 * reports as a means for attacking against TKIP if
2319 * more than one MIC failure is noticed with the same
2320 * PTK. We delay the transmission of the reports by a
2321 * random time between 0 and 60 seconds in order to
2322 * force the attacker wait 60 seconds before getting
2323 * the information on whether a frame resulted in a MIC
2324 * failure.
2325 */
2326 u8 rval[4];
2327 int sec;
2328
2329 if (os_get_random(rval, sizeof(rval)) < 0)
2330 sec = os_random() % 60;
2331 else
2332 sec = WPA_GET_BE32(rval) % 60;
2333 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2334 "report %d seconds", sec);
2335 wpa_s->pending_mic_error_report = 1;
2336 wpa_s->pending_mic_error_pairwise = pairwise;
2337 eloop_cancel_timeout(
2338 wpa_supplicant_delayed_mic_error_report,
2339 wpa_s, NULL);
2340 eloop_register_timeout(
2341 sec, os_random() % 1000000,
2342 wpa_supplicant_delayed_mic_error_report,
2343 wpa_s, NULL);
2344 } else {
2345 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2346 }
2347#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2348 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2349#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2350 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002351 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 wpa_s->mic_errors_seen++;
2353}
2354
2355
2356#ifdef CONFIG_TERMINATE_ONLASTIF
2357static int any_interfaces(struct wpa_supplicant *head)
2358{
2359 struct wpa_supplicant *wpa_s;
2360
2361 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2362 if (!wpa_s->interface_removed)
2363 return 1;
2364 return 0;
2365}
2366#endif /* CONFIG_TERMINATE_ONLASTIF */
2367
2368
2369static void
2370wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2371 union wpa_event_data *data)
2372{
2373 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2374 return;
2375
2376 switch (data->interface_status.ievent) {
2377 case EVENT_INTERFACE_ADDED:
2378 if (!wpa_s->interface_removed)
2379 break;
2380 wpa_s->interface_removed = 0;
2381 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2382 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2383 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2384 "driver after interface was added");
2385 }
2386 break;
2387 case EVENT_INTERFACE_REMOVED:
2388 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2389 wpa_s->interface_removed = 1;
2390 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 l2_packet_deinit(wpa_s->l2);
2393 wpa_s->l2 = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394#ifdef CONFIG_TERMINATE_ONLASTIF
2395 /* check if last interface */
2396 if (!any_interfaces(wpa_s->global->ifaces))
2397 eloop_terminate();
2398#endif /* CONFIG_TERMINATE_ONLASTIF */
2399 break;
2400 }
2401}
2402
2403
2404#ifdef CONFIG_PEERKEY
2405static void
2406wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2407 union wpa_event_data *data)
2408{
2409 if (data == NULL)
2410 return;
2411 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2412}
2413#endif /* CONFIG_PEERKEY */
2414
2415
2416#ifdef CONFIG_TDLS
2417static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2418 union wpa_event_data *data)
2419{
2420 if (data == NULL)
2421 return;
2422 switch (data->tdls.oper) {
2423 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002424 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2425 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2426 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2427 else
2428 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429 break;
2430 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002431 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2432 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2433 data->tdls.reason_code);
2434 else
2435 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2436 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002437 break;
2438 }
2439}
2440#endif /* CONFIG_TDLS */
2441
2442
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002443#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002444static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2445 union wpa_event_data *data)
2446{
2447 if (data == NULL)
2448 return;
2449 switch (data->wnm.oper) {
2450 case WNM_OPER_SLEEP:
2451 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2452 "(action=%d, intval=%d)",
2453 data->wnm.sleep_action, data->wnm.sleep_intval);
2454 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002455 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002456 break;
2457 }
2458}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002459#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002460
2461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462#ifdef CONFIG_IEEE80211R
2463static void
2464wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2465 union wpa_event_data *data)
2466{
2467 if (data == NULL)
2468 return;
2469
2470 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2471 data->ft_ies.ies_len,
2472 data->ft_ies.ft_action,
2473 data->ft_ies.target_ap,
2474 data->ft_ies.ric_ies,
2475 data->ft_ies.ric_ies_len) < 0) {
2476 /* TODO: prevent MLME/driver from trying to associate? */
2477 }
2478}
2479#endif /* CONFIG_IEEE80211R */
2480
2481
2482#ifdef CONFIG_IBSS_RSN
2483static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2484 union wpa_event_data *data)
2485{
2486 struct wpa_ssid *ssid;
2487 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2488 return;
2489 if (data == NULL)
2490 return;
2491 ssid = wpa_s->current_ssid;
2492 if (ssid == NULL)
2493 return;
2494 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2495 return;
2496
2497 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2498}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002499
2500
2501static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2502 union wpa_event_data *data)
2503{
2504 struct wpa_ssid *ssid = wpa_s->current_ssid;
2505
2506 if (ssid == NULL)
2507 return;
2508
2509 /* check if the ssid is correctly configured as IBSS/RSN */
2510 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2511 return;
2512
2513 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2514 data->rx_mgmt.frame_len);
2515}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516#endif /* CONFIG_IBSS_RSN */
2517
2518
2519#ifdef CONFIG_IEEE80211R
2520static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2521 size_t len)
2522{
2523 const u8 *sta_addr, *target_ap_addr;
2524 u16 status;
2525
2526 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2527 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2528 return; /* only SME case supported for now */
2529 if (len < 1 + 2 * ETH_ALEN + 2)
2530 return;
2531 if (data[0] != 2)
2532 return; /* Only FT Action Response is supported for now */
2533 sta_addr = data + 1;
2534 target_ap_addr = data + 1 + ETH_ALEN;
2535 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2536 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2537 MACSTR " TargetAP " MACSTR " status %u",
2538 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2539
2540 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2541 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2542 " in FT Action Response", MAC2STR(sta_addr));
2543 return;
2544 }
2545
2546 if (status) {
2547 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2548 "failure (status code %d)", status);
2549 /* TODO: report error to FT code(?) */
2550 return;
2551 }
2552
2553 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2554 len - (1 + 2 * ETH_ALEN + 2), 1,
2555 target_ap_addr, NULL, 0) < 0)
2556 return;
2557
2558#ifdef CONFIG_SME
2559 {
2560 struct wpa_bss *bss;
2561 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2562 if (bss)
2563 wpa_s->sme.freq = bss->freq;
2564 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2565 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2566 WLAN_AUTH_FT);
2567 }
2568#endif /* CONFIG_SME */
2569}
2570#endif /* CONFIG_IEEE80211R */
2571
2572
2573static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2574 struct unprot_deauth *e)
2575{
2576#ifdef CONFIG_IEEE80211W
2577 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2578 "dropped: " MACSTR " -> " MACSTR
2579 " (reason code %u)",
2580 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2581 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2582#endif /* CONFIG_IEEE80211W */
2583}
2584
2585
2586static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2587 struct unprot_disassoc *e)
2588{
2589#ifdef CONFIG_IEEE80211W
2590 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2591 "dropped: " MACSTR " -> " MACSTR
2592 " (reason code %u)",
2593 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2594 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2595#endif /* CONFIG_IEEE80211W */
2596}
2597
2598
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002599static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2600 u16 reason_code, int locally_generated,
2601 const u8 *ie, size_t ie_len, int deauth)
2602{
2603#ifdef CONFIG_AP
2604 if (wpa_s->ap_iface && addr) {
2605 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2606 return;
2607 }
2608
2609 if (wpa_s->ap_iface) {
2610 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2611 return;
2612 }
2613#endif /* CONFIG_AP */
2614
2615 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2616
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002617 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2618 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2619 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2620 eapol_sm_failed(wpa_s->eapol))) &&
2621 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002622 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002623
2624#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002625 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002626 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2627 locally_generated) > 0) {
2628 /*
2629 * The interface was removed, so cannot continue
2630 * processing any additional operations after this.
2631 */
2632 return;
2633 }
2634 }
2635#endif /* CONFIG_P2P */
2636
2637 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2638 locally_generated);
2639}
2640
2641
2642static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2643 struct disassoc_info *info)
2644{
2645 u16 reason_code = 0;
2646 int locally_generated = 0;
2647 const u8 *addr = NULL;
2648 const u8 *ie = NULL;
2649 size_t ie_len = 0;
2650
2651 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2652
2653 if (info) {
2654 addr = info->addr;
2655 ie = info->ie;
2656 ie_len = info->ie_len;
2657 reason_code = info->reason_code;
2658 locally_generated = info->locally_generated;
2659 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2660 locally_generated ? " (locally generated)" : "");
2661 if (addr)
2662 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2663 MAC2STR(addr));
2664 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2665 ie, ie_len);
2666 }
2667
2668#ifdef CONFIG_AP
2669 if (wpa_s->ap_iface && info && info->addr) {
2670 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2671 return;
2672 }
2673
2674 if (wpa_s->ap_iface) {
2675 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2676 return;
2677 }
2678#endif /* CONFIG_AP */
2679
2680#ifdef CONFIG_P2P
2681 if (info) {
2682 wpas_p2p_disassoc_notif(
2683 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2684 locally_generated);
2685 }
2686#endif /* CONFIG_P2P */
2687
2688 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2689 sme_event_disassoc(wpa_s, info);
2690
2691 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2692 ie, ie_len, 0);
2693}
2694
2695
2696static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2697 struct deauth_info *info)
2698{
2699 u16 reason_code = 0;
2700 int locally_generated = 0;
2701 const u8 *addr = NULL;
2702 const u8 *ie = NULL;
2703 size_t ie_len = 0;
2704
2705 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2706
2707 if (info) {
2708 addr = info->addr;
2709 ie = info->ie;
2710 ie_len = info->ie_len;
2711 reason_code = info->reason_code;
2712 locally_generated = info->locally_generated;
2713 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2714 reason_code,
2715 locally_generated ? " (locally generated)" : "");
2716 if (addr) {
2717 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2718 MAC2STR(addr));
2719 }
2720 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2721 ie, ie_len);
2722 }
2723
2724 wpa_reset_ft_completed(wpa_s->wpa);
2725
2726 wpas_event_disconnect(wpa_s, addr, reason_code,
2727 locally_generated, ie, ie_len, 1);
2728}
2729
2730
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002731static const char * reg_init_str(enum reg_change_initiator init)
2732{
2733 switch (init) {
2734 case REGDOM_SET_BY_CORE:
2735 return "CORE";
2736 case REGDOM_SET_BY_USER:
2737 return "USER";
2738 case REGDOM_SET_BY_DRIVER:
2739 return "DRIVER";
2740 case REGDOM_SET_BY_COUNTRY_IE:
2741 return "COUNTRY_IE";
2742 case REGDOM_BEACON_HINT:
2743 return "BEACON_HINT";
2744 }
2745 return "?";
2746}
2747
2748
2749static const char * reg_type_str(enum reg_type type)
2750{
2751 switch (type) {
2752 case REGDOM_TYPE_UNKNOWN:
2753 return "UNKNOWN";
2754 case REGDOM_TYPE_COUNTRY:
2755 return "COUNTRY";
2756 case REGDOM_TYPE_WORLD:
2757 return "WORLD";
2758 case REGDOM_TYPE_CUSTOM_WORLD:
2759 return "CUSTOM_WORLD";
2760 case REGDOM_TYPE_INTERSECTION:
2761 return "INTERSECTION";
2762 }
2763 return "?";
2764}
2765
2766
2767static void wpa_supplicant_update_channel_list(
2768 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002769{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002770 struct wpa_supplicant *ifs;
2771
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002772 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002773 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002774 info->alpha2[0] ? " alpha2=" : "",
2775 info->alpha2[0] ? info->alpha2 : "");
2776
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002777 if (wpa_s->drv_priv == NULL)
2778 return; /* Ignore event during drv initialization */
2779
2780 free_hw_features(wpa_s);
2781 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2782 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2783
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002784 wpas_p2p_update_channel_list(wpa_s);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002785
2786 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002787 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002788 * so, they get updated with this same hw mode info.
2789 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002790 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2791 radio_list) {
2792 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002793 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2794 ifs->ifname);
2795 free_hw_features(ifs);
2796 ifs->hw.modes = wpa_drv_get_hw_feature_data(
2797 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2798 }
2799 }
2800}
2801
2802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002803static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002804 const u8 *frame, size_t len, int freq)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002805{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002806 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002807 const u8 *payload;
2808 size_t plen;
2809 u8 category;
2810
2811 if (len < IEEE80211_HDRLEN + 2)
2812 return;
2813
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002814 mgmt = (const struct ieee80211_mgmt *) frame;
2815 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002816 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002817 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002818
2819 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2820 " Category=%u DataLen=%d freq=%d MHz",
2821 MAC2STR(mgmt->sa), category, (int) plen, freq);
2822
2823#ifdef CONFIG_IEEE80211R
2824 if (category == WLAN_ACTION_FT) {
2825 ft_rx_action(wpa_s, payload, plen);
2826 return;
2827 }
2828#endif /* CONFIG_IEEE80211R */
2829
2830#ifdef CONFIG_IEEE80211W
2831#ifdef CONFIG_SME
2832 if (category == WLAN_ACTION_SA_QUERY) {
2833 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2834 return;
2835 }
2836#endif /* CONFIG_SME */
2837#endif /* CONFIG_IEEE80211W */
2838
2839#ifdef CONFIG_WNM
2840 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2841 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2842 return;
2843 }
2844#endif /* CONFIG_WNM */
2845
2846#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08002847 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2848 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002849 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08002850 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002851 payload, plen, freq) == 0)
2852 return;
2853#endif /* CONFIG_GAS */
2854
2855#ifdef CONFIG_TDLS
2856 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2857 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2858 wpa_dbg(wpa_s, MSG_DEBUG,
2859 "TDLS: Received Discovery Response from " MACSTR,
2860 MAC2STR(mgmt->sa));
2861 return;
2862 }
2863#endif /* CONFIG_TDLS */
2864
2865#ifdef CONFIG_INTERWORKING
2866 if (category == WLAN_ACTION_QOS && plen >= 1 &&
2867 payload[0] == QOS_QOS_MAP_CONFIG) {
2868 const u8 *pos = payload + 1;
2869 size_t qlen = plen - 1;
2870 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2871 MACSTR, MAC2STR(mgmt->sa));
2872 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2873 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2874 pos[1] <= qlen - 2 && pos[1] >= 16)
2875 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2876 return;
2877 }
2878#endif /* CONFIG_INTERWORKING */
2879
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002880 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2881 category, payload, plen, freq);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002882}
2883
2884
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002885static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2886 union wpa_event_data *event)
2887{
2888#ifdef CONFIG_P2P
2889 struct wpa_supplicant *ifs;
2890#endif /* CONFIG_P2P */
2891 struct wpa_freq_range_list *list;
2892 char *str = NULL;
2893
2894 list = &event->freq_range;
2895
2896 if (list->num)
2897 str = freq_range_list_str(list);
2898 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2899 str ? str : "");
2900
2901#ifdef CONFIG_P2P
2902 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2903 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2904 __func__);
2905 } else {
2906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2907 wpas_p2p_update_channel_list(wpa_s);
2908 }
2909
2910 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2911 int freq;
2912 if (!ifs->current_ssid ||
2913 !ifs->current_ssid->p2p_group ||
2914 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2915 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2916 continue;
2917
2918 freq = ifs->current_ssid->frequency;
2919 if (!freq_range_list_includes(list, freq)) {
2920 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2921 freq);
2922 continue;
2923 }
2924
2925 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2926 freq);
2927 /* TODO: Consider using CSA or removing the group within
2928 * wpa_supplicant */
2929 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2930 }
2931#endif /* CONFIG_P2P */
2932
2933 os_free(str);
2934}
2935
2936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2938 union wpa_event_data *data)
2939{
2940 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941
2942 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2943 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002944 event != EVENT_INTERFACE_STATUS &&
2945 event != EVENT_SCHED_SCAN_STOPPED) {
2946 wpa_dbg(wpa_s, MSG_DEBUG,
2947 "Ignore event %s (%d) while interface is disabled",
2948 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949 return;
2950 }
2951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952#ifndef CONFIG_NO_STDOUT_DEBUG
2953{
2954 int level = MSG_DEBUG;
2955
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002957 const struct ieee80211_hdr *hdr;
2958 u16 fc;
2959 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2960 fc = le_to_host16(hdr->frame_control);
2961 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2962 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2963 level = MSG_EXCESSIVE;
2964 }
2965
2966 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2967 event_to_string(event), event);
2968}
2969#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970
2971 switch (event) {
2972 case EVENT_AUTH:
2973 sme_event_auth(wpa_s, data);
2974 break;
2975 case EVENT_ASSOC:
2976 wpa_supplicant_event_assoc(wpa_s, data);
2977 break;
2978 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002979 wpas_event_disassoc(wpa_s,
2980 data ? &data->disassoc_info : NULL);
2981 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002983 wpas_event_deauth(wpa_s,
2984 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985 break;
2986 case EVENT_MICHAEL_MIC_FAILURE:
2987 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2988 break;
2989#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002990 case EVENT_SCAN_STARTED:
2991 os_get_reltime(&wpa_s->scan_start_time);
2992 if (wpa_s->own_scan_requested) {
2993 struct os_reltime diff;
2994
2995 os_reltime_sub(&wpa_s->scan_start_time,
2996 &wpa_s->scan_trigger_time, &diff);
2997 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2998 diff.sec, diff.usec);
2999 wpa_s->own_scan_requested = 0;
3000 wpa_s->own_scan_running = 1;
3001 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3002 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003003 wpa_msg_ctrl(wpa_s, MSG_INFO,
3004 WPA_EVENT_SCAN_STARTED "id=%u",
3005 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003006 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003007 wpa_msg_ctrl(wpa_s, MSG_INFO,
3008 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003009 }
3010 } else {
3011 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3012 wpa_s->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003013 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003014 }
3015 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003017 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3018 struct os_reltime now, diff;
3019 os_get_reltime(&now);
3020 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3021 wpa_s->scan_start_time.sec = 0;
3022 wpa_s->scan_start_time.usec = 0;
3023 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3024 diff.sec, diff.usec);
3025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003027 wpa_s->own_scan_running = 0;
3028 wpa_s->external_scan_running = 0;
3029 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 break;
3031#endif /* CONFIG_NO_SCAN_PROCESSING */
3032 case EVENT_ASSOCINFO:
3033 wpa_supplicant_event_associnfo(wpa_s, data);
3034 break;
3035 case EVENT_INTERFACE_STATUS:
3036 wpa_supplicant_event_interface_status(wpa_s, data);
3037 break;
3038 case EVENT_PMKID_CANDIDATE:
3039 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3040 break;
3041#ifdef CONFIG_PEERKEY
3042 case EVENT_STKSTART:
3043 wpa_supplicant_event_stkstart(wpa_s, data);
3044 break;
3045#endif /* CONFIG_PEERKEY */
3046#ifdef CONFIG_TDLS
3047 case EVENT_TDLS:
3048 wpa_supplicant_event_tdls(wpa_s, data);
3049 break;
3050#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003051#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003052 case EVENT_WNM:
3053 wpa_supplicant_event_wnm(wpa_s, data);
3054 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003055#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056#ifdef CONFIG_IEEE80211R
3057 case EVENT_FT_RESPONSE:
3058 wpa_supplicant_event_ft_response(wpa_s, data);
3059 break;
3060#endif /* CONFIG_IEEE80211R */
3061#ifdef CONFIG_IBSS_RSN
3062 case EVENT_IBSS_RSN_START:
3063 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3064 break;
3065#endif /* CONFIG_IBSS_RSN */
3066 case EVENT_ASSOC_REJECT:
3067 if (data->assoc_reject.bssid)
3068 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3069 "bssid=" MACSTR " status_code=%u",
3070 MAC2STR(data->assoc_reject.bssid),
3071 data->assoc_reject.status_code);
3072 else
3073 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3074 "status_code=%u",
3075 data->assoc_reject.status_code);
3076 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3077 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003078 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003079 const u8 *bssid = data->assoc_reject.bssid;
3080 if (bssid == NULL || is_zero_ether_addr(bssid))
3081 bssid = wpa_s->pending_bssid;
3082 wpas_connection_failed(wpa_s, bssid);
3083 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003084 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003085 break;
3086 case EVENT_AUTH_TIMED_OUT:
3087 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3088 sme_event_auth_timed_out(wpa_s, data);
3089 break;
3090 case EVENT_ASSOC_TIMED_OUT:
3091 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3092 sme_event_assoc_timed_out(wpa_s, data);
3093 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 case EVENT_TX_STATUS:
3095 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3096 " type=%d stype=%d",
3097 MAC2STR(data->tx_status.dst),
3098 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003099#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003101#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3103 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003104 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 wpa_s, data->tx_status.dst,
3106 data->tx_status.data,
3107 data->tx_status.data_len,
3108 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003109 OFFCHANNEL_SEND_ACTION_SUCCESS :
3110 OFFCHANNEL_SEND_ACTION_NO_ACK);
3111#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 break;
3113 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114#endif /* CONFIG_AP */
3115#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3117 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3118 /*
3119 * Catch TX status events for Action frames we sent via group
3120 * interface in GO mode.
3121 */
3122 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3123 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3124 os_memcmp(wpa_s->parent->pending_action_dst,
3125 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003126 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 wpa_s->parent, data->tx_status.dst,
3128 data->tx_status.data,
3129 data->tx_status.data_len,
3130 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003131 OFFCHANNEL_SEND_ACTION_SUCCESS :
3132 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 break;
3134 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003135#endif /* CONFIG_OFFCHANNEL */
3136#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 switch (data->tx_status.type) {
3138 case WLAN_FC_TYPE_MGMT:
3139 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3140 data->tx_status.data_len,
3141 data->tx_status.stype,
3142 data->tx_status.ack);
3143 break;
3144 case WLAN_FC_TYPE_DATA:
3145 ap_tx_status(wpa_s, data->tx_status.dst,
3146 data->tx_status.data,
3147 data->tx_status.data_len,
3148 data->tx_status.ack);
3149 break;
3150 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003151#endif /* CONFIG_AP */
3152 break;
3153#ifdef CONFIG_AP
3154 case EVENT_EAPOL_TX_STATUS:
3155 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3156 data->eapol_tx_status.data,
3157 data->eapol_tx_status.data_len,
3158 data->eapol_tx_status.ack);
3159 break;
3160 case EVENT_DRIVER_CLIENT_POLL_OK:
3161 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003162 break;
3163 case EVENT_RX_FROM_UNKNOWN:
3164 if (wpa_s->ap_iface == NULL)
3165 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003166 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3167 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003169 case EVENT_CH_SWITCH:
3170 if (!data)
3171 break;
3172 if (!wpa_s->ap_iface) {
3173 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3174 "event in non-AP mode");
3175 break;
3176 }
3177
Dmitry Shmidt04949592012-07-19 12:16:46 -07003178 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3179 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003180 data->ch_switch.ch_offset,
3181 data->ch_switch.ch_width,
3182 data->ch_switch.cf1,
3183 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003184 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003185#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003186 case EVENT_RX_MGMT: {
3187 u16 fc, stype;
3188 const struct ieee80211_mgmt *mgmt;
3189
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003190#ifdef CONFIG_TESTING_OPTIONS
3191 if (wpa_s->ext_mgmt_frame_handling) {
3192 struct rx_mgmt *rx = &data->rx_mgmt;
3193 size_t hex_len = 2 * rx->frame_len + 1;
3194 char *hex = os_malloc(hex_len);
3195 if (hex) {
3196 wpa_snprintf_hex(hex, hex_len,
3197 rx->frame, rx->frame_len);
3198 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3199 rx->freq, rx->datarate, rx->ssi_signal,
3200 hex);
3201 os_free(hex);
3202 }
3203 break;
3204 }
3205#endif /* CONFIG_TESTING_OPTIONS */
3206
Dmitry Shmidt04949592012-07-19 12:16:46 -07003207 mgmt = (const struct ieee80211_mgmt *)
3208 data->rx_mgmt.frame;
3209 fc = le_to_host16(mgmt->frame_control);
3210 stype = WLAN_FC_GET_STYPE(fc);
3211
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003212#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003214#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3217 data->rx_mgmt.frame_len > 24) {
3218 const u8 *src = mgmt->sa;
3219 const u8 *ie = mgmt->u.probe_req.variable;
3220 size_t ie_len = data->rx_mgmt.frame_len -
3221 (mgmt->u.probe_req.variable -
3222 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003223 wpas_p2p_probe_req_rx(
3224 wpa_s, src, mgmt->da,
3225 mgmt->bssid, ie, ie_len,
3226 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227 break;
3228 }
3229#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003230#ifdef CONFIG_IBSS_RSN
3231 if (stype == WLAN_FC_STYPE_AUTH &&
3232 data->rx_mgmt.frame_len >= 30) {
3233 wpa_supplicant_event_ibss_auth(wpa_s, data);
3234 break;
3235 }
3236#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003237
3238 if (stype == WLAN_FC_STYPE_ACTION) {
3239 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003240 wpa_s, data->rx_mgmt.frame,
3241 data->rx_mgmt.frame_len,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003242 data->rx_mgmt.freq);
3243 break;
3244 }
3245
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3247 "management frame in non-AP mode");
3248 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003249#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003251
3252 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3253 data->rx_mgmt.frame_len > 24) {
3254 const u8 *ie = mgmt->u.probe_req.variable;
3255 size_t ie_len = data->rx_mgmt.frame_len -
3256 (mgmt->u.probe_req.variable -
3257 data->rx_mgmt.frame);
3258
3259 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3260 mgmt->bssid, ie, ie_len,
3261 data->rx_mgmt.ssi_signal);
3262 }
3263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003264 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003265#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 case EVENT_RX_PROBE_REQ:
3269 if (data->rx_probe_req.sa == NULL ||
3270 data->rx_probe_req.ie == NULL)
3271 break;
3272#ifdef CONFIG_AP
3273 if (wpa_s->ap_iface) {
3274 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3275 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003276 data->rx_probe_req.da,
3277 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003279 data->rx_probe_req.ie_len,
3280 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281 break;
3282 }
3283#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003285 data->rx_probe_req.da,
3286 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003288 data->rx_probe_req.ie_len,
3289 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003292#ifdef CONFIG_OFFCHANNEL
3293 offchannel_remain_on_channel_cb(
3294 wpa_s, data->remain_on_channel.freq,
3295 data->remain_on_channel.duration);
3296#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 wpas_p2p_remain_on_channel_cb(
3298 wpa_s, data->remain_on_channel.freq,
3299 data->remain_on_channel.duration);
3300 break;
3301 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003302#ifdef CONFIG_OFFCHANNEL
3303 offchannel_cancel_remain_on_channel_cb(
3304 wpa_s, data->remain_on_channel.freq);
3305#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003306 wpas_p2p_cancel_remain_on_channel_cb(
3307 wpa_s, data->remain_on_channel.freq);
3308 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 case EVENT_EAPOL_RX:
3310 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3311 data->eapol_rx.data,
3312 data->eapol_rx.data_len);
3313 break;
3314 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003315 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3316 "above=%d signal=%d noise=%d txrate=%d",
3317 data->signal_change.above_threshold,
3318 data->signal_change.current_signal,
3319 data->signal_change.current_noise,
3320 data->signal_change.current_txrate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 bgscan_notify_signal_change(
3322 wpa_s, data->signal_change.above_threshold,
3323 data->signal_change.current_signal,
3324 data->signal_change.current_noise,
3325 data->signal_change.current_txrate);
3326 break;
3327 case EVENT_INTERFACE_ENABLED:
3328 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3329 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003330 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003331 if (wpa_s->p2p_mgmt) {
3332 wpa_supplicant_set_state(wpa_s,
3333 WPA_DISCONNECTED);
3334 break;
3335 }
3336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337#ifdef CONFIG_AP
3338 if (!wpa_s->ap_iface) {
3339 wpa_supplicant_set_state(wpa_s,
3340 WPA_DISCONNECTED);
3341 wpa_supplicant_req_scan(wpa_s, 0, 0);
3342 } else
3343 wpa_supplicant_set_state(wpa_s,
3344 WPA_COMPLETED);
3345#else /* CONFIG_AP */
3346 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3347 wpa_supplicant_req_scan(wpa_s, 0, 0);
3348#endif /* CONFIG_AP */
3349 }
3350 break;
3351 case EVENT_INTERFACE_DISABLED:
3352 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003353#ifdef CONFIG_P2P
3354 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3355 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3356 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3357 /*
3358 * The interface was externally disabled. Remove
3359 * it assuming an external entity will start a
3360 * new session if needed.
3361 */
3362 wpas_p2p_disconnect(wpa_s);
3363 break;
3364 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003365 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3366 p2p_in_progress(wpa_s->global->p2p) > 1) {
3367 /* This radio work will be cancelled, so clear P2P
3368 * state as well.
3369 */
3370 p2p_stop_find(wpa_s->global->p2p);
3371 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003372#endif /* CONFIG_P2P */
3373
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003374 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3375 /*
3376 * Indicate disconnection to keep ctrl_iface events
3377 * consistent.
3378 */
3379 wpa_supplicant_event_disassoc(
3380 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3381 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003383 radio_remove_works(wpa_s, NULL, 0);
3384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3386 break;
3387 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003388 wpa_supplicant_update_channel_list(
3389 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 break;
3391 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003392 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 break;
3394 case EVENT_BEST_CHANNEL:
3395 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3396 "(%d %d %d)",
3397 data->best_chan.freq_24, data->best_chan.freq_5,
3398 data->best_chan.freq_overall);
3399 wpa_s->best_24_freq = data->best_chan.freq_24;
3400 wpa_s->best_5_freq = data->best_chan.freq_5;
3401 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3403 data->best_chan.freq_5,
3404 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 break;
3406 case EVENT_UNPROT_DEAUTH:
3407 wpa_supplicant_event_unprot_deauth(wpa_s,
3408 &data->unprot_deauth);
3409 break;
3410 case EVENT_UNPROT_DISASSOC:
3411 wpa_supplicant_event_unprot_disassoc(wpa_s,
3412 &data->unprot_disassoc);
3413 break;
3414 case EVENT_STATION_LOW_ACK:
3415#ifdef CONFIG_AP
3416 if (wpa_s->ap_iface && data)
3417 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3418 data->low_ack.addr);
3419#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003420#ifdef CONFIG_TDLS
3421 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003422 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3423 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003424#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003425 break;
3426 case EVENT_IBSS_PEER_LOST:
3427#ifdef CONFIG_IBSS_RSN
3428 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3429#endif /* CONFIG_IBSS_RSN */
3430 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003431 case EVENT_DRIVER_GTK_REKEY:
3432 if (os_memcmp(data->driver_gtk_rekey.bssid,
3433 wpa_s->bssid, ETH_ALEN))
3434 break;
3435 if (!wpa_s->wpa)
3436 break;
3437 wpa_sm_update_replay_ctr(wpa_s->wpa,
3438 data->driver_gtk_rekey.replay_ctr);
3439 break;
3440 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003441 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003442 wpa_s->sched_scanning = 0;
3443 wpa_supplicant_notify_scanning(wpa_s, 0);
3444
3445 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3446 break;
3447
3448 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003449 * Start a new sched scan to continue searching for more SSIDs
3450 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003451 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07003452 if (wpa_s->sched_scan_timed_out) {
3453 wpa_supplicant_req_sched_scan(wpa_s);
3454 } else if (wpa_s->pno_sched_pending) {
3455 wpa_s->pno_sched_pending = 0;
3456 wpas_start_pno(wpa_s);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003457 }
3458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003459 break;
3460 case EVENT_WPS_BUTTON_PUSHED:
3461#ifdef CONFIG_WPS
3462 wpas_wps_start_pbc(wpa_s, NULL, 0);
3463#endif /* CONFIG_WPS */
3464 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003465 case EVENT_AVOID_FREQUENCIES:
3466 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3467 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003468 case EVENT_CONNECT_FAILED_REASON:
3469#ifdef CONFIG_AP
3470 if (!wpa_s->ap_iface || !data)
3471 break;
3472 hostapd_event_connect_failed_reason(
3473 wpa_s->ap_iface->bss[0],
3474 data->connect_failed_reason.addr,
3475 data->connect_failed_reason.code);
3476#endif /* CONFIG_AP */
3477 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478 default:
3479 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3480 break;
3481 }
3482}