blob: 2dbc39ffba9219206a59136c04e68370716da22e [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, 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"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080045#include "mesh.h"
46#include "mesh_mpm.h"
47#include "wmm_ac.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070048
49
Dmitry Shmidt34af3062013-07-11 10:46:32 -070050#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070051static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080052 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070053#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080054
55
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070056static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
57 struct wpa_ssid *ssid)
58{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080059 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070060
61 if (ssid == NULL || ssid->disabled_until.sec == 0)
62 return 0;
63
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080064 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070065 if (ssid->disabled_until.sec > now.sec)
66 return ssid->disabled_until.sec - now.sec;
67
68 wpas_clear_temp_disabled(wpa_s, ssid, 0);
69
70 return 0;
71}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
73
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070074/**
75 * wpas_reenabled_network_time - Time until first network is re-enabled
76 * @wpa_s: Pointer to wpa_supplicant data
77 * Returns: If all enabled networks are temporarily disabled, returns the time
78 * (in sec) until the first network is re-enabled. Otherwise returns 0.
79 *
80 * This function is used in case all enabled networks are temporarily disabled,
81 * in which case it returns the time (in sec) that the first network will be
82 * re-enabled. The function assumes that at least one network is enabled.
83 */
84static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
85{
86 struct wpa_ssid *ssid;
87 int disabled_for, res = 0;
88
89#ifdef CONFIG_INTERWORKING
90 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
91 wpa_s->conf->cred)
92 return 0;
93#endif /* CONFIG_INTERWORKING */
94
95 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
96 if (ssid->disabled)
97 continue;
98
99 disabled_for = wpas_temp_disabled(wpa_s, ssid);
100 if (!disabled_for)
101 return 0;
102
103 if (!res || disabled_for < res)
104 res = disabled_for;
105 }
106
107 return res;
108}
109
110
111void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
112{
113 struct wpa_supplicant *wpa_s = eloop_ctx;
114
115 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
116 return;
117
118 wpa_dbg(wpa_s, MSG_DEBUG,
119 "Try to associate due to network getting re-enabled");
120 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
121 wpa_supplicant_cancel_sched_scan(wpa_s);
122 wpa_supplicant_req_scan(wpa_s, 0, 0);
123 }
124}
125
126
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800127static struct wpa_bss * wpa_supplicant_get_new_bss(
128 struct wpa_supplicant *wpa_s, const u8 *bssid)
129{
130 struct wpa_bss *bss = NULL;
131 struct wpa_ssid *ssid = wpa_s->current_ssid;
132
133 if (ssid->ssid_len > 0)
134 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
135 if (!bss)
136 bss = wpa_bss_get_bssid(wpa_s, bssid);
137
138 return bss;
139}
140
141
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700142static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
143{
144 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
145
146 if (!bss) {
147 wpa_supplicant_update_scan_results(wpa_s);
148
149 /* Get the BSS from the new scan results */
150 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
151 }
152
153 if (bss)
154 wpa_s->current_bss = bss;
155}
156
157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
159{
160 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700161 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700162 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700163 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700165 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
166 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700167
168 if (wpa_s->current_ssid->ssid_len == 0)
169 return 0; /* current profile still in use */
170 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
171 if (res < 0) {
172 wpa_msg(wpa_s, MSG_INFO,
173 "Failed to read SSID from driver");
174 return 0; /* try to use current profile */
175 }
176 drv_ssid_len = res;
177
178 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
179 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
180 drv_ssid_len) == 0)
181 return 0; /* current profile still in use */
182
183 wpa_msg(wpa_s, MSG_DEBUG,
184 "Driver-initiated BSS selection changed the SSID to %s",
185 wpa_ssid_txt(drv_ssid, drv_ssid_len));
186 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188
189 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
190 "information");
191 ssid = wpa_supplicant_get_ssid(wpa_s);
192 if (ssid == NULL) {
193 wpa_msg(wpa_s, MSG_INFO,
194 "No network configuration found for the current AP");
195 return -1;
196 }
197
Dmitry Shmidt04949592012-07-19 12:16:46 -0700198 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
200 return -1;
201 }
202
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800203 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
204 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
205 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
206 return -1;
207 }
208
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700209 res = wpas_temp_disabled(wpa_s, ssid);
210 if (res > 0) {
211 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
212 "disabled for %d second(s)", res);
213 return -1;
214 }
215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
217 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800218 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 u8 wpa_ie[80];
220 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800221 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
222 wpa_ie, &wpa_ie_len) < 0)
223 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224 } else {
225 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
226 }
227
228 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
229 eapol_sm_invalidate_cached_session(wpa_s->eapol);
230 old_ssid = wpa_s->current_ssid;
231 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800232
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700233 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
236 wpa_supplicant_initiate_eapol(wpa_s);
237 if (old_ssid != wpa_s->current_ssid)
238 wpas_notify_network_changed(wpa_s);
239
240 return 0;
241}
242
243
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800244void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245{
246 struct wpa_supplicant *wpa_s = eloop_ctx;
247
248 if (wpa_s->countermeasures) {
249 wpa_s->countermeasures = 0;
250 wpa_drv_set_countermeasures(wpa_s, 0);
251 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800252
253 /*
254 * It is possible that the device is sched scanning, which means
255 * that a connection attempt will be done only when we receive
256 * scan results. However, in this case, it would be preferable
257 * to scan and connect immediately, so cancel the sched_scan and
258 * issue a regular scan flow.
259 */
260 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 wpa_supplicant_req_scan(wpa_s, 0, 0);
262 }
263}
264
265
266void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
267{
268 int bssid_changed;
269
Dmitry Shmidt04949592012-07-19 12:16:46 -0700270 wnm_bss_keep_alive_deinit(wpa_s);
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272#ifdef CONFIG_IBSS_RSN
273 ibss_rsn_deinit(wpa_s->ibss_rsn);
274 wpa_s->ibss_rsn = NULL;
275#endif /* CONFIG_IBSS_RSN */
276
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700277#ifdef CONFIG_AP
278 wpa_supplicant_ap_deinit(wpa_s);
279#endif /* CONFIG_AP */
280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
282 return;
283
284 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
285 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
286 os_memset(wpa_s->bssid, 0, ETH_ALEN);
287 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800288 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800289#ifdef CONFIG_P2P
290 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
291#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 wpa_s->current_bss = NULL;
293 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 if (bssid_changed)
296 wpas_notify_bssid_changed(wpa_s);
297
298 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
299 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
300 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
301 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
302 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700304 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700305 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800306
307 wpas_rrm_reset(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308}
309
310
311static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
312{
313 struct wpa_ie_data ie;
314 int pmksa_set = -1;
315 size_t i;
316
317 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
318 ie.pmkid == NULL)
319 return;
320
321 for (i = 0; i < ie.num_pmkid; i++) {
322 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
323 ie.pmkid + i * PMKID_LEN,
324 NULL, NULL, 0);
325 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800326 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 break;
328 }
329 }
330
331 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
332 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
333}
334
335
336static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
337 union wpa_event_data *data)
338{
339 if (data == NULL) {
340 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
341 "event");
342 return;
343 }
344 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
345 " index=%d preauth=%d",
346 MAC2STR(data->pmkid_candidate.bssid),
347 data->pmkid_candidate.index,
348 data->pmkid_candidate.preauth);
349
350 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
351 data->pmkid_candidate.index,
352 data->pmkid_candidate.preauth);
353}
354
355
356static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
357{
358 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
359 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
360 return 0;
361
362#ifdef IEEE8021X_EAPOL
363 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
364 wpa_s->current_ssid &&
365 !(wpa_s->current_ssid->eapol_flags &
366 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
367 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
368 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
369 * plaintext or static WEP keys). */
370 return 0;
371 }
372#endif /* IEEE8021X_EAPOL */
373
374 return 1;
375}
376
377
378/**
379 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
380 * @wpa_s: pointer to wpa_supplicant data
381 * @ssid: Configuration data for the network
382 * Returns: 0 on success, -1 on failure
383 *
384 * This function is called when starting authentication with a network that is
385 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
386 */
387int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
388 struct wpa_ssid *ssid)
389{
390#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700392 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700393
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700394 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
395 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 return 0;
397
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700398 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 sim = 1;
400 aka = 1;
401 } else {
402 struct eap_method_type *eap = ssid->eap.eap_methods;
403 while (eap->vendor != EAP_VENDOR_IETF ||
404 eap->method != EAP_TYPE_NONE) {
405 if (eap->vendor == EAP_VENDOR_IETF) {
406 if (eap->method == EAP_TYPE_SIM)
407 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700408 else if (eap->method == EAP_TYPE_AKA ||
409 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 aka = 1;
411 }
412 eap++;
413 }
414 }
415
416 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
417 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700418 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
419 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
420 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 aka = 0;
422
423 if (!sim && !aka) {
424 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
425 "use SIM, but neither EAP-SIM nor EAP-AKA are "
426 "enabled");
427 return 0;
428 }
429
430 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
431 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700433 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 if (wpa_s->scard == NULL) {
435 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
436 "(pcsc-lite)");
437 return -1;
438 }
439 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
440 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800441#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442#endif /* IEEE8021X_EAPOL */
443
444 return 0;
445}
446
447
448#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700449
450static int has_wep_key(struct wpa_ssid *ssid)
451{
452 int i;
453
454 for (i = 0; i < NUM_WEP_KEYS; i++) {
455 if (ssid->wep_key_len[i])
456 return 1;
457 }
458
459 return 0;
460}
461
462
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700463static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 struct wpa_ssid *ssid)
465{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700466 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467
468 if (ssid->mixed_cell)
469 return 1;
470
471#ifdef CONFIG_WPS
472 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
473 return 1;
474#endif /* CONFIG_WPS */
475
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700476 if (has_wep_key(ssid))
477 privacy = 1;
478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479#ifdef IEEE8021X_EAPOL
480 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
481 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
482 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
483 privacy = 1;
484#endif /* IEEE8021X_EAPOL */
485
Jouni Malinen75ecf522011-06-27 15:19:46 -0700486 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
487 privacy = 1;
488
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800489 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
490 privacy = 1;
491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 if (bss->caps & IEEE80211_CAP_PRIVACY)
493 return privacy;
494 return !privacy;
495}
496
497
498static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
499 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700500 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501{
502 struct wpa_ie_data ie;
503 int proto_match = 0;
504 const u8 *rsn_ie, *wpa_ie;
505 int ret;
506 int wep_ok;
507
508 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
509 if (ret >= 0)
510 return ret;
511
512 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
513 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
514 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
515 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
516 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
517
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700518 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
520 proto_match++;
521
522 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
523 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
524 "failed");
525 break;
526 }
527
528 if (wep_ok &&
529 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
530 {
531 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
532 "in RSN IE");
533 return 1;
534 }
535
536 if (!(ie.proto & ssid->proto)) {
537 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
538 "mismatch");
539 break;
540 }
541
542 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
543 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
544 "cipher mismatch");
545 break;
546 }
547
548 if (!(ie.group_cipher & ssid->group_cipher)) {
549 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
550 "cipher mismatch");
551 break;
552 }
553
554 if (!(ie.key_mgmt & ssid->key_mgmt)) {
555 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
556 "mismatch");
557 break;
558 }
559
560#ifdef CONFIG_IEEE80211W
561 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800562 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800563 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
565 "frame protection");
566 break;
567 }
568#endif /* CONFIG_IEEE80211W */
569
570 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
571 return 1;
572 }
573
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700574 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
576 proto_match++;
577
578 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
579 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
580 "failed");
581 break;
582 }
583
584 if (wep_ok &&
585 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
586 {
587 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
588 "in WPA IE");
589 return 1;
590 }
591
592 if (!(ie.proto & ssid->proto)) {
593 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
594 "mismatch");
595 break;
596 }
597
598 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
599 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
600 "cipher mismatch");
601 break;
602 }
603
604 if (!(ie.group_cipher & ssid->group_cipher)) {
605 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
606 "cipher mismatch");
607 break;
608 }
609
610 if (!(ie.key_mgmt & ssid->key_mgmt)) {
611 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
612 "mismatch");
613 break;
614 }
615
616 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
617 return 1;
618 }
619
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700620 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
621 !rsn_ie) {
622 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
623 return 1;
624 }
625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
627 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
628 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
629 return 0;
630 }
631
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800632 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
633 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
634 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
635 return 1;
636 }
637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
639 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
640 return 1;
641 }
642
643 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
644 "WPA/WPA2");
645
646 return 0;
647}
648
649
650static int freq_allowed(int *freqs, int freq)
651{
652 int i;
653
654 if (freqs == NULL)
655 return 1;
656
657 for (i = 0; freqs[i]; i++)
658 if (freqs[i] == freq)
659 return 1;
660 return 0;
661}
662
663
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700664static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800665{
666 const struct hostapd_hw_modes *mode = NULL, *modes;
667 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
668 const u8 *rate_ie;
669 int i, j, k;
670
671 if (bss->freq == 0)
672 return 1; /* Cannot do matching without knowing band */
673
674 modes = wpa_s->hw.modes;
675 if (modes == NULL) {
676 /*
677 * The driver does not provide any additional information
678 * about the utilized hardware, so allow the connection attempt
679 * to continue.
680 */
681 return 1;
682 }
683
684 for (i = 0; i < wpa_s->hw.num_modes; i++) {
685 for (j = 0; j < modes[i].num_channels; j++) {
686 int freq = modes[i].channels[j].freq;
687 if (freq == bss->freq) {
688 if (mode &&
689 mode->mode == HOSTAPD_MODE_IEEE80211G)
690 break; /* do not allow 802.11b replace
691 * 802.11g */
692 mode = &modes[i];
693 break;
694 }
695 }
696 }
697
698 if (mode == NULL)
699 return 0;
700
701 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700702 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800703 if (rate_ie == NULL)
704 continue;
705
706 for (j = 2; j < rate_ie[1] + 2; j++) {
707 int flagged = !!(rate_ie[j] & 0x80);
708 int r = (rate_ie[j] & 0x7f) * 5;
709
710 /*
711 * IEEE Std 802.11n-2009 7.3.2.2:
712 * The new BSS Membership selector value is encoded
713 * like a legacy basic rate, but it is not a rate and
714 * only indicates if the BSS members are required to
715 * support the mandatory features of Clause 20 [HT PHY]
716 * in order to join the BSS.
717 */
718 if (flagged && ((rate_ie[j] & 0x7f) ==
719 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
720 if (!ht_supported(mode)) {
721 wpa_dbg(wpa_s, MSG_DEBUG,
722 " hardware does not support "
723 "HT PHY");
724 return 0;
725 }
726 continue;
727 }
728
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700729 /* There's also a VHT selector for 802.11ac */
730 if (flagged && ((rate_ie[j] & 0x7f) ==
731 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
732 if (!vht_supported(mode)) {
733 wpa_dbg(wpa_s, MSG_DEBUG,
734 " hardware does not support "
735 "VHT PHY");
736 return 0;
737 }
738 continue;
739 }
740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800741 if (!flagged)
742 continue;
743
744 /* check for legacy basic rates */
745 for (k = 0; k < mode->num_rates; k++) {
746 if (mode->rates[k] == r)
747 break;
748 }
749 if (k == mode->num_rates) {
750 /*
751 * IEEE Std 802.11-2007 7.3.2.2 demands that in
752 * order to join a BSS all required rates
753 * have to be supported by the hardware.
754 */
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -0700755 wpa_dbg(wpa_s, MSG_DEBUG,
756 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
757 r / 10, r % 10,
758 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800759 return 0;
760 }
761 }
762 }
763
764 return 1;
765}
766
767
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800768/*
769 * Test whether BSS is in an ESS.
770 * This is done differently in DMG (60 GHz) and non-DMG bands
771 */
772static int bss_is_ess(struct wpa_bss *bss)
773{
774 if (bss_is_dmg(bss)) {
775 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
776 IEEE80211_CAP_DMG_AP;
777 }
778
779 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
780 IEEE80211_CAP_ESS);
781}
782
783
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800784static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
785{
786 size_t i;
787
788 for (i = 0; i < ETH_ALEN; i++) {
789 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
790 return 0;
791 }
792 return 1;
793}
794
795
796static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
797{
798 size_t i;
799
800 for (i = 0; i < num; i++) {
801 const u8 *a = list + i * ETH_ALEN * 2;
802 const u8 *m = a + ETH_ALEN;
803
804 if (match_mac_mask(a, addr, m))
805 return 1;
806 }
807 return 0;
808}
809
810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700812 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800813 struct wpa_ssid *group,
814 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700816 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 int wpa;
818 struct wpa_blacklist *e;
819 const u8 *ie;
820 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800821 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700823 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 wpa_ie_len = ie ? ie[1] : 0;
825
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700826 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 rsn_ie_len = ie ? ie[1] : 0;
828
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800829 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
830 osen = ie != NULL;
831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800833 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700834 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700836 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
837 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
838 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800839 " p2p" : "",
840 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841
842 e = wpa_blacklist_get(wpa_s, bss->bssid);
843 if (e) {
844 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 /*
847 * When only a single network is enabled, we can
848 * trigger blacklisting on the first failure. This
849 * should not be done with multiple enabled networks to
850 * avoid getting forced to move into a worse ESS on
851 * single error if there are no other BSSes of the
852 * current ESS.
853 */
854 limit = 0;
855 }
856 if (e->count > limit) {
857 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
858 "(count=%d limit=%d)", e->count, limit);
859 return NULL;
860 }
861 }
862
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700863 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
865 return NULL;
866 }
867
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800868 if (disallowed_bssid(wpa_s, bss->bssid)) {
869 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
870 return NULL;
871 }
872
873 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
874 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
875 return NULL;
876 }
877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
879
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800880 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700882 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883
Dmitry Shmidt04949592012-07-19 12:16:46 -0700884 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
886 continue;
887 }
888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700889 res = wpas_temp_disabled(wpa_s, ssid);
890 if (res > 0) {
891 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
892 "temporarily for %d second(s)", res);
893 continue;
894 }
895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896#ifdef CONFIG_WPS
897 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
898 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
899 "(WPS)");
900 continue;
901 }
902
903 if (wpa && ssid->ssid_len == 0 &&
904 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
905 check_ssid = 0;
906
907 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
908 /* Only allow wildcard SSID match if an AP
909 * advertises active WPS operation that matches
910 * with our mode. */
911 check_ssid = 1;
912 if (ssid->ssid_len == 0 &&
913 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
914 check_ssid = 0;
915 }
916#endif /* CONFIG_WPS */
917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800918 if (ssid->bssid_set && ssid->ssid_len == 0 &&
919 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
920 check_ssid = 0;
921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700922 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700923 (bss->ssid_len != ssid->ssid_len ||
924 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
926 continue;
927 }
928
929 if (ssid->bssid_set &&
930 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
931 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
932 continue;
933 }
934
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800935 /* check blacklist */
936 if (ssid->num_bssid_blacklist &&
937 addr_in_list(bss->bssid, ssid->bssid_blacklist,
938 ssid->num_bssid_blacklist)) {
939 wpa_dbg(wpa_s, MSG_DEBUG,
940 " skip - BSSID blacklisted");
941 continue;
942 }
943
944 /* if there is a whitelist, only accept those APs */
945 if (ssid->num_bssid_whitelist &&
946 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
947 ssid->num_bssid_whitelist)) {
948 wpa_dbg(wpa_s, MSG_DEBUG,
949 " skip - BSSID not in whitelist");
950 continue;
951 }
952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
954 continue;
955
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800956 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
958 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
959 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
960 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
961 "not allowed");
962 continue;
963 }
964
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700965 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
966 has_wep_key(ssid)) {
967 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
968 continue;
969 }
970
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800971 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
972 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
973 "not allowed");
974 continue;
975 }
976
Jouni Malinen75ecf522011-06-27 15:19:46 -0700977 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
979 "mismatch");
980 continue;
981 }
982
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800983 if (!bss_is_ess(bss)) {
984 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 continue;
986 }
987
988 if (!freq_allowed(ssid->freq_list, bss->freq)) {
989 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
990 "allowed");
991 continue;
992 }
993
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800994 if (!rate_match(wpa_s, bss)) {
995 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
996 "not match");
997 continue;
998 }
999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001001 if (ssid->p2p_group &&
1002 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1003 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1004 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
1005 continue;
1006 }
1007
Dmitry Shmidt54605472013-11-08 11:10:19 -08001008 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1009 struct wpabuf *p2p_ie;
1010 u8 dev_addr[ETH_ALEN];
1011
1012 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1013 if (ie == NULL) {
1014 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
1015 continue;
1016 }
1017 p2p_ie = wpa_bss_get_vendor_ie_multi(
1018 bss, P2P_IE_VENDOR_TYPE);
1019 if (p2p_ie == NULL) {
1020 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
1021 continue;
1022 }
1023
1024 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1025 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1026 ETH_ALEN) != 0) {
1027 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
1028 wpabuf_free(p2p_ie);
1029 continue;
1030 }
1031 wpabuf_free(p2p_ie);
1032 }
1033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034 /*
1035 * TODO: skip the AP if its P2P IE has Group Formation
1036 * bit set in the P2P Group Capability Bitmap and we
1037 * are not in Group Formation with that device.
1038 */
1039#endif /* CONFIG_P2P */
1040
1041 /* Matching configuration found */
1042 return ssid;
1043 }
1044
1045 /* No matching configuration found */
1046 return NULL;
1047}
1048
1049
1050static struct wpa_bss *
1051wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001053 struct wpa_ssid **selected_ssid,
1054 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001056 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001058 if (only_first_ssid)
1059 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1060 group->id);
1061 else
1062 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1063 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001065 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1066 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001067 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1068 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 if (!*selected_ssid)
1070 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1072 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001073 MAC2STR(bss->bssid),
1074 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1075 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 }
1077
1078 return NULL;
1079}
1080
1081
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001082struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1083 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084{
1085 struct wpa_bss *selected = NULL;
1086 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001087 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001088 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001090 if (wpa_s->last_scan_res == NULL ||
1091 wpa_s->last_scan_res_used == 0)
1092 return NULL; /* no scan results from last update */
1093
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001094 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001095 /* check that next_ssid is still valid */
1096 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1097 if (ssid == wpa_s->next_ssid)
1098 break;
1099 }
1100 next_ssid = ssid;
1101 wpa_s->next_ssid = NULL;
1102 }
1103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001105 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1106 if (next_ssid && next_ssid->priority ==
1107 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001108 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001109 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001110 if (selected)
1111 break;
1112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001114 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001115 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 if (selected)
1117 break;
1118 }
1119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001120 if (selected == NULL && wpa_s->blacklist &&
1121 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1123 "blacklist and try again");
1124 wpa_blacklist_clear(wpa_s);
1125 wpa_s->blacklist_cleared++;
1126 } else if (selected == NULL)
1127 break;
1128 }
1129
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001130 ssid = *selected_ssid;
1131 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1132 !ssid->passphrase && !ssid->ext_psk) {
1133 const char *field_name, *txt = NULL;
1134
1135 wpa_dbg(wpa_s, MSG_DEBUG,
1136 "PSK/passphrase not yet available for the selected network");
1137
1138 wpas_notify_network_request(wpa_s, ssid,
1139 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1140
1141 field_name = wpa_supplicant_ctrl_req_to_string(
1142 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1143 if (field_name == NULL)
1144 return NULL;
1145
1146 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1147
1148 selected = NULL;
1149 }
1150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 return selected;
1152}
1153
1154
1155static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1156 int timeout_sec, int timeout_usec)
1157{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001158 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 /*
1160 * No networks are enabled; short-circuit request so
1161 * we don't wait timeout seconds before transitioning
1162 * to INACTIVE state.
1163 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001164 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1165 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1167 return;
1168 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001169
1170 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1172}
1173
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001175int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 struct wpa_bss *selected,
1177 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178{
1179 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1180 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1181 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001182 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001184 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1185 wpa_s->p2p_in_provisioning) {
1186 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1187 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001188 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190#endif /* CONFIG_P2P */
1191
1192#ifdef CONFIG_WPS
1193 wpas_wps_cancel(wpa_s);
1194#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001195 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 }
1197
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001198 wpa_msg(wpa_s, MSG_DEBUG,
1199 "Considering connect request: reassociate: %d selected: "
1200 MACSTR " bssid: " MACSTR " pending: " MACSTR
1201 " wpa_state: %s ssid=%p current_ssid=%p",
1202 wpa_s->reassociate, MAC2STR(selected->bssid),
1203 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1204 wpa_supplicant_state_txt(wpa_s->wpa_state),
1205 ssid, wpa_s->current_ssid);
1206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 /*
1208 * Do not trigger new association unless the BSSID has changed or if
1209 * reassociation is requested. If we are in process of associating with
1210 * the selected BSSID, do not trigger new attempt.
1211 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001212 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1214 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1215 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001216 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1217 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1218 0) ||
1219 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1220 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001221 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1222 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001223 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001225 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1226 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 wpa_supplicant_associate(wpa_s, selected, ssid);
1228 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001229 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1230 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001231 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001233 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234}
1235
1236
1237static struct wpa_ssid *
1238wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1239{
1240 int prio;
1241 struct wpa_ssid *ssid;
1242
1243 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1244 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1245 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001246 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 continue;
1248 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001249 ssid->mode == IEEE80211_MODE_AP ||
1250 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 return ssid;
1252 }
1253 }
1254 return NULL;
1255}
1256
1257
1258/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1259 * on BSS added and BSS changed events */
1260static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001261 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001263 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264
1265 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1266 return;
1267
Jouni Malinen87fd2792011-05-16 18:35:42 +03001268 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270
Jouni Malinen87fd2792011-05-16 18:35:42 +03001271 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001272 if (ssid == NULL)
1273 continue;
1274
Jouni Malinen87fd2792011-05-16 18:35:42 +03001275 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 if (rsn == NULL)
1277 continue;
1278
Jouni Malinen87fd2792011-05-16 18:35:42 +03001279 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 }
1281
1282}
1283
1284
1285static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1286 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001287 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001289 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001290#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 int min_diff;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001292#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293
1294 if (wpa_s->reassociate)
1295 return 1; /* explicit request to reassociate */
1296 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1297 return 1; /* we are not associated; continue */
1298 if (wpa_s->current_ssid == NULL)
1299 return 1; /* unknown current SSID */
1300 if (wpa_s->current_ssid != ssid)
1301 return 1; /* different network block */
1302
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001303 if (wpas_driver_bss_selection(wpa_s))
1304 return 0; /* Driver-based roaming */
1305
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001306 if (wpa_s->current_ssid->ssid)
1307 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1308 wpa_s->current_ssid->ssid,
1309 wpa_s->current_ssid->ssid_len);
1310 if (!current_bss)
1311 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
1313 if (!current_bss)
1314 return 1; /* current BSS not seen in scan results */
1315
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001316 if (current_bss == selected)
1317 return 0;
1318
1319 if (selected->last_update_idx > current_bss->last_update_idx)
1320 return 1; /* current BSS not seen in the last scan */
1321
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001322#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001324 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1325 " level=%d snr=%d est_throughput=%u",
1326 MAC2STR(current_bss->bssid), current_bss->level,
1327 current_bss->snr, current_bss->est_throughput);
1328 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1329 " level=%d snr=%d est_throughput=%u",
1330 MAC2STR(selected->bssid), selected->level,
1331 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332
1333 if (wpa_s->current_ssid->bssid_set &&
1334 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1335 0) {
1336 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1337 "has preferred BSSID");
1338 return 1;
1339 }
1340
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001341 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1342 wpa_dbg(wpa_s, MSG_DEBUG,
1343 "Allow reassociation - selected BSS has better estimated throughput");
1344 return 1;
1345 }
1346
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001347 if (current_bss->level < 0 && current_bss->level > selected->level) {
1348 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1349 "signal level");
1350 return 0;
1351 }
1352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 min_diff = 2;
1354 if (current_bss->level < 0) {
1355 if (current_bss->level < -85)
1356 min_diff = 1;
1357 else if (current_bss->level < -80)
1358 min_diff = 2;
1359 else if (current_bss->level < -75)
1360 min_diff = 3;
1361 else if (current_bss->level < -70)
1362 min_diff = 4;
1363 else
1364 min_diff = 5;
1365 }
1366 if (abs(current_bss->level - selected->level) < min_diff) {
1367 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1368 "in signal level");
1369 return 0;
1370 }
1371
1372 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001373#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001374 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001375#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376}
1377
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001378
Jouni Malinen89ca7022012-09-14 13:03:12 -07001379/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001380 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001381static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001382 union wpa_event_data *data,
1383 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001385 struct wpa_scan_results *scan_res = NULL;
1386 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001388#ifndef CONFIG_NO_RANDOM_POOL
1389 size_t i, num;
1390#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
1392#ifdef CONFIG_AP
1393 if (wpa_s->ap_iface)
1394 ap = 1;
1395#endif /* CONFIG_AP */
1396
1397 wpa_supplicant_notify_scanning(wpa_s, 0);
1398
1399 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1400 data ? &data->scan_info :
1401 NULL, 1);
1402 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001403 if (wpa_s->conf->ap_scan == 2 || ap ||
1404 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001406 if (!own_request)
1407 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1409 "scanning again");
1410 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001411 ret = -1;
1412 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 }
1414
1415#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 num = scan_res->num;
1417 if (num > 10)
1418 num = 10;
1419 for (i = 0; i < num; i++) {
1420 u8 buf[5];
1421 struct wpa_scan_res *res = scan_res->res[i];
1422 buf[0] = res->bssid[5];
1423 buf[1] = res->qual & 0xff;
1424 buf[2] = res->noise & 0xff;
1425 buf[3] = res->level & 0xff;
1426 buf[4] = res->tsf & 0xff;
1427 random_add_randomness(buf, sizeof(buf));
1428 }
1429#endif /* CONFIG_NO_RANDOM_POOL */
1430
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001431 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001432 (wpa_s->own_scan_running || !wpa_s->radio->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1434 struct wpa_scan_results *scan_res);
1435
1436 scan_res_handler = wpa_s->scan_res_handler;
1437 wpa_s->scan_res_handler = NULL;
1438 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439 ret = -2;
1440 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 }
1442
1443 if (ap) {
1444 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1445#ifdef CONFIG_AP
1446 if (wpa_s->ap_iface->scan_cb)
1447 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1448#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001449 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001451
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001452 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001453 wpa_s->own_scan_running, wpa_s->radio->external_scan_running);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001454 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1455 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1456 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1457 wpa_s->manual_scan_id);
1458 wpa_s->manual_scan_use_id = 0;
1459 } else {
1460 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1461 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001462 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463
1464 wpas_notify_scan_done(wpa_s, 1);
1465
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001466 if (!wpa_s->own_scan_running && wpa_s->radio->external_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001467 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 -07001468 wpa_scan_results_free(scan_res);
1469 return 0;
1470 }
1471
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001472 if (wnm_scan_process(wpa_s, 1) > 0)
1473 goto scan_work_done;
1474
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001475 if (sme_proc_obss_scan(wpa_s) > 0)
1476 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001478 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1479 goto scan_work_done;
1480
1481 if (autoscan_notify_scan(wpa_s, scan_res))
1482 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 if (wpa_s->disconnected) {
1485 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001486 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 }
1488
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001489 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490 bgscan_notify_scan(wpa_s, scan_res) == 1)
1491 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001493 wpas_wps_update_ap_info(wpa_s, scan_res);
1494
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001495 wpa_scan_results_free(scan_res);
1496
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001497 if (wpa_s->scan_work) {
1498 struct wpa_radio_work *work = wpa_s->scan_work;
1499 wpa_s->scan_work = NULL;
1500 radio_work_done(work);
1501 }
1502
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001503 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001504
1505scan_work_done:
1506 wpa_scan_results_free(scan_res);
1507 if (wpa_s->scan_work) {
1508 struct wpa_radio_work *work = wpa_s->scan_work;
1509 wpa_s->scan_work = NULL;
1510 radio_work_done(work);
1511 }
1512 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001513}
1514
1515
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001516static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001517 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001518{
1519 struct wpa_bss *selected;
1520 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001521 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1522
1523 if (time_to_reenable > 0) {
1524 wpa_dbg(wpa_s, MSG_DEBUG,
1525 "Postpone network selection by %d seconds since all networks are disabled",
1526 time_to_reenable);
1527 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1528 eloop_register_timeout(time_to_reenable, 0,
1529 wpas_network_reenabled, wpa_s, NULL);
1530 return 0;
1531 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001532
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001533 if (wpa_s->p2p_mgmt)
1534 return 0; /* no normal connection on p2p_mgmt interface */
1535
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001536 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537
1538 if (selected) {
1539 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001540 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001541 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001542 if (new_scan)
1543 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001545 }
1546
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001547 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001548 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001549 return -1;
1550 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001551 if (new_scan)
1552 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001553 /*
1554 * Do not notify other virtual radios of scan results since we do not
1555 * want them to start other associations at the same time.
1556 */
1557 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001559#ifdef CONFIG_MESH
1560 if (wpa_s->ifmsh) {
1561 wpa_msg(wpa_s, MSG_INFO,
1562 "Avoiding join because we already joined a mesh group");
1563 return 0;
1564 }
1565#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1567 ssid = wpa_supplicant_pick_new_network(wpa_s);
1568 if (ssid) {
1569 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1570 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001571 if (new_scan)
1572 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001573 } else if (own_request) {
1574 /*
1575 * No SSID found. If SCAN results are as a result of
1576 * own scan request and not due to a scan request on
1577 * another shared interface, try another scan.
1578 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001579 int timeout_sec = wpa_s->scan_interval;
1580 int timeout_usec = 0;
1581#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001582 int res;
1583
1584 res = wpas_p2p_scan_no_go_seen(wpa_s);
1585 if (res == 2)
1586 return 2;
1587 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001588 return 0;
1589
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001590 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001591 wpa_s->show_group_started ||
1592 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 /*
1594 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001595 * state and during P2P join-a-group operation
1596 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001597 */
1598 timeout_sec = 0;
1599 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001600 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1601 timeout_usec);
1602 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 }
1604#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001605#ifdef CONFIG_INTERWORKING
1606 if (wpa_s->conf->auto_interworking &&
1607 wpa_s->conf->interworking &&
1608 wpa_s->conf->cred) {
1609 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1610 "start ANQP fetch since no matching "
1611 "networks found");
1612 wpa_s->network_select = 1;
1613 wpa_s->auto_network_select = 1;
1614 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001615 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001616 }
1617#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001618#ifdef CONFIG_WPS
1619 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1620 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1621 timeout_sec = 0;
1622 timeout_usec = 500000;
1623 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1624 timeout_usec);
1625 return 0;
1626 }
1627#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001628 if (wpa_supplicant_req_sched_scan(wpa_s))
1629 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1630 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001631
1632 wpa_msg_ctrl(wpa_s, MSG_INFO,
1633 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 }
1635 }
1636 return 0;
1637}
1638
1639
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001640static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1641 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001644 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001645
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001646 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1);
1647 if (res == 2) {
1648 /*
1649 * Interface may have been removed, so must not dereference
1650 * wpa_s after this.
1651 */
1652 return 1;
1653 }
1654 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 /*
1656 * If no scan results could be fetched, then no need to
1657 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001658 * this scan. Similarly, if scan results started a new operation on this
1659 * interface, do not notify other interfaces to avoid concurrent
1660 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001662 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 }
1664
1665 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001666 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 * so, they get updated with this same scan info.
1668 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001669 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1670 radio_list) {
1671 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1673 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001674 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 }
1676 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001677
1678 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679}
1680
1681#endif /* CONFIG_NO_SCAN_PROCESSING */
1682
1683
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001684int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1685{
1686#ifdef CONFIG_NO_SCAN_PROCESSING
1687 return -1;
1688#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001689 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001690
Dmitry Shmidt41712582015-06-29 11:02:15 -07001691 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001692 return -1;
1693
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001694 os_get_reltime(&now);
1695 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001696 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1697 return -1;
1698 }
1699
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001700 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001701#endif /* CONFIG_NO_SCAN_PROCESSING */
1702}
1703
Dmitry Shmidt04949592012-07-19 12:16:46 -07001704#ifdef CONFIG_WNM
1705
1706static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1707{
1708 struct wpa_supplicant *wpa_s = eloop_ctx;
1709
1710 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1711 return;
1712
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001713 if (!wpa_s->no_keep_alive) {
1714 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1715 MAC2STR(wpa_s->bssid));
1716 /* TODO: could skip this if normal data traffic has been sent */
1717 /* TODO: Consider using some more appropriate data frame for
1718 * this */
1719 if (wpa_s->l2)
1720 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1721 (u8 *) "", 0);
1722 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001723
1724#ifdef CONFIG_SME
1725 if (wpa_s->sme.bss_max_idle_period) {
1726 unsigned int msec;
1727 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1728 if (msec > 100)
1729 msec -= 100;
1730 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1731 wnm_bss_keep_alive, wpa_s, NULL);
1732 }
1733#endif /* CONFIG_SME */
1734}
1735
1736
1737static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1738 const u8 *ies, size_t ies_len)
1739{
1740 struct ieee802_11_elems elems;
1741
1742 if (ies == NULL)
1743 return;
1744
1745 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1746 return;
1747
1748#ifdef CONFIG_SME
1749 if (elems.bss_max_idle_period) {
1750 unsigned int msec;
1751 wpa_s->sme.bss_max_idle_period =
1752 WPA_GET_LE16(elems.bss_max_idle_period);
1753 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1754 "TU)%s", wpa_s->sme.bss_max_idle_period,
1755 (elems.bss_max_idle_period[2] & 0x01) ?
1756 " (protected keep-live required)" : "");
1757 if (wpa_s->sme.bss_max_idle_period == 0)
1758 wpa_s->sme.bss_max_idle_period = 1;
1759 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1760 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1761 /* msec times 1000 */
1762 msec = wpa_s->sme.bss_max_idle_period * 1024;
1763 if (msec > 100)
1764 msec -= 100;
1765 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1766 wnm_bss_keep_alive, wpa_s,
1767 NULL);
1768 }
1769 }
1770#endif /* CONFIG_SME */
1771}
1772
1773#endif /* CONFIG_WNM */
1774
1775
1776void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1777{
1778#ifdef CONFIG_WNM
1779 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1780#endif /* CONFIG_WNM */
1781}
1782
1783
Dmitry Shmidt051af732013-10-22 13:52:46 -07001784#ifdef CONFIG_INTERWORKING
1785
1786static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1787 size_t len)
1788{
1789 int res;
1790
1791 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1792 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1793 if (res) {
1794 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1795 }
1796
1797 return res;
1798}
1799
1800
1801static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1802 const u8 *ies, size_t ies_len)
1803{
1804 struct ieee802_11_elems elems;
1805
1806 if (ies == NULL)
1807 return;
1808
1809 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1810 return;
1811
1812 if (elems.qos_map_set) {
1813 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1814 elems.qos_map_set_len);
1815 }
1816}
1817
1818#endif /* CONFIG_INTERWORKING */
1819
1820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1822 union wpa_event_data *data)
1823{
1824 int l, len, found = 0, wpa_found, rsn_found;
1825 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001826#ifdef CONFIG_IEEE80211R
1827 u8 bssid[ETH_ALEN];
1828#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829
1830 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1831 if (data->assoc_info.req_ies)
1832 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1833 data->assoc_info.req_ies_len);
1834 if (data->assoc_info.resp_ies) {
1835 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1836 data->assoc_info.resp_ies_len);
1837#ifdef CONFIG_TDLS
1838 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1839 data->assoc_info.resp_ies_len);
1840#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001841#ifdef CONFIG_WNM
1842 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1843 data->assoc_info.resp_ies_len);
1844#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001845#ifdef CONFIG_INTERWORKING
1846 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1847 data->assoc_info.resp_ies_len);
1848#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 }
1850 if (data->assoc_info.beacon_ies)
1851 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1852 data->assoc_info.beacon_ies,
1853 data->assoc_info.beacon_ies_len);
1854 if (data->assoc_info.freq)
1855 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1856 data->assoc_info.freq);
1857
1858 p = data->assoc_info.req_ies;
1859 l = data->assoc_info.req_ies_len;
1860
1861 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1862 while (p && l >= 2) {
1863 len = p[1] + 2;
1864 if (len > l) {
1865 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1866 p, l);
1867 break;
1868 }
1869 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1870 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1871 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1872 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1873 break;
1874 found = 1;
1875 wpa_find_assoc_pmkid(wpa_s);
1876 break;
1877 }
1878 l -= len;
1879 p += len;
1880 }
1881 if (!found && data->assoc_info.req_ies)
1882 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1883
1884#ifdef CONFIG_IEEE80211R
1885#ifdef CONFIG_SME
1886 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1888 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1889 data->assoc_info.resp_ies,
1890 data->assoc_info.resp_ies_len,
1891 bssid) < 0) {
1892 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1893 "Reassociation Response failed");
1894 wpa_supplicant_deauthenticate(
1895 wpa_s, WLAN_REASON_INVALID_IE);
1896 return -1;
1897 }
1898 }
1899
1900 p = data->assoc_info.resp_ies;
1901 l = data->assoc_info.resp_ies_len;
1902
1903#ifdef CONFIG_WPS_STRICT
1904 if (p && wpa_s->current_ssid &&
1905 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1906 struct wpabuf *wps;
1907 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1908 if (wps == NULL) {
1909 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1910 "include WPS IE in (Re)Association Response");
1911 return -1;
1912 }
1913
1914 if (wps_validate_assoc_resp(wps) < 0) {
1915 wpabuf_free(wps);
1916 wpa_supplicant_deauthenticate(
1917 wpa_s, WLAN_REASON_INVALID_IE);
1918 return -1;
1919 }
1920 wpabuf_free(wps);
1921 }
1922#endif /* CONFIG_WPS_STRICT */
1923
1924 /* Go through the IEs and make a copy of the MDIE, if present. */
1925 while (p && l >= 2) {
1926 len = p[1] + 2;
1927 if (len > l) {
1928 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1929 p, l);
1930 break;
1931 }
1932 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1933 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1934 wpa_s->sme.ft_used = 1;
1935 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1936 MOBILITY_DOMAIN_ID_LEN);
1937 break;
1938 }
1939 l -= len;
1940 p += len;
1941 }
1942#endif /* CONFIG_SME */
1943
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001944 /* Process FT when SME is in the driver */
1945 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1946 wpa_ft_is_completed(wpa_s->wpa)) {
1947 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1948 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1949 data->assoc_info.resp_ies,
1950 data->assoc_info.resp_ies_len,
1951 bssid) < 0) {
1952 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1953 "Reassociation Response failed");
1954 wpa_supplicant_deauthenticate(
1955 wpa_s, WLAN_REASON_INVALID_IE);
1956 return -1;
1957 }
1958 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1959 }
1960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1962 data->assoc_info.resp_ies_len);
1963#endif /* CONFIG_IEEE80211R */
1964
1965 /* WPA/RSN IE from Beacon/ProbeResp */
1966 p = data->assoc_info.beacon_ies;
1967 l = data->assoc_info.beacon_ies_len;
1968
1969 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1970 */
1971 wpa_found = rsn_found = 0;
1972 while (p && l >= 2) {
1973 len = p[1] + 2;
1974 if (len > l) {
1975 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1976 p, l);
1977 break;
1978 }
1979 if (!wpa_found &&
1980 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1981 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1982 wpa_found = 1;
1983 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1984 }
1985
1986 if (!rsn_found &&
1987 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1988 rsn_found = 1;
1989 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1990 }
1991
1992 l -= len;
1993 p += len;
1994 }
1995
1996 if (!wpa_found && data->assoc_info.beacon_ies)
1997 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1998 if (!rsn_found && data->assoc_info.beacon_ies)
1999 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2000 if (wpa_found || rsn_found)
2001 wpa_s->ap_ies_from_associnfo = 1;
2002
Jouni Malinen87fd2792011-05-16 18:35:42 +03002003 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2004 wpa_s->assoc_freq != data->assoc_info.freq) {
2005 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2006 "%u to %u MHz",
2007 wpa_s->assoc_freq, data->assoc_info.freq);
2008 wpa_supplicant_update_scan_results(wpa_s);
2009 }
2010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 wpa_s->assoc_freq = data->assoc_info.freq;
2012
2013 return 0;
2014}
2015
2016
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002017static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2018{
2019 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2020
2021 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2022 return -1;
2023
2024 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2025 return 0;
2026
2027 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2028 WPA_IE_VENDOR_TYPE);
2029 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2030
2031 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2032 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2033 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2034 bss_rsn ? 2 + bss_rsn[1] : 0))
2035 return -1;
2036
2037 return 0;
2038}
2039
2040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2042 union wpa_event_data *data)
2043{
2044 u8 bssid[ETH_ALEN];
2045 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046
2047#ifdef CONFIG_AP
2048 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002049 if (!data)
2050 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2052 data->assoc_info.addr,
2053 data->assoc_info.req_ies,
2054 data->assoc_info.req_ies_len,
2055 data->assoc_info.reassoc);
2056 return;
2057 }
2058#endif /* CONFIG_AP */
2059
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002060 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2063 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2064 return;
2065
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002066 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2067 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002068 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002069 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2070 return;
2071 }
2072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002074 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2076 MACSTR, MAC2STR(bssid));
2077 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2079 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002080 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
2082 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2083 wpa_clear_keys(wpa_s, bssid);
2084 }
2085 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002086 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2088 return;
2089 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002090
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002091#ifdef ANDROID
2092 if (wpa_s->conf->ap_scan == 1) {
2093#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002094 if (wpa_s->conf->ap_scan == 1 &&
2095 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002096#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002097 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
2098 wpa_msg(wpa_s, MSG_WARNING,
2099 "WPA/RSN IEs not updated");
2100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 }
2102
2103#ifdef CONFIG_SME
2104 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2105 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002106 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107#endif /* CONFIG_SME */
2108
2109 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2110 if (wpa_s->current_ssid) {
2111 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2112 * initialized before association, but for other modes,
2113 * initialize PC/SC here, if the current configuration needs
2114 * smartcard or SIM/USIM. */
2115 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2116 }
2117 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2118 if (wpa_s->l2)
2119 l2_packet_notify_auth_start(wpa_s->l2);
2120
2121 /*
2122 * Set portEnabled first to FALSE in order to get EAP state machine out
2123 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2124 * state machine may transit to AUTHENTICATING state based on obsolete
2125 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2126 * AUTHENTICATED without ever giving chance to EAP state machine to
2127 * reset the state.
2128 */
2129 if (!ft_completed) {
2130 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2131 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2132 }
2133 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
2134 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2135 /* 802.1X::portControl = Auto */
2136 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2137 wpa_s->eapol_received = 0;
2138 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2139 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2140 (wpa_s->current_ssid &&
2141 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002142 if (wpa_s->current_ssid &&
2143 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002144 (wpa_s->drv_flags &
2145 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2146 /*
2147 * Set the key after having received joined-IBSS event
2148 * from the driver.
2149 */
2150 wpa_supplicant_set_wpa_none_key(wpa_s,
2151 wpa_s->current_ssid);
2152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 wpa_supplicant_cancel_auth_timeout(wpa_s);
2154 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2155 } else if (!ft_completed) {
2156 /* Timeout for receiving the first EAPOL packet */
2157 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2158 }
2159 wpa_supplicant_cancel_scan(wpa_s);
2160
2161 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2162 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2163 /*
2164 * We are done; the driver will take care of RSN 4-way
2165 * handshake.
2166 */
2167 wpa_supplicant_cancel_auth_timeout(wpa_s);
2168 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2169 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2170 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2171 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2172 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2173 /*
2174 * The driver will take care of RSN 4-way handshake, so we need
2175 * to allow EAPOL supplicant to complete its work without
2176 * waiting for WPA supplicant.
2177 */
2178 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2179 } else if (ft_completed) {
2180 /*
2181 * FT protocol completed - make sure EAPOL state machine ends
2182 * up in authenticated.
2183 */
2184 wpa_supplicant_cancel_auth_timeout(wpa_s);
2185 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2186 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2187 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2188 }
2189
Jouni Malinena05074c2012-12-21 21:35:35 +02002190 wpa_s->last_eapol_matches_bssid = 0;
2191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002193 struct os_reltime now, age;
2194 os_get_reltime(&now);
2195 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 if (age.sec == 0 && age.usec < 100000 &&
2197 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2198 0) {
2199 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2200 "frame that was received just before "
2201 "association notification");
2202 wpa_supplicant_rx_eapol(
2203 wpa_s, wpa_s->pending_eapol_rx_src,
2204 wpabuf_head(wpa_s->pending_eapol_rx),
2205 wpabuf_len(wpa_s->pending_eapol_rx));
2206 }
2207 wpabuf_free(wpa_s->pending_eapol_rx);
2208 wpa_s->pending_eapol_rx = NULL;
2209 }
2210
2211 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2212 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002213 wpa_s->current_ssid &&
2214 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 /* Set static WEP keys again */
2216 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2217 }
2218
2219#ifdef CONFIG_IBSS_RSN
2220 if (wpa_s->current_ssid &&
2221 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2222 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2223 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2224 wpa_s->ibss_rsn == NULL) {
2225 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2226 if (!wpa_s->ibss_rsn) {
2227 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2228 wpa_supplicant_deauthenticate(
2229 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2230 return;
2231 }
2232
2233 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2234 }
2235#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002236
2237 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002238
2239 if (data) {
2240 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2241 data->assoc_info.resp_ies_len,
2242 &data->assoc_info.wmm_params);
2243
2244 if (wpa_s->reassoc_same_bss)
2245 wmm_ac_restore_tspecs(wpa_s);
2246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247}
2248
2249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002250static int disconnect_reason_recoverable(u16 reason_code)
2251{
2252 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2253 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2254 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2255}
2256
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002259 u16 reason_code,
2260 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261{
2262 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002263
2264 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2265 /*
2266 * At least Host AP driver and a Prism3 card seemed to be
2267 * generating streams of disconnected events when configuring
2268 * IBSS for WPA-None. Ignore them for now.
2269 */
2270 return;
2271 }
2272
2273 bssid = wpa_s->bssid;
2274 if (is_zero_ether_addr(bssid))
2275 bssid = wpa_s->pending_bssid;
2276
2277 if (!is_zero_ether_addr(bssid) ||
2278 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2279 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2280 " reason=%d%s",
2281 MAC2STR(bssid), reason_code,
2282 locally_generated ? " locally_generated=1" : "");
2283 }
2284}
2285
2286
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002287static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2288 int locally_generated)
2289{
2290 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2291 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2292 return 0; /* Not in 4-way handshake with PSK */
2293
2294 /*
2295 * It looks like connection was lost while trying to go through PSK
2296 * 4-way handshake. Filter out known disconnection cases that are caused
2297 * by something else than PSK mismatch to avoid confusing reports.
2298 */
2299
2300 if (locally_generated) {
2301 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2302 return 0;
2303 }
2304
2305 return 1;
2306}
2307
2308
Jouni Malinen2b89da82012-08-31 22:04:41 +03002309static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2310 u16 reason_code,
2311 int locally_generated)
2312{
2313 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 int authenticating;
2315 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 struct wpa_bss *fast_reconnect = NULL;
2317 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002318 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319
2320 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2321 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2322
2323 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2324 /*
2325 * At least Host AP driver and a Prism3 card seemed to be
2326 * generating streams of disconnected events when configuring
2327 * IBSS for WPA-None. Ignore them for now.
2328 */
2329 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2330 "IBSS/WPA-None mode");
2331 return;
2332 }
2333
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002334 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2336 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002337 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2338 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002339 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002341 if (!wpa_s->disconnected &&
2342 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002343 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2344 wpas_wps_searching(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002345 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002346 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002347 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002348 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002349 wpa_s->wpa_state);
2350 if (wpa_s->wpa_state == WPA_COMPLETED &&
2351 wpa_s->current_ssid &&
2352 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002353 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002354 disconnect_reason_recoverable(reason_code)) {
2355 /*
2356 * It looks like the AP has dropped association with
2357 * us, but could allow us to get back in. Try to
2358 * reconnect to the same BSS without full scan to save
2359 * time for some common cases.
2360 */
2361 fast_reconnect = wpa_s->current_bss;
2362 fast_reconnect_ssid = wpa_s->current_ssid;
2363 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002364 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002365 else
2366 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2367 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002369 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 "try to re-connect");
2371 wpa_s->reassociate = 0;
2372 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002373 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 }
2375 bssid = wpa_s->bssid;
2376 if (is_zero_ether_addr(bssid))
2377 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2379 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 if (locally_generated)
2382 wpa_s->disconnect_reason = -reason_code;
2383 else
2384 wpa_s->disconnect_reason = reason_code;
2385 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2387 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002388 wpa_clear_keys(wpa_s, wpa_s->bssid);
2389 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002390 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 wpa_supplicant_mark_disassoc(wpa_s);
2392
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002393 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002395 wpa_s->current_ssid = last_ssid;
2396 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002397
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002398 if (fast_reconnect &&
2399 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2400 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2401 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2402 fast_reconnect->ssid_len) &&
2403 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002404#ifndef CONFIG_NO_SCAN_PROCESSING
2405 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2406 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2407 fast_reconnect_ssid) < 0) {
2408 /* Recover through full scan */
2409 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2410 }
2411#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002412 } else if (fast_reconnect) {
2413 /*
2414 * Could not reconnect to the same BSS due to network being
2415 * disabled. Use a new scan to match the alternative behavior
2416 * above, i.e., to continue automatic reconnection attempt in a
2417 * way that enforces disabled network rules.
2418 */
2419 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421}
2422
2423
2424#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002425void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426{
2427 struct wpa_supplicant *wpa_s = eloop_ctx;
2428
2429 if (!wpa_s->pending_mic_error_report)
2430 return;
2431
2432 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2433 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2434 wpa_s->pending_mic_error_report = 0;
2435}
2436#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2437
2438
2439static void
2440wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2441 union wpa_event_data *data)
2442{
2443 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002444 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445
2446 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2447 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002448 os_get_reltime(&t);
2449 if ((wpa_s->last_michael_mic_error.sec &&
2450 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 wpa_s->pending_mic_error_report) {
2452 if (wpa_s->pending_mic_error_report) {
2453 /*
2454 * Send the pending MIC error report immediately since
2455 * we are going to start countermeasures and AP better
2456 * do the same.
2457 */
2458 wpa_sm_key_request(wpa_s->wpa, 1,
2459 wpa_s->pending_mic_error_pairwise);
2460 }
2461
2462 /* Send the new MIC error report immediately since we are going
2463 * to start countermeasures and AP better do the same.
2464 */
2465 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2466
2467 /* initialize countermeasures */
2468 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002469
2470 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2473
2474 /*
2475 * Need to wait for completion of request frame. We do not get
2476 * any callback for the message completion, so just wait a
2477 * short while and hope for the best. */
2478 os_sleep(0, 10000);
2479
2480 wpa_drv_set_countermeasures(wpa_s, 1);
2481 wpa_supplicant_deauthenticate(wpa_s,
2482 WLAN_REASON_MICHAEL_MIC_FAILURE);
2483 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2484 wpa_s, NULL);
2485 eloop_register_timeout(60, 0,
2486 wpa_supplicant_stop_countermeasures,
2487 wpa_s, NULL);
2488 /* TODO: mark the AP rejected for 60 second. STA is
2489 * allowed to associate with another AP.. */
2490 } else {
2491#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2492 if (wpa_s->mic_errors_seen) {
2493 /*
2494 * Reduce the effectiveness of Michael MIC error
2495 * reports as a means for attacking against TKIP if
2496 * more than one MIC failure is noticed with the same
2497 * PTK. We delay the transmission of the reports by a
2498 * random time between 0 and 60 seconds in order to
2499 * force the attacker wait 60 seconds before getting
2500 * the information on whether a frame resulted in a MIC
2501 * failure.
2502 */
2503 u8 rval[4];
2504 int sec;
2505
2506 if (os_get_random(rval, sizeof(rval)) < 0)
2507 sec = os_random() % 60;
2508 else
2509 sec = WPA_GET_BE32(rval) % 60;
2510 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2511 "report %d seconds", sec);
2512 wpa_s->pending_mic_error_report = 1;
2513 wpa_s->pending_mic_error_pairwise = pairwise;
2514 eloop_cancel_timeout(
2515 wpa_supplicant_delayed_mic_error_report,
2516 wpa_s, NULL);
2517 eloop_register_timeout(
2518 sec, os_random() % 1000000,
2519 wpa_supplicant_delayed_mic_error_report,
2520 wpa_s, NULL);
2521 } else {
2522 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2523 }
2524#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2525 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2526#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2527 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002528 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 wpa_s->mic_errors_seen++;
2530}
2531
2532
2533#ifdef CONFIG_TERMINATE_ONLASTIF
2534static int any_interfaces(struct wpa_supplicant *head)
2535{
2536 struct wpa_supplicant *wpa_s;
2537
2538 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2539 if (!wpa_s->interface_removed)
2540 return 1;
2541 return 0;
2542}
2543#endif /* CONFIG_TERMINATE_ONLASTIF */
2544
2545
2546static void
2547wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2548 union wpa_event_data *data)
2549{
2550 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2551 return;
2552
2553 switch (data->interface_status.ievent) {
2554 case EVENT_INTERFACE_ADDED:
2555 if (!wpa_s->interface_removed)
2556 break;
2557 wpa_s->interface_removed = 0;
2558 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2559 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2560 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2561 "driver after interface was added");
2562 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002563
2564#ifdef CONFIG_P2P
2565 if (!wpa_s->global->p2p &&
2566 !wpa_s->global->p2p_disabled &&
2567 !wpa_s->conf->p2p_disabled &&
2568 (wpa_s->drv_flags &
2569 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2570 wpas_p2p_add_p2pdev_interface(
2571 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2572 wpa_printf(MSG_INFO,
2573 "P2P: Failed to enable P2P Device interface");
2574 /* Try to continue without. P2P will be disabled. */
2575 }
2576#endif /* CONFIG_P2P */
2577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 break;
2579 case EVENT_INTERFACE_REMOVED:
2580 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2581 wpa_s->interface_removed = 1;
2582 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002583 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 l2_packet_deinit(wpa_s->l2);
2585 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002586
2587#ifdef CONFIG_P2P
2588 if (wpa_s->global->p2p &&
2589 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2590 (wpa_s->drv_flags &
2591 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2592 wpa_dbg(wpa_s, MSG_DEBUG,
2593 "Removing P2P Device interface");
2594 wpa_supplicant_remove_iface(
2595 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2596 0);
2597 wpa_s->global->p2p_init_wpa_s = NULL;
2598 }
2599#endif /* CONFIG_P2P */
2600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601#ifdef CONFIG_TERMINATE_ONLASTIF
2602 /* check if last interface */
2603 if (!any_interfaces(wpa_s->global->ifaces))
2604 eloop_terminate();
2605#endif /* CONFIG_TERMINATE_ONLASTIF */
2606 break;
2607 }
2608}
2609
2610
2611#ifdef CONFIG_PEERKEY
2612static void
2613wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2614 union wpa_event_data *data)
2615{
2616 if (data == NULL)
2617 return;
2618 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2619}
2620#endif /* CONFIG_PEERKEY */
2621
2622
2623#ifdef CONFIG_TDLS
2624static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2625 union wpa_event_data *data)
2626{
2627 if (data == NULL)
2628 return;
2629 switch (data->tdls.oper) {
2630 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002631 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2632 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2633 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2634 else
2635 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 break;
2637 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002638 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2639 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2640 data->tdls.reason_code);
2641 else
2642 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2643 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002645 case TDLS_REQUEST_DISCOVER:
2646 wpa_tdls_send_discovery_request(wpa_s->wpa,
2647 data->tdls.peer);
2648 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649 }
2650}
2651#endif /* CONFIG_TDLS */
2652
2653
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002654#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002655static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2656 union wpa_event_data *data)
2657{
2658 if (data == NULL)
2659 return;
2660 switch (data->wnm.oper) {
2661 case WNM_OPER_SLEEP:
2662 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2663 "(action=%d, intval=%d)",
2664 data->wnm.sleep_action, data->wnm.sleep_intval);
2665 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002666 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002667 break;
2668 }
2669}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002670#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002671
2672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673#ifdef CONFIG_IEEE80211R
2674static void
2675wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2676 union wpa_event_data *data)
2677{
2678 if (data == NULL)
2679 return;
2680
2681 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2682 data->ft_ies.ies_len,
2683 data->ft_ies.ft_action,
2684 data->ft_ies.target_ap,
2685 data->ft_ies.ric_ies,
2686 data->ft_ies.ric_ies_len) < 0) {
2687 /* TODO: prevent MLME/driver from trying to associate? */
2688 }
2689}
2690#endif /* CONFIG_IEEE80211R */
2691
2692
2693#ifdef CONFIG_IBSS_RSN
2694static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2695 union wpa_event_data *data)
2696{
2697 struct wpa_ssid *ssid;
2698 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2699 return;
2700 if (data == NULL)
2701 return;
2702 ssid = wpa_s->current_ssid;
2703 if (ssid == NULL)
2704 return;
2705 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2706 return;
2707
2708 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2709}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002710
2711
2712static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2713 union wpa_event_data *data)
2714{
2715 struct wpa_ssid *ssid = wpa_s->current_ssid;
2716
2717 if (ssid == NULL)
2718 return;
2719
2720 /* check if the ssid is correctly configured as IBSS/RSN */
2721 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2722 return;
2723
2724 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2725 data->rx_mgmt.frame_len);
2726}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727#endif /* CONFIG_IBSS_RSN */
2728
2729
2730#ifdef CONFIG_IEEE80211R
2731static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2732 size_t len)
2733{
2734 const u8 *sta_addr, *target_ap_addr;
2735 u16 status;
2736
2737 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2738 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2739 return; /* only SME case supported for now */
2740 if (len < 1 + 2 * ETH_ALEN + 2)
2741 return;
2742 if (data[0] != 2)
2743 return; /* Only FT Action Response is supported for now */
2744 sta_addr = data + 1;
2745 target_ap_addr = data + 1 + ETH_ALEN;
2746 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2747 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2748 MACSTR " TargetAP " MACSTR " status %u",
2749 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2750
2751 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2752 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2753 " in FT Action Response", MAC2STR(sta_addr));
2754 return;
2755 }
2756
2757 if (status) {
2758 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2759 "failure (status code %d)", status);
2760 /* TODO: report error to FT code(?) */
2761 return;
2762 }
2763
2764 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2765 len - (1 + 2 * ETH_ALEN + 2), 1,
2766 target_ap_addr, NULL, 0) < 0)
2767 return;
2768
2769#ifdef CONFIG_SME
2770 {
2771 struct wpa_bss *bss;
2772 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2773 if (bss)
2774 wpa_s->sme.freq = bss->freq;
2775 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2776 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2777 WLAN_AUTH_FT);
2778 }
2779#endif /* CONFIG_SME */
2780}
2781#endif /* CONFIG_IEEE80211R */
2782
2783
2784static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2785 struct unprot_deauth *e)
2786{
2787#ifdef CONFIG_IEEE80211W
2788 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2789 "dropped: " MACSTR " -> " MACSTR
2790 " (reason code %u)",
2791 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2792 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2793#endif /* CONFIG_IEEE80211W */
2794}
2795
2796
2797static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2798 struct unprot_disassoc *e)
2799{
2800#ifdef CONFIG_IEEE80211W
2801 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2802 "dropped: " MACSTR " -> " MACSTR
2803 " (reason code %u)",
2804 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2805 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2806#endif /* CONFIG_IEEE80211W */
2807}
2808
2809
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002810static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2811 u16 reason_code, int locally_generated,
2812 const u8 *ie, size_t ie_len, int deauth)
2813{
2814#ifdef CONFIG_AP
2815 if (wpa_s->ap_iface && addr) {
2816 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2817 return;
2818 }
2819
2820 if (wpa_s->ap_iface) {
2821 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2822 return;
2823 }
2824#endif /* CONFIG_AP */
2825
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002826 if (!locally_generated)
2827 wpa_s->own_disconnect_req = 0;
2828
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002829 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2830
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002831 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2832 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2833 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2834 eapol_sm_failed(wpa_s->eapol))) &&
2835 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002836 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002837
2838#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002839 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002840 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2841 locally_generated) > 0) {
2842 /*
2843 * The interface was removed, so cannot continue
2844 * processing any additional operations after this.
2845 */
2846 return;
2847 }
2848 }
2849#endif /* CONFIG_P2P */
2850
2851 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2852 locally_generated);
2853}
2854
2855
2856static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2857 struct disassoc_info *info)
2858{
2859 u16 reason_code = 0;
2860 int locally_generated = 0;
2861 const u8 *addr = NULL;
2862 const u8 *ie = NULL;
2863 size_t ie_len = 0;
2864
2865 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2866
2867 if (info) {
2868 addr = info->addr;
2869 ie = info->ie;
2870 ie_len = info->ie_len;
2871 reason_code = info->reason_code;
2872 locally_generated = info->locally_generated;
2873 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2874 locally_generated ? " (locally generated)" : "");
2875 if (addr)
2876 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2877 MAC2STR(addr));
2878 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2879 ie, ie_len);
2880 }
2881
2882#ifdef CONFIG_AP
2883 if (wpa_s->ap_iface && info && info->addr) {
2884 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2885 return;
2886 }
2887
2888 if (wpa_s->ap_iface) {
2889 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2890 return;
2891 }
2892#endif /* CONFIG_AP */
2893
2894#ifdef CONFIG_P2P
2895 if (info) {
2896 wpas_p2p_disassoc_notif(
2897 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2898 locally_generated);
2899 }
2900#endif /* CONFIG_P2P */
2901
2902 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2903 sme_event_disassoc(wpa_s, info);
2904
2905 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2906 ie, ie_len, 0);
2907}
2908
2909
2910static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2911 struct deauth_info *info)
2912{
2913 u16 reason_code = 0;
2914 int locally_generated = 0;
2915 const u8 *addr = NULL;
2916 const u8 *ie = NULL;
2917 size_t ie_len = 0;
2918
2919 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2920
2921 if (info) {
2922 addr = info->addr;
2923 ie = info->ie;
2924 ie_len = info->ie_len;
2925 reason_code = info->reason_code;
2926 locally_generated = info->locally_generated;
2927 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2928 reason_code,
2929 locally_generated ? " (locally generated)" : "");
2930 if (addr) {
2931 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2932 MAC2STR(addr));
2933 }
2934 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2935 ie, ie_len);
2936 }
2937
2938 wpa_reset_ft_completed(wpa_s->wpa);
2939
2940 wpas_event_disconnect(wpa_s, addr, reason_code,
2941 locally_generated, ie, ie_len, 1);
2942}
2943
2944
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002945static const char * reg_init_str(enum reg_change_initiator init)
2946{
2947 switch (init) {
2948 case REGDOM_SET_BY_CORE:
2949 return "CORE";
2950 case REGDOM_SET_BY_USER:
2951 return "USER";
2952 case REGDOM_SET_BY_DRIVER:
2953 return "DRIVER";
2954 case REGDOM_SET_BY_COUNTRY_IE:
2955 return "COUNTRY_IE";
2956 case REGDOM_BEACON_HINT:
2957 return "BEACON_HINT";
2958 }
2959 return "?";
2960}
2961
2962
2963static const char * reg_type_str(enum reg_type type)
2964{
2965 switch (type) {
2966 case REGDOM_TYPE_UNKNOWN:
2967 return "UNKNOWN";
2968 case REGDOM_TYPE_COUNTRY:
2969 return "COUNTRY";
2970 case REGDOM_TYPE_WORLD:
2971 return "WORLD";
2972 case REGDOM_TYPE_CUSTOM_WORLD:
2973 return "CUSTOM_WORLD";
2974 case REGDOM_TYPE_INTERSECTION:
2975 return "INTERSECTION";
2976 }
2977 return "?";
2978}
2979
2980
2981static void wpa_supplicant_update_channel_list(
2982 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002983{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002984 struct wpa_supplicant *ifs;
2985
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002986 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002987 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002988 info->alpha2[0] ? " alpha2=" : "",
2989 info->alpha2[0] ? info->alpha2 : "");
2990
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002991 if (wpa_s->drv_priv == NULL)
2992 return; /* Ignore event during drv initialization */
2993
2994 free_hw_features(wpa_s);
2995 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2996 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2997
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002998 wpas_p2p_update_channel_list(wpa_s);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002999
3000 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003001 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003002 * so, they get updated with this same hw mode info.
3003 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003004 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3005 radio_list) {
3006 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003007 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3008 ifs->ifname);
3009 free_hw_features(ifs);
3010 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3011 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
3012 }
3013 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003014
3015 /* Restart sched_scan with updated channel list */
3016 if (wpa_s->sched_scanning) {
3017 wpa_dbg(wpa_s, MSG_DEBUG,
3018 "Channel list changed restart sched scan.");
3019 wpa_supplicant_cancel_sched_scan(wpa_s);
3020 wpa_supplicant_req_scan(wpa_s, 0, 0);
3021 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003022}
3023
3024
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003025static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003026 const u8 *frame, size_t len, int freq,
3027 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003028{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003029 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003030 const u8 *payload;
3031 size_t plen;
3032 u8 category;
3033
3034 if (len < IEEE80211_HDRLEN + 2)
3035 return;
3036
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003037 mgmt = (const struct ieee80211_mgmt *) frame;
3038 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003039 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003040 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003041
3042 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3043 " Category=%u DataLen=%d freq=%d MHz",
3044 MAC2STR(mgmt->sa), category, (int) plen, freq);
3045
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003046 if (category == WLAN_ACTION_WMM) {
3047 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3048 return;
3049 }
3050
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003051#ifdef CONFIG_IEEE80211R
3052 if (category == WLAN_ACTION_FT) {
3053 ft_rx_action(wpa_s, payload, plen);
3054 return;
3055 }
3056#endif /* CONFIG_IEEE80211R */
3057
3058#ifdef CONFIG_IEEE80211W
3059#ifdef CONFIG_SME
3060 if (category == WLAN_ACTION_SA_QUERY) {
3061 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3062 return;
3063 }
3064#endif /* CONFIG_SME */
3065#endif /* CONFIG_IEEE80211W */
3066
3067#ifdef CONFIG_WNM
3068 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3069 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3070 return;
3071 }
3072#endif /* CONFIG_WNM */
3073
3074#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003075 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3076 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003077 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003078 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003079 payload, plen, freq) == 0)
3080 return;
3081#endif /* CONFIG_GAS */
3082
3083#ifdef CONFIG_TDLS
3084 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3085 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3086 wpa_dbg(wpa_s, MSG_DEBUG,
3087 "TDLS: Received Discovery Response from " MACSTR,
3088 MAC2STR(mgmt->sa));
3089 return;
3090 }
3091#endif /* CONFIG_TDLS */
3092
3093#ifdef CONFIG_INTERWORKING
3094 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3095 payload[0] == QOS_QOS_MAP_CONFIG) {
3096 const u8 *pos = payload + 1;
3097 size_t qlen = plen - 1;
3098 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3099 MACSTR, MAC2STR(mgmt->sa));
3100 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3101 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3102 pos[1] <= qlen - 2 && pos[1] >= 16)
3103 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3104 return;
3105 }
3106#endif /* CONFIG_INTERWORKING */
3107
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003108 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3109 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3110 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3111 return;
3112 }
3113
3114 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3115 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3116 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3117 payload + 1, plen - 1,
3118 rssi);
3119 return;
3120 }
3121
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003122 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3123 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003124 if (wpa_s->ifmsh)
3125 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003126}
3127
3128
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003129static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3130 union wpa_event_data *event)
3131{
3132#ifdef CONFIG_P2P
3133 struct wpa_supplicant *ifs;
3134#endif /* CONFIG_P2P */
3135 struct wpa_freq_range_list *list;
3136 char *str = NULL;
3137
3138 list = &event->freq_range;
3139
3140 if (list->num)
3141 str = freq_range_list_str(list);
3142 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3143 str ? str : "");
3144
3145#ifdef CONFIG_P2P
3146 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3147 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3148 __func__);
3149 } else {
3150 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
3151 wpas_p2p_update_channel_list(wpa_s);
3152 }
3153
3154 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3155 int freq;
3156 if (!ifs->current_ssid ||
3157 !ifs->current_ssid->p2p_group ||
3158 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
3159 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
3160 continue;
3161
3162 freq = ifs->current_ssid->frequency;
3163 if (!freq_range_list_includes(list, freq)) {
3164 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
3165 freq);
3166 continue;
3167 }
3168
3169 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
3170 freq);
3171 /* TODO: Consider using CSA or removing the group within
3172 * wpa_supplicant */
3173 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
3174 }
3175#endif /* CONFIG_P2P */
3176
3177 os_free(str);
3178}
3179
3180
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003181static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3182 union wpa_event_data *data)
3183{
3184 wpa_dbg(wpa_s, MSG_DEBUG,
3185 "Connection authorized by device, previous state %d",
3186 wpa_s->wpa_state);
3187 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3188 wpa_supplicant_cancel_auth_timeout(wpa_s);
3189 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3190 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3191 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3192 }
3193 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3194 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003195 data->assoc_info.ptk_kck_len,
3196 data->assoc_info.ptk_kek,
3197 data->assoc_info.ptk_kek_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003198}
3199
3200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3202 union wpa_event_data *data)
3203{
3204 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003205 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206
3207 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3208 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003209 event != EVENT_INTERFACE_STATUS &&
3210 event != EVENT_SCHED_SCAN_STOPPED) {
3211 wpa_dbg(wpa_s, MSG_DEBUG,
3212 "Ignore event %s (%d) while interface is disabled",
3213 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 return;
3215 }
3216
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003217#ifndef CONFIG_NO_STDOUT_DEBUG
3218{
3219 int level = MSG_DEBUG;
3220
Dmitry Shmidt04949592012-07-19 12:16:46 -07003221 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003222 const struct ieee80211_hdr *hdr;
3223 u16 fc;
3224 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3225 fc = le_to_host16(hdr->frame_control);
3226 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3227 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3228 level = MSG_EXCESSIVE;
3229 }
3230
3231 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3232 event_to_string(event), event);
3233}
3234#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235
3236 switch (event) {
3237 case EVENT_AUTH:
3238 sme_event_auth(wpa_s, data);
3239 break;
3240 case EVENT_ASSOC:
3241 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003242 if (data && data->assoc_info.authorized)
3243 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003244 break;
3245 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003246 wpas_event_disassoc(wpa_s,
3247 data ? &data->disassoc_info : NULL);
3248 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003250 wpas_event_deauth(wpa_s,
3251 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252 break;
3253 case EVENT_MICHAEL_MIC_FAILURE:
3254 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3255 break;
3256#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003257 case EVENT_SCAN_STARTED:
3258 os_get_reltime(&wpa_s->scan_start_time);
3259 if (wpa_s->own_scan_requested) {
3260 struct os_reltime diff;
3261
3262 os_reltime_sub(&wpa_s->scan_start_time,
3263 &wpa_s->scan_trigger_time, &diff);
3264 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3265 diff.sec, diff.usec);
3266 wpa_s->own_scan_requested = 0;
3267 wpa_s->own_scan_running = 1;
3268 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3269 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003270 wpa_msg_ctrl(wpa_s, MSG_INFO,
3271 WPA_EVENT_SCAN_STARTED "id=%u",
3272 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003273 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003274 wpa_msg_ctrl(wpa_s, MSG_INFO,
3275 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003276 }
3277 } else {
3278 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003279 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003280 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003281 }
3282 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003283 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003284 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3285 struct os_reltime now, diff;
3286 os_get_reltime(&now);
3287 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3288 wpa_s->scan_start_time.sec = 0;
3289 wpa_s->scan_start_time.usec = 0;
3290 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3291 diff.sec, diff.usec);
3292 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003293 if (wpa_supplicant_event_scan_results(wpa_s, data))
3294 break; /* interface may have been removed */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003295 wpa_s->own_scan_running = 0;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003296 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003297 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 break;
3299#endif /* CONFIG_NO_SCAN_PROCESSING */
3300 case EVENT_ASSOCINFO:
3301 wpa_supplicant_event_associnfo(wpa_s, data);
3302 break;
3303 case EVENT_INTERFACE_STATUS:
3304 wpa_supplicant_event_interface_status(wpa_s, data);
3305 break;
3306 case EVENT_PMKID_CANDIDATE:
3307 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3308 break;
3309#ifdef CONFIG_PEERKEY
3310 case EVENT_STKSTART:
3311 wpa_supplicant_event_stkstart(wpa_s, data);
3312 break;
3313#endif /* CONFIG_PEERKEY */
3314#ifdef CONFIG_TDLS
3315 case EVENT_TDLS:
3316 wpa_supplicant_event_tdls(wpa_s, data);
3317 break;
3318#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003319#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003320 case EVENT_WNM:
3321 wpa_supplicant_event_wnm(wpa_s, data);
3322 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003323#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324#ifdef CONFIG_IEEE80211R
3325 case EVENT_FT_RESPONSE:
3326 wpa_supplicant_event_ft_response(wpa_s, data);
3327 break;
3328#endif /* CONFIG_IEEE80211R */
3329#ifdef CONFIG_IBSS_RSN
3330 case EVENT_IBSS_RSN_START:
3331 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3332 break;
3333#endif /* CONFIG_IBSS_RSN */
3334 case EVENT_ASSOC_REJECT:
3335 if (data->assoc_reject.bssid)
3336 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3337 "bssid=" MACSTR " status_code=%u",
3338 MAC2STR(data->assoc_reject.bssid),
3339 data->assoc_reject.status_code);
3340 else
3341 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3342 "status_code=%u",
3343 data->assoc_reject.status_code);
3344 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3345 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003346 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003347 const u8 *bssid = data->assoc_reject.bssid;
3348 if (bssid == NULL || is_zero_ether_addr(bssid))
3349 bssid = wpa_s->pending_bssid;
3350 wpas_connection_failed(wpa_s, bssid);
3351 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003352 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353 break;
3354 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003355 /* It is possible to get this event from earlier connection */
3356 if (wpa_s->current_ssid &&
3357 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3358 wpa_dbg(wpa_s, MSG_DEBUG,
3359 "Ignore AUTH_TIMED_OUT in mesh configuration");
3360 break;
3361 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3363 sme_event_auth_timed_out(wpa_s, data);
3364 break;
3365 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003366 /* It is possible to get this event from earlier connection */
3367 if (wpa_s->current_ssid &&
3368 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3369 wpa_dbg(wpa_s, MSG_DEBUG,
3370 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3371 break;
3372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3374 sme_event_assoc_timed_out(wpa_s, data);
3375 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003376 case EVENT_TX_STATUS:
3377 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3378 " type=%d stype=%d",
3379 MAC2STR(data->tx_status.dst),
3380 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003381#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003383#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3385 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003386 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387 wpa_s, data->tx_status.dst,
3388 data->tx_status.data,
3389 data->tx_status.data_len,
3390 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003391 OFFCHANNEL_SEND_ACTION_SUCCESS :
3392 OFFCHANNEL_SEND_ACTION_NO_ACK);
3393#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 break;
3395 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003396#endif /* CONFIG_AP */
3397#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3399 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3400 /*
3401 * Catch TX status events for Action frames we sent via group
3402 * interface in GO mode.
3403 */
3404 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3405 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3406 os_memcmp(wpa_s->parent->pending_action_dst,
3407 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 wpa_s->parent, data->tx_status.dst,
3410 data->tx_status.data,
3411 data->tx_status.data_len,
3412 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003413 OFFCHANNEL_SEND_ACTION_SUCCESS :
3414 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 break;
3416 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003417#endif /* CONFIG_OFFCHANNEL */
3418#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 switch (data->tx_status.type) {
3420 case WLAN_FC_TYPE_MGMT:
3421 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3422 data->tx_status.data_len,
3423 data->tx_status.stype,
3424 data->tx_status.ack);
3425 break;
3426 case WLAN_FC_TYPE_DATA:
3427 ap_tx_status(wpa_s, data->tx_status.dst,
3428 data->tx_status.data,
3429 data->tx_status.data_len,
3430 data->tx_status.ack);
3431 break;
3432 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003433#endif /* CONFIG_AP */
3434 break;
3435#ifdef CONFIG_AP
3436 case EVENT_EAPOL_TX_STATUS:
3437 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3438 data->eapol_tx_status.data,
3439 data->eapol_tx_status.data_len,
3440 data->eapol_tx_status.ack);
3441 break;
3442 case EVENT_DRIVER_CLIENT_POLL_OK:
3443 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444 break;
3445 case EVENT_RX_FROM_UNKNOWN:
3446 if (wpa_s->ap_iface == NULL)
3447 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003448 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3449 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003451 case EVENT_CH_SWITCH:
3452 if (!data)
3453 break;
3454 if (!wpa_s->ap_iface) {
3455 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3456 "event in non-AP mode");
3457 break;
3458 }
3459
Dmitry Shmidt04949592012-07-19 12:16:46 -07003460 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3461 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003462 data->ch_switch.ch_offset,
3463 data->ch_switch.ch_width,
3464 data->ch_switch.cf1,
3465 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003466 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003467#ifdef NEED_AP_MLME
3468 case EVENT_DFS_RADAR_DETECTED:
3469 if (data)
3470 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3471 break;
3472 case EVENT_DFS_CAC_STARTED:
3473 if (data)
3474 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3475 break;
3476 case EVENT_DFS_CAC_FINISHED:
3477 if (data)
3478 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3479 break;
3480 case EVENT_DFS_CAC_ABORTED:
3481 if (data)
3482 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3483 break;
3484 case EVENT_DFS_NOP_FINISHED:
3485 if (data)
3486 wpas_event_dfs_cac_nop_finished(wpa_s,
3487 &data->dfs_event);
3488 break;
3489#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003490#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003491 case EVENT_RX_MGMT: {
3492 u16 fc, stype;
3493 const struct ieee80211_mgmt *mgmt;
3494
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003495#ifdef CONFIG_TESTING_OPTIONS
3496 if (wpa_s->ext_mgmt_frame_handling) {
3497 struct rx_mgmt *rx = &data->rx_mgmt;
3498 size_t hex_len = 2 * rx->frame_len + 1;
3499 char *hex = os_malloc(hex_len);
3500 if (hex) {
3501 wpa_snprintf_hex(hex, hex_len,
3502 rx->frame, rx->frame_len);
3503 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3504 rx->freq, rx->datarate, rx->ssi_signal,
3505 hex);
3506 os_free(hex);
3507 }
3508 break;
3509 }
3510#endif /* CONFIG_TESTING_OPTIONS */
3511
Dmitry Shmidt04949592012-07-19 12:16:46 -07003512 mgmt = (const struct ieee80211_mgmt *)
3513 data->rx_mgmt.frame;
3514 fc = le_to_host16(mgmt->frame_control);
3515 stype = WLAN_FC_GET_STYPE(fc);
3516
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003517#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003519#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3522 data->rx_mgmt.frame_len > 24) {
3523 const u8 *src = mgmt->sa;
3524 const u8 *ie = mgmt->u.probe_req.variable;
3525 size_t ie_len = data->rx_mgmt.frame_len -
3526 (mgmt->u.probe_req.variable -
3527 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003528 wpas_p2p_probe_req_rx(
3529 wpa_s, src, mgmt->da,
3530 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003531 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003532 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 break;
3534 }
3535#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003536#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003537 if (wpa_s->current_ssid &&
3538 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3539 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003540 data->rx_mgmt.frame_len >= 30) {
3541 wpa_supplicant_event_ibss_auth(wpa_s, data);
3542 break;
3543 }
3544#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003545
3546 if (stype == WLAN_FC_STYPE_ACTION) {
3547 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003548 wpa_s, data->rx_mgmt.frame,
3549 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003550 data->rx_mgmt.freq,
3551 data->rx_mgmt.ssi_signal);
3552 break;
3553 }
3554
3555 if (wpa_s->ifmsh) {
3556 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003557 break;
3558 }
3559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3561 "management frame in non-AP mode");
3562 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003563#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003565
3566 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3567 data->rx_mgmt.frame_len > 24) {
3568 const u8 *ie = mgmt->u.probe_req.variable;
3569 size_t ie_len = data->rx_mgmt.frame_len -
3570 (mgmt->u.probe_req.variable -
3571 data->rx_mgmt.frame);
3572
3573 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3574 mgmt->bssid, ie, ie_len,
3575 data->rx_mgmt.ssi_signal);
3576 }
3577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003579#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582 case EVENT_RX_PROBE_REQ:
3583 if (data->rx_probe_req.sa == NULL ||
3584 data->rx_probe_req.ie == NULL)
3585 break;
3586#ifdef CONFIG_AP
3587 if (wpa_s->ap_iface) {
3588 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3589 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003590 data->rx_probe_req.da,
3591 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003593 data->rx_probe_req.ie_len,
3594 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 break;
3596 }
3597#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003599 data->rx_probe_req.da,
3600 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003602 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003603 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003604 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003607#ifdef CONFIG_OFFCHANNEL
3608 offchannel_remain_on_channel_cb(
3609 wpa_s, data->remain_on_channel.freq,
3610 data->remain_on_channel.duration);
3611#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 wpas_p2p_remain_on_channel_cb(
3613 wpa_s, data->remain_on_channel.freq,
3614 data->remain_on_channel.duration);
3615 break;
3616 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617#ifdef CONFIG_OFFCHANNEL
3618 offchannel_cancel_remain_on_channel_cb(
3619 wpa_s, data->remain_on_channel.freq);
3620#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 wpas_p2p_cancel_remain_on_channel_cb(
3622 wpa_s, data->remain_on_channel.freq);
3623 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 case EVENT_EAPOL_RX:
3625 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3626 data->eapol_rx.data,
3627 data->eapol_rx.data_len);
3628 break;
3629 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003630 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3631 "above=%d signal=%d noise=%d txrate=%d",
3632 data->signal_change.above_threshold,
3633 data->signal_change.current_signal,
3634 data->signal_change.current_noise,
3635 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003636 wpa_bss_update_level(wpa_s->current_bss,
3637 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 bgscan_notify_signal_change(
3639 wpa_s, data->signal_change.above_threshold,
3640 data->signal_change.current_signal,
3641 data->signal_change.current_noise,
3642 data->signal_change.current_txrate);
3643 break;
3644 case EVENT_INTERFACE_ENABLED:
3645 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3646 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003647 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003648 if (wpa_s->p2p_mgmt) {
3649 wpa_supplicant_set_state(wpa_s,
3650 WPA_DISCONNECTED);
3651 break;
3652 }
3653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654#ifdef CONFIG_AP
3655 if (!wpa_s->ap_iface) {
3656 wpa_supplicant_set_state(wpa_s,
3657 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003658 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 wpa_supplicant_req_scan(wpa_s, 0, 0);
3660 } else
3661 wpa_supplicant_set_state(wpa_s,
3662 WPA_COMPLETED);
3663#else /* CONFIG_AP */
3664 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3665 wpa_supplicant_req_scan(wpa_s, 0, 0);
3666#endif /* CONFIG_AP */
3667 }
3668 break;
3669 case EVENT_INTERFACE_DISABLED:
3670 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003671#ifdef CONFIG_P2P
3672 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3673 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3674 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3675 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003676 * Mark interface disabled if this happens to end up not
3677 * being removed as a separate P2P group interface.
3678 */
3679 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3680 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003681 * The interface was externally disabled. Remove
3682 * it assuming an external entity will start a
3683 * new session if needed.
3684 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003685 if (wpa_s->current_ssid &&
3686 wpa_s->current_ssid->p2p_group)
3687 wpas_p2p_interface_unavailable(wpa_s);
3688 else
3689 wpas_p2p_disconnect(wpa_s);
3690 /*
3691 * wpa_s instance may have been freed, so must not use
3692 * it here anymore.
3693 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003694 break;
3695 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003696 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3697 p2p_in_progress(wpa_s->global->p2p) > 1) {
3698 /* This radio work will be cancelled, so clear P2P
3699 * state as well.
3700 */
3701 p2p_stop_find(wpa_s->global->p2p);
3702 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003703#endif /* CONFIG_P2P */
3704
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003705 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3706 /*
3707 * Indicate disconnection to keep ctrl_iface events
3708 * consistent.
3709 */
3710 wpa_supplicant_event_disassoc(
3711 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3712 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003714 radio_remove_works(wpa_s, NULL, 0);
3715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3717 break;
3718 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003719 wpa_supplicant_update_channel_list(
3720 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003721 break;
3722 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 break;
3725 case EVENT_BEST_CHANNEL:
3726 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3727 "(%d %d %d)",
3728 data->best_chan.freq_24, data->best_chan.freq_5,
3729 data->best_chan.freq_overall);
3730 wpa_s->best_24_freq = data->best_chan.freq_24;
3731 wpa_s->best_5_freq = data->best_chan.freq_5;
3732 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003733 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3734 data->best_chan.freq_5,
3735 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 break;
3737 case EVENT_UNPROT_DEAUTH:
3738 wpa_supplicant_event_unprot_deauth(wpa_s,
3739 &data->unprot_deauth);
3740 break;
3741 case EVENT_UNPROT_DISASSOC:
3742 wpa_supplicant_event_unprot_disassoc(wpa_s,
3743 &data->unprot_disassoc);
3744 break;
3745 case EVENT_STATION_LOW_ACK:
3746#ifdef CONFIG_AP
3747 if (wpa_s->ap_iface && data)
3748 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3749 data->low_ack.addr);
3750#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003751#ifdef CONFIG_TDLS
3752 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003753 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3754 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003755#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 break;
3757 case EVENT_IBSS_PEER_LOST:
3758#ifdef CONFIG_IBSS_RSN
3759 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3760#endif /* CONFIG_IBSS_RSN */
3761 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003762 case EVENT_DRIVER_GTK_REKEY:
3763 if (os_memcmp(data->driver_gtk_rekey.bssid,
3764 wpa_s->bssid, ETH_ALEN))
3765 break;
3766 if (!wpa_s->wpa)
3767 break;
3768 wpa_sm_update_replay_ctr(wpa_s->wpa,
3769 data->driver_gtk_rekey.replay_ctr);
3770 break;
3771 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003772 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003773 wpa_s->sched_scanning = 0;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003774 resched = wpa_s->scanning;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003775 wpa_supplicant_notify_scanning(wpa_s, 0);
3776
3777 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3778 break;
3779
3780 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003781 * Start a new sched scan to continue searching for more SSIDs
3782 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003783 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07003784 if (wpa_s->sched_scan_timed_out) {
3785 wpa_supplicant_req_sched_scan(wpa_s);
3786 } else if (wpa_s->pno_sched_pending) {
3787 wpa_s->pno_sched_pending = 0;
3788 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003789 } else if (resched) {
3790 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003791 }
3792
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003793 break;
3794 case EVENT_WPS_BUTTON_PUSHED:
3795#ifdef CONFIG_WPS
3796 wpas_wps_start_pbc(wpa_s, NULL, 0);
3797#endif /* CONFIG_WPS */
3798 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003799 case EVENT_AVOID_FREQUENCIES:
3800 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3801 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003802 case EVENT_CONNECT_FAILED_REASON:
3803#ifdef CONFIG_AP
3804 if (!wpa_s->ap_iface || !data)
3805 break;
3806 hostapd_event_connect_failed_reason(
3807 wpa_s->ap_iface->bss[0],
3808 data->connect_failed_reason.addr,
3809 data->connect_failed_reason.code);
3810#endif /* CONFIG_AP */
3811 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003812 case EVENT_NEW_PEER_CANDIDATE:
3813#ifdef CONFIG_MESH
3814 if (!wpa_s->ifmsh || !data)
3815 break;
3816 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3817 data->mesh_peer.ies,
3818 data->mesh_peer.ie_len);
3819#endif /* CONFIG_MESH */
3820 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 default:
3822 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3823 break;
3824 }
3825}