blob: cf2550c708ff681f2dc42a705cedc579f461ac9a [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 Shmidtd80a4012015-11-05 16:35:40 -080026#include "fst/fst.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070027#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "common/ieee802_11_defs.h"
30#include "common/ieee802_11_common.h"
31#include "crypto/random.h"
32#include "blacklist.h"
33#include "wpas_glue.h"
34#include "wps_supplicant.h"
35#include "ibss_rsn.h"
36#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "p2p_supplicant.h"
39#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070040#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "ap.h"
42#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#include "interworking.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "mesh.h"
47#include "mesh_mpm.h"
48#include "wmm_ac.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049
50
Dmitry Shmidt34af3062013-07-11 10:46:32 -070051#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070052static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080053 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080055
56
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
58 struct wpa_ssid *ssid)
59{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070061
62 if (ssid == NULL || ssid->disabled_until.sec == 0)
63 return 0;
64
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080065 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 if (ssid->disabled_until.sec > now.sec)
67 return ssid->disabled_until.sec - now.sec;
68
69 wpas_clear_temp_disabled(wpa_s, ssid, 0);
70
71 return 0;
72}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
74
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070075/**
76 * wpas_reenabled_network_time - Time until first network is re-enabled
77 * @wpa_s: Pointer to wpa_supplicant data
78 * Returns: If all enabled networks are temporarily disabled, returns the time
79 * (in sec) until the first network is re-enabled. Otherwise returns 0.
80 *
81 * This function is used in case all enabled networks are temporarily disabled,
82 * in which case it returns the time (in sec) that the first network will be
83 * re-enabled. The function assumes that at least one network is enabled.
84 */
85static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
86{
87 struct wpa_ssid *ssid;
88 int disabled_for, res = 0;
89
90#ifdef CONFIG_INTERWORKING
91 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
92 wpa_s->conf->cred)
93 return 0;
94#endif /* CONFIG_INTERWORKING */
95
96 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
97 if (ssid->disabled)
98 continue;
99
100 disabled_for = wpas_temp_disabled(wpa_s, ssid);
101 if (!disabled_for)
102 return 0;
103
104 if (!res || disabled_for < res)
105 res = disabled_for;
106 }
107
108 return res;
109}
110
111
112void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
113{
114 struct wpa_supplicant *wpa_s = eloop_ctx;
115
116 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
117 return;
118
119 wpa_dbg(wpa_s, MSG_DEBUG,
120 "Try to associate due to network getting re-enabled");
121 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
122 wpa_supplicant_cancel_sched_scan(wpa_s);
123 wpa_supplicant_req_scan(wpa_s, 0, 0);
124 }
125}
126
127
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static struct wpa_bss * wpa_supplicant_get_new_bss(
129 struct wpa_supplicant *wpa_s, const u8 *bssid)
130{
131 struct wpa_bss *bss = NULL;
132 struct wpa_ssid *ssid = wpa_s->current_ssid;
133
134 if (ssid->ssid_len > 0)
135 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
136 if (!bss)
137 bss = wpa_bss_get_bssid(wpa_s, bssid);
138
139 return bss;
140}
141
142
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700143static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
144{
145 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
146
147 if (!bss) {
148 wpa_supplicant_update_scan_results(wpa_s);
149
150 /* Get the BSS from the new scan results */
151 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
152 }
153
154 if (bss)
155 wpa_s->current_bss = bss;
156}
157
158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
160{
161 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700162 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700163 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700164 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700166 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
167 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700168
169 if (wpa_s->current_ssid->ssid_len == 0)
170 return 0; /* current profile still in use */
171 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
172 if (res < 0) {
173 wpa_msg(wpa_s, MSG_INFO,
174 "Failed to read SSID from driver");
175 return 0; /* try to use current profile */
176 }
177 drv_ssid_len = res;
178
179 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
180 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
181 drv_ssid_len) == 0)
182 return 0; /* current profile still in use */
183
184 wpa_msg(wpa_s, MSG_DEBUG,
185 "Driver-initiated BSS selection changed the SSID to %s",
186 wpa_ssid_txt(drv_ssid, drv_ssid_len));
187 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
190 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
191 "information");
192 ssid = wpa_supplicant_get_ssid(wpa_s);
193 if (ssid == NULL) {
194 wpa_msg(wpa_s, MSG_INFO,
195 "No network configuration found for the current AP");
196 return -1;
197 }
198
Dmitry Shmidt04949592012-07-19 12:16:46 -0700199 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
201 return -1;
202 }
203
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800204 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
205 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
206 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
207 return -1;
208 }
209
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700210 res = wpas_temp_disabled(wpa_s, ssid);
211 if (res > 0) {
212 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
213 "disabled for %d second(s)", res);
214 return -1;
215 }
216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
218 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 u8 wpa_ie[80];
221 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800222 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
223 wpa_ie, &wpa_ie_len) < 0)
224 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 } else {
226 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
227 }
228
229 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
230 eapol_sm_invalidate_cached_session(wpa_s->eapol);
231 old_ssid = wpa_s->current_ssid;
232 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800233
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700234 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
237 wpa_supplicant_initiate_eapol(wpa_s);
238 if (old_ssid != wpa_s->current_ssid)
239 wpas_notify_network_changed(wpa_s);
240
241 return 0;
242}
243
244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246{
247 struct wpa_supplicant *wpa_s = eloop_ctx;
248
249 if (wpa_s->countermeasures) {
250 wpa_s->countermeasures = 0;
251 wpa_drv_set_countermeasures(wpa_s, 0);
252 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800253
254 /*
255 * It is possible that the device is sched scanning, which means
256 * that a connection attempt will be done only when we receive
257 * scan results. However, in this case, it would be preferable
258 * to scan and connect immediately, so cancel the sched_scan and
259 * issue a regular scan flow.
260 */
261 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 wpa_supplicant_req_scan(wpa_s, 0, 0);
263 }
264}
265
266
267void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
268{
269 int bssid_changed;
270
Dmitry Shmidt04949592012-07-19 12:16:46 -0700271 wnm_bss_keep_alive_deinit(wpa_s);
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273#ifdef CONFIG_IBSS_RSN
274 ibss_rsn_deinit(wpa_s->ibss_rsn);
275 wpa_s->ibss_rsn = NULL;
276#endif /* CONFIG_IBSS_RSN */
277
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700278#ifdef CONFIG_AP
279 wpa_supplicant_ap_deinit(wpa_s);
280#endif /* CONFIG_AP */
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
283 return;
284
285 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
286 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
287 os_memset(wpa_s->bssid, 0, ETH_ALEN);
288 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800289 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 wpa_s->current_bss = NULL;
291 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if (bssid_changed)
294 wpas_notify_bssid_changed(wpa_s);
295
296 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
297 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
298 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
299 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
300 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700302 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800304
305 wpas_rrm_reset(wpa_s);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800306 wpa_s->wnmsleep_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307}
308
309
310static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
311{
312 struct wpa_ie_data ie;
313 int pmksa_set = -1;
314 size_t i;
315
316 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
317 ie.pmkid == NULL)
318 return;
319
320 for (i = 0; i < ie.num_pmkid; i++) {
321 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
322 ie.pmkid + i * PMKID_LEN,
323 NULL, NULL, 0);
324 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800325 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 break;
327 }
328 }
329
330 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
331 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
332}
333
334
335static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
336 union wpa_event_data *data)
337{
338 if (data == NULL) {
339 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
340 "event");
341 return;
342 }
343 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
344 " index=%d preauth=%d",
345 MAC2STR(data->pmkid_candidate.bssid),
346 data->pmkid_candidate.index,
347 data->pmkid_candidate.preauth);
348
349 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
350 data->pmkid_candidate.index,
351 data->pmkid_candidate.preauth);
352}
353
354
355static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
356{
357 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
358 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
359 return 0;
360
361#ifdef IEEE8021X_EAPOL
362 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
363 wpa_s->current_ssid &&
364 !(wpa_s->current_ssid->eapol_flags &
365 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
366 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
367 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
368 * plaintext or static WEP keys). */
369 return 0;
370 }
371#endif /* IEEE8021X_EAPOL */
372
373 return 1;
374}
375
376
377/**
378 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
379 * @wpa_s: pointer to wpa_supplicant data
380 * @ssid: Configuration data for the network
381 * Returns: 0 on success, -1 on failure
382 *
383 * This function is called when starting authentication with a network that is
384 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
385 */
386int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
387 struct wpa_ssid *ssid)
388{
389#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800390#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700391 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700393 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
394 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 return 0;
396
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700397 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700398 sim = 1;
399 aka = 1;
400 } else {
401 struct eap_method_type *eap = ssid->eap.eap_methods;
402 while (eap->vendor != EAP_VENDOR_IETF ||
403 eap->method != EAP_TYPE_NONE) {
404 if (eap->vendor == EAP_VENDOR_IETF) {
405 if (eap->method == EAP_TYPE_SIM)
406 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700407 else if (eap->method == EAP_TYPE_AKA ||
408 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 aka = 1;
410 }
411 eap++;
412 }
413 }
414
415 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
416 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700417 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
418 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
419 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 aka = 0;
421
422 if (!sim && !aka) {
423 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
424 "use SIM, but neither EAP-SIM nor EAP-AKA are "
425 "enabled");
426 return 0;
427 }
428
429 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
430 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700432 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 if (wpa_s->scard == NULL) {
434 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
435 "(pcsc-lite)");
436 return -1;
437 }
438 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
439 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441#endif /* IEEE8021X_EAPOL */
442
443 return 0;
444}
445
446
447#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700448
449static int has_wep_key(struct wpa_ssid *ssid)
450{
451 int i;
452
453 for (i = 0; i < NUM_WEP_KEYS; i++) {
454 if (ssid->wep_key_len[i])
455 return 1;
456 }
457
458 return 0;
459}
460
461
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700462static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 struct wpa_ssid *ssid)
464{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700465 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466
467 if (ssid->mixed_cell)
468 return 1;
469
470#ifdef CONFIG_WPS
471 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
472 return 1;
473#endif /* CONFIG_WPS */
474
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700475 if (has_wep_key(ssid))
476 privacy = 1;
477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478#ifdef IEEE8021X_EAPOL
479 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
480 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
481 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
482 privacy = 1;
483#endif /* IEEE8021X_EAPOL */
484
Jouni Malinen75ecf522011-06-27 15:19:46 -0700485 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
486 privacy = 1;
487
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800488 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
489 privacy = 1;
490
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 if (bss->caps & IEEE80211_CAP_PRIVACY)
492 return privacy;
493 return !privacy;
494}
495
496
497static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
498 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700499 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500{
501 struct wpa_ie_data ie;
502 int proto_match = 0;
503 const u8 *rsn_ie, *wpa_ie;
504 int ret;
505 int wep_ok;
506
507 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
508 if (ret >= 0)
509 return ret;
510
511 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
512 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
513 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
514 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
515 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
516
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700517 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
519 proto_match++;
520
521 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
522 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
523 "failed");
524 break;
525 }
526
527 if (wep_ok &&
528 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
529 {
530 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
531 "in RSN IE");
532 return 1;
533 }
534
535 if (!(ie.proto & ssid->proto)) {
536 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
537 "mismatch");
538 break;
539 }
540
541 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
542 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
543 "cipher mismatch");
544 break;
545 }
546
547 if (!(ie.group_cipher & ssid->group_cipher)) {
548 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
549 "cipher mismatch");
550 break;
551 }
552
553 if (!(ie.key_mgmt & ssid->key_mgmt)) {
554 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
555 "mismatch");
556 break;
557 }
558
559#ifdef CONFIG_IEEE80211W
560 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800561 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800562 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
564 "frame protection");
565 break;
566 }
567#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800568 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
569 wpas_get_ssid_pmf(wpa_s, ssid) ==
570 NO_MGMT_FRAME_PROTECTION) {
571 wpa_dbg(wpa_s, MSG_DEBUG,
572 " skip RSN IE - no mgmt frame protection enabled but AP requires it");
573 break;
574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575
576 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
577 return 1;
578 }
579
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700580 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
582 proto_match++;
583
584 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
585 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
586 "failed");
587 break;
588 }
589
590 if (wep_ok &&
591 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
592 {
593 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
594 "in WPA IE");
595 return 1;
596 }
597
598 if (!(ie.proto & ssid->proto)) {
599 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
600 "mismatch");
601 break;
602 }
603
604 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
605 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
606 "cipher mismatch");
607 break;
608 }
609
610 if (!(ie.group_cipher & ssid->group_cipher)) {
611 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
612 "cipher mismatch");
613 break;
614 }
615
616 if (!(ie.key_mgmt & ssid->key_mgmt)) {
617 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
618 "mismatch");
619 break;
620 }
621
622 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
623 return 1;
624 }
625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700626 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
627 !rsn_ie) {
628 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
629 return 1;
630 }
631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
633 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
634 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
635 return 0;
636 }
637
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800638 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
639 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
640 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
641 return 1;
642 }
643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
645 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
646 return 1;
647 }
648
649 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
650 "WPA/WPA2");
651
652 return 0;
653}
654
655
656static int freq_allowed(int *freqs, int freq)
657{
658 int i;
659
660 if (freqs == NULL)
661 return 1;
662
663 for (i = 0; freqs[i]; i++)
664 if (freqs[i] == freq)
665 return 1;
666 return 0;
667}
668
669
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700670static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800671{
672 const struct hostapd_hw_modes *mode = NULL, *modes;
673 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
674 const u8 *rate_ie;
675 int i, j, k;
676
677 if (bss->freq == 0)
678 return 1; /* Cannot do matching without knowing band */
679
680 modes = wpa_s->hw.modes;
681 if (modes == NULL) {
682 /*
683 * The driver does not provide any additional information
684 * about the utilized hardware, so allow the connection attempt
685 * to continue.
686 */
687 return 1;
688 }
689
690 for (i = 0; i < wpa_s->hw.num_modes; i++) {
691 for (j = 0; j < modes[i].num_channels; j++) {
692 int freq = modes[i].channels[j].freq;
693 if (freq == bss->freq) {
694 if (mode &&
695 mode->mode == HOSTAPD_MODE_IEEE80211G)
696 break; /* do not allow 802.11b replace
697 * 802.11g */
698 mode = &modes[i];
699 break;
700 }
701 }
702 }
703
704 if (mode == NULL)
705 return 0;
706
707 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700708 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709 if (rate_ie == NULL)
710 continue;
711
712 for (j = 2; j < rate_ie[1] + 2; j++) {
713 int flagged = !!(rate_ie[j] & 0x80);
714 int r = (rate_ie[j] & 0x7f) * 5;
715
716 /*
717 * IEEE Std 802.11n-2009 7.3.2.2:
718 * The new BSS Membership selector value is encoded
719 * like a legacy basic rate, but it is not a rate and
720 * only indicates if the BSS members are required to
721 * support the mandatory features of Clause 20 [HT PHY]
722 * in order to join the BSS.
723 */
724 if (flagged && ((rate_ie[j] & 0x7f) ==
725 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
726 if (!ht_supported(mode)) {
727 wpa_dbg(wpa_s, MSG_DEBUG,
728 " hardware does not support "
729 "HT PHY");
730 return 0;
731 }
732 continue;
733 }
734
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700735 /* There's also a VHT selector for 802.11ac */
736 if (flagged && ((rate_ie[j] & 0x7f) ==
737 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
738 if (!vht_supported(mode)) {
739 wpa_dbg(wpa_s, MSG_DEBUG,
740 " hardware does not support "
741 "VHT PHY");
742 return 0;
743 }
744 continue;
745 }
746
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800747 if (!flagged)
748 continue;
749
750 /* check for legacy basic rates */
751 for (k = 0; k < mode->num_rates; k++) {
752 if (mode->rates[k] == r)
753 break;
754 }
755 if (k == mode->num_rates) {
756 /*
757 * IEEE Std 802.11-2007 7.3.2.2 demands that in
758 * order to join a BSS all required rates
759 * have to be supported by the hardware.
760 */
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -0700761 wpa_dbg(wpa_s, MSG_DEBUG,
762 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
763 r / 10, r % 10,
764 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 return 0;
766 }
767 }
768 }
769
770 return 1;
771}
772
773
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800774/*
775 * Test whether BSS is in an ESS.
776 * This is done differently in DMG (60 GHz) and non-DMG bands
777 */
778static int bss_is_ess(struct wpa_bss *bss)
779{
780 if (bss_is_dmg(bss)) {
781 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
782 IEEE80211_CAP_DMG_AP;
783 }
784
785 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
786 IEEE80211_CAP_ESS);
787}
788
789
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800790static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
791{
792 size_t i;
793
794 for (i = 0; i < ETH_ALEN; i++) {
795 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
796 return 0;
797 }
798 return 1;
799}
800
801
802static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
803{
804 size_t i;
805
806 for (i = 0; i < num; i++) {
807 const u8 *a = list + i * ETH_ALEN * 2;
808 const u8 *m = a + ETH_ALEN;
809
810 if (match_mac_mask(a, addr, m))
811 return 1;
812 }
813 return 0;
814}
815
816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700818 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800819 struct wpa_ssid *group,
820 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700822 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 int wpa;
824 struct wpa_blacklist *e;
825 const u8 *ie;
826 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800827 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700829 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 wpa_ie_len = ie ? ie[1] : 0;
831
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700832 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 rsn_ie_len = ie ? ie[1] : 0;
834
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800835 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
836 osen = ie != NULL;
837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800839 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700840 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800841 wpa_ie_len, rsn_ie_len, bss->caps, bss->level, bss->freq,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700842 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
843 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
844 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800845 " p2p" : "",
846 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847
848 e = wpa_blacklist_get(wpa_s, bss->bssid);
849 if (e) {
850 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700851 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 /*
853 * When only a single network is enabled, we can
854 * trigger blacklisting on the first failure. This
855 * should not be done with multiple enabled networks to
856 * avoid getting forced to move into a worse ESS on
857 * single error if there are no other BSSes of the
858 * current ESS.
859 */
860 limit = 0;
861 }
862 if (e->count > limit) {
863 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
864 "(count=%d limit=%d)", e->count, limit);
865 return NULL;
866 }
867 }
868
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700869 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
871 return NULL;
872 }
873
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800874 if (disallowed_bssid(wpa_s, bss->bssid)) {
875 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
876 return NULL;
877 }
878
879 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
880 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
881 return NULL;
882 }
883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
885
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800886 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700888 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889
Dmitry Shmidt04949592012-07-19 12:16:46 -0700890 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
892 continue;
893 }
894
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700895 res = wpas_temp_disabled(wpa_s, ssid);
896 if (res > 0) {
897 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
898 "temporarily for %d second(s)", res);
899 continue;
900 }
901
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902#ifdef CONFIG_WPS
903 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
904 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
905 "(WPS)");
906 continue;
907 }
908
909 if (wpa && ssid->ssid_len == 0 &&
910 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
911 check_ssid = 0;
912
913 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
914 /* Only allow wildcard SSID match if an AP
915 * advertises active WPS operation that matches
916 * with our mode. */
917 check_ssid = 1;
918 if (ssid->ssid_len == 0 &&
919 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
920 check_ssid = 0;
921 }
922#endif /* CONFIG_WPS */
923
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 if (ssid->bssid_set && ssid->ssid_len == 0 &&
925 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
926 check_ssid = 0;
927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700929 (bss->ssid_len != ssid->ssid_len ||
930 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
932 continue;
933 }
934
935 if (ssid->bssid_set &&
936 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
937 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
938 continue;
939 }
940
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800941 /* check blacklist */
942 if (ssid->num_bssid_blacklist &&
943 addr_in_list(bss->bssid, ssid->bssid_blacklist,
944 ssid->num_bssid_blacklist)) {
945 wpa_dbg(wpa_s, MSG_DEBUG,
946 " skip - BSSID blacklisted");
947 continue;
948 }
949
950 /* if there is a whitelist, only accept those APs */
951 if (ssid->num_bssid_whitelist &&
952 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
953 ssid->num_bssid_whitelist)) {
954 wpa_dbg(wpa_s, MSG_DEBUG,
955 " skip - BSSID not in whitelist");
956 continue;
957 }
958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
960 continue;
961
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800962 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
964 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
965 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
966 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
967 "not allowed");
968 continue;
969 }
970
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700971 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
972 has_wep_key(ssid)) {
973 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
974 continue;
975 }
976
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800977 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
978 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
979 "not allowed");
980 continue;
981 }
982
Jouni Malinen75ecf522011-06-27 15:19:46 -0700983 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
985 "mismatch");
986 continue;
987 }
988
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800989 if (!bss_is_ess(bss)) {
990 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 continue;
992 }
993
994 if (!freq_allowed(ssid->freq_list, bss->freq)) {
995 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
996 "allowed");
997 continue;
998 }
999
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 if (!rate_match(wpa_s, bss)) {
1001 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
1002 "not match");
1003 continue;
1004 }
1005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001007 if (ssid->p2p_group &&
1008 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1009 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1010 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
1011 continue;
1012 }
1013
Dmitry Shmidt54605472013-11-08 11:10:19 -08001014 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1015 struct wpabuf *p2p_ie;
1016 u8 dev_addr[ETH_ALEN];
1017
1018 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1019 if (ie == NULL) {
1020 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
1021 continue;
1022 }
1023 p2p_ie = wpa_bss_get_vendor_ie_multi(
1024 bss, P2P_IE_VENDOR_TYPE);
1025 if (p2p_ie == NULL) {
1026 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
1027 continue;
1028 }
1029
1030 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1031 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1032 ETH_ALEN) != 0) {
1033 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
1034 wpabuf_free(p2p_ie);
1035 continue;
1036 }
1037 wpabuf_free(p2p_ie);
1038 }
1039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040 /*
1041 * TODO: skip the AP if its P2P IE has Group Formation
1042 * bit set in the P2P Group Capability Bitmap and we
1043 * are not in Group Formation with that device.
1044 */
1045#endif /* CONFIG_P2P */
1046
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001047 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1048 {
1049 struct os_reltime diff;
1050
1051 os_reltime_sub(&wpa_s->scan_min_time,
1052 &bss->last_update, &diff);
1053 wpa_dbg(wpa_s, MSG_DEBUG,
1054 " skip - scan result not recent enough (%u.%06u seconds too old)",
1055 (unsigned int) diff.sec,
1056 (unsigned int) diff.usec);
1057 continue;
1058 }
1059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 /* Matching configuration found */
1061 return ssid;
1062 }
1063
1064 /* No matching configuration found */
1065 return NULL;
1066}
1067
1068
1069static struct wpa_bss *
1070wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001072 struct wpa_ssid **selected_ssid,
1073 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001075 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001077 if (only_first_ssid)
1078 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1079 group->id);
1080 else
1081 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1082 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001084 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1085 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001086 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1087 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 if (!*selected_ssid)
1089 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1091 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001092 MAC2STR(bss->bssid),
1093 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1094 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 }
1096
1097 return NULL;
1098}
1099
1100
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001101struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1102 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103{
1104 struct wpa_bss *selected = NULL;
1105 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001106 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001107 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001109 if (wpa_s->last_scan_res == NULL ||
1110 wpa_s->last_scan_res_used == 0)
1111 return NULL; /* no scan results from last update */
1112
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001113 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001114 /* check that next_ssid is still valid */
1115 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1116 if (ssid == wpa_s->next_ssid)
1117 break;
1118 }
1119 next_ssid = ssid;
1120 wpa_s->next_ssid = NULL;
1121 }
1122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001124 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1125 if (next_ssid && next_ssid->priority ==
1126 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001127 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001128 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001129 if (selected)
1130 break;
1131 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001133 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001134 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 if (selected)
1136 break;
1137 }
1138
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001139 if (selected == NULL && wpa_s->blacklist &&
1140 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1142 "blacklist and try again");
1143 wpa_blacklist_clear(wpa_s);
1144 wpa_s->blacklist_cleared++;
1145 } else if (selected == NULL)
1146 break;
1147 }
1148
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001149 ssid = *selected_ssid;
1150 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1151 !ssid->passphrase && !ssid->ext_psk) {
1152 const char *field_name, *txt = NULL;
1153
1154 wpa_dbg(wpa_s, MSG_DEBUG,
1155 "PSK/passphrase not yet available for the selected network");
1156
1157 wpas_notify_network_request(wpa_s, ssid,
1158 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1159
1160 field_name = wpa_supplicant_ctrl_req_to_string(
1161 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1162 if (field_name == NULL)
1163 return NULL;
1164
1165 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1166
1167 selected = NULL;
1168 }
1169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001170 return selected;
1171}
1172
1173
1174static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1175 int timeout_sec, int timeout_usec)
1176{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001177 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 /*
1179 * No networks are enabled; short-circuit request so
1180 * we don't wait timeout seconds before transitioning
1181 * to INACTIVE state.
1182 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001183 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1184 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1186 return;
1187 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001188
1189 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1191}
1192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001193
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001194int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001195 struct wpa_bss *selected,
1196 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197{
1198 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1199 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1200 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001201 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001203 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1204 wpa_s->p2p_in_provisioning) {
1205 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1206 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001207 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209#endif /* CONFIG_P2P */
1210
1211#ifdef CONFIG_WPS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001212 wpas_wps_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 wpas_wps_cancel(wpa_s);
1214#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001215 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216 }
1217
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001218 wpa_msg(wpa_s, MSG_DEBUG,
1219 "Considering connect request: reassociate: %d selected: "
1220 MACSTR " bssid: " MACSTR " pending: " MACSTR
1221 " wpa_state: %s ssid=%p current_ssid=%p",
1222 wpa_s->reassociate, MAC2STR(selected->bssid),
1223 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1224 wpa_supplicant_state_txt(wpa_s->wpa_state),
1225 ssid, wpa_s->current_ssid);
1226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 /*
1228 * Do not trigger new association unless the BSSID has changed or if
1229 * reassociation is requested. If we are in process of associating with
1230 * the selected BSSID, do not trigger new attempt.
1231 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1234 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1235 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001236 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1237 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1238 0) ||
1239 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1240 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1242 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001243 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001245 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1246 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 wpa_supplicant_associate(wpa_s, selected, ssid);
1248 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001249 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1250 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001251 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001253 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254}
1255
1256
1257static struct wpa_ssid *
1258wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1259{
1260 int prio;
1261 struct wpa_ssid *ssid;
1262
1263 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1264 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1265 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001266 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 continue;
1268 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001269 ssid->mode == IEEE80211_MODE_AP ||
1270 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 return ssid;
1272 }
1273 }
1274 return NULL;
1275}
1276
1277
1278/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1279 * on BSS added and BSS changed events */
1280static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001281 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001283 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
1285 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1286 return;
1287
Jouni Malinen87fd2792011-05-16 18:35:42 +03001288 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290
Jouni Malinen87fd2792011-05-16 18:35:42 +03001291 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292 if (ssid == NULL)
1293 continue;
1294
Jouni Malinen87fd2792011-05-16 18:35:42 +03001295 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 if (rsn == NULL)
1297 continue;
1298
Jouni Malinen87fd2792011-05-16 18:35:42 +03001299 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 }
1301
1302}
1303
1304
1305static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1306 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001307 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001309 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001310#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 int min_diff;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001312#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313
1314 if (wpa_s->reassociate)
1315 return 1; /* explicit request to reassociate */
1316 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1317 return 1; /* we are not associated; continue */
1318 if (wpa_s->current_ssid == NULL)
1319 return 1; /* unknown current SSID */
1320 if (wpa_s->current_ssid != ssid)
1321 return 1; /* different network block */
1322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001323 if (wpas_driver_bss_selection(wpa_s))
1324 return 0; /* Driver-based roaming */
1325
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001326 if (wpa_s->current_ssid->ssid)
1327 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1328 wpa_s->current_ssid->ssid,
1329 wpa_s->current_ssid->ssid_len);
1330 if (!current_bss)
1331 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332
1333 if (!current_bss)
1334 return 1; /* current BSS not seen in scan results */
1335
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001336 if (current_bss == selected)
1337 return 0;
1338
1339 if (selected->last_update_idx > current_bss->last_update_idx)
1340 return 1; /* current BSS not seen in the last scan */
1341
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001342#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001344 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1345 " level=%d snr=%d est_throughput=%u",
1346 MAC2STR(current_bss->bssid), current_bss->level,
1347 current_bss->snr, current_bss->est_throughput);
1348 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1349 " level=%d snr=%d est_throughput=%u",
1350 MAC2STR(selected->bssid), selected->level,
1351 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352
1353 if (wpa_s->current_ssid->bssid_set &&
1354 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1355 0) {
1356 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1357 "has preferred BSSID");
1358 return 1;
1359 }
1360
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001361 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1362 wpa_dbg(wpa_s, MSG_DEBUG,
1363 "Allow reassociation - selected BSS has better estimated throughput");
1364 return 1;
1365 }
1366
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001367 if (current_bss->level < 0 && current_bss->level > selected->level) {
1368 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1369 "signal level");
1370 return 0;
1371 }
1372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 min_diff = 2;
1374 if (current_bss->level < 0) {
1375 if (current_bss->level < -85)
1376 min_diff = 1;
1377 else if (current_bss->level < -80)
1378 min_diff = 2;
1379 else if (current_bss->level < -75)
1380 min_diff = 3;
1381 else if (current_bss->level < -70)
1382 min_diff = 4;
1383 else
1384 min_diff = 5;
1385 }
1386 if (abs(current_bss->level - selected->level) < min_diff) {
1387 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1388 "in signal level");
1389 return 0;
1390 }
1391
1392 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001393#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001394 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001395#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396}
1397
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001398
Jouni Malinen89ca7022012-09-14 13:03:12 -07001399/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001400 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001401static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001402 union wpa_event_data *data,
1403 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001405 struct wpa_scan_results *scan_res = NULL;
1406 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001408#ifndef CONFIG_NO_RANDOM_POOL
1409 size_t i, num;
1410#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
1412#ifdef CONFIG_AP
1413 if (wpa_s->ap_iface)
1414 ap = 1;
1415#endif /* CONFIG_AP */
1416
1417 wpa_supplicant_notify_scanning(wpa_s, 0);
1418
1419 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1420 data ? &data->scan_info :
1421 NULL, 1);
1422 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001423 if (wpa_s->conf->ap_scan == 2 || ap ||
1424 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001426 if (!own_request)
1427 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001428 if (data && data->scan_info.external_scan)
1429 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1431 "scanning again");
1432 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001433 ret = -1;
1434 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001435 }
1436
1437#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 num = scan_res->num;
1439 if (num > 10)
1440 num = 10;
1441 for (i = 0; i < num; i++) {
1442 u8 buf[5];
1443 struct wpa_scan_res *res = scan_res->res[i];
1444 buf[0] = res->bssid[5];
1445 buf[1] = res->qual & 0xff;
1446 buf[2] = res->noise & 0xff;
1447 buf[3] = res->level & 0xff;
1448 buf[4] = res->tsf & 0xff;
1449 random_add_randomness(buf, sizeof(buf));
1450 }
1451#endif /* CONFIG_NO_RANDOM_POOL */
1452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001453 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001454 !(data && data->scan_info.external_scan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1456 struct wpa_scan_results *scan_res);
1457
1458 scan_res_handler = wpa_s->scan_res_handler;
1459 wpa_s->scan_res_handler = NULL;
1460 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001461 ret = -2;
1462 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463 }
1464
1465 if (ap) {
1466 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1467#ifdef CONFIG_AP
1468 if (wpa_s->ap_iface->scan_cb)
1469 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1470#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001471 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001473
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001474 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001475 wpa_s->own_scan_running,
1476 data ? data->scan_info.external_scan : 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001477 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001478 wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1479 own_request && !(data && data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001480 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1481 wpa_s->manual_scan_id);
1482 wpa_s->manual_scan_use_id = 0;
1483 } else {
1484 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1485 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001486 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487
1488 wpas_notify_scan_done(wpa_s, 1);
1489
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001490 if (data && data->scan_info.external_scan) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001491 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 -07001492 wpa_scan_results_free(scan_res);
1493 return 0;
1494 }
1495
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001496 if (wnm_scan_process(wpa_s, 1) > 0)
1497 goto scan_work_done;
1498
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001499 if (sme_proc_obss_scan(wpa_s) > 0)
1500 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1503 goto scan_work_done;
1504
1505 if (autoscan_notify_scan(wpa_s, scan_res))
1506 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001507
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 if (wpa_s->disconnected) {
1509 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001510 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 }
1512
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001513 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001514 bgscan_notify_scan(wpa_s, scan_res) == 1)
1515 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001517 wpas_wps_update_ap_info(wpa_s, scan_res);
1518
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001519 wpa_scan_results_free(scan_res);
1520
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001521 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522 struct wpa_radio_work *work = wpa_s->scan_work;
1523 wpa_s->scan_work = NULL;
1524 radio_work_done(work);
1525 }
1526
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001527 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001528
1529scan_work_done:
1530 wpa_scan_results_free(scan_res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001531 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001532 struct wpa_radio_work *work = wpa_s->scan_work;
1533 wpa_s->scan_work = NULL;
1534 radio_work_done(work);
1535 }
1536 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001537}
1538
1539
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001540static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001541 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001542{
1543 struct wpa_bss *selected;
1544 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001545 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1546
1547 if (time_to_reenable > 0) {
1548 wpa_dbg(wpa_s, MSG_DEBUG,
1549 "Postpone network selection by %d seconds since all networks are disabled",
1550 time_to_reenable);
1551 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1552 eloop_register_timeout(time_to_reenable, 0,
1553 wpas_network_reenabled, wpa_s, NULL);
1554 return 0;
1555 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001556
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001557 if (wpa_s->p2p_mgmt)
1558 return 0; /* no normal connection on p2p_mgmt interface */
1559
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001560 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561
1562 if (selected) {
1563 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001564 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001565 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001566 if (new_scan)
1567 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001569 }
1570
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001571 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001572 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001573 return -1;
1574 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001575 if (new_scan)
1576 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001577 /*
1578 * Do not notify other virtual radios of scan results since we do not
1579 * want them to start other associations at the same time.
1580 */
1581 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001583#ifdef CONFIG_MESH
1584 if (wpa_s->ifmsh) {
1585 wpa_msg(wpa_s, MSG_INFO,
1586 "Avoiding join because we already joined a mesh group");
1587 return 0;
1588 }
1589#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1591 ssid = wpa_supplicant_pick_new_network(wpa_s);
1592 if (ssid) {
1593 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1594 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001595 if (new_scan)
1596 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001597 } else if (own_request) {
1598 /*
1599 * No SSID found. If SCAN results are as a result of
1600 * own scan request and not due to a scan request on
1601 * another shared interface, try another scan.
1602 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 int timeout_sec = wpa_s->scan_interval;
1604 int timeout_usec = 0;
1605#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001606 int res;
1607
1608 res = wpas_p2p_scan_no_go_seen(wpa_s);
1609 if (res == 2)
1610 return 2;
1611 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001612 return 0;
1613
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001614 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001615 wpa_s->show_group_started ||
1616 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 /*
1618 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001619 * state and during P2P join-a-group operation
1620 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621 */
1622 timeout_sec = 0;
1623 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1625 timeout_usec);
1626 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 }
1628#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001629#ifdef CONFIG_INTERWORKING
1630 if (wpa_s->conf->auto_interworking &&
1631 wpa_s->conf->interworking &&
1632 wpa_s->conf->cred) {
1633 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1634 "start ANQP fetch since no matching "
1635 "networks found");
1636 wpa_s->network_select = 1;
1637 wpa_s->auto_network_select = 1;
1638 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001639 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001640 }
1641#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001642#ifdef CONFIG_WPS
1643 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1644 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1645 timeout_sec = 0;
1646 timeout_usec = 500000;
1647 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1648 timeout_usec);
1649 return 0;
1650 }
1651#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001652 if (wpa_supplicant_req_sched_scan(wpa_s))
1653 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1654 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001655
1656 wpa_msg_ctrl(wpa_s, MSG_INFO,
1657 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 }
1659 }
1660 return 0;
1661}
1662
1663
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001664static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1665 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001668 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001669
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001670 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1);
1671 if (res == 2) {
1672 /*
1673 * Interface may have been removed, so must not dereference
1674 * wpa_s after this.
1675 */
1676 return 1;
1677 }
1678 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 /*
1680 * If no scan results could be fetched, then no need to
1681 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001682 * this scan. Similarly, if scan results started a new operation on this
1683 * interface, do not notify other interfaces to avoid concurrent
1684 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001686 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 }
1688
1689 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001690 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 * so, they get updated with this same scan info.
1692 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001693 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1694 radio_list) {
1695 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1697 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001698 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 }
1700 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001701
1702 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703}
1704
1705#endif /* CONFIG_NO_SCAN_PROCESSING */
1706
1707
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001708int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1709{
1710#ifdef CONFIG_NO_SCAN_PROCESSING
1711 return -1;
1712#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001713 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001714
Dmitry Shmidt41712582015-06-29 11:02:15 -07001715 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001716 return -1;
1717
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001718 os_get_reltime(&now);
1719 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001720 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1721 return -1;
1722 }
1723
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001724 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001725#endif /* CONFIG_NO_SCAN_PROCESSING */
1726}
1727
Dmitry Shmidt04949592012-07-19 12:16:46 -07001728#ifdef CONFIG_WNM
1729
1730static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1731{
1732 struct wpa_supplicant *wpa_s = eloop_ctx;
1733
1734 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1735 return;
1736
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001737 if (!wpa_s->no_keep_alive) {
1738 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1739 MAC2STR(wpa_s->bssid));
1740 /* TODO: could skip this if normal data traffic has been sent */
1741 /* TODO: Consider using some more appropriate data frame for
1742 * this */
1743 if (wpa_s->l2)
1744 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1745 (u8 *) "", 0);
1746 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001747
1748#ifdef CONFIG_SME
1749 if (wpa_s->sme.bss_max_idle_period) {
1750 unsigned int msec;
1751 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1752 if (msec > 100)
1753 msec -= 100;
1754 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1755 wnm_bss_keep_alive, wpa_s, NULL);
1756 }
1757#endif /* CONFIG_SME */
1758}
1759
1760
1761static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1762 const u8 *ies, size_t ies_len)
1763{
1764 struct ieee802_11_elems elems;
1765
1766 if (ies == NULL)
1767 return;
1768
1769 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1770 return;
1771
1772#ifdef CONFIG_SME
1773 if (elems.bss_max_idle_period) {
1774 unsigned int msec;
1775 wpa_s->sme.bss_max_idle_period =
1776 WPA_GET_LE16(elems.bss_max_idle_period);
1777 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1778 "TU)%s", wpa_s->sme.bss_max_idle_period,
1779 (elems.bss_max_idle_period[2] & 0x01) ?
1780 " (protected keep-live required)" : "");
1781 if (wpa_s->sme.bss_max_idle_period == 0)
1782 wpa_s->sme.bss_max_idle_period = 1;
1783 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1784 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1785 /* msec times 1000 */
1786 msec = wpa_s->sme.bss_max_idle_period * 1024;
1787 if (msec > 100)
1788 msec -= 100;
1789 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1790 wnm_bss_keep_alive, wpa_s,
1791 NULL);
1792 }
1793 }
1794#endif /* CONFIG_SME */
1795}
1796
1797#endif /* CONFIG_WNM */
1798
1799
1800void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1801{
1802#ifdef CONFIG_WNM
1803 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1804#endif /* CONFIG_WNM */
1805}
1806
1807
Dmitry Shmidt051af732013-10-22 13:52:46 -07001808#ifdef CONFIG_INTERWORKING
1809
1810static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1811 size_t len)
1812{
1813 int res;
1814
1815 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1816 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1817 if (res) {
1818 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1819 }
1820
1821 return res;
1822}
1823
1824
1825static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1826 const u8 *ies, size_t ies_len)
1827{
1828 struct ieee802_11_elems elems;
1829
1830 if (ies == NULL)
1831 return;
1832
1833 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1834 return;
1835
1836 if (elems.qos_map_set) {
1837 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1838 elems.qos_map_set_len);
1839 }
1840}
1841
1842#endif /* CONFIG_INTERWORKING */
1843
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1846 union wpa_event_data *data)
1847{
1848 int l, len, found = 0, wpa_found, rsn_found;
1849 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001850#ifdef CONFIG_IEEE80211R
1851 u8 bssid[ETH_ALEN];
1852#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853
1854 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1855 if (data->assoc_info.req_ies)
1856 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1857 data->assoc_info.req_ies_len);
1858 if (data->assoc_info.resp_ies) {
1859 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1860 data->assoc_info.resp_ies_len);
1861#ifdef CONFIG_TDLS
1862 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1863 data->assoc_info.resp_ies_len);
1864#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001865#ifdef CONFIG_WNM
1866 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1867 data->assoc_info.resp_ies_len);
1868#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001869#ifdef CONFIG_INTERWORKING
1870 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1871 data->assoc_info.resp_ies_len);
1872#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 }
1874 if (data->assoc_info.beacon_ies)
1875 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1876 data->assoc_info.beacon_ies,
1877 data->assoc_info.beacon_ies_len);
1878 if (data->assoc_info.freq)
1879 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1880 data->assoc_info.freq);
1881
1882 p = data->assoc_info.req_ies;
1883 l = data->assoc_info.req_ies_len;
1884
1885 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1886 while (p && l >= 2) {
1887 len = p[1] + 2;
1888 if (len > l) {
1889 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1890 p, l);
1891 break;
1892 }
1893 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1894 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1895 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1896 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1897 break;
1898 found = 1;
1899 wpa_find_assoc_pmkid(wpa_s);
1900 break;
1901 }
1902 l -= len;
1903 p += len;
1904 }
1905 if (!found && data->assoc_info.req_ies)
1906 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1907
1908#ifdef CONFIG_IEEE80211R
1909#ifdef CONFIG_SME
1910 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1912 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1913 data->assoc_info.resp_ies,
1914 data->assoc_info.resp_ies_len,
1915 bssid) < 0) {
1916 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1917 "Reassociation Response failed");
1918 wpa_supplicant_deauthenticate(
1919 wpa_s, WLAN_REASON_INVALID_IE);
1920 return -1;
1921 }
1922 }
1923
1924 p = data->assoc_info.resp_ies;
1925 l = data->assoc_info.resp_ies_len;
1926
1927#ifdef CONFIG_WPS_STRICT
1928 if (p && wpa_s->current_ssid &&
1929 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1930 struct wpabuf *wps;
1931 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1932 if (wps == NULL) {
1933 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1934 "include WPS IE in (Re)Association Response");
1935 return -1;
1936 }
1937
1938 if (wps_validate_assoc_resp(wps) < 0) {
1939 wpabuf_free(wps);
1940 wpa_supplicant_deauthenticate(
1941 wpa_s, WLAN_REASON_INVALID_IE);
1942 return -1;
1943 }
1944 wpabuf_free(wps);
1945 }
1946#endif /* CONFIG_WPS_STRICT */
1947
1948 /* Go through the IEs and make a copy of the MDIE, if present. */
1949 while (p && l >= 2) {
1950 len = p[1] + 2;
1951 if (len > l) {
1952 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1953 p, l);
1954 break;
1955 }
1956 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1957 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1958 wpa_s->sme.ft_used = 1;
1959 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1960 MOBILITY_DOMAIN_ID_LEN);
1961 break;
1962 }
1963 l -= len;
1964 p += len;
1965 }
1966#endif /* CONFIG_SME */
1967
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001968 /* Process FT when SME is in the driver */
1969 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1970 wpa_ft_is_completed(wpa_s->wpa)) {
1971 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1972 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1973 data->assoc_info.resp_ies,
1974 data->assoc_info.resp_ies_len,
1975 bssid) < 0) {
1976 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1977 "Reassociation Response failed");
1978 wpa_supplicant_deauthenticate(
1979 wpa_s, WLAN_REASON_INVALID_IE);
1980 return -1;
1981 }
1982 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1983 }
1984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1986 data->assoc_info.resp_ies_len);
1987#endif /* CONFIG_IEEE80211R */
1988
1989 /* WPA/RSN IE from Beacon/ProbeResp */
1990 p = data->assoc_info.beacon_ies;
1991 l = data->assoc_info.beacon_ies_len;
1992
1993 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1994 */
1995 wpa_found = rsn_found = 0;
1996 while (p && l >= 2) {
1997 len = p[1] + 2;
1998 if (len > l) {
1999 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
2000 p, l);
2001 break;
2002 }
2003 if (!wpa_found &&
2004 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2005 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2006 wpa_found = 1;
2007 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2008 }
2009
2010 if (!rsn_found &&
2011 p[0] == WLAN_EID_RSN && p[1] >= 2) {
2012 rsn_found = 1;
2013 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2014 }
2015
2016 l -= len;
2017 p += len;
2018 }
2019
2020 if (!wpa_found && data->assoc_info.beacon_ies)
2021 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2022 if (!rsn_found && data->assoc_info.beacon_ies)
2023 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2024 if (wpa_found || rsn_found)
2025 wpa_s->ap_ies_from_associnfo = 1;
2026
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002027#ifdef CONFIG_FST
2028 wpabuf_free(wpa_s->received_mb_ies);
2029 wpa_s->received_mb_ies = NULL;
2030 if (wpa_s->fst) {
2031 struct mb_ies_info mb_ies;
2032
2033 wpa_printf(MSG_DEBUG, "Looking for MB IE");
2034 if (!mb_ies_info_by_ies(&mb_ies, data->assoc_info.resp_ies,
2035 data->assoc_info.resp_ies_len))
2036 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2037 }
2038#endif /* CONFIG_FST */
2039
Jouni Malinen87fd2792011-05-16 18:35:42 +03002040 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2041 wpa_s->assoc_freq != data->assoc_info.freq) {
2042 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2043 "%u to %u MHz",
2044 wpa_s->assoc_freq, data->assoc_info.freq);
2045 wpa_supplicant_update_scan_results(wpa_s);
2046 }
2047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 wpa_s->assoc_freq = data->assoc_info.freq;
2049
2050 return 0;
2051}
2052
2053
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002054static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2055{
2056 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2057
2058 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2059 return -1;
2060
2061 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2062 return 0;
2063
2064 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2065 WPA_IE_VENDOR_TYPE);
2066 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2067
2068 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2069 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2070 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2071 bss_rsn ? 2 + bss_rsn[1] : 0))
2072 return -1;
2073
2074 return 0;
2075}
2076
2077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2079 union wpa_event_data *data)
2080{
2081 u8 bssid[ETH_ALEN];
2082 int ft_completed;
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002083 int new_bss = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084
2085#ifdef CONFIG_AP
2086 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002087 if (!data)
2088 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2090 data->assoc_info.addr,
2091 data->assoc_info.req_ies,
2092 data->assoc_info.req_ies_len,
2093 data->assoc_info.reassoc);
2094 return;
2095 }
2096#endif /* CONFIG_AP */
2097
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002098 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2101 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2102 return;
2103
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002104 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2105 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002106 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002107 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2108 return;
2109 }
2110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002112 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2114 MACSTR, MAC2STR(bssid));
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002115 new_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2118 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002119 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120
2121 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2122 wpa_clear_keys(wpa_s, bssid);
2123 }
2124 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002125 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2127 return;
2128 }
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002129 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002130
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002131#ifdef ANDROID
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002132 if (wpa_s->conf->ap_scan == 1) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002133#else
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002134 if (wpa_s->conf->ap_scan == 1 &&
2135 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002136#endif
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002137 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2138 wpa_msg(wpa_s, MSG_WARNING,
2139 "WPA/RSN IEs not updated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 }
2141
2142#ifdef CONFIG_SME
2143 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2144 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002145 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002146#endif /* CONFIG_SME */
2147
2148 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2149 if (wpa_s->current_ssid) {
2150 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2151 * initialized before association, but for other modes,
2152 * initialize PC/SC here, if the current configuration needs
2153 * smartcard or SIM/USIM. */
2154 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2155 }
2156 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2157 if (wpa_s->l2)
2158 l2_packet_notify_auth_start(wpa_s->l2);
2159
2160 /*
2161 * Set portEnabled first to FALSE in order to get EAP state machine out
2162 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2163 * state machine may transit to AUTHENTICATING state based on obsolete
2164 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2165 * AUTHENTICATED without ever giving chance to EAP state machine to
2166 * reset the state.
2167 */
2168 if (!ft_completed) {
2169 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2170 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2171 }
2172 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
2173 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2174 /* 802.1X::portControl = Auto */
2175 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2176 wpa_s->eapol_received = 0;
2177 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2178 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2179 (wpa_s->current_ssid &&
2180 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002181 if (wpa_s->current_ssid &&
2182 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002183 (wpa_s->drv_flags &
2184 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2185 /*
2186 * Set the key after having received joined-IBSS event
2187 * from the driver.
2188 */
2189 wpa_supplicant_set_wpa_none_key(wpa_s,
2190 wpa_s->current_ssid);
2191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192 wpa_supplicant_cancel_auth_timeout(wpa_s);
2193 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2194 } else if (!ft_completed) {
2195 /* Timeout for receiving the first EAPOL packet */
2196 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2197 }
2198 wpa_supplicant_cancel_scan(wpa_s);
2199
2200 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2201 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2202 /*
2203 * We are done; the driver will take care of RSN 4-way
2204 * handshake.
2205 */
2206 wpa_supplicant_cancel_auth_timeout(wpa_s);
2207 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2208 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2209 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2210 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2211 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2212 /*
2213 * The driver will take care of RSN 4-way handshake, so we need
2214 * to allow EAPOL supplicant to complete its work without
2215 * waiting for WPA supplicant.
2216 */
2217 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2218 } else if (ft_completed) {
2219 /*
2220 * FT protocol completed - make sure EAPOL state machine ends
2221 * up in authenticated.
2222 */
2223 wpa_supplicant_cancel_auth_timeout(wpa_s);
2224 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2225 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2226 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2227 }
2228
Jouni Malinena05074c2012-12-21 21:35:35 +02002229 wpa_s->last_eapol_matches_bssid = 0;
2230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002232 struct os_reltime now, age;
2233 os_get_reltime(&now);
2234 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 if (age.sec == 0 && age.usec < 100000 &&
2236 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2237 0) {
2238 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2239 "frame that was received just before "
2240 "association notification");
2241 wpa_supplicant_rx_eapol(
2242 wpa_s, wpa_s->pending_eapol_rx_src,
2243 wpabuf_head(wpa_s->pending_eapol_rx),
2244 wpabuf_len(wpa_s->pending_eapol_rx));
2245 }
2246 wpabuf_free(wpa_s->pending_eapol_rx);
2247 wpa_s->pending_eapol_rx = NULL;
2248 }
2249
2250 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2251 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002252 wpa_s->current_ssid &&
2253 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002254 /* Set static WEP keys again */
2255 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2256 }
2257
2258#ifdef CONFIG_IBSS_RSN
2259 if (wpa_s->current_ssid &&
2260 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2261 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2262 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2263 wpa_s->ibss_rsn == NULL) {
2264 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2265 if (!wpa_s->ibss_rsn) {
2266 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2267 wpa_supplicant_deauthenticate(
2268 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2269 return;
2270 }
2271
2272 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2273 }
2274#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002275
2276 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002277
2278 if (data) {
2279 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2280 data->assoc_info.resp_ies_len,
2281 &data->assoc_info.wmm_params);
2282
2283 if (wpa_s->reassoc_same_bss)
2284 wmm_ac_restore_tspecs(wpa_s);
2285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286}
2287
2288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002289static int disconnect_reason_recoverable(u16 reason_code)
2290{
2291 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2292 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2293 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2294}
2295
2296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002298 u16 reason_code,
2299 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300{
2301 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002302
2303 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2304 /*
2305 * At least Host AP driver and a Prism3 card seemed to be
2306 * generating streams of disconnected events when configuring
2307 * IBSS for WPA-None. Ignore them for now.
2308 */
2309 return;
2310 }
2311
2312 bssid = wpa_s->bssid;
2313 if (is_zero_ether_addr(bssid))
2314 bssid = wpa_s->pending_bssid;
2315
2316 if (!is_zero_ether_addr(bssid) ||
2317 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2318 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2319 " reason=%d%s",
2320 MAC2STR(bssid), reason_code,
2321 locally_generated ? " locally_generated=1" : "");
2322 }
2323}
2324
2325
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002326static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2327 int locally_generated)
2328{
2329 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2330 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2331 return 0; /* Not in 4-way handshake with PSK */
2332
2333 /*
2334 * It looks like connection was lost while trying to go through PSK
2335 * 4-way handshake. Filter out known disconnection cases that are caused
2336 * by something else than PSK mismatch to avoid confusing reports.
2337 */
2338
2339 if (locally_generated) {
2340 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2341 return 0;
2342 }
2343
2344 return 1;
2345}
2346
2347
Jouni Malinen2b89da82012-08-31 22:04:41 +03002348static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2349 u16 reason_code,
2350 int locally_generated)
2351{
2352 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 int authenticating;
2354 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002355 struct wpa_bss *fast_reconnect = NULL;
2356 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002357 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358
2359 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2360 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2361
2362 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2363 /*
2364 * At least Host AP driver and a Prism3 card seemed to be
2365 * generating streams of disconnected events when configuring
2366 * IBSS for WPA-None. Ignore them for now.
2367 */
2368 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2369 "IBSS/WPA-None mode");
2370 return;
2371 }
2372
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002373 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2375 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002376 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2377 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002378 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002380 if (!wpa_s->disconnected &&
2381 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002382 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2383 wpas_wps_searching(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002384 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002385 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002387 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002388 wpa_s->wpa_state);
2389 if (wpa_s->wpa_state == WPA_COMPLETED &&
2390 wpa_s->current_ssid &&
2391 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002392 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002393 disconnect_reason_recoverable(reason_code)) {
2394 /*
2395 * It looks like the AP has dropped association with
2396 * us, but could allow us to get back in. Try to
2397 * reconnect to the same BSS without full scan to save
2398 * time for some common cases.
2399 */
2400 fast_reconnect = wpa_s->current_bss;
2401 fast_reconnect_ssid = wpa_s->current_ssid;
2402 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002403 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002404 else
2405 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2406 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409 "try to re-connect");
2410 wpa_s->reassociate = 0;
2411 wpa_s->disconnected = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002412 if (!wpa_s->pno)
2413 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414 }
2415 bssid = wpa_s->bssid;
2416 if (is_zero_ether_addr(bssid))
2417 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002418 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2419 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002421 if (locally_generated)
2422 wpa_s->disconnect_reason = -reason_code;
2423 else
2424 wpa_s->disconnect_reason = reason_code;
2425 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002426 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2427 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428 wpa_clear_keys(wpa_s, wpa_s->bssid);
2429 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002430 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002431 wpa_supplicant_mark_disassoc(wpa_s);
2432
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002433 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002435 wpa_s->current_ssid = last_ssid;
2436 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002437
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002438 if (fast_reconnect &&
2439 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2440 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2441 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2442 fast_reconnect->ssid_len) &&
2443 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444#ifndef CONFIG_NO_SCAN_PROCESSING
2445 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2446 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2447 fast_reconnect_ssid) < 0) {
2448 /* Recover through full scan */
2449 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2450 }
2451#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002452 } else if (fast_reconnect) {
2453 /*
2454 * Could not reconnect to the same BSS due to network being
2455 * disabled. Use a new scan to match the alternative behavior
2456 * above, i.e., to continue automatic reconnection attempt in a
2457 * way that enforces disabled network rules.
2458 */
2459 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002460 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002461}
2462
2463
2464#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002465void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466{
2467 struct wpa_supplicant *wpa_s = eloop_ctx;
2468
2469 if (!wpa_s->pending_mic_error_report)
2470 return;
2471
2472 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2473 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2474 wpa_s->pending_mic_error_report = 0;
2475}
2476#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2477
2478
2479static void
2480wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2481 union wpa_event_data *data)
2482{
2483 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002484 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002485
2486 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2487 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002488 os_get_reltime(&t);
2489 if ((wpa_s->last_michael_mic_error.sec &&
2490 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491 wpa_s->pending_mic_error_report) {
2492 if (wpa_s->pending_mic_error_report) {
2493 /*
2494 * Send the pending MIC error report immediately since
2495 * we are going to start countermeasures and AP better
2496 * do the same.
2497 */
2498 wpa_sm_key_request(wpa_s->wpa, 1,
2499 wpa_s->pending_mic_error_pairwise);
2500 }
2501
2502 /* Send the new MIC error report immediately since we are going
2503 * to start countermeasures and AP better do the same.
2504 */
2505 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2506
2507 /* initialize countermeasures */
2508 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002509
2510 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2513
2514 /*
2515 * Need to wait for completion of request frame. We do not get
2516 * any callback for the message completion, so just wait a
2517 * short while and hope for the best. */
2518 os_sleep(0, 10000);
2519
2520 wpa_drv_set_countermeasures(wpa_s, 1);
2521 wpa_supplicant_deauthenticate(wpa_s,
2522 WLAN_REASON_MICHAEL_MIC_FAILURE);
2523 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2524 wpa_s, NULL);
2525 eloop_register_timeout(60, 0,
2526 wpa_supplicant_stop_countermeasures,
2527 wpa_s, NULL);
2528 /* TODO: mark the AP rejected for 60 second. STA is
2529 * allowed to associate with another AP.. */
2530 } else {
2531#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2532 if (wpa_s->mic_errors_seen) {
2533 /*
2534 * Reduce the effectiveness of Michael MIC error
2535 * reports as a means for attacking against TKIP if
2536 * more than one MIC failure is noticed with the same
2537 * PTK. We delay the transmission of the reports by a
2538 * random time between 0 and 60 seconds in order to
2539 * force the attacker wait 60 seconds before getting
2540 * the information on whether a frame resulted in a MIC
2541 * failure.
2542 */
2543 u8 rval[4];
2544 int sec;
2545
2546 if (os_get_random(rval, sizeof(rval)) < 0)
2547 sec = os_random() % 60;
2548 else
2549 sec = WPA_GET_BE32(rval) % 60;
2550 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2551 "report %d seconds", sec);
2552 wpa_s->pending_mic_error_report = 1;
2553 wpa_s->pending_mic_error_pairwise = pairwise;
2554 eloop_cancel_timeout(
2555 wpa_supplicant_delayed_mic_error_report,
2556 wpa_s, NULL);
2557 eloop_register_timeout(
2558 sec, os_random() % 1000000,
2559 wpa_supplicant_delayed_mic_error_report,
2560 wpa_s, NULL);
2561 } else {
2562 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2563 }
2564#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2565 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2566#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2567 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002568 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 wpa_s->mic_errors_seen++;
2570}
2571
2572
2573#ifdef CONFIG_TERMINATE_ONLASTIF
2574static int any_interfaces(struct wpa_supplicant *head)
2575{
2576 struct wpa_supplicant *wpa_s;
2577
2578 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2579 if (!wpa_s->interface_removed)
2580 return 1;
2581 return 0;
2582}
2583#endif /* CONFIG_TERMINATE_ONLASTIF */
2584
2585
2586static void
2587wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2588 union wpa_event_data *data)
2589{
2590 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2591 return;
2592
2593 switch (data->interface_status.ievent) {
2594 case EVENT_INTERFACE_ADDED:
2595 if (!wpa_s->interface_removed)
2596 break;
2597 wpa_s->interface_removed = 0;
2598 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2599 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2600 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2601 "driver after interface was added");
2602 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002603
2604#ifdef CONFIG_P2P
2605 if (!wpa_s->global->p2p &&
2606 !wpa_s->global->p2p_disabled &&
2607 !wpa_s->conf->p2p_disabled &&
2608 (wpa_s->drv_flags &
2609 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2610 wpas_p2p_add_p2pdev_interface(
2611 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2612 wpa_printf(MSG_INFO,
2613 "P2P: Failed to enable P2P Device interface");
2614 /* Try to continue without. P2P will be disabled. */
2615 }
2616#endif /* CONFIG_P2P */
2617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 break;
2619 case EVENT_INTERFACE_REMOVED:
2620 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2621 wpa_s->interface_removed = 1;
2622 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002623 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 l2_packet_deinit(wpa_s->l2);
2625 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002626
2627#ifdef CONFIG_P2P
2628 if (wpa_s->global->p2p &&
2629 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2630 (wpa_s->drv_flags &
2631 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2632 wpa_dbg(wpa_s, MSG_DEBUG,
2633 "Removing P2P Device interface");
2634 wpa_supplicant_remove_iface(
2635 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2636 0);
2637 wpa_s->global->p2p_init_wpa_s = NULL;
2638 }
2639#endif /* CONFIG_P2P */
2640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641#ifdef CONFIG_TERMINATE_ONLASTIF
2642 /* check if last interface */
2643 if (!any_interfaces(wpa_s->global->ifaces))
2644 eloop_terminate();
2645#endif /* CONFIG_TERMINATE_ONLASTIF */
2646 break;
2647 }
2648}
2649
2650
2651#ifdef CONFIG_PEERKEY
2652static void
2653wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2654 union wpa_event_data *data)
2655{
2656 if (data == NULL)
2657 return;
2658 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2659}
2660#endif /* CONFIG_PEERKEY */
2661
2662
2663#ifdef CONFIG_TDLS
2664static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2665 union wpa_event_data *data)
2666{
2667 if (data == NULL)
2668 return;
2669 switch (data->tdls.oper) {
2670 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002671 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2672 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2673 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2674 else
2675 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 break;
2677 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002678 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2679 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2680 data->tdls.reason_code);
2681 else
2682 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2683 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002685 case TDLS_REQUEST_DISCOVER:
2686 wpa_tdls_send_discovery_request(wpa_s->wpa,
2687 data->tdls.peer);
2688 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 }
2690}
2691#endif /* CONFIG_TDLS */
2692
2693
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002694#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002695static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2696 union wpa_event_data *data)
2697{
2698 if (data == NULL)
2699 return;
2700 switch (data->wnm.oper) {
2701 case WNM_OPER_SLEEP:
2702 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2703 "(action=%d, intval=%d)",
2704 data->wnm.sleep_action, data->wnm.sleep_intval);
2705 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002706 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002707 break;
2708 }
2709}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002710#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002711
2712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713#ifdef CONFIG_IEEE80211R
2714static void
2715wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2716 union wpa_event_data *data)
2717{
2718 if (data == NULL)
2719 return;
2720
2721 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2722 data->ft_ies.ies_len,
2723 data->ft_ies.ft_action,
2724 data->ft_ies.target_ap,
2725 data->ft_ies.ric_ies,
2726 data->ft_ies.ric_ies_len) < 0) {
2727 /* TODO: prevent MLME/driver from trying to associate? */
2728 }
2729}
2730#endif /* CONFIG_IEEE80211R */
2731
2732
2733#ifdef CONFIG_IBSS_RSN
2734static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2735 union wpa_event_data *data)
2736{
2737 struct wpa_ssid *ssid;
2738 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2739 return;
2740 if (data == NULL)
2741 return;
2742 ssid = wpa_s->current_ssid;
2743 if (ssid == NULL)
2744 return;
2745 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2746 return;
2747
2748 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2749}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002750
2751
2752static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2753 union wpa_event_data *data)
2754{
2755 struct wpa_ssid *ssid = wpa_s->current_ssid;
2756
2757 if (ssid == NULL)
2758 return;
2759
2760 /* check if the ssid is correctly configured as IBSS/RSN */
2761 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2762 return;
2763
2764 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2765 data->rx_mgmt.frame_len);
2766}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767#endif /* CONFIG_IBSS_RSN */
2768
2769
2770#ifdef CONFIG_IEEE80211R
2771static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2772 size_t len)
2773{
2774 const u8 *sta_addr, *target_ap_addr;
2775 u16 status;
2776
2777 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2778 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2779 return; /* only SME case supported for now */
2780 if (len < 1 + 2 * ETH_ALEN + 2)
2781 return;
2782 if (data[0] != 2)
2783 return; /* Only FT Action Response is supported for now */
2784 sta_addr = data + 1;
2785 target_ap_addr = data + 1 + ETH_ALEN;
2786 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2787 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2788 MACSTR " TargetAP " MACSTR " status %u",
2789 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2790
2791 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2792 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2793 " in FT Action Response", MAC2STR(sta_addr));
2794 return;
2795 }
2796
2797 if (status) {
2798 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2799 "failure (status code %d)", status);
2800 /* TODO: report error to FT code(?) */
2801 return;
2802 }
2803
2804 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2805 len - (1 + 2 * ETH_ALEN + 2), 1,
2806 target_ap_addr, NULL, 0) < 0)
2807 return;
2808
2809#ifdef CONFIG_SME
2810 {
2811 struct wpa_bss *bss;
2812 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2813 if (bss)
2814 wpa_s->sme.freq = bss->freq;
2815 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2816 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2817 WLAN_AUTH_FT);
2818 }
2819#endif /* CONFIG_SME */
2820}
2821#endif /* CONFIG_IEEE80211R */
2822
2823
2824static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2825 struct unprot_deauth *e)
2826{
2827#ifdef CONFIG_IEEE80211W
2828 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2829 "dropped: " MACSTR " -> " MACSTR
2830 " (reason code %u)",
2831 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2832 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2833#endif /* CONFIG_IEEE80211W */
2834}
2835
2836
2837static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2838 struct unprot_disassoc *e)
2839{
2840#ifdef CONFIG_IEEE80211W
2841 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2842 "dropped: " MACSTR " -> " MACSTR
2843 " (reason code %u)",
2844 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2845 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2846#endif /* CONFIG_IEEE80211W */
2847}
2848
2849
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002850static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2851 u16 reason_code, int locally_generated,
2852 const u8 *ie, size_t ie_len, int deauth)
2853{
2854#ifdef CONFIG_AP
2855 if (wpa_s->ap_iface && addr) {
2856 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2857 return;
2858 }
2859
2860 if (wpa_s->ap_iface) {
2861 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2862 return;
2863 }
2864#endif /* CONFIG_AP */
2865
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002866 if (!locally_generated)
2867 wpa_s->own_disconnect_req = 0;
2868
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002869 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2870
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002871 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2872 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2873 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2874 eapol_sm_failed(wpa_s->eapol))) &&
2875 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002876 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002877
2878#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002879 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002880 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2881 locally_generated) > 0) {
2882 /*
2883 * The interface was removed, so cannot continue
2884 * processing any additional operations after this.
2885 */
2886 return;
2887 }
2888 }
2889#endif /* CONFIG_P2P */
2890
2891 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2892 locally_generated);
2893}
2894
2895
2896static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2897 struct disassoc_info *info)
2898{
2899 u16 reason_code = 0;
2900 int locally_generated = 0;
2901 const u8 *addr = NULL;
2902 const u8 *ie = NULL;
2903 size_t ie_len = 0;
2904
2905 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2906
2907 if (info) {
2908 addr = info->addr;
2909 ie = info->ie;
2910 ie_len = info->ie_len;
2911 reason_code = info->reason_code;
2912 locally_generated = info->locally_generated;
2913 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2914 locally_generated ? " (locally generated)" : "");
2915 if (addr)
2916 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2917 MAC2STR(addr));
2918 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2919 ie, ie_len);
2920 }
2921
2922#ifdef CONFIG_AP
2923 if (wpa_s->ap_iface && info && info->addr) {
2924 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2925 return;
2926 }
2927
2928 if (wpa_s->ap_iface) {
2929 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2930 return;
2931 }
2932#endif /* CONFIG_AP */
2933
2934#ifdef CONFIG_P2P
2935 if (info) {
2936 wpas_p2p_disassoc_notif(
2937 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2938 locally_generated);
2939 }
2940#endif /* CONFIG_P2P */
2941
2942 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2943 sme_event_disassoc(wpa_s, info);
2944
2945 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2946 ie, ie_len, 0);
2947}
2948
2949
2950static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2951 struct deauth_info *info)
2952{
2953 u16 reason_code = 0;
2954 int locally_generated = 0;
2955 const u8 *addr = NULL;
2956 const u8 *ie = NULL;
2957 size_t ie_len = 0;
2958
2959 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2960
2961 if (info) {
2962 addr = info->addr;
2963 ie = info->ie;
2964 ie_len = info->ie_len;
2965 reason_code = info->reason_code;
2966 locally_generated = info->locally_generated;
2967 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2968 reason_code,
2969 locally_generated ? " (locally generated)" : "");
2970 if (addr) {
2971 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2972 MAC2STR(addr));
2973 }
2974 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2975 ie, ie_len);
2976 }
2977
2978 wpa_reset_ft_completed(wpa_s->wpa);
2979
2980 wpas_event_disconnect(wpa_s, addr, reason_code,
2981 locally_generated, ie, ie_len, 1);
2982}
2983
2984
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002985static const char * reg_init_str(enum reg_change_initiator init)
2986{
2987 switch (init) {
2988 case REGDOM_SET_BY_CORE:
2989 return "CORE";
2990 case REGDOM_SET_BY_USER:
2991 return "USER";
2992 case REGDOM_SET_BY_DRIVER:
2993 return "DRIVER";
2994 case REGDOM_SET_BY_COUNTRY_IE:
2995 return "COUNTRY_IE";
2996 case REGDOM_BEACON_HINT:
2997 return "BEACON_HINT";
2998 }
2999 return "?";
3000}
3001
3002
3003static const char * reg_type_str(enum reg_type type)
3004{
3005 switch (type) {
3006 case REGDOM_TYPE_UNKNOWN:
3007 return "UNKNOWN";
3008 case REGDOM_TYPE_COUNTRY:
3009 return "COUNTRY";
3010 case REGDOM_TYPE_WORLD:
3011 return "WORLD";
3012 case REGDOM_TYPE_CUSTOM_WORLD:
3013 return "CUSTOM_WORLD";
3014 case REGDOM_TYPE_INTERSECTION:
3015 return "INTERSECTION";
3016 }
3017 return "?";
3018}
3019
3020
3021static void wpa_supplicant_update_channel_list(
3022 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003023{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003024 struct wpa_supplicant *ifs;
3025
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003026 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003027 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003028 info->alpha2[0] ? " alpha2=" : "",
3029 info->alpha2[0] ? info->alpha2 : "");
3030
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003031 if (wpa_s->drv_priv == NULL)
3032 return; /* Ignore event during drv initialization */
3033
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003034 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3035 radio_list) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003036 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3037 ifs->ifname);
3038 free_hw_features(ifs);
3039 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3040 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003041 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003042
3043 /* Restart sched_scan with updated channel list */
3044 if (wpa_s->sched_scanning) {
3045 wpa_dbg(wpa_s, MSG_DEBUG,
3046 "Channel list changed restart sched scan.");
3047 wpa_supplicant_cancel_sched_scan(wpa_s);
3048 wpa_supplicant_req_scan(wpa_s, 0, 0);
3049 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003050
3051 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003052}
3053
3054
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003055static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003056 const u8 *frame, size_t len, int freq,
3057 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003058{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003059 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003060 const u8 *payload;
3061 size_t plen;
3062 u8 category;
3063
3064 if (len < IEEE80211_HDRLEN + 2)
3065 return;
3066
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003067 mgmt = (const struct ieee80211_mgmt *) frame;
3068 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003069 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003070 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003071
3072 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3073 " Category=%u DataLen=%d freq=%d MHz",
3074 MAC2STR(mgmt->sa), category, (int) plen, freq);
3075
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003076 if (category == WLAN_ACTION_WMM) {
3077 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3078 return;
3079 }
3080
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003081#ifdef CONFIG_IEEE80211R
3082 if (category == WLAN_ACTION_FT) {
3083 ft_rx_action(wpa_s, payload, plen);
3084 return;
3085 }
3086#endif /* CONFIG_IEEE80211R */
3087
3088#ifdef CONFIG_IEEE80211W
3089#ifdef CONFIG_SME
3090 if (category == WLAN_ACTION_SA_QUERY) {
3091 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3092 return;
3093 }
3094#endif /* CONFIG_SME */
3095#endif /* CONFIG_IEEE80211W */
3096
3097#ifdef CONFIG_WNM
3098 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3099 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3100 return;
3101 }
3102#endif /* CONFIG_WNM */
3103
3104#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003105 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3106 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003107 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003108 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003109 payload, plen, freq) == 0)
3110 return;
3111#endif /* CONFIG_GAS */
3112
3113#ifdef CONFIG_TDLS
3114 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3115 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3116 wpa_dbg(wpa_s, MSG_DEBUG,
3117 "TDLS: Received Discovery Response from " MACSTR,
3118 MAC2STR(mgmt->sa));
3119 return;
3120 }
3121#endif /* CONFIG_TDLS */
3122
3123#ifdef CONFIG_INTERWORKING
3124 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3125 payload[0] == QOS_QOS_MAP_CONFIG) {
3126 const u8 *pos = payload + 1;
3127 size_t qlen = plen - 1;
3128 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3129 MACSTR, MAC2STR(mgmt->sa));
3130 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3131 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3132 pos[1] <= qlen - 2 && pos[1] >= 16)
3133 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3134 return;
3135 }
3136#endif /* CONFIG_INTERWORKING */
3137
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003138 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3139 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3140 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3141 return;
3142 }
3143
3144 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3145 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3146 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3147 payload + 1, plen - 1,
3148 rssi);
3149 return;
3150 }
3151
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003152#ifdef CONFIG_FST
3153 if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3154 fst_rx_action(wpa_s->fst, mgmt, len);
3155 return;
3156 }
3157#endif /* CONFIG_FST */
3158
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003159 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3160 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003161 if (wpa_s->ifmsh)
3162 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003163}
3164
3165
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003166static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3167 union wpa_event_data *event)
3168{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003169 struct wpa_freq_range_list *list;
3170 char *str = NULL;
3171
3172 list = &event->freq_range;
3173
3174 if (list->num)
3175 str = freq_range_list_str(list);
3176 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3177 str ? str : "");
3178
3179#ifdef CONFIG_P2P
3180 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3181 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3182 __func__);
3183 } else {
3184 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003186 /*
3187 * The update channel flow will also take care of moving a GO
3188 * from the unsafe frequency if needed.
3189 */
3190 wpas_p2p_update_channel_list(wpa_s,
3191 WPAS_P2P_CHANNEL_UPDATE_AVOID);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003192 }
3193#endif /* CONFIG_P2P */
3194
3195 os_free(str);
3196}
3197
3198
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003199static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3200 union wpa_event_data *data)
3201{
3202 wpa_dbg(wpa_s, MSG_DEBUG,
3203 "Connection authorized by device, previous state %d",
3204 wpa_s->wpa_state);
3205 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3206 wpa_supplicant_cancel_auth_timeout(wpa_s);
3207 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3208 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3209 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3210 }
3211 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3212 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003213 data->assoc_info.ptk_kck_len,
3214 data->assoc_info.ptk_kek,
3215 data->assoc_info.ptk_kek_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003216}
3217
3218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003219void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3220 union wpa_event_data *data)
3221{
3222 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003223 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224
3225 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3226 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003227 event != EVENT_INTERFACE_STATUS &&
3228 event != EVENT_SCHED_SCAN_STOPPED) {
3229 wpa_dbg(wpa_s, MSG_DEBUG,
3230 "Ignore event %s (%d) while interface is disabled",
3231 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 return;
3233 }
3234
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003235#ifndef CONFIG_NO_STDOUT_DEBUG
3236{
3237 int level = MSG_DEBUG;
3238
Dmitry Shmidt04949592012-07-19 12:16:46 -07003239 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003240 const struct ieee80211_hdr *hdr;
3241 u16 fc;
3242 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3243 fc = le_to_host16(hdr->frame_control);
3244 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3245 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3246 level = MSG_EXCESSIVE;
3247 }
3248
3249 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3250 event_to_string(event), event);
3251}
3252#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253
3254 switch (event) {
3255 case EVENT_AUTH:
3256 sme_event_auth(wpa_s, data);
3257 break;
3258 case EVENT_ASSOC:
3259 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003260 if (data && data->assoc_info.authorized)
3261 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262 break;
3263 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003264 wpas_event_disassoc(wpa_s,
3265 data ? &data->disassoc_info : NULL);
3266 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003268 wpas_event_deauth(wpa_s,
3269 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 break;
3271 case EVENT_MICHAEL_MIC_FAILURE:
3272 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3273 break;
3274#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003275 case EVENT_SCAN_STARTED:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003276 if (wpa_s->own_scan_requested ||
3277 (data && !data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003278 struct os_reltime diff;
3279
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003280 os_get_reltime(&wpa_s->scan_start_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003281 os_reltime_sub(&wpa_s->scan_start_time,
3282 &wpa_s->scan_trigger_time, &diff);
3283 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3284 diff.sec, diff.usec);
3285 wpa_s->own_scan_requested = 0;
3286 wpa_s->own_scan_running = 1;
3287 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3288 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003289 wpa_msg_ctrl(wpa_s, MSG_INFO,
3290 WPA_EVENT_SCAN_STARTED "id=%u",
3291 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003292 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003293 wpa_msg_ctrl(wpa_s, MSG_INFO,
3294 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003295 }
3296 } else {
3297 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003298 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003299 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003300 }
3301 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 case EVENT_SCAN_RESULTS:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003303 if (!(data && data->scan_info.external_scan) &&
3304 os_reltime_initialized(&wpa_s->scan_start_time)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003305 struct os_reltime now, diff;
3306 os_get_reltime(&now);
3307 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3308 wpa_s->scan_start_time.sec = 0;
3309 wpa_s->scan_start_time.usec = 0;
3310 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3311 diff.sec, diff.usec);
3312 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003313 if (wpa_supplicant_event_scan_results(wpa_s, data))
3314 break; /* interface may have been removed */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003315 if (!(data && data->scan_info.external_scan))
3316 wpa_s->own_scan_running = 0;
3317 if (data && data->scan_info.nl_scan_event)
3318 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003319 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 break;
3321#endif /* CONFIG_NO_SCAN_PROCESSING */
3322 case EVENT_ASSOCINFO:
3323 wpa_supplicant_event_associnfo(wpa_s, data);
3324 break;
3325 case EVENT_INTERFACE_STATUS:
3326 wpa_supplicant_event_interface_status(wpa_s, data);
3327 break;
3328 case EVENT_PMKID_CANDIDATE:
3329 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3330 break;
3331#ifdef CONFIG_PEERKEY
3332 case EVENT_STKSTART:
3333 wpa_supplicant_event_stkstart(wpa_s, data);
3334 break;
3335#endif /* CONFIG_PEERKEY */
3336#ifdef CONFIG_TDLS
3337 case EVENT_TDLS:
3338 wpa_supplicant_event_tdls(wpa_s, data);
3339 break;
3340#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003341#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003342 case EVENT_WNM:
3343 wpa_supplicant_event_wnm(wpa_s, data);
3344 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003345#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346#ifdef CONFIG_IEEE80211R
3347 case EVENT_FT_RESPONSE:
3348 wpa_supplicant_event_ft_response(wpa_s, data);
3349 break;
3350#endif /* CONFIG_IEEE80211R */
3351#ifdef CONFIG_IBSS_RSN
3352 case EVENT_IBSS_RSN_START:
3353 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3354 break;
3355#endif /* CONFIG_IBSS_RSN */
3356 case EVENT_ASSOC_REJECT:
3357 if (data->assoc_reject.bssid)
3358 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3359 "bssid=" MACSTR " status_code=%u",
3360 MAC2STR(data->assoc_reject.bssid),
3361 data->assoc_reject.status_code);
3362 else
3363 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3364 "status_code=%u",
3365 data->assoc_reject.status_code);
3366 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3367 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003368 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003369 const u8 *bssid = data->assoc_reject.bssid;
3370 if (bssid == NULL || is_zero_ether_addr(bssid))
3371 bssid = wpa_s->pending_bssid;
3372 wpas_connection_failed(wpa_s, bssid);
3373 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003374 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003375 break;
3376 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003377 /* It is possible to get this event from earlier connection */
3378 if (wpa_s->current_ssid &&
3379 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3380 wpa_dbg(wpa_s, MSG_DEBUG,
3381 "Ignore AUTH_TIMED_OUT in mesh configuration");
3382 break;
3383 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3385 sme_event_auth_timed_out(wpa_s, data);
3386 break;
3387 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003388 /* It is possible to get this event from earlier connection */
3389 if (wpa_s->current_ssid &&
3390 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3391 wpa_dbg(wpa_s, MSG_DEBUG,
3392 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3393 break;
3394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3396 sme_event_assoc_timed_out(wpa_s, data);
3397 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 case EVENT_TX_STATUS:
3399 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3400 " type=%d stype=%d",
3401 MAC2STR(data->tx_status.dst),
3402 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003403#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003405#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003406 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3407 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003409 wpa_s, 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);
3415#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416 break;
3417 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003418#endif /* CONFIG_AP */
3419#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3421 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3422 /*
3423 * Catch TX status events for Action frames we sent via group
3424 * interface in GO mode.
3425 */
3426 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3427 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3428 os_memcmp(wpa_s->parent->pending_action_dst,
3429 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003430 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 wpa_s->parent, data->tx_status.dst,
3432 data->tx_status.data,
3433 data->tx_status.data_len,
3434 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003435 OFFCHANNEL_SEND_ACTION_SUCCESS :
3436 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003437 break;
3438 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439#endif /* CONFIG_OFFCHANNEL */
3440#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 switch (data->tx_status.type) {
3442 case WLAN_FC_TYPE_MGMT:
3443 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3444 data->tx_status.data_len,
3445 data->tx_status.stype,
3446 data->tx_status.ack);
3447 break;
3448 case WLAN_FC_TYPE_DATA:
3449 ap_tx_status(wpa_s, data->tx_status.dst,
3450 data->tx_status.data,
3451 data->tx_status.data_len,
3452 data->tx_status.ack);
3453 break;
3454 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003455#endif /* CONFIG_AP */
3456 break;
3457#ifdef CONFIG_AP
3458 case EVENT_EAPOL_TX_STATUS:
3459 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3460 data->eapol_tx_status.data,
3461 data->eapol_tx_status.data_len,
3462 data->eapol_tx_status.ack);
3463 break;
3464 case EVENT_DRIVER_CLIENT_POLL_OK:
3465 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466 break;
3467 case EVENT_RX_FROM_UNKNOWN:
3468 if (wpa_s->ap_iface == NULL)
3469 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003470 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3471 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003473 case EVENT_CH_SWITCH:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003474 if (!data || !wpa_s->current_ssid)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003475 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003476
3477 wpa_s->assoc_freq = data->ch_switch.freq;
3478 wpa_s->current_ssid->frequency = data->ch_switch.freq;
3479
3480 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
3481 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
3482 wpa_s->current_ssid->mode ==
3483 WPAS_MODE_P2P_GROUP_FORMATION) {
3484 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3485 data->ch_switch.ht_enabled,
3486 data->ch_switch.ch_offset,
3487 data->ch_switch.ch_width,
3488 data->ch_switch.cf1,
3489 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003490 }
3491
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003492 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003493 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003494#ifdef NEED_AP_MLME
3495 case EVENT_DFS_RADAR_DETECTED:
3496 if (data)
3497 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3498 break;
3499 case EVENT_DFS_CAC_STARTED:
3500 if (data)
3501 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3502 break;
3503 case EVENT_DFS_CAC_FINISHED:
3504 if (data)
3505 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3506 break;
3507 case EVENT_DFS_CAC_ABORTED:
3508 if (data)
3509 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3510 break;
3511 case EVENT_DFS_NOP_FINISHED:
3512 if (data)
3513 wpas_event_dfs_cac_nop_finished(wpa_s,
3514 &data->dfs_event);
3515 break;
3516#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003517#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003518 case EVENT_RX_MGMT: {
3519 u16 fc, stype;
3520 const struct ieee80211_mgmt *mgmt;
3521
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003522#ifdef CONFIG_TESTING_OPTIONS
3523 if (wpa_s->ext_mgmt_frame_handling) {
3524 struct rx_mgmt *rx = &data->rx_mgmt;
3525 size_t hex_len = 2 * rx->frame_len + 1;
3526 char *hex = os_malloc(hex_len);
3527 if (hex) {
3528 wpa_snprintf_hex(hex, hex_len,
3529 rx->frame, rx->frame_len);
3530 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3531 rx->freq, rx->datarate, rx->ssi_signal,
3532 hex);
3533 os_free(hex);
3534 }
3535 break;
3536 }
3537#endif /* CONFIG_TESTING_OPTIONS */
3538
Dmitry Shmidt04949592012-07-19 12:16:46 -07003539 mgmt = (const struct ieee80211_mgmt *)
3540 data->rx_mgmt.frame;
3541 fc = le_to_host16(mgmt->frame_control);
3542 stype = WLAN_FC_GET_STYPE(fc);
3543
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003544#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003546#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3549 data->rx_mgmt.frame_len > 24) {
3550 const u8 *src = mgmt->sa;
3551 const u8 *ie = mgmt->u.probe_req.variable;
3552 size_t ie_len = data->rx_mgmt.frame_len -
3553 (mgmt->u.probe_req.variable -
3554 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003555 wpas_p2p_probe_req_rx(
3556 wpa_s, src, mgmt->da,
3557 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003558 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003559 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 break;
3561 }
3562#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003563#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003564 if (wpa_s->current_ssid &&
3565 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3566 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003567 data->rx_mgmt.frame_len >= 30) {
3568 wpa_supplicant_event_ibss_auth(wpa_s, data);
3569 break;
3570 }
3571#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003572
3573 if (stype == WLAN_FC_STYPE_ACTION) {
3574 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003575 wpa_s, data->rx_mgmt.frame,
3576 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003577 data->rx_mgmt.freq,
3578 data->rx_mgmt.ssi_signal);
3579 break;
3580 }
3581
3582 if (wpa_s->ifmsh) {
3583 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003584 break;
3585 }
3586
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003587 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3588 "management frame in non-AP mode");
3589 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003590#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003592
3593 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3594 data->rx_mgmt.frame_len > 24) {
3595 const u8 *ie = mgmt->u.probe_req.variable;
3596 size_t ie_len = data->rx_mgmt.frame_len -
3597 (mgmt->u.probe_req.variable -
3598 data->rx_mgmt.frame);
3599
3600 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3601 mgmt->bssid, ie, ie_len,
3602 data->rx_mgmt.ssi_signal);
3603 }
3604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003606#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 case EVENT_RX_PROBE_REQ:
3610 if (data->rx_probe_req.sa == NULL ||
3611 data->rx_probe_req.ie == NULL)
3612 break;
3613#ifdef CONFIG_AP
3614 if (wpa_s->ap_iface) {
3615 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3616 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003617 data->rx_probe_req.da,
3618 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003620 data->rx_probe_req.ie_len,
3621 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 break;
3623 }
3624#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003626 data->rx_probe_req.da,
3627 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003629 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003630 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003631 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003634#ifdef CONFIG_OFFCHANNEL
3635 offchannel_remain_on_channel_cb(
3636 wpa_s, data->remain_on_channel.freq,
3637 data->remain_on_channel.duration);
3638#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 wpas_p2p_remain_on_channel_cb(
3640 wpa_s, data->remain_on_channel.freq,
3641 data->remain_on_channel.duration);
3642 break;
3643 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003644#ifdef CONFIG_OFFCHANNEL
3645 offchannel_cancel_remain_on_channel_cb(
3646 wpa_s, data->remain_on_channel.freq);
3647#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 wpas_p2p_cancel_remain_on_channel_cb(
3649 wpa_s, data->remain_on_channel.freq);
3650 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 case EVENT_EAPOL_RX:
3652 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3653 data->eapol_rx.data,
3654 data->eapol_rx.data_len);
3655 break;
3656 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003657 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3658 "above=%d signal=%d noise=%d txrate=%d",
3659 data->signal_change.above_threshold,
3660 data->signal_change.current_signal,
3661 data->signal_change.current_noise,
3662 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003663 wpa_bss_update_level(wpa_s->current_bss,
3664 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665 bgscan_notify_signal_change(
3666 wpa_s, data->signal_change.above_threshold,
3667 data->signal_change.current_signal,
3668 data->signal_change.current_noise,
3669 data->signal_change.current_txrate);
3670 break;
3671 case EVENT_INTERFACE_ENABLED:
3672 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3673 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003674 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003675 if (wpa_s->p2p_mgmt) {
3676 wpa_supplicant_set_state(wpa_s,
3677 WPA_DISCONNECTED);
3678 break;
3679 }
3680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003681#ifdef CONFIG_AP
3682 if (!wpa_s->ap_iface) {
3683 wpa_supplicant_set_state(wpa_s,
3684 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003685 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003686 wpa_supplicant_req_scan(wpa_s, 0, 0);
3687 } else
3688 wpa_supplicant_set_state(wpa_s,
3689 WPA_COMPLETED);
3690#else /* CONFIG_AP */
3691 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3692 wpa_supplicant_req_scan(wpa_s, 0, 0);
3693#endif /* CONFIG_AP */
3694 }
3695 break;
3696 case EVENT_INTERFACE_DISABLED:
3697 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003698#ifdef CONFIG_P2P
3699 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3700 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3701 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3702 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003703 * Mark interface disabled if this happens to end up not
3704 * being removed as a separate P2P group interface.
3705 */
3706 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3707 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003708 * The interface was externally disabled. Remove
3709 * it assuming an external entity will start a
3710 * new session if needed.
3711 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003712 if (wpa_s->current_ssid &&
3713 wpa_s->current_ssid->p2p_group)
3714 wpas_p2p_interface_unavailable(wpa_s);
3715 else
3716 wpas_p2p_disconnect(wpa_s);
3717 /*
3718 * wpa_s instance may have been freed, so must not use
3719 * it here anymore.
3720 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003721 break;
3722 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003723 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3724 p2p_in_progress(wpa_s->global->p2p) > 1) {
3725 /* This radio work will be cancelled, so clear P2P
3726 * state as well.
3727 */
3728 p2p_stop_find(wpa_s->global->p2p);
3729 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003730#endif /* CONFIG_P2P */
3731
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003732 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3733 /*
3734 * Indicate disconnection to keep ctrl_iface events
3735 * consistent.
3736 */
3737 wpa_supplicant_event_disassoc(
3738 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003741 radio_remove_works(wpa_s, NULL, 0);
3742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003743 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3744 break;
3745 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003746 wpa_supplicant_update_channel_list(
3747 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 break;
3749 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 break;
3752 case EVENT_BEST_CHANNEL:
3753 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3754 "(%d %d %d)",
3755 data->best_chan.freq_24, data->best_chan.freq_5,
3756 data->best_chan.freq_overall);
3757 wpa_s->best_24_freq = data->best_chan.freq_24;
3758 wpa_s->best_5_freq = data->best_chan.freq_5;
3759 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3761 data->best_chan.freq_5,
3762 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 break;
3764 case EVENT_UNPROT_DEAUTH:
3765 wpa_supplicant_event_unprot_deauth(wpa_s,
3766 &data->unprot_deauth);
3767 break;
3768 case EVENT_UNPROT_DISASSOC:
3769 wpa_supplicant_event_unprot_disassoc(wpa_s,
3770 &data->unprot_disassoc);
3771 break;
3772 case EVENT_STATION_LOW_ACK:
3773#ifdef CONFIG_AP
3774 if (wpa_s->ap_iface && data)
3775 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3776 data->low_ack.addr);
3777#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003778#ifdef CONFIG_TDLS
3779 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003780 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3781 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003782#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 break;
3784 case EVENT_IBSS_PEER_LOST:
3785#ifdef CONFIG_IBSS_RSN
3786 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3787#endif /* CONFIG_IBSS_RSN */
3788 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003789 case EVENT_DRIVER_GTK_REKEY:
3790 if (os_memcmp(data->driver_gtk_rekey.bssid,
3791 wpa_s->bssid, ETH_ALEN))
3792 break;
3793 if (!wpa_s->wpa)
3794 break;
3795 wpa_sm_update_replay_ctr(wpa_s->wpa,
3796 data->driver_gtk_rekey.replay_ctr);
3797 break;
3798 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003799 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 wpa_s->sched_scanning = 0;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003801 resched = wpa_s->scanning;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003802 wpa_supplicant_notify_scanning(wpa_s, 0);
3803
3804 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3805 break;
3806
3807 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003808 * Start a new sched scan to continue searching for more SSIDs
3809 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003810 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07003811 if (wpa_s->sched_scan_timed_out) {
3812 wpa_supplicant_req_sched_scan(wpa_s);
3813 } else if (wpa_s->pno_sched_pending) {
3814 wpa_s->pno_sched_pending = 0;
3815 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003816 } else if (resched) {
3817 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003818 }
3819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003820 break;
3821 case EVENT_WPS_BUTTON_PUSHED:
3822#ifdef CONFIG_WPS
3823 wpas_wps_start_pbc(wpa_s, NULL, 0);
3824#endif /* CONFIG_WPS */
3825 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003826 case EVENT_AVOID_FREQUENCIES:
3827 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3828 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003829 case EVENT_CONNECT_FAILED_REASON:
3830#ifdef CONFIG_AP
3831 if (!wpa_s->ap_iface || !data)
3832 break;
3833 hostapd_event_connect_failed_reason(
3834 wpa_s->ap_iface->bss[0],
3835 data->connect_failed_reason.addr,
3836 data->connect_failed_reason.code);
3837#endif /* CONFIG_AP */
3838 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003839 case EVENT_NEW_PEER_CANDIDATE:
3840#ifdef CONFIG_MESH
3841 if (!wpa_s->ifmsh || !data)
3842 break;
3843 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3844 data->mesh_peer.ies,
3845 data->mesh_peer.ie_len);
3846#endif /* CONFIG_MESH */
3847 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848 default:
3849 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3850 break;
3851 }
3852}