blob: 17f057a3a7a435fb694a33d2f995dfbbf6089dd2 [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 Shmidtd7ff03d2015-12-04 14:49:35 -080075#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070076/**
77 * wpas_reenabled_network_time - Time until first network is re-enabled
78 * @wpa_s: Pointer to wpa_supplicant data
79 * Returns: If all enabled networks are temporarily disabled, returns the time
80 * (in sec) until the first network is re-enabled. Otherwise returns 0.
81 *
82 * This function is used in case all enabled networks are temporarily disabled,
83 * in which case it returns the time (in sec) that the first network will be
84 * re-enabled. The function assumes that at least one network is enabled.
85 */
86static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
87{
88 struct wpa_ssid *ssid;
89 int disabled_for, res = 0;
90
91#ifdef CONFIG_INTERWORKING
92 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
93 wpa_s->conf->cred)
94 return 0;
95#endif /* CONFIG_INTERWORKING */
96
97 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
98 if (ssid->disabled)
99 continue;
100
101 disabled_for = wpas_temp_disabled(wpa_s, ssid);
102 if (!disabled_for)
103 return 0;
104
105 if (!res || disabled_for < res)
106 res = disabled_for;
107 }
108
109 return res;
110}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800111#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700112
113
114void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
115{
116 struct wpa_supplicant *wpa_s = eloop_ctx;
117
118 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
119 return;
120
121 wpa_dbg(wpa_s, MSG_DEBUG,
122 "Try to associate due to network getting re-enabled");
123 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
124 wpa_supplicant_cancel_sched_scan(wpa_s);
125 wpa_supplicant_req_scan(wpa_s, 0, 0);
126 }
127}
128
129
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800130static struct wpa_bss * wpa_supplicant_get_new_bss(
131 struct wpa_supplicant *wpa_s, const u8 *bssid)
132{
133 struct wpa_bss *bss = NULL;
134 struct wpa_ssid *ssid = wpa_s->current_ssid;
135
136 if (ssid->ssid_len > 0)
137 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
138 if (!bss)
139 bss = wpa_bss_get_bssid(wpa_s, bssid);
140
141 return bss;
142}
143
144
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700145static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
146{
147 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
148
149 if (!bss) {
150 wpa_supplicant_update_scan_results(wpa_s);
151
152 /* Get the BSS from the new scan results */
153 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
154 }
155
156 if (bss)
157 wpa_s->current_bss = bss;
158}
159
160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
162{
163 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700164 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700165 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700168 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
169 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700170
171 if (wpa_s->current_ssid->ssid_len == 0)
172 return 0; /* current profile still in use */
173 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
174 if (res < 0) {
175 wpa_msg(wpa_s, MSG_INFO,
176 "Failed to read SSID from driver");
177 return 0; /* try to use current profile */
178 }
179 drv_ssid_len = res;
180
181 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
182 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
183 drv_ssid_len) == 0)
184 return 0; /* current profile still in use */
185
186 wpa_msg(wpa_s, MSG_DEBUG,
187 "Driver-initiated BSS selection changed the SSID to %s",
188 wpa_ssid_txt(drv_ssid, drv_ssid_len));
189 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191
192 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
193 "information");
194 ssid = wpa_supplicant_get_ssid(wpa_s);
195 if (ssid == NULL) {
196 wpa_msg(wpa_s, MSG_INFO,
197 "No network configuration found for the current AP");
198 return -1;
199 }
200
Dmitry Shmidt04949592012-07-19 12:16:46 -0700201 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
203 return -1;
204 }
205
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800206 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
207 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
208 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
209 return -1;
210 }
211
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700212 res = wpas_temp_disabled(wpa_s, ssid);
213 if (res > 0) {
214 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
215 "disabled for %d second(s)", res);
216 return -1;
217 }
218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
220 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 u8 wpa_ie[80];
223 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800224 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
225 wpa_ie, &wpa_ie_len) < 0)
226 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 } else {
228 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
229 }
230
231 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
232 eapol_sm_invalidate_cached_session(wpa_s->eapol);
233 old_ssid = wpa_s->current_ssid;
234 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800235
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700236 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
239 wpa_supplicant_initiate_eapol(wpa_s);
240 if (old_ssid != wpa_s->current_ssid)
241 wpas_notify_network_changed(wpa_s);
242
243 return 0;
244}
245
246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248{
249 struct wpa_supplicant *wpa_s = eloop_ctx;
250
251 if (wpa_s->countermeasures) {
252 wpa_s->countermeasures = 0;
253 wpa_drv_set_countermeasures(wpa_s, 0);
254 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800255
256 /*
257 * It is possible that the device is sched scanning, which means
258 * that a connection attempt will be done only when we receive
259 * scan results. However, in this case, it would be preferable
260 * to scan and connect immediately, so cancel the sched_scan and
261 * issue a regular scan flow.
262 */
263 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 wpa_supplicant_req_scan(wpa_s, 0, 0);
265 }
266}
267
268
269void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
270{
271 int bssid_changed;
272
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wnm_bss_keep_alive_deinit(wpa_s);
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275#ifdef CONFIG_IBSS_RSN
276 ibss_rsn_deinit(wpa_s->ibss_rsn);
277 wpa_s->ibss_rsn = NULL;
278#endif /* CONFIG_IBSS_RSN */
279
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700280#ifdef CONFIG_AP
281 wpa_supplicant_ap_deinit(wpa_s);
282#endif /* CONFIG_AP */
283
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700284#ifdef CONFIG_HS20
285 /* Clear possibly configured frame filters */
286 wpa_drv_configure_frame_filters(wpa_s, 0);
287#endif /* CONFIG_HS20 */
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
290 return;
291
292 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
293 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
294 os_memset(wpa_s->bssid, 0, ETH_ALEN);
295 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800296 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 wpa_s->current_bss = NULL;
298 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 if (bssid_changed)
301 wpas_notify_bssid_changed(wpa_s);
302
303 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
304 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
305 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
306 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
307 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700308 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700309 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800311
312 wpas_rrm_reset(wpa_s);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800313 wpa_s->wnmsleep_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314}
315
316
317static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
318{
319 struct wpa_ie_data ie;
320 int pmksa_set = -1;
321 size_t i;
322
323 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
324 ie.pmkid == NULL)
325 return;
326
327 for (i = 0; i < ie.num_pmkid; i++) {
328 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
329 ie.pmkid + i * PMKID_LEN,
330 NULL, NULL, 0);
331 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800332 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 break;
334 }
335 }
336
337 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
338 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
339}
340
341
342static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
343 union wpa_event_data *data)
344{
345 if (data == NULL) {
346 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
347 "event");
348 return;
349 }
350 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
351 " index=%d preauth=%d",
352 MAC2STR(data->pmkid_candidate.bssid),
353 data->pmkid_candidate.index,
354 data->pmkid_candidate.preauth);
355
356 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
357 data->pmkid_candidate.index,
358 data->pmkid_candidate.preauth);
359}
360
361
362static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
363{
364 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
365 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
366 return 0;
367
368#ifdef IEEE8021X_EAPOL
369 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
370 wpa_s->current_ssid &&
371 !(wpa_s->current_ssid->eapol_flags &
372 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
373 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
374 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
375 * plaintext or static WEP keys). */
376 return 0;
377 }
378#endif /* IEEE8021X_EAPOL */
379
380 return 1;
381}
382
383
384/**
385 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
386 * @wpa_s: pointer to wpa_supplicant data
387 * @ssid: Configuration data for the network
388 * Returns: 0 on success, -1 on failure
389 *
390 * This function is called when starting authentication with a network that is
391 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
392 */
393int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
394 struct wpa_ssid *ssid)
395{
396#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800397#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700398 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700400 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
401 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 return 0;
403
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700404 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 sim = 1;
406 aka = 1;
407 } else {
408 struct eap_method_type *eap = ssid->eap.eap_methods;
409 while (eap->vendor != EAP_VENDOR_IETF ||
410 eap->method != EAP_TYPE_NONE) {
411 if (eap->vendor == EAP_VENDOR_IETF) {
412 if (eap->method == EAP_TYPE_SIM)
413 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700414 else if (eap->method == EAP_TYPE_AKA ||
415 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 aka = 1;
417 }
418 eap++;
419 }
420 }
421
422 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
423 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700424 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
425 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
426 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 aka = 0;
428
429 if (!sim && !aka) {
430 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
431 "use SIM, but neither EAP-SIM nor EAP-AKA are "
432 "enabled");
433 return 0;
434 }
435
436 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
437 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700439 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 if (wpa_s->scard == NULL) {
441 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
442 "(pcsc-lite)");
443 return -1;
444 }
445 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
446 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448#endif /* IEEE8021X_EAPOL */
449
450 return 0;
451}
452
453
454#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700455
456static int has_wep_key(struct wpa_ssid *ssid)
457{
458 int i;
459
460 for (i = 0; i < NUM_WEP_KEYS; i++) {
461 if (ssid->wep_key_len[i])
462 return 1;
463 }
464
465 return 0;
466}
467
468
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700469static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 struct wpa_ssid *ssid)
471{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700472 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473
474 if (ssid->mixed_cell)
475 return 1;
476
477#ifdef CONFIG_WPS
478 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
479 return 1;
480#endif /* CONFIG_WPS */
481
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700482 if (has_wep_key(ssid))
483 privacy = 1;
484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485#ifdef IEEE8021X_EAPOL
486 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
487 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
488 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
489 privacy = 1;
490#endif /* IEEE8021X_EAPOL */
491
Jouni Malinen75ecf522011-06-27 15:19:46 -0700492 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
493 privacy = 1;
494
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800495 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
496 privacy = 1;
497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 if (bss->caps & IEEE80211_CAP_PRIVACY)
499 return privacy;
500 return !privacy;
501}
502
503
504static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
505 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700506 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507{
508 struct wpa_ie_data ie;
509 int proto_match = 0;
510 const u8 *rsn_ie, *wpa_ie;
511 int ret;
512 int wep_ok;
513
514 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
515 if (ret >= 0)
516 return ret;
517
518 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
519 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
520 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
521 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
522 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
523
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700524 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
526 proto_match++;
527
528 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
529 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
530 "failed");
531 break;
532 }
533
534 if (wep_ok &&
535 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
536 {
537 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
538 "in RSN IE");
539 return 1;
540 }
541
542 if (!(ie.proto & ssid->proto)) {
543 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
544 "mismatch");
545 break;
546 }
547
548 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
549 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
550 "cipher mismatch");
551 break;
552 }
553
554 if (!(ie.group_cipher & ssid->group_cipher)) {
555 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
556 "cipher mismatch");
557 break;
558 }
559
560 if (!(ie.key_mgmt & ssid->key_mgmt)) {
561 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
562 "mismatch");
563 break;
564 }
565
566#ifdef CONFIG_IEEE80211W
567 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800568 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800569 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
571 "frame protection");
572 break;
573 }
574#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800575 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
576 wpas_get_ssid_pmf(wpa_s, ssid) ==
577 NO_MGMT_FRAME_PROTECTION) {
578 wpa_dbg(wpa_s, MSG_DEBUG,
579 " skip RSN IE - no mgmt frame protection enabled but AP requires it");
580 break;
581 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800582#ifdef CONFIG_MBO
583 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
584 wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
585 wpas_get_ssid_pmf(wpa_s, ssid) !=
586 NO_MGMT_FRAME_PROTECTION) {
587 wpa_dbg(wpa_s, MSG_DEBUG,
588 " skip RSN IE - no mgmt frame protection enabled on MBO AP");
589 break;
590 }
591#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592
593 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
594 return 1;
595 }
596
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700597#ifdef CONFIG_IEEE80211W
598 if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
599 wpa_dbg(wpa_s, MSG_DEBUG,
600 " skip - MFP Required but network not MFP Capable");
601 return 0;
602 }
603#endif /* CONFIG_IEEE80211W */
604
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700605 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
607 proto_match++;
608
609 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
610 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
611 "failed");
612 break;
613 }
614
615 if (wep_ok &&
616 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
617 {
618 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
619 "in WPA IE");
620 return 1;
621 }
622
623 if (!(ie.proto & ssid->proto)) {
624 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
625 "mismatch");
626 break;
627 }
628
629 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
630 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
631 "cipher mismatch");
632 break;
633 }
634
635 if (!(ie.group_cipher & ssid->group_cipher)) {
636 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
637 "cipher mismatch");
638 break;
639 }
640
641 if (!(ie.key_mgmt & ssid->key_mgmt)) {
642 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
643 "mismatch");
644 break;
645 }
646
647 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
648 return 1;
649 }
650
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700651 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
652 !rsn_ie) {
653 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
654 return 1;
655 }
656
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
658 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
659 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
660 return 0;
661 }
662
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800663 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
664 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
665 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
666 return 1;
667 }
668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
670 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
671 return 1;
672 }
673
674 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
675 "WPA/WPA2");
676
677 return 0;
678}
679
680
681static int freq_allowed(int *freqs, int freq)
682{
683 int i;
684
685 if (freqs == NULL)
686 return 1;
687
688 for (i = 0; freqs[i]; i++)
689 if (freqs[i] == freq)
690 return 1;
691 return 0;
692}
693
694
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700695static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800696{
697 const struct hostapd_hw_modes *mode = NULL, *modes;
698 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
699 const u8 *rate_ie;
700 int i, j, k;
701
702 if (bss->freq == 0)
703 return 1; /* Cannot do matching without knowing band */
704
705 modes = wpa_s->hw.modes;
706 if (modes == NULL) {
707 /*
708 * The driver does not provide any additional information
709 * about the utilized hardware, so allow the connection attempt
710 * to continue.
711 */
712 return 1;
713 }
714
715 for (i = 0; i < wpa_s->hw.num_modes; i++) {
716 for (j = 0; j < modes[i].num_channels; j++) {
717 int freq = modes[i].channels[j].freq;
718 if (freq == bss->freq) {
719 if (mode &&
720 mode->mode == HOSTAPD_MODE_IEEE80211G)
721 break; /* do not allow 802.11b replace
722 * 802.11g */
723 mode = &modes[i];
724 break;
725 }
726 }
727 }
728
729 if (mode == NULL)
730 return 0;
731
732 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700733 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800734 if (rate_ie == NULL)
735 continue;
736
737 for (j = 2; j < rate_ie[1] + 2; j++) {
738 int flagged = !!(rate_ie[j] & 0x80);
739 int r = (rate_ie[j] & 0x7f) * 5;
740
741 /*
742 * IEEE Std 802.11n-2009 7.3.2.2:
743 * The new BSS Membership selector value is encoded
744 * like a legacy basic rate, but it is not a rate and
745 * only indicates if the BSS members are required to
746 * support the mandatory features of Clause 20 [HT PHY]
747 * in order to join the BSS.
748 */
749 if (flagged && ((rate_ie[j] & 0x7f) ==
750 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
751 if (!ht_supported(mode)) {
752 wpa_dbg(wpa_s, MSG_DEBUG,
753 " hardware does not support "
754 "HT PHY");
755 return 0;
756 }
757 continue;
758 }
759
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700760 /* There's also a VHT selector for 802.11ac */
761 if (flagged && ((rate_ie[j] & 0x7f) ==
762 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
763 if (!vht_supported(mode)) {
764 wpa_dbg(wpa_s, MSG_DEBUG,
765 " hardware does not support "
766 "VHT PHY");
767 return 0;
768 }
769 continue;
770 }
771
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800772 if (!flagged)
773 continue;
774
775 /* check for legacy basic rates */
776 for (k = 0; k < mode->num_rates; k++) {
777 if (mode->rates[k] == r)
778 break;
779 }
780 if (k == mode->num_rates) {
781 /*
782 * IEEE Std 802.11-2007 7.3.2.2 demands that in
783 * order to join a BSS all required rates
784 * have to be supported by the hardware.
785 */
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -0700786 wpa_dbg(wpa_s, MSG_DEBUG,
787 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
788 r / 10, r % 10,
789 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800790 return 0;
791 }
792 }
793 }
794
795 return 1;
796}
797
798
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800799/*
800 * Test whether BSS is in an ESS.
801 * This is done differently in DMG (60 GHz) and non-DMG bands
802 */
803static int bss_is_ess(struct wpa_bss *bss)
804{
805 if (bss_is_dmg(bss)) {
806 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
807 IEEE80211_CAP_DMG_AP;
808 }
809
810 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
811 IEEE80211_CAP_ESS);
812}
813
814
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800815static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
816{
817 size_t i;
818
819 for (i = 0; i < ETH_ALEN; i++) {
820 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
821 return 0;
822 }
823 return 1;
824}
825
826
827static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
828{
829 size_t i;
830
831 for (i = 0; i < num; i++) {
832 const u8 *a = list + i * ETH_ALEN * 2;
833 const u8 *m = a + ETH_ALEN;
834
835 if (match_mac_mask(a, addr, m))
836 return 1;
837 }
838 return 0;
839}
840
841
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800842struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
843 int i, struct wpa_bss *bss,
844 struct wpa_ssid *group,
845 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700847 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 int wpa;
849 struct wpa_blacklist *e;
850 const u8 *ie;
851 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800852 int osen;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800853#ifdef CONFIG_MBO
854 const u8 *assoc_disallow;
855#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700857 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 wpa_ie_len = ie ? ie[1] : 0;
859
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700860 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 rsn_ie_len = ie ? ie[1] : 0;
862
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800863 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
864 osen = ie != NULL;
865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800867 "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 -0700868 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800869 wpa_ie_len, rsn_ie_len, bss->caps, bss->level, bss->freq,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700870 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
871 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
872 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800873 " p2p" : "",
874 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875
876 e = wpa_blacklist_get(wpa_s, bss->bssid);
877 if (e) {
878 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700879 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 /*
881 * When only a single network is enabled, we can
882 * trigger blacklisting on the first failure. This
883 * should not be done with multiple enabled networks to
884 * avoid getting forced to move into a worse ESS on
885 * single error if there are no other BSSes of the
886 * current ESS.
887 */
888 limit = 0;
889 }
890 if (e->count > limit) {
891 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
892 "(count=%d limit=%d)", e->count, limit);
893 return NULL;
894 }
895 }
896
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700897 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
899 return NULL;
900 }
901
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 if (disallowed_bssid(wpa_s, bss->bssid)) {
903 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
904 return NULL;
905 }
906
907 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
908 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
909 return NULL;
910 }
911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
913
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800914 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700916 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
Dmitry Shmidt04949592012-07-19 12:16:46 -0700918 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
920 continue;
921 }
922
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700923 res = wpas_temp_disabled(wpa_s, ssid);
924 if (res > 0) {
925 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
926 "temporarily for %d second(s)", res);
927 continue;
928 }
929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930#ifdef CONFIG_WPS
931 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
932 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
933 "(WPS)");
934 continue;
935 }
936
937 if (wpa && ssid->ssid_len == 0 &&
938 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
939 check_ssid = 0;
940
941 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
942 /* Only allow wildcard SSID match if an AP
943 * advertises active WPS operation that matches
944 * with our mode. */
945 check_ssid = 1;
946 if (ssid->ssid_len == 0 &&
947 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
948 check_ssid = 0;
949 }
950#endif /* CONFIG_WPS */
951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800952 if (ssid->bssid_set && ssid->ssid_len == 0 &&
953 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
954 check_ssid = 0;
955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700957 (bss->ssid_len != ssid->ssid_len ||
958 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
960 continue;
961 }
962
963 if (ssid->bssid_set &&
964 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
965 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
966 continue;
967 }
968
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800969 /* check blacklist */
970 if (ssid->num_bssid_blacklist &&
971 addr_in_list(bss->bssid, ssid->bssid_blacklist,
972 ssid->num_bssid_blacklist)) {
973 wpa_dbg(wpa_s, MSG_DEBUG,
974 " skip - BSSID blacklisted");
975 continue;
976 }
977
978 /* if there is a whitelist, only accept those APs */
979 if (ssid->num_bssid_whitelist &&
980 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
981 ssid->num_bssid_whitelist)) {
982 wpa_dbg(wpa_s, MSG_DEBUG,
983 " skip - BSSID not in whitelist");
984 continue;
985 }
986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
988 continue;
989
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800990 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
992 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
993 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
994 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
995 "not allowed");
996 continue;
997 }
998
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700999 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
1000 has_wep_key(ssid)) {
1001 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
1002 continue;
1003 }
1004
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001005 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
1006 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
1007 "not allowed");
1008 continue;
1009 }
1010
Jouni Malinen75ecf522011-06-27 15:19:46 -07001011 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
1013 "mismatch");
1014 continue;
1015 }
1016
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001017 if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
1018 !bss_is_pbss(bss)) {
1019 wpa_dbg(wpa_s, MSG_DEBUG,
1020 " skip - not ESS, PBSS, or MBSS");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001021 continue;
1022 }
1023
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001024 if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001025 wpa_dbg(wpa_s, MSG_DEBUG, " skip - PBSS mismatch (ssid %d bss %d)",
1026 ssid->pbss, bss_is_pbss(bss));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027 continue;
1028 }
1029
1030 if (!freq_allowed(ssid->freq_list, bss->freq)) {
1031 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
1032 "allowed");
1033 continue;
1034 }
1035
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001036#ifdef CONFIG_MESH
1037 if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
1038 ssid->frequency != bss->freq) {
1039 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not allowed (mesh)");
1040 continue;
1041 }
1042#endif /* CONFIG_MESH */
1043
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001044 if (!rate_match(wpa_s, bss)) {
1045 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
1046 "not match");
1047 continue;
1048 }
1049
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001050#ifndef CONFIG_IBSS_RSN
1051 if (ssid->mode == WPAS_MODE_IBSS &&
1052 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1053 WPA_KEY_MGMT_WPA_NONE))) {
1054 wpa_dbg(wpa_s, MSG_DEBUG,
1055 " skip - IBSS RSN not supported in the build");
1056 continue;
1057 }
1058#endif /* !CONFIG_IBSS_RSN */
1059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001061 if (ssid->p2p_group &&
1062 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1063 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1064 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
1065 continue;
1066 }
1067
Dmitry Shmidt54605472013-11-08 11:10:19 -08001068 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1069 struct wpabuf *p2p_ie;
1070 u8 dev_addr[ETH_ALEN];
1071
1072 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1073 if (ie == NULL) {
1074 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
1075 continue;
1076 }
1077 p2p_ie = wpa_bss_get_vendor_ie_multi(
1078 bss, P2P_IE_VENDOR_TYPE);
1079 if (p2p_ie == NULL) {
1080 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
1081 continue;
1082 }
1083
1084 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1085 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1086 ETH_ALEN) != 0) {
1087 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
1088 wpabuf_free(p2p_ie);
1089 continue;
1090 }
1091 wpabuf_free(p2p_ie);
1092 }
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 /*
1095 * TODO: skip the AP if its P2P IE has Group Formation
1096 * bit set in the P2P Group Capability Bitmap and we
1097 * are not in Group Formation with that device.
1098 */
1099#endif /* CONFIG_P2P */
1100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001101 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1102 {
1103 struct os_reltime diff;
1104
1105 os_reltime_sub(&wpa_s->scan_min_time,
1106 &bss->last_update, &diff);
1107 wpa_dbg(wpa_s, MSG_DEBUG,
1108 " skip - scan result not recent enough (%u.%06u seconds too old)",
1109 (unsigned int) diff.sec,
1110 (unsigned int) diff.usec);
1111 continue;
1112 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001113#ifdef CONFIG_MBO
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001114#ifdef CONFIG_TESTING_OPTIONS
1115 if (wpa_s->ignore_assoc_disallow)
1116 goto skip_assoc_disallow;
1117#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001118 assoc_disallow = wpas_mbo_get_bss_attr(
1119 bss, MBO_ATTR_ID_ASSOC_DISALLOW);
1120 if (assoc_disallow && assoc_disallow[1] >= 1) {
1121 wpa_dbg(wpa_s, MSG_DEBUG,
1122 " skip - MBO association disallowed (reason %u)",
1123 assoc_disallow[2]);
1124 continue;
1125 }
1126
1127 if (wpa_is_bss_tmp_disallowed(wpa_s, bss->bssid)) {
1128 wpa_dbg(wpa_s, MSG_DEBUG,
1129 " skip - MBO retry delay has not passed yet");
1130 continue;
1131 }
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001132#ifdef CONFIG_TESTING_OPTIONS
1133 skip_assoc_disallow:
1134#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001135#endif /* CONFIG_MBO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137 /* Matching configuration found */
1138 return ssid;
1139 }
1140
1141 /* No matching configuration found */
1142 return NULL;
1143}
1144
1145
1146static struct wpa_bss *
1147wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001149 struct wpa_ssid **selected_ssid,
1150 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001152 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001154 if (only_first_ssid)
1155 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1156 group->id);
1157 else
1158 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1159 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001161 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1162 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001163 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1164 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 if (!*selected_ssid)
1166 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1168 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001169 MAC2STR(bss->bssid),
1170 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1171 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 }
1173
1174 return NULL;
1175}
1176
1177
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001178struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1179 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180{
1181 struct wpa_bss *selected = NULL;
1182 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001183 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001184 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001186 if (wpa_s->last_scan_res == NULL ||
1187 wpa_s->last_scan_res_used == 0)
1188 return NULL; /* no scan results from last update */
1189
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001190 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001191 /* check that next_ssid is still valid */
1192 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1193 if (ssid == wpa_s->next_ssid)
1194 break;
1195 }
1196 next_ssid = ssid;
1197 wpa_s->next_ssid = NULL;
1198 }
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001201 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1202 if (next_ssid && next_ssid->priority ==
1203 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001204 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001205 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001206 if (selected)
1207 break;
1208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001210 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001211 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 if (selected)
1213 break;
1214 }
1215
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216 if (selected == NULL && wpa_s->blacklist &&
1217 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1219 "blacklist and try again");
1220 wpa_blacklist_clear(wpa_s);
1221 wpa_s->blacklist_cleared++;
1222 } else if (selected == NULL)
1223 break;
1224 }
1225
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001226 ssid = *selected_ssid;
1227 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1228 !ssid->passphrase && !ssid->ext_psk) {
1229 const char *field_name, *txt = NULL;
1230
1231 wpa_dbg(wpa_s, MSG_DEBUG,
1232 "PSK/passphrase not yet available for the selected network");
1233
1234 wpas_notify_network_request(wpa_s, ssid,
1235 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1236
1237 field_name = wpa_supplicant_ctrl_req_to_string(
1238 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1239 if (field_name == NULL)
1240 return NULL;
1241
1242 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1243
1244 selected = NULL;
1245 }
1246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247 return selected;
1248}
1249
1250
1251static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1252 int timeout_sec, int timeout_usec)
1253{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001254 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 /*
1256 * No networks are enabled; short-circuit request so
1257 * we don't wait timeout seconds before transitioning
1258 * to INACTIVE state.
1259 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001260 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1261 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1263 return;
1264 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001265
1266 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1268}
1269
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001270
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001271int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 struct wpa_bss *selected,
1273 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274{
1275 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1276 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1277 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001278 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001280 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1281 wpa_s->p2p_in_provisioning) {
1282 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1283 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001284 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286#endif /* CONFIG_P2P */
1287
1288#ifdef CONFIG_WPS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001289 wpas_wps_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 wpas_wps_cancel(wpa_s);
1291#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001292 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 }
1294
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001295 wpa_msg(wpa_s, MSG_DEBUG,
1296 "Considering connect request: reassociate: %d selected: "
1297 MACSTR " bssid: " MACSTR " pending: " MACSTR
1298 " wpa_state: %s ssid=%p current_ssid=%p",
1299 wpa_s->reassociate, MAC2STR(selected->bssid),
1300 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1301 wpa_supplicant_state_txt(wpa_s->wpa_state),
1302 ssid, wpa_s->current_ssid);
1303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 /*
1305 * Do not trigger new association unless the BSSID has changed or if
1306 * reassociation is requested. If we are in process of associating with
1307 * the selected BSSID, do not trigger new attempt.
1308 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001309 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1311 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1312 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001313 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1314 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1315 0) ||
1316 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1317 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1319 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001320 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001322 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1323 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 wpa_supplicant_associate(wpa_s, selected, ssid);
1325 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001326 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1327 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001329
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001330 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331}
1332
1333
1334static struct wpa_ssid *
1335wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1336{
1337 int prio;
1338 struct wpa_ssid *ssid;
1339
1340 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1341 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1342 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001343 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 continue;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001345#ifndef CONFIG_IBSS_RSN
1346 if (ssid->mode == WPAS_MODE_IBSS &&
1347 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1348 WPA_KEY_MGMT_WPA_NONE))) {
1349 wpa_msg(wpa_s, MSG_INFO,
1350 "IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
1351 wpa_ssid_txt(ssid->ssid,
1352 ssid->ssid_len));
1353 continue;
1354 }
1355#endif /* !CONFIG_IBSS_RSN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001357 ssid->mode == IEEE80211_MODE_AP ||
1358 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 return ssid;
1360 }
1361 }
1362 return NULL;
1363}
1364
1365
1366/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1367 * on BSS added and BSS changed events */
1368static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001369 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001371 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372
1373 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1374 return;
1375
Jouni Malinen87fd2792011-05-16 18:35:42 +03001376 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378
Jouni Malinen87fd2792011-05-16 18:35:42 +03001379 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 if (ssid == NULL)
1381 continue;
1382
Jouni Malinen87fd2792011-05-16 18:35:42 +03001383 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 if (rsn == NULL)
1385 continue;
1386
Jouni Malinen87fd2792011-05-16 18:35:42 +03001387 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 }
1389
1390}
1391
1392
1393static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1394 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001395 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001397 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001398#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 int min_diff;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001400 int to_5ghz;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001401#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402
1403 if (wpa_s->reassociate)
1404 return 1; /* explicit request to reassociate */
1405 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1406 return 1; /* we are not associated; continue */
1407 if (wpa_s->current_ssid == NULL)
1408 return 1; /* unknown current SSID */
1409 if (wpa_s->current_ssid != ssid)
1410 return 1; /* different network block */
1411
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001412 if (wpas_driver_bss_selection(wpa_s))
1413 return 0; /* Driver-based roaming */
1414
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001415 if (wpa_s->current_ssid->ssid)
1416 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1417 wpa_s->current_ssid->ssid,
1418 wpa_s->current_ssid->ssid_len);
1419 if (!current_bss)
1420 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
1422 if (!current_bss)
1423 return 1; /* current BSS not seen in scan results */
1424
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001425 if (current_bss == selected)
1426 return 0;
1427
1428 if (selected->last_update_idx > current_bss->last_update_idx)
1429 return 1; /* current BSS not seen in the last scan */
1430
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001431#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001433 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1434 " level=%d snr=%d est_throughput=%u",
1435 MAC2STR(current_bss->bssid), current_bss->level,
1436 current_bss->snr, current_bss->est_throughput);
1437 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1438 " level=%d snr=%d est_throughput=%u",
1439 MAC2STR(selected->bssid), selected->level,
1440 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441
1442 if (wpa_s->current_ssid->bssid_set &&
1443 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1444 0) {
1445 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1446 "has preferred BSSID");
1447 return 1;
1448 }
1449
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001450 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1451 wpa_dbg(wpa_s, MSG_DEBUG,
1452 "Allow reassociation - selected BSS has better estimated throughput");
1453 return 1;
1454 }
1455
Dmitry Shmidte4663042016-04-04 10:07:49 -07001456 to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
1457
1458 if (current_bss->level < 0 &&
1459 current_bss->level > selected->level + to_5ghz * 2) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001460 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1461 "signal level");
1462 return 0;
1463 }
1464
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 min_diff = 2;
1466 if (current_bss->level < 0) {
1467 if (current_bss->level < -85)
1468 min_diff = 1;
1469 else if (current_bss->level < -80)
1470 min_diff = 2;
1471 else if (current_bss->level < -75)
1472 min_diff = 3;
1473 else if (current_bss->level < -70)
1474 min_diff = 4;
1475 else
1476 min_diff = 5;
1477 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001478 if (to_5ghz) {
1479 /* Make it easier to move to 5 GHz band */
1480 if (min_diff > 2)
1481 min_diff -= 2;
1482 else
1483 min_diff = 0;
1484 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485 if (abs(current_bss->level - selected->level) < min_diff) {
1486 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1487 "in signal level");
1488 return 0;
1489 }
1490
1491 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001492#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001493 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001494#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495}
1496
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001497
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001498/*
1499 * Return a negative value if no scan results could be fetched or if scan
1500 * results should not be shared with other virtual interfaces.
1501 * Return 0 if scan results were fetched and may be shared with other
1502 * interfaces.
1503 * Return 1 if scan results may be shared with other virtual interfaces but may
1504 * not trigger any operations.
1505 * Return 2 if the interface was removed and cannot be used.
1506 */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001507static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001508 union wpa_event_data *data,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001509 int own_request, int update_only)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001511 struct wpa_scan_results *scan_res = NULL;
1512 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001514#ifndef CONFIG_NO_RANDOM_POOL
1515 size_t i, num;
1516#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517
1518#ifdef CONFIG_AP
1519 if (wpa_s->ap_iface)
1520 ap = 1;
1521#endif /* CONFIG_AP */
1522
1523 wpa_supplicant_notify_scanning(wpa_s, 0);
1524
1525 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1526 data ? &data->scan_info :
1527 NULL, 1);
1528 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001529 if (wpa_s->conf->ap_scan == 2 || ap ||
1530 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001532 if (!own_request)
1533 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001534 if (data && data->scan_info.external_scan)
1535 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1537 "scanning again");
1538 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001539 ret = -1;
1540 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 }
1542
1543#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 num = scan_res->num;
1545 if (num > 10)
1546 num = 10;
1547 for (i = 0; i < num; i++) {
1548 u8 buf[5];
1549 struct wpa_scan_res *res = scan_res->res[i];
1550 buf[0] = res->bssid[5];
1551 buf[1] = res->qual & 0xff;
1552 buf[2] = res->noise & 0xff;
1553 buf[3] = res->level & 0xff;
1554 buf[4] = res->tsf & 0xff;
1555 random_add_randomness(buf, sizeof(buf));
1556 }
1557#endif /* CONFIG_NO_RANDOM_POOL */
1558
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001559 if (update_only) {
1560 ret = 1;
1561 goto scan_work_done;
1562 }
1563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001564 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001565 !(data && data->scan_info.external_scan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1567 struct wpa_scan_results *scan_res);
1568
1569 scan_res_handler = wpa_s->scan_res_handler;
1570 wpa_s->scan_res_handler = NULL;
1571 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001572 ret = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001573 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574 }
1575
1576 if (ap) {
1577 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1578#ifdef CONFIG_AP
1579 if (wpa_s->ap_iface->scan_cb)
1580 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1581#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001582 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001584
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001585 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001586 wpa_s->own_scan_running,
1587 data ? data->scan_info.external_scan : 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001588 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001589 wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1590 own_request && !(data && data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001591 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1592 wpa_s->manual_scan_id);
1593 wpa_s->manual_scan_use_id = 0;
1594 } else {
1595 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1596 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001597 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598
1599 wpas_notify_scan_done(wpa_s, 1);
1600
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001601 if (data && data->scan_info.external_scan) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001602 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 -07001603 wpa_scan_results_free(scan_res);
1604 return 0;
1605 }
1606
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001607 if (wnm_scan_process(wpa_s, 1) > 0)
1608 goto scan_work_done;
1609
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001610 if (sme_proc_obss_scan(wpa_s) > 0)
1611 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1614 goto scan_work_done;
1615
1616 if (autoscan_notify_scan(wpa_s, scan_res))
1617 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 if (wpa_s->disconnected) {
1620 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 }
1623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001625 bgscan_notify_scan(wpa_s, scan_res) == 1)
1626 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628 wpas_wps_update_ap_info(wpa_s, scan_res);
1629
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001630 if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
1631 wpa_s->wpa_state < WPA_COMPLETED)
1632 goto scan_work_done;
1633
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001634 wpa_scan_results_free(scan_res);
1635
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001636 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001637 struct wpa_radio_work *work = wpa_s->scan_work;
1638 wpa_s->scan_work = NULL;
1639 radio_work_done(work);
1640 }
1641
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001642 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001643
1644scan_work_done:
1645 wpa_scan_results_free(scan_res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001646 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001647 struct wpa_radio_work *work = wpa_s->scan_work;
1648 wpa_s->scan_work = NULL;
1649 radio_work_done(work);
1650 }
1651 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001652}
1653
1654
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001655static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001656 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001657{
1658 struct wpa_bss *selected;
1659 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001660 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1661
1662 if (time_to_reenable > 0) {
1663 wpa_dbg(wpa_s, MSG_DEBUG,
1664 "Postpone network selection by %d seconds since all networks are disabled",
1665 time_to_reenable);
1666 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1667 eloop_register_timeout(time_to_reenable, 0,
1668 wpas_network_reenabled, wpa_s, NULL);
1669 return 0;
1670 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001671
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001672 if (wpa_s->p2p_mgmt)
1673 return 0; /* no normal connection on p2p_mgmt interface */
1674
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001675 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001677#ifdef CONFIG_MESH
1678 if (wpa_s->ifmsh) {
1679 wpa_msg(wpa_s, MSG_INFO,
1680 "Avoiding join because we already joined a mesh group");
1681 return 0;
1682 }
1683#endif /* CONFIG_MESH */
1684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 if (selected) {
1686 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001687 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001689 if (new_scan)
1690 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001692 }
1693
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001694 if (ssid != wpa_s->current_ssid &&
1695 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1696 wpa_s->own_disconnect_req = 1;
1697 wpa_supplicant_deauthenticate(
1698 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1699 }
1700
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001701 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001702 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001703 return -1;
1704 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001705 if (new_scan)
1706 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001707 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001708 * Do not allow other virtual radios to trigger operations based
1709 * on these scan results since we do not want them to start
1710 * other associations at the same time.
Jouni Malinen89ca7022012-09-14 13:03:12 -07001711 */
1712 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1715 ssid = wpa_supplicant_pick_new_network(wpa_s);
1716 if (ssid) {
1717 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1718 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001719 if (new_scan)
1720 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001721 } else if (own_request) {
1722 /*
1723 * No SSID found. If SCAN results are as a result of
1724 * own scan request and not due to a scan request on
1725 * another shared interface, try another scan.
1726 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 int timeout_sec = wpa_s->scan_interval;
1728 int timeout_usec = 0;
1729#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001730 int res;
1731
1732 res = wpas_p2p_scan_no_go_seen(wpa_s);
1733 if (res == 2)
1734 return 2;
1735 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001736 return 0;
1737
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001738 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001739 wpa_s->show_group_started ||
1740 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 /*
1742 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001743 * state and during P2P join-a-group operation
1744 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 */
1746 timeout_sec = 0;
1747 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001748 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1749 timeout_usec);
1750 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001751 }
1752#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001753#ifdef CONFIG_INTERWORKING
1754 if (wpa_s->conf->auto_interworking &&
1755 wpa_s->conf->interworking &&
1756 wpa_s->conf->cred) {
1757 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1758 "start ANQP fetch since no matching "
1759 "networks found");
1760 wpa_s->network_select = 1;
1761 wpa_s->auto_network_select = 1;
1762 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001763 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001764 }
1765#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001766#ifdef CONFIG_WPS
1767 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1768 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1769 timeout_sec = 0;
1770 timeout_usec = 500000;
1771 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1772 timeout_usec);
1773 return 0;
1774 }
1775#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 if (wpa_supplicant_req_sched_scan(wpa_s))
1777 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1778 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001779
1780 wpa_msg_ctrl(wpa_s, MSG_INFO,
1781 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 }
1783 }
1784 return 0;
1785}
1786
1787
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001788static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1789 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001792 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001793
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001794 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001795 if (res == 2) {
1796 /*
1797 * Interface may have been removed, so must not dereference
1798 * wpa_s after this.
1799 */
1800 return 1;
1801 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001802
1803 if (res < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 /*
1805 * If no scan results could be fetched, then no need to
1806 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001807 * this scan. Similarly, if scan results started a new operation on this
1808 * interface, do not notify other interfaces to avoid concurrent
1809 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001811 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812 }
1813
1814 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001815 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 * so, they get updated with this same scan info.
1817 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001818 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1819 radio_list) {
1820 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1822 "sibling", ifs->ifname);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001823 res = _wpa_supplicant_event_scan_results(ifs, data, 0,
1824 res > 0);
1825 if (res < 0)
1826 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 }
1828 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001829
1830 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831}
1832
1833#endif /* CONFIG_NO_SCAN_PROCESSING */
1834
1835
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001836int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1837{
1838#ifdef CONFIG_NO_SCAN_PROCESSING
1839 return -1;
1840#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001841 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001842
Dmitry Shmidt41712582015-06-29 11:02:15 -07001843 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001844 return -1;
1845
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001846 os_get_reltime(&now);
1847 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001848 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1849 return -1;
1850 }
1851
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001852 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001853#endif /* CONFIG_NO_SCAN_PROCESSING */
1854}
1855
Dmitry Shmidt04949592012-07-19 12:16:46 -07001856#ifdef CONFIG_WNM
1857
1858static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1859{
1860 struct wpa_supplicant *wpa_s = eloop_ctx;
1861
1862 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1863 return;
1864
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001865 if (!wpa_s->no_keep_alive) {
1866 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1867 MAC2STR(wpa_s->bssid));
1868 /* TODO: could skip this if normal data traffic has been sent */
1869 /* TODO: Consider using some more appropriate data frame for
1870 * this */
1871 if (wpa_s->l2)
1872 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1873 (u8 *) "", 0);
1874 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001875
1876#ifdef CONFIG_SME
1877 if (wpa_s->sme.bss_max_idle_period) {
1878 unsigned int msec;
1879 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1880 if (msec > 100)
1881 msec -= 100;
1882 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1883 wnm_bss_keep_alive, wpa_s, NULL);
1884 }
1885#endif /* CONFIG_SME */
1886}
1887
1888
1889static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1890 const u8 *ies, size_t ies_len)
1891{
1892 struct ieee802_11_elems elems;
1893
1894 if (ies == NULL)
1895 return;
1896
1897 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1898 return;
1899
1900#ifdef CONFIG_SME
1901 if (elems.bss_max_idle_period) {
1902 unsigned int msec;
1903 wpa_s->sme.bss_max_idle_period =
1904 WPA_GET_LE16(elems.bss_max_idle_period);
1905 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1906 "TU)%s", wpa_s->sme.bss_max_idle_period,
1907 (elems.bss_max_idle_period[2] & 0x01) ?
1908 " (protected keep-live required)" : "");
1909 if (wpa_s->sme.bss_max_idle_period == 0)
1910 wpa_s->sme.bss_max_idle_period = 1;
1911 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1912 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1913 /* msec times 1000 */
1914 msec = wpa_s->sme.bss_max_idle_period * 1024;
1915 if (msec > 100)
1916 msec -= 100;
1917 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1918 wnm_bss_keep_alive, wpa_s,
1919 NULL);
1920 }
1921 }
1922#endif /* CONFIG_SME */
1923}
1924
1925#endif /* CONFIG_WNM */
1926
1927
1928void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1929{
1930#ifdef CONFIG_WNM
1931 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1932#endif /* CONFIG_WNM */
1933}
1934
1935
Dmitry Shmidt051af732013-10-22 13:52:46 -07001936#ifdef CONFIG_INTERWORKING
1937
1938static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1939 size_t len)
1940{
1941 int res;
1942
1943 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1944 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1945 if (res) {
1946 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1947 }
1948
1949 return res;
1950}
1951
1952
1953static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1954 const u8 *ies, size_t ies_len)
1955{
1956 struct ieee802_11_elems elems;
1957
1958 if (ies == NULL)
1959 return;
1960
1961 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1962 return;
1963
1964 if (elems.qos_map_set) {
1965 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1966 elems.qos_map_set_len);
1967 }
1968}
1969
1970#endif /* CONFIG_INTERWORKING */
1971
1972
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001973#ifdef CONFIG_FST
1974static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
1975 const u8 *ie, size_t ie_len)
1976{
1977 struct mb_ies_info mb_ies;
1978
1979 if (!ie || !ie_len || !wpa_s->fst)
1980 return -ENOENT;
1981
1982 os_memset(&mb_ies, 0, sizeof(mb_ies));
1983
1984 while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
1985 size_t len;
1986
1987 len = 2 + ie[1];
1988 if (len > ie_len) {
1989 wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
1990 ie, ie_len);
1991 break;
1992 }
1993
1994 if (ie[0] == WLAN_EID_MULTI_BAND) {
1995 wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
1996 (unsigned int) len);
1997 mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
1998 mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
1999 mb_ies.nof_ies++;
2000 }
2001
2002 ie_len -= len;
2003 ie += len;
2004 }
2005
2006 if (mb_ies.nof_ies > 0) {
2007 wpabuf_free(wpa_s->received_mb_ies);
2008 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2009 return 0;
2010 }
2011
2012 return -ENOENT;
2013}
2014#endif /* CONFIG_FST */
2015
2016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
2018 union wpa_event_data *data)
2019{
2020 int l, len, found = 0, wpa_found, rsn_found;
2021 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002022#ifdef CONFIG_IEEE80211R
2023 u8 bssid[ETH_ALEN];
2024#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025
2026 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
2027 if (data->assoc_info.req_ies)
2028 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
2029 data->assoc_info.req_ies_len);
2030 if (data->assoc_info.resp_ies) {
2031 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
2032 data->assoc_info.resp_ies_len);
2033#ifdef CONFIG_TDLS
2034 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
2035 data->assoc_info.resp_ies_len);
2036#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002037#ifdef CONFIG_WNM
2038 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2039 data->assoc_info.resp_ies_len);
2040#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002041#ifdef CONFIG_INTERWORKING
2042 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2043 data->assoc_info.resp_ies_len);
2044#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 }
2046 if (data->assoc_info.beacon_ies)
2047 wpa_hexdump(MSG_DEBUG, "beacon_ies",
2048 data->assoc_info.beacon_ies,
2049 data->assoc_info.beacon_ies_len);
2050 if (data->assoc_info.freq)
2051 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
2052 data->assoc_info.freq);
2053
2054 p = data->assoc_info.req_ies;
2055 l = data->assoc_info.req_ies_len;
2056
2057 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
2058 while (p && l >= 2) {
2059 len = p[1] + 2;
2060 if (len > l) {
2061 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2062 p, l);
2063 break;
2064 }
2065 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2066 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
Dmitry Shmidte4663042016-04-04 10:07:49 -07002067 (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
2068 (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
2070 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
2071 break;
2072 found = 1;
2073 wpa_find_assoc_pmkid(wpa_s);
2074 break;
2075 }
2076 l -= len;
2077 p += len;
2078 }
2079 if (!found && data->assoc_info.req_ies)
2080 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2081
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002082#ifdef CONFIG_FILS
2083#ifdef CONFIG_SME
2084 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS &&
2085 (!data->assoc_info.resp_frame ||
2086 fils_process_assoc_resp(wpa_s->wpa,
2087 data->assoc_info.resp_frame,
2088 data->assoc_info.resp_frame_len) < 0)) {
2089 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2090 return -1;
2091 }
2092#endif /* CONFIG_SME */
2093#endif /* CONFIG_FILS */
2094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095#ifdef CONFIG_IEEE80211R
2096#ifdef CONFIG_SME
2097 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2099 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2100 data->assoc_info.resp_ies,
2101 data->assoc_info.resp_ies_len,
2102 bssid) < 0) {
2103 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2104 "Reassociation Response failed");
2105 wpa_supplicant_deauthenticate(
2106 wpa_s, WLAN_REASON_INVALID_IE);
2107 return -1;
2108 }
2109 }
2110
2111 p = data->assoc_info.resp_ies;
2112 l = data->assoc_info.resp_ies_len;
2113
2114#ifdef CONFIG_WPS_STRICT
2115 if (p && wpa_s->current_ssid &&
2116 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
2117 struct wpabuf *wps;
2118 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
2119 if (wps == NULL) {
2120 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
2121 "include WPS IE in (Re)Association Response");
2122 return -1;
2123 }
2124
2125 if (wps_validate_assoc_resp(wps) < 0) {
2126 wpabuf_free(wps);
2127 wpa_supplicant_deauthenticate(
2128 wpa_s, WLAN_REASON_INVALID_IE);
2129 return -1;
2130 }
2131 wpabuf_free(wps);
2132 }
2133#endif /* CONFIG_WPS_STRICT */
2134
2135 /* Go through the IEs and make a copy of the MDIE, if present. */
2136 while (p && l >= 2) {
2137 len = p[1] + 2;
2138 if (len > l) {
2139 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2140 p, l);
2141 break;
2142 }
2143 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
2144 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
2145 wpa_s->sme.ft_used = 1;
2146 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
2147 MOBILITY_DOMAIN_ID_LEN);
2148 break;
2149 }
2150 l -= len;
2151 p += len;
2152 }
2153#endif /* CONFIG_SME */
2154
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002155 /* Process FT when SME is in the driver */
2156 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2157 wpa_ft_is_completed(wpa_s->wpa)) {
2158 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2159 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2160 data->assoc_info.resp_ies,
2161 data->assoc_info.resp_ies_len,
2162 bssid) < 0) {
2163 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2164 "Reassociation Response failed");
2165 wpa_supplicant_deauthenticate(
2166 wpa_s, WLAN_REASON_INVALID_IE);
2167 return -1;
2168 }
2169 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
2170 }
2171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
2173 data->assoc_info.resp_ies_len);
2174#endif /* CONFIG_IEEE80211R */
2175
2176 /* WPA/RSN IE from Beacon/ProbeResp */
2177 p = data->assoc_info.beacon_ies;
2178 l = data->assoc_info.beacon_ies_len;
2179
2180 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
2181 */
2182 wpa_found = rsn_found = 0;
2183 while (p && l >= 2) {
2184 len = p[1] + 2;
2185 if (len > l) {
2186 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
2187 p, l);
2188 break;
2189 }
2190 if (!wpa_found &&
2191 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2192 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2193 wpa_found = 1;
2194 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2195 }
2196
2197 if (!rsn_found &&
2198 p[0] == WLAN_EID_RSN && p[1] >= 2) {
2199 rsn_found = 1;
2200 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2201 }
2202
2203 l -= len;
2204 p += len;
2205 }
2206
2207 if (!wpa_found && data->assoc_info.beacon_ies)
2208 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2209 if (!rsn_found && data->assoc_info.beacon_ies)
2210 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2211 if (wpa_found || rsn_found)
2212 wpa_s->ap_ies_from_associnfo = 1;
2213
Jouni Malinen87fd2792011-05-16 18:35:42 +03002214 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2215 wpa_s->assoc_freq != data->assoc_info.freq) {
2216 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2217 "%u to %u MHz",
2218 wpa_s->assoc_freq, data->assoc_info.freq);
2219 wpa_supplicant_update_scan_results(wpa_s);
2220 }
2221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 wpa_s->assoc_freq = data->assoc_info.freq;
2223
2224 return 0;
2225}
2226
2227
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002228static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2229{
2230 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2231
2232 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2233 return -1;
2234
2235 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2236 return 0;
2237
2238 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2239 WPA_IE_VENDOR_TYPE);
2240 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2241
2242 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2243 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2244 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2245 bss_rsn ? 2 + bss_rsn[1] : 0))
2246 return -1;
2247
2248 return 0;
2249}
2250
2251
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002252static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
2253 union wpa_event_data *data)
2254{
2255#ifdef CONFIG_FST
2256 struct assoc_info *ai = data ? &data->assoc_info : NULL;
2257 struct wpa_bss *bss = wpa_s->current_bss;
2258 const u8 *ieprb, *iebcn;
2259
2260 wpabuf_free(wpa_s->received_mb_ies);
2261 wpa_s->received_mb_ies = NULL;
2262
2263 if (ai &&
2264 !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
2265 wpa_printf(MSG_DEBUG,
2266 "FST: MB IEs updated from Association Response frame");
2267 return;
2268 }
2269
2270 if (ai &&
2271 !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
2272 wpa_printf(MSG_DEBUG,
2273 "FST: MB IEs updated from association event Beacon IEs");
2274 return;
2275 }
2276
2277 if (!bss)
2278 return;
2279
2280 ieprb = (const u8 *) (bss + 1);
2281 iebcn = ieprb + bss->ie_len;
2282
2283 if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
2284 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
2285 else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
2286 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
2287#endif /* CONFIG_FST */
2288}
2289
2290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2292 union wpa_event_data *data)
2293{
2294 u8 bssid[ETH_ALEN];
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002295 int ft_completed, already_authorized;
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002296 int new_bss = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297
2298#ifdef CONFIG_AP
2299 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002300 if (!data)
2301 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2303 data->assoc_info.addr,
2304 data->assoc_info.req_ies,
2305 data->assoc_info.req_ies_len,
2306 data->assoc_info.reassoc);
2307 return;
2308 }
2309#endif /* CONFIG_AP */
2310
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002311 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2314 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2315 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002316 /*
2317 * FILS authentication can share the same mechanism to mark the
2318 * connection fully authenticated, so set ft_completed also based on
2319 * FILS result.
2320 */
2321 if (!ft_completed)
2322 ft_completed = wpa_fils_is_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002323
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002324 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2325 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002326 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002327 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2328 return;
2329 }
2330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002332 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2334 MACSTR, MAC2STR(bssid));
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002335 new_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2338 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002339 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340
2341 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2342 wpa_clear_keys(wpa_s, bssid);
2343 }
2344 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002345 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2347 return;
2348 }
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002349 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002350
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002351 if (wpa_s->conf->ap_scan == 1 &&
2352 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002353 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2354 wpa_msg(wpa_s, MSG_WARNING,
2355 "WPA/RSN IEs not updated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 }
2357
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002358 wpas_fst_update_mb_assoc(wpa_s, data);
2359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360#ifdef CONFIG_SME
2361 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2362 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002363 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364#endif /* CONFIG_SME */
2365
2366 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2367 if (wpa_s->current_ssid) {
2368 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2369 * initialized before association, but for other modes,
2370 * initialize PC/SC here, if the current configuration needs
2371 * smartcard or SIM/USIM. */
2372 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2373 }
2374 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2375 if (wpa_s->l2)
2376 l2_packet_notify_auth_start(wpa_s->l2);
2377
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002378 already_authorized = data && data->assoc_info.authorized;
2379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 /*
2381 * Set portEnabled first to FALSE in order to get EAP state machine out
2382 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2383 * state machine may transit to AUTHENTICATING state based on obsolete
2384 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2385 * AUTHENTICATED without ever giving chance to EAP state machine to
2386 * reset the state.
2387 */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002388 if (!ft_completed && !already_authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2390 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2391 }
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002392 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed ||
2393 already_authorized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2395 /* 802.1X::portControl = Auto */
2396 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2397 wpa_s->eapol_received = 0;
2398 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2399 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2400 (wpa_s->current_ssid &&
2401 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002402 if (wpa_s->current_ssid &&
2403 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002404 (wpa_s->drv_flags &
2405 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2406 /*
2407 * Set the key after having received joined-IBSS event
2408 * from the driver.
2409 */
2410 wpa_supplicant_set_wpa_none_key(wpa_s,
2411 wpa_s->current_ssid);
2412 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 wpa_supplicant_cancel_auth_timeout(wpa_s);
2414 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2415 } else if (!ft_completed) {
2416 /* Timeout for receiving the first EAPOL packet */
2417 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2418 }
2419 wpa_supplicant_cancel_scan(wpa_s);
2420
2421 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2422 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2423 /*
2424 * We are done; the driver will take care of RSN 4-way
2425 * handshake.
2426 */
2427 wpa_supplicant_cancel_auth_timeout(wpa_s);
2428 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2429 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2430 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2431 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2432 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2433 /*
2434 * The driver will take care of RSN 4-way handshake, so we need
2435 * to allow EAPOL supplicant to complete its work without
2436 * waiting for WPA supplicant.
2437 */
2438 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2439 } else if (ft_completed) {
2440 /*
2441 * FT protocol completed - make sure EAPOL state machine ends
2442 * up in authenticated.
2443 */
2444 wpa_supplicant_cancel_auth_timeout(wpa_s);
2445 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2446 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2447 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2448 }
2449
Jouni Malinena05074c2012-12-21 21:35:35 +02002450 wpa_s->last_eapol_matches_bssid = 0;
2451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002453 struct os_reltime now, age;
2454 os_get_reltime(&now);
2455 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 if (age.sec == 0 && age.usec < 100000 &&
2457 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2458 0) {
2459 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2460 "frame that was received just before "
2461 "association notification");
2462 wpa_supplicant_rx_eapol(
2463 wpa_s, wpa_s->pending_eapol_rx_src,
2464 wpabuf_head(wpa_s->pending_eapol_rx),
2465 wpabuf_len(wpa_s->pending_eapol_rx));
2466 }
2467 wpabuf_free(wpa_s->pending_eapol_rx);
2468 wpa_s->pending_eapol_rx = NULL;
2469 }
2470
2471 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2472 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002473 wpa_s->current_ssid &&
2474 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 /* Set static WEP keys again */
2476 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2477 }
2478
2479#ifdef CONFIG_IBSS_RSN
2480 if (wpa_s->current_ssid &&
2481 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2482 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2483 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2484 wpa_s->ibss_rsn == NULL) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002485 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 if (!wpa_s->ibss_rsn) {
2487 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2488 wpa_supplicant_deauthenticate(
2489 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2490 return;
2491 }
2492
2493 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2494 }
2495#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002496
2497 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002498
2499 if (data) {
2500 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2501 data->assoc_info.resp_ies_len,
2502 &data->assoc_info.wmm_params);
2503
2504 if (wpa_s->reassoc_same_bss)
2505 wmm_ac_restore_tspecs(wpa_s);
2506 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507}
2508
2509
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002510static int disconnect_reason_recoverable(u16 reason_code)
2511{
2512 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2513 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2514 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2515}
2516
2517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002519 u16 reason_code,
2520 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521{
2522 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002523
2524 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2525 /*
2526 * At least Host AP driver and a Prism3 card seemed to be
2527 * generating streams of disconnected events when configuring
2528 * IBSS for WPA-None. Ignore them for now.
2529 */
2530 return;
2531 }
2532
2533 bssid = wpa_s->bssid;
2534 if (is_zero_ether_addr(bssid))
2535 bssid = wpa_s->pending_bssid;
2536
2537 if (!is_zero_ether_addr(bssid) ||
2538 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2539 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2540 " reason=%d%s",
2541 MAC2STR(bssid), reason_code,
2542 locally_generated ? " locally_generated=1" : "");
2543 }
2544}
2545
2546
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002547static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2548 int locally_generated)
2549{
2550 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2551 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2552 return 0; /* Not in 4-way handshake with PSK */
2553
2554 /*
2555 * It looks like connection was lost while trying to go through PSK
2556 * 4-way handshake. Filter out known disconnection cases that are caused
2557 * by something else than PSK mismatch to avoid confusing reports.
2558 */
2559
2560 if (locally_generated) {
2561 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2562 return 0;
2563 }
2564
2565 return 1;
2566}
2567
2568
Jouni Malinen2b89da82012-08-31 22:04:41 +03002569static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2570 u16 reason_code,
2571 int locally_generated)
2572{
2573 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 int authenticating;
2575 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002576 struct wpa_bss *fast_reconnect = NULL;
2577 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002578 struct wpa_ssid *last_ssid;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002579 struct wpa_bss *curr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580
2581 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2582 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2583
2584 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2585 /*
2586 * At least Host AP driver and a Prism3 card seemed to be
2587 * generating streams of disconnected events when configuring
2588 * IBSS for WPA-None. Ignore them for now.
2589 */
2590 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2591 "IBSS/WPA-None mode");
2592 return;
2593 }
2594
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002595 if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
2596 reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
2597 locally_generated)
2598 /*
2599 * Remove the inactive AP (which is probably out of range) from
2600 * the BSS list after marking disassociation. In particular
2601 * mac80211-based drivers use the
2602 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
2603 * locally generated disconnection events for cases where the
2604 * AP does not reply anymore.
2605 */
2606 curr = wpa_s->current_bss;
2607
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002608 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2610 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002611 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2612 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002613 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002615 if (!wpa_s->disconnected &&
2616 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002617 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002618 wpas_wps_searching(wpa_s) ||
2619 wpas_wps_reenable_networks_pending(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002620 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002621 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002622 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002623 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002624 wpa_s->wpa_state);
2625 if (wpa_s->wpa_state == WPA_COMPLETED &&
2626 wpa_s->current_ssid &&
2627 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002628 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002629 disconnect_reason_recoverable(reason_code)) {
2630 /*
2631 * It looks like the AP has dropped association with
2632 * us, but could allow us to get back in. Try to
2633 * reconnect to the same BSS without full scan to save
2634 * time for some common cases.
2635 */
2636 fast_reconnect = wpa_s->current_bss;
2637 fast_reconnect_ssid = wpa_s->current_ssid;
2638 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002639 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640 else
2641 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2642 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002644 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 "try to re-connect");
2646 wpa_s->reassociate = 0;
2647 wpa_s->disconnected = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002648 if (!wpa_s->pno)
2649 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650 }
2651 bssid = wpa_s->bssid;
2652 if (is_zero_ether_addr(bssid))
2653 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002654 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2655 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002657 if (locally_generated)
2658 wpa_s->disconnect_reason = -reason_code;
2659 else
2660 wpa_s->disconnect_reason = reason_code;
2661 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2663 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 wpa_clear_keys(wpa_s, wpa_s->bssid);
2665 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002666 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002667 wpa_supplicant_mark_disassoc(wpa_s);
2668
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002669 if (curr)
2670 wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
2671
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002672 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002674 wpa_s->current_ssid = last_ssid;
2675 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002676
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002677 if (fast_reconnect &&
2678 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2679 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2680 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2681 fast_reconnect->ssid_len) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002682 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
2683 !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect->bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002684#ifndef CONFIG_NO_SCAN_PROCESSING
2685 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2686 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2687 fast_reconnect_ssid) < 0) {
2688 /* Recover through full scan */
2689 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2690 }
2691#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002692 } else if (fast_reconnect) {
2693 /*
2694 * Could not reconnect to the same BSS due to network being
2695 * disabled. Use a new scan to match the alternative behavior
2696 * above, i.e., to continue automatic reconnection attempt in a
2697 * way that enforces disabled network rules.
2698 */
2699 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701}
2702
2703
2704#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002705void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706{
2707 struct wpa_supplicant *wpa_s = eloop_ctx;
2708
2709 if (!wpa_s->pending_mic_error_report)
2710 return;
2711
2712 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2713 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2714 wpa_s->pending_mic_error_report = 0;
2715}
2716#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2717
2718
2719static void
2720wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2721 union wpa_event_data *data)
2722{
2723 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002724 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725
2726 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2727 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002728 os_get_reltime(&t);
2729 if ((wpa_s->last_michael_mic_error.sec &&
2730 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 wpa_s->pending_mic_error_report) {
2732 if (wpa_s->pending_mic_error_report) {
2733 /*
2734 * Send the pending MIC error report immediately since
2735 * we are going to start countermeasures and AP better
2736 * do the same.
2737 */
2738 wpa_sm_key_request(wpa_s->wpa, 1,
2739 wpa_s->pending_mic_error_pairwise);
2740 }
2741
2742 /* Send the new MIC error report immediately since we are going
2743 * to start countermeasures and AP better do the same.
2744 */
2745 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2746
2747 /* initialize countermeasures */
2748 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002749
2750 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2753
2754 /*
2755 * Need to wait for completion of request frame. We do not get
2756 * any callback for the message completion, so just wait a
2757 * short while and hope for the best. */
2758 os_sleep(0, 10000);
2759
2760 wpa_drv_set_countermeasures(wpa_s, 1);
2761 wpa_supplicant_deauthenticate(wpa_s,
2762 WLAN_REASON_MICHAEL_MIC_FAILURE);
2763 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2764 wpa_s, NULL);
2765 eloop_register_timeout(60, 0,
2766 wpa_supplicant_stop_countermeasures,
2767 wpa_s, NULL);
2768 /* TODO: mark the AP rejected for 60 second. STA is
2769 * allowed to associate with another AP.. */
2770 } else {
2771#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2772 if (wpa_s->mic_errors_seen) {
2773 /*
2774 * Reduce the effectiveness of Michael MIC error
2775 * reports as a means for attacking against TKIP if
2776 * more than one MIC failure is noticed with the same
2777 * PTK. We delay the transmission of the reports by a
2778 * random time between 0 and 60 seconds in order to
2779 * force the attacker wait 60 seconds before getting
2780 * the information on whether a frame resulted in a MIC
2781 * failure.
2782 */
2783 u8 rval[4];
2784 int sec;
2785
2786 if (os_get_random(rval, sizeof(rval)) < 0)
2787 sec = os_random() % 60;
2788 else
2789 sec = WPA_GET_BE32(rval) % 60;
2790 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2791 "report %d seconds", sec);
2792 wpa_s->pending_mic_error_report = 1;
2793 wpa_s->pending_mic_error_pairwise = pairwise;
2794 eloop_cancel_timeout(
2795 wpa_supplicant_delayed_mic_error_report,
2796 wpa_s, NULL);
2797 eloop_register_timeout(
2798 sec, os_random() % 1000000,
2799 wpa_supplicant_delayed_mic_error_report,
2800 wpa_s, NULL);
2801 } else {
2802 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2803 }
2804#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2805 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2806#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2807 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002808 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 wpa_s->mic_errors_seen++;
2810}
2811
2812
2813#ifdef CONFIG_TERMINATE_ONLASTIF
2814static int any_interfaces(struct wpa_supplicant *head)
2815{
2816 struct wpa_supplicant *wpa_s;
2817
2818 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2819 if (!wpa_s->interface_removed)
2820 return 1;
2821 return 0;
2822}
2823#endif /* CONFIG_TERMINATE_ONLASTIF */
2824
2825
2826static void
2827wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2828 union wpa_event_data *data)
2829{
2830 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2831 return;
2832
2833 switch (data->interface_status.ievent) {
2834 case EVENT_INTERFACE_ADDED:
2835 if (!wpa_s->interface_removed)
2836 break;
2837 wpa_s->interface_removed = 0;
2838 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2839 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2840 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2841 "driver after interface was added");
2842 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002843
2844#ifdef CONFIG_P2P
2845 if (!wpa_s->global->p2p &&
2846 !wpa_s->global->p2p_disabled &&
2847 !wpa_s->conf->p2p_disabled &&
2848 (wpa_s->drv_flags &
2849 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2850 wpas_p2p_add_p2pdev_interface(
2851 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2852 wpa_printf(MSG_INFO,
2853 "P2P: Failed to enable P2P Device interface");
2854 /* Try to continue without. P2P will be disabled. */
2855 }
2856#endif /* CONFIG_P2P */
2857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 break;
2859 case EVENT_INTERFACE_REMOVED:
2860 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2861 wpa_s->interface_removed = 1;
2862 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002863 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 l2_packet_deinit(wpa_s->l2);
2865 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002866
2867#ifdef CONFIG_P2P
2868 if (wpa_s->global->p2p &&
2869 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2870 (wpa_s->drv_flags &
2871 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2872 wpa_dbg(wpa_s, MSG_DEBUG,
2873 "Removing P2P Device interface");
2874 wpa_supplicant_remove_iface(
2875 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2876 0);
2877 wpa_s->global->p2p_init_wpa_s = NULL;
2878 }
2879#endif /* CONFIG_P2P */
2880
Dmitry Shmidte4663042016-04-04 10:07:49 -07002881#ifdef CONFIG_MATCH_IFACE
2882 if (wpa_s->matched) {
2883 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
2884 break;
2885 }
2886#endif /* CONFIG_MATCH_IFACE */
2887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888#ifdef CONFIG_TERMINATE_ONLASTIF
2889 /* check if last interface */
2890 if (!any_interfaces(wpa_s->global->ifaces))
2891 eloop_terminate();
2892#endif /* CONFIG_TERMINATE_ONLASTIF */
2893 break;
2894 }
2895}
2896
2897
2898#ifdef CONFIG_PEERKEY
2899static void
2900wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2901 union wpa_event_data *data)
2902{
2903 if (data == NULL)
2904 return;
2905 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2906}
2907#endif /* CONFIG_PEERKEY */
2908
2909
2910#ifdef CONFIG_TDLS
2911static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2912 union wpa_event_data *data)
2913{
2914 if (data == NULL)
2915 return;
2916 switch (data->tdls.oper) {
2917 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002918 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2919 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2920 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2921 else
2922 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923 break;
2924 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002925 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2926 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2927 data->tdls.reason_code);
2928 else
2929 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2930 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002932 case TDLS_REQUEST_DISCOVER:
2933 wpa_tdls_send_discovery_request(wpa_s->wpa,
2934 data->tdls.peer);
2935 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 }
2937}
2938#endif /* CONFIG_TDLS */
2939
2940
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002941#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002942static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2943 union wpa_event_data *data)
2944{
2945 if (data == NULL)
2946 return;
2947 switch (data->wnm.oper) {
2948 case WNM_OPER_SLEEP:
2949 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2950 "(action=%d, intval=%d)",
2951 data->wnm.sleep_action, data->wnm.sleep_intval);
2952 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002953 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002954 break;
2955 }
2956}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002957#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960#ifdef CONFIG_IEEE80211R
2961static void
2962wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2963 union wpa_event_data *data)
2964{
2965 if (data == NULL)
2966 return;
2967
2968 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2969 data->ft_ies.ies_len,
2970 data->ft_ies.ft_action,
2971 data->ft_ies.target_ap,
2972 data->ft_ies.ric_ies,
2973 data->ft_ies.ric_ies_len) < 0) {
2974 /* TODO: prevent MLME/driver from trying to associate? */
2975 }
2976}
2977#endif /* CONFIG_IEEE80211R */
2978
2979
2980#ifdef CONFIG_IBSS_RSN
2981static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2982 union wpa_event_data *data)
2983{
2984 struct wpa_ssid *ssid;
2985 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2986 return;
2987 if (data == NULL)
2988 return;
2989 ssid = wpa_s->current_ssid;
2990 if (ssid == NULL)
2991 return;
2992 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2993 return;
2994
2995 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2996}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002997
2998
2999static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
3000 union wpa_event_data *data)
3001{
3002 struct wpa_ssid *ssid = wpa_s->current_ssid;
3003
3004 if (ssid == NULL)
3005 return;
3006
3007 /* check if the ssid is correctly configured as IBSS/RSN */
3008 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3009 return;
3010
3011 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
3012 data->rx_mgmt.frame_len);
3013}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014#endif /* CONFIG_IBSS_RSN */
3015
3016
3017#ifdef CONFIG_IEEE80211R
3018static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
3019 size_t len)
3020{
3021 const u8 *sta_addr, *target_ap_addr;
3022 u16 status;
3023
3024 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
3025 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
3026 return; /* only SME case supported for now */
3027 if (len < 1 + 2 * ETH_ALEN + 2)
3028 return;
3029 if (data[0] != 2)
3030 return; /* Only FT Action Response is supported for now */
3031 sta_addr = data + 1;
3032 target_ap_addr = data + 1 + ETH_ALEN;
3033 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
3034 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
3035 MACSTR " TargetAP " MACSTR " status %u",
3036 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
3037
3038 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
3039 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
3040 " in FT Action Response", MAC2STR(sta_addr));
3041 return;
3042 }
3043
3044 if (status) {
3045 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
3046 "failure (status code %d)", status);
3047 /* TODO: report error to FT code(?) */
3048 return;
3049 }
3050
3051 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
3052 len - (1 + 2 * ETH_ALEN + 2), 1,
3053 target_ap_addr, NULL, 0) < 0)
3054 return;
3055
3056#ifdef CONFIG_SME
3057 {
3058 struct wpa_bss *bss;
3059 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
3060 if (bss)
3061 wpa_s->sme.freq = bss->freq;
3062 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
3063 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
3064 WLAN_AUTH_FT);
3065 }
3066#endif /* CONFIG_SME */
3067}
3068#endif /* CONFIG_IEEE80211R */
3069
3070
3071static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
3072 struct unprot_deauth *e)
3073{
3074#ifdef CONFIG_IEEE80211W
3075 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
3076 "dropped: " MACSTR " -> " MACSTR
3077 " (reason code %u)",
3078 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3079 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3080#endif /* CONFIG_IEEE80211W */
3081}
3082
3083
3084static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
3085 struct unprot_disassoc *e)
3086{
3087#ifdef CONFIG_IEEE80211W
3088 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
3089 "dropped: " MACSTR " -> " MACSTR
3090 " (reason code %u)",
3091 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3092 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3093#endif /* CONFIG_IEEE80211W */
3094}
3095
3096
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003097static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
3098 u16 reason_code, int locally_generated,
3099 const u8 *ie, size_t ie_len, int deauth)
3100{
3101#ifdef CONFIG_AP
3102 if (wpa_s->ap_iface && addr) {
3103 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
3104 return;
3105 }
3106
3107 if (wpa_s->ap_iface) {
3108 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
3109 return;
3110 }
3111#endif /* CONFIG_AP */
3112
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003113 if (!locally_generated)
3114 wpa_s->own_disconnect_req = 0;
3115
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003116 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
3117
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003118 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
3119 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
3120 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
3121 eapol_sm_failed(wpa_s->eapol))) &&
3122 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003123 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003124
3125#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003126 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003127 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
3128 locally_generated) > 0) {
3129 /*
3130 * The interface was removed, so cannot continue
3131 * processing any additional operations after this.
3132 */
3133 return;
3134 }
3135 }
3136#endif /* CONFIG_P2P */
3137
3138 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
3139 locally_generated);
3140}
3141
3142
3143static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
3144 struct disassoc_info *info)
3145{
3146 u16 reason_code = 0;
3147 int locally_generated = 0;
3148 const u8 *addr = NULL;
3149 const u8 *ie = NULL;
3150 size_t ie_len = 0;
3151
3152 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
3153
3154 if (info) {
3155 addr = info->addr;
3156 ie = info->ie;
3157 ie_len = info->ie_len;
3158 reason_code = info->reason_code;
3159 locally_generated = info->locally_generated;
3160 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
3161 locally_generated ? " (locally generated)" : "");
3162 if (addr)
3163 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3164 MAC2STR(addr));
3165 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
3166 ie, ie_len);
3167 }
3168
3169#ifdef CONFIG_AP
3170 if (wpa_s->ap_iface && info && info->addr) {
3171 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
3172 return;
3173 }
3174
3175 if (wpa_s->ap_iface) {
3176 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
3177 return;
3178 }
3179#endif /* CONFIG_AP */
3180
3181#ifdef CONFIG_P2P
3182 if (info) {
3183 wpas_p2p_disassoc_notif(
3184 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
3185 locally_generated);
3186 }
3187#endif /* CONFIG_P2P */
3188
3189 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3190 sme_event_disassoc(wpa_s, info);
3191
3192 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
3193 ie, ie_len, 0);
3194}
3195
3196
3197static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
3198 struct deauth_info *info)
3199{
3200 u16 reason_code = 0;
3201 int locally_generated = 0;
3202 const u8 *addr = NULL;
3203 const u8 *ie = NULL;
3204 size_t ie_len = 0;
3205
3206 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
3207
3208 if (info) {
3209 addr = info->addr;
3210 ie = info->ie;
3211 ie_len = info->ie_len;
3212 reason_code = info->reason_code;
3213 locally_generated = info->locally_generated;
3214 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
3215 reason_code,
3216 locally_generated ? " (locally generated)" : "");
3217 if (addr) {
3218 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3219 MAC2STR(addr));
3220 }
3221 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
3222 ie, ie_len);
3223 }
3224
3225 wpa_reset_ft_completed(wpa_s->wpa);
3226
3227 wpas_event_disconnect(wpa_s, addr, reason_code,
3228 locally_generated, ie, ie_len, 1);
3229}
3230
3231
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003232static const char * reg_init_str(enum reg_change_initiator init)
3233{
3234 switch (init) {
3235 case REGDOM_SET_BY_CORE:
3236 return "CORE";
3237 case REGDOM_SET_BY_USER:
3238 return "USER";
3239 case REGDOM_SET_BY_DRIVER:
3240 return "DRIVER";
3241 case REGDOM_SET_BY_COUNTRY_IE:
3242 return "COUNTRY_IE";
3243 case REGDOM_BEACON_HINT:
3244 return "BEACON_HINT";
3245 }
3246 return "?";
3247}
3248
3249
3250static const char * reg_type_str(enum reg_type type)
3251{
3252 switch (type) {
3253 case REGDOM_TYPE_UNKNOWN:
3254 return "UNKNOWN";
3255 case REGDOM_TYPE_COUNTRY:
3256 return "COUNTRY";
3257 case REGDOM_TYPE_WORLD:
3258 return "WORLD";
3259 case REGDOM_TYPE_CUSTOM_WORLD:
3260 return "CUSTOM_WORLD";
3261 case REGDOM_TYPE_INTERSECTION:
3262 return "INTERSECTION";
3263 }
3264 return "?";
3265}
3266
3267
3268static void wpa_supplicant_update_channel_list(
3269 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003270{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003271 struct wpa_supplicant *ifs;
3272
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003273 /*
3274 * To allow backwards compatibility with higher level layers that
3275 * assumed the REGDOM_CHANGE event is sent over the initially added
3276 * interface. Find the highest parent of this interface and use it to
3277 * send the event.
3278 */
3279 for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
3280 ;
3281
3282 wpa_msg(ifs, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003283 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003284 info->alpha2[0] ? " alpha2=" : "",
3285 info->alpha2[0] ? info->alpha2 : "");
3286
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003287 if (wpa_s->drv_priv == NULL)
3288 return; /* Ignore event during drv initialization */
3289
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003290 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3291 radio_list) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003292 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3293 ifs->ifname);
3294 free_hw_features(ifs);
3295 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3296 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003297
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003298 /* Restart PNO/sched_scan with updated channel list */
3299 if (ifs->pno) {
3300 wpas_stop_pno(ifs);
3301 wpas_start_pno(ifs);
3302 } else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
3303 wpa_dbg(ifs, MSG_DEBUG,
3304 "Channel list changed - restart sched_scan");
3305 wpas_scan_restart_sched_scan(ifs);
3306 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003307 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003308
3309 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003310}
3311
3312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003313static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003314 const u8 *frame, size_t len, int freq,
3315 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003316{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003317 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003318 const u8 *payload;
3319 size_t plen;
3320 u8 category;
3321
3322 if (len < IEEE80211_HDRLEN + 2)
3323 return;
3324
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003325 mgmt = (const struct ieee80211_mgmt *) frame;
3326 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003327 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003328 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003329
3330 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3331 " Category=%u DataLen=%d freq=%d MHz",
3332 MAC2STR(mgmt->sa), category, (int) plen, freq);
3333
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003334 if (category == WLAN_ACTION_WMM) {
3335 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3336 return;
3337 }
3338
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003339#ifdef CONFIG_IEEE80211R
3340 if (category == WLAN_ACTION_FT) {
3341 ft_rx_action(wpa_s, payload, plen);
3342 return;
3343 }
3344#endif /* CONFIG_IEEE80211R */
3345
3346#ifdef CONFIG_IEEE80211W
3347#ifdef CONFIG_SME
3348 if (category == WLAN_ACTION_SA_QUERY) {
3349 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3350 return;
3351 }
3352#endif /* CONFIG_SME */
3353#endif /* CONFIG_IEEE80211W */
3354
3355#ifdef CONFIG_WNM
3356 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3357 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3358 return;
3359 }
3360#endif /* CONFIG_WNM */
3361
3362#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003363 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3364 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003365 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003366 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003367 payload, plen, freq) == 0)
3368 return;
3369#endif /* CONFIG_GAS */
3370
3371#ifdef CONFIG_TDLS
3372 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3373 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3374 wpa_dbg(wpa_s, MSG_DEBUG,
3375 "TDLS: Received Discovery Response from " MACSTR,
3376 MAC2STR(mgmt->sa));
3377 return;
3378 }
3379#endif /* CONFIG_TDLS */
3380
3381#ifdef CONFIG_INTERWORKING
3382 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3383 payload[0] == QOS_QOS_MAP_CONFIG) {
3384 const u8 *pos = payload + 1;
3385 size_t qlen = plen - 1;
3386 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3387 MACSTR, MAC2STR(mgmt->sa));
3388 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3389 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3390 pos[1] <= qlen - 2 && pos[1] >= 16)
3391 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3392 return;
3393 }
3394#endif /* CONFIG_INTERWORKING */
3395
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003396 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003397 payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
3398 wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
3399 payload + 1,
3400 plen - 1);
3401 return;
3402 }
3403
3404 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003405 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3406 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3407 return;
3408 }
3409
3410 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3411 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3412 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3413 payload + 1, plen - 1,
3414 rssi);
3415 return;
3416 }
3417
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003418#ifdef CONFIG_FST
3419 if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3420 fst_rx_action(wpa_s->fst, mgmt, len);
3421 return;
3422 }
3423#endif /* CONFIG_FST */
3424
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003425 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3426 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003427 if (wpa_s->ifmsh)
3428 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003429}
3430
3431
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003432static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3433 union wpa_event_data *event)
3434{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003435 struct wpa_freq_range_list *list;
3436 char *str = NULL;
3437
3438 list = &event->freq_range;
3439
3440 if (list->num)
3441 str = freq_range_list_str(list);
3442 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3443 str ? str : "");
3444
3445#ifdef CONFIG_P2P
3446 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3447 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3448 __func__);
3449 } else {
3450 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003451
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003452 /*
3453 * The update channel flow will also take care of moving a GO
3454 * from the unsafe frequency if needed.
3455 */
3456 wpas_p2p_update_channel_list(wpa_s,
3457 WPAS_P2P_CHANNEL_UPDATE_AVOID);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003458 }
3459#endif /* CONFIG_P2P */
3460
3461 os_free(str);
3462}
3463
3464
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003465static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3466 union wpa_event_data *data)
3467{
3468 wpa_dbg(wpa_s, MSG_DEBUG,
3469 "Connection authorized by device, previous state %d",
3470 wpa_s->wpa_state);
3471 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3472 wpa_supplicant_cancel_auth_timeout(wpa_s);
3473 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3474 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3475 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3476 }
3477 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3478 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003479 data->assoc_info.ptk_kck_len,
3480 data->assoc_info.ptk_kek,
3481 data->assoc_info.ptk_kek_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003482}
3483
3484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003485void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3486 union wpa_event_data *data)
3487{
3488 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003489 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490
3491 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3492 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003493 event != EVENT_INTERFACE_STATUS &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003494 event != EVENT_SCAN_RESULTS &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003495 event != EVENT_SCHED_SCAN_STOPPED) {
3496 wpa_dbg(wpa_s, MSG_DEBUG,
3497 "Ignore event %s (%d) while interface is disabled",
3498 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003499 return;
3500 }
3501
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003502#ifndef CONFIG_NO_STDOUT_DEBUG
3503{
3504 int level = MSG_DEBUG;
3505
Dmitry Shmidt04949592012-07-19 12:16:46 -07003506 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003507 const struct ieee80211_hdr *hdr;
3508 u16 fc;
3509 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3510 fc = le_to_host16(hdr->frame_control);
3511 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3512 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3513 level = MSG_EXCESSIVE;
3514 }
3515
3516 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3517 event_to_string(event), event);
3518}
3519#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520
3521 switch (event) {
3522 case EVENT_AUTH:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003523#ifdef CONFIG_FST
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003524 if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
3525 data->auth.ies_len))
3526 wpa_printf(MSG_DEBUG,
3527 "FST: MB IEs updated from auth IE");
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003528#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 sme_event_auth(wpa_s, data);
3530 break;
3531 case EVENT_ASSOC:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003532#ifdef CONFIG_TESTING_OPTIONS
3533 if (wpa_s->ignore_auth_resp) {
3534 wpa_printf(MSG_INFO,
3535 "EVENT_ASSOC - ignore_auth_resp active!");
3536 break;
3537 }
3538#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003540 if (data && data->assoc_info.authorized)
3541 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003542 if (data) {
3543 wpa_msg(wpa_s, MSG_INFO,
3544 WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
3545 data->assoc_info.subnet_status);
3546 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 break;
3548 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003549 wpas_event_disassoc(wpa_s,
3550 data ? &data->disassoc_info : NULL);
3551 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 case EVENT_DEAUTH:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003553#ifdef CONFIG_TESTING_OPTIONS
3554 if (wpa_s->ignore_auth_resp) {
3555 wpa_printf(MSG_INFO,
3556 "EVENT_DEAUTH - ignore_auth_resp active!");
3557 break;
3558 }
3559#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003560 wpas_event_deauth(wpa_s,
3561 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 break;
3563 case EVENT_MICHAEL_MIC_FAILURE:
3564 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3565 break;
3566#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003567 case EVENT_SCAN_STARTED:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003568 if (wpa_s->own_scan_requested ||
3569 (data && !data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003570 struct os_reltime diff;
3571
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003572 os_get_reltime(&wpa_s->scan_start_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003573 os_reltime_sub(&wpa_s->scan_start_time,
3574 &wpa_s->scan_trigger_time, &diff);
3575 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3576 diff.sec, diff.usec);
3577 wpa_s->own_scan_requested = 0;
3578 wpa_s->own_scan_running = 1;
3579 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3580 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003581 wpa_msg_ctrl(wpa_s, MSG_INFO,
3582 WPA_EVENT_SCAN_STARTED "id=%u",
3583 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003584 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003585 wpa_msg_ctrl(wpa_s, MSG_INFO,
3586 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003587 }
3588 } else {
3589 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003590 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003591 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003592 }
3593 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594 case EVENT_SCAN_RESULTS:
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003595 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3596 wpa_s->scan_res_handler = NULL;
3597 wpa_s->own_scan_running = 0;
3598 wpa_s->radio->external_scan_running = 0;
3599 wpa_s->last_scan_req = NORMAL_SCAN_REQ;
3600 break;
3601 }
3602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003603 if (!(data && data->scan_info.external_scan) &&
3604 os_reltime_initialized(&wpa_s->scan_start_time)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003605 struct os_reltime now, diff;
3606 os_get_reltime(&now);
3607 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3608 wpa_s->scan_start_time.sec = 0;
3609 wpa_s->scan_start_time.usec = 0;
3610 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3611 diff.sec, diff.usec);
3612 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003613 if (wpa_supplicant_event_scan_results(wpa_s, data))
3614 break; /* interface may have been removed */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003615 if (!(data && data->scan_info.external_scan))
3616 wpa_s->own_scan_running = 0;
3617 if (data && data->scan_info.nl_scan_event)
3618 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003619 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 break;
3621#endif /* CONFIG_NO_SCAN_PROCESSING */
3622 case EVENT_ASSOCINFO:
3623 wpa_supplicant_event_associnfo(wpa_s, data);
3624 break;
3625 case EVENT_INTERFACE_STATUS:
3626 wpa_supplicant_event_interface_status(wpa_s, data);
3627 break;
3628 case EVENT_PMKID_CANDIDATE:
3629 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3630 break;
3631#ifdef CONFIG_PEERKEY
3632 case EVENT_STKSTART:
3633 wpa_supplicant_event_stkstart(wpa_s, data);
3634 break;
3635#endif /* CONFIG_PEERKEY */
3636#ifdef CONFIG_TDLS
3637 case EVENT_TDLS:
3638 wpa_supplicant_event_tdls(wpa_s, data);
3639 break;
3640#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003641#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003642 case EVENT_WNM:
3643 wpa_supplicant_event_wnm(wpa_s, data);
3644 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003645#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003646#ifdef CONFIG_IEEE80211R
3647 case EVENT_FT_RESPONSE:
3648 wpa_supplicant_event_ft_response(wpa_s, data);
3649 break;
3650#endif /* CONFIG_IEEE80211R */
3651#ifdef CONFIG_IBSS_RSN
3652 case EVENT_IBSS_RSN_START:
3653 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3654 break;
3655#endif /* CONFIG_IBSS_RSN */
3656 case EVENT_ASSOC_REJECT:
3657 if (data->assoc_reject.bssid)
3658 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003659 "bssid=" MACSTR " status_code=%u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 MAC2STR(data->assoc_reject.bssid),
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003661 data->assoc_reject.status_code,
3662 data->assoc_reject.timed_out ? " timeout" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 else
3664 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003665 "status_code=%u%s",
3666 data->assoc_reject.status_code,
3667 data->assoc_reject.timed_out ? " timeout" : "");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003668 wpa_s->assoc_status_code = data->assoc_reject.status_code;
3669 wpas_notify_assoc_status_code(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3671 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003672 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003673 const u8 *bssid = data->assoc_reject.bssid;
3674 if (bssid == NULL || is_zero_ether_addr(bssid))
3675 bssid = wpa_s->pending_bssid;
3676 wpas_connection_failed(wpa_s, bssid);
3677 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003678 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 break;
3680 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003681 /* It is possible to get this event from earlier connection */
3682 if (wpa_s->current_ssid &&
3683 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3684 wpa_dbg(wpa_s, MSG_DEBUG,
3685 "Ignore AUTH_TIMED_OUT in mesh configuration");
3686 break;
3687 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3689 sme_event_auth_timed_out(wpa_s, data);
3690 break;
3691 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003692 /* It is possible to get this event from earlier connection */
3693 if (wpa_s->current_ssid &&
3694 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3695 wpa_dbg(wpa_s, MSG_DEBUG,
3696 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3697 break;
3698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3700 sme_event_assoc_timed_out(wpa_s, data);
3701 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 case EVENT_TX_STATUS:
3703 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3704 " type=%d stype=%d",
3705 MAC2STR(data->tx_status.dst),
3706 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003707#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003709#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3711 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003712 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003713 wpa_s, data->tx_status.dst,
3714 data->tx_status.data,
3715 data->tx_status.data_len,
3716 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003717 OFFCHANNEL_SEND_ACTION_SUCCESS :
3718 OFFCHANNEL_SEND_ACTION_NO_ACK);
3719#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 break;
3721 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003722#endif /* CONFIG_AP */
3723#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003725 MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 /*
3727 * Catch TX status events for Action frames we sent via group
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003728 * interface in GO mode, or via standalone AP interface.
3729 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
3730 * except when the primary interface is used as a GO interface
3731 * (for drivers which do not have group interface concurrency)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003732 */
3733 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3734 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003735 os_memcmp(wpa_s->p2pdev->pending_action_dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003737 offchannel_send_action_tx_status(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003738 wpa_s->p2pdev, data->tx_status.dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 data->tx_status.data,
3740 data->tx_status.data_len,
3741 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003742 OFFCHANNEL_SEND_ACTION_SUCCESS :
3743 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003744 break;
3745 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003746#endif /* CONFIG_OFFCHANNEL */
3747#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 switch (data->tx_status.type) {
3749 case WLAN_FC_TYPE_MGMT:
3750 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3751 data->tx_status.data_len,
3752 data->tx_status.stype,
3753 data->tx_status.ack);
3754 break;
3755 case WLAN_FC_TYPE_DATA:
3756 ap_tx_status(wpa_s, data->tx_status.dst,
3757 data->tx_status.data,
3758 data->tx_status.data_len,
3759 data->tx_status.ack);
3760 break;
3761 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003762#endif /* CONFIG_AP */
3763 break;
3764#ifdef CONFIG_AP
3765 case EVENT_EAPOL_TX_STATUS:
3766 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3767 data->eapol_tx_status.data,
3768 data->eapol_tx_status.data_len,
3769 data->eapol_tx_status.ack);
3770 break;
3771 case EVENT_DRIVER_CLIENT_POLL_OK:
3772 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 break;
3774 case EVENT_RX_FROM_UNKNOWN:
3775 if (wpa_s->ap_iface == NULL)
3776 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003777 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3778 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003780 case EVENT_CH_SWITCH:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003781 if (!data || !wpa_s->current_ssid)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003782 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003783
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003784 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
3785 "freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
3786 data->ch_switch.freq,
3787 data->ch_switch.ht_enabled,
3788 data->ch_switch.ch_offset,
3789 channel_width_to_string(data->ch_switch.ch_width),
3790 data->ch_switch.cf1,
3791 data->ch_switch.cf2);
3792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003793 wpa_s->assoc_freq = data->ch_switch.freq;
3794 wpa_s->current_ssid->frequency = data->ch_switch.freq;
3795
3796 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
3797 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
3798 wpa_s->current_ssid->mode ==
3799 WPAS_MODE_P2P_GROUP_FORMATION) {
3800 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3801 data->ch_switch.ht_enabled,
3802 data->ch_switch.ch_offset,
3803 data->ch_switch.ch_width,
3804 data->ch_switch.cf1,
3805 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003806 }
3807
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003808 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003809 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003810#ifdef NEED_AP_MLME
3811 case EVENT_DFS_RADAR_DETECTED:
3812 if (data)
3813 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3814 break;
3815 case EVENT_DFS_CAC_STARTED:
3816 if (data)
3817 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3818 break;
3819 case EVENT_DFS_CAC_FINISHED:
3820 if (data)
3821 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3822 break;
3823 case EVENT_DFS_CAC_ABORTED:
3824 if (data)
3825 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3826 break;
3827 case EVENT_DFS_NOP_FINISHED:
3828 if (data)
3829 wpas_event_dfs_cac_nop_finished(wpa_s,
3830 &data->dfs_event);
3831 break;
3832#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003833#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003834 case EVENT_RX_MGMT: {
3835 u16 fc, stype;
3836 const struct ieee80211_mgmt *mgmt;
3837
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003838#ifdef CONFIG_TESTING_OPTIONS
3839 if (wpa_s->ext_mgmt_frame_handling) {
3840 struct rx_mgmt *rx = &data->rx_mgmt;
3841 size_t hex_len = 2 * rx->frame_len + 1;
3842 char *hex = os_malloc(hex_len);
3843 if (hex) {
3844 wpa_snprintf_hex(hex, hex_len,
3845 rx->frame, rx->frame_len);
3846 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3847 rx->freq, rx->datarate, rx->ssi_signal,
3848 hex);
3849 os_free(hex);
3850 }
3851 break;
3852 }
3853#endif /* CONFIG_TESTING_OPTIONS */
3854
Dmitry Shmidt04949592012-07-19 12:16:46 -07003855 mgmt = (const struct ieee80211_mgmt *)
3856 data->rx_mgmt.frame;
3857 fc = le_to_host16(mgmt->frame_control);
3858 stype = WLAN_FC_GET_STYPE(fc);
3859
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003860#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003862#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07003865 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 const u8 *src = mgmt->sa;
Dmitry Shmidte4663042016-04-04 10:07:49 -07003867 const u8 *ie;
3868 size_t ie_len;
3869
3870 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
3871 ie_len = data->rx_mgmt.frame_len -
3872 IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003873 wpas_p2p_probe_req_rx(
3874 wpa_s, src, mgmt->da,
3875 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003876 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003877 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 break;
3879 }
3880#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003881#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003882 if (wpa_s->current_ssid &&
3883 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3884 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003885 data->rx_mgmt.frame_len >= 30) {
3886 wpa_supplicant_event_ibss_auth(wpa_s, data);
3887 break;
3888 }
3889#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003890
3891 if (stype == WLAN_FC_STYPE_ACTION) {
3892 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003893 wpa_s, data->rx_mgmt.frame,
3894 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003895 data->rx_mgmt.freq,
3896 data->rx_mgmt.ssi_signal);
3897 break;
3898 }
3899
3900 if (wpa_s->ifmsh) {
3901 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003902 break;
3903 }
3904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3906 "management frame in non-AP mode");
3907 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003908#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003910
3911 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07003912 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
3913 const u8 *ie;
3914 size_t ie_len;
3915
3916 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
3917 ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003918
3919 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3920 mgmt->bssid, ie, ie_len,
3921 data->rx_mgmt.ssi_signal);
3922 }
3923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003925#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003927 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 case EVENT_RX_PROBE_REQ:
3929 if (data->rx_probe_req.sa == NULL ||
3930 data->rx_probe_req.ie == NULL)
3931 break;
3932#ifdef CONFIG_AP
3933 if (wpa_s->ap_iface) {
3934 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3935 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003936 data->rx_probe_req.da,
3937 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003939 data->rx_probe_req.ie_len,
3940 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 break;
3942 }
3943#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003945 data->rx_probe_req.da,
3946 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003948 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003949 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003950 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003953#ifdef CONFIG_OFFCHANNEL
3954 offchannel_remain_on_channel_cb(
3955 wpa_s, data->remain_on_channel.freq,
3956 data->remain_on_channel.duration);
3957#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 wpas_p2p_remain_on_channel_cb(
3959 wpa_s, data->remain_on_channel.freq,
3960 data->remain_on_channel.duration);
3961 break;
3962 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003963#ifdef CONFIG_OFFCHANNEL
3964 offchannel_cancel_remain_on_channel_cb(
3965 wpa_s, data->remain_on_channel.freq);
3966#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 wpas_p2p_cancel_remain_on_channel_cb(
3968 wpa_s, data->remain_on_channel.freq);
3969 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 case EVENT_EAPOL_RX:
3971 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3972 data->eapol_rx.data,
3973 data->eapol_rx.data_len);
3974 break;
3975 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003976 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3977 "above=%d signal=%d noise=%d txrate=%d",
3978 data->signal_change.above_threshold,
3979 data->signal_change.current_signal,
3980 data->signal_change.current_noise,
3981 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003982 wpa_bss_update_level(wpa_s->current_bss,
3983 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 bgscan_notify_signal_change(
3985 wpa_s, data->signal_change.above_threshold,
3986 data->signal_change.current_signal,
3987 data->signal_change.current_noise,
3988 data->signal_change.current_txrate);
3989 break;
3990 case EVENT_INTERFACE_ENABLED:
3991 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3992 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003993 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003994 if (wpa_s->p2p_mgmt) {
3995 wpa_supplicant_set_state(wpa_s,
3996 WPA_DISCONNECTED);
3997 break;
3998 }
3999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000#ifdef CONFIG_AP
4001 if (!wpa_s->ap_iface) {
4002 wpa_supplicant_set_state(wpa_s,
4003 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004004 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005 wpa_supplicant_req_scan(wpa_s, 0, 0);
4006 } else
4007 wpa_supplicant_set_state(wpa_s,
4008 WPA_COMPLETED);
4009#else /* CONFIG_AP */
4010 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4011 wpa_supplicant_req_scan(wpa_s, 0, 0);
4012#endif /* CONFIG_AP */
4013 }
4014 break;
4015 case EVENT_INTERFACE_DISABLED:
4016 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004017#ifdef CONFIG_P2P
4018 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
4019 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
4020 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
4021 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004022 * Mark interface disabled if this happens to end up not
4023 * being removed as a separate P2P group interface.
4024 */
4025 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4026 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004027 * The interface was externally disabled. Remove
4028 * it assuming an external entity will start a
4029 * new session if needed.
4030 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004031 if (wpa_s->current_ssid &&
4032 wpa_s->current_ssid->p2p_group)
4033 wpas_p2p_interface_unavailable(wpa_s);
4034 else
4035 wpas_p2p_disconnect(wpa_s);
4036 /*
4037 * wpa_s instance may have been freed, so must not use
4038 * it here anymore.
4039 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004040 break;
4041 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004042 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
4043 p2p_in_progress(wpa_s->global->p2p) > 1) {
4044 /* This radio work will be cancelled, so clear P2P
4045 * state as well.
4046 */
4047 p2p_stop_find(wpa_s->global->p2p);
4048 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004049#endif /* CONFIG_P2P */
4050
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004051 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4052 /*
4053 * Indicate disconnection to keep ctrl_iface events
4054 * consistent.
4055 */
4056 wpa_supplicant_event_disassoc(
4057 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
4058 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004060 wpa_bss_flush(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004061 radio_remove_works(wpa_s, NULL, 0);
4062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4064 break;
4065 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004066 wpa_supplicant_update_channel_list(
4067 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 break;
4069 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071 break;
4072 case EVENT_BEST_CHANNEL:
4073 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
4074 "(%d %d %d)",
4075 data->best_chan.freq_24, data->best_chan.freq_5,
4076 data->best_chan.freq_overall);
4077 wpa_s->best_24_freq = data->best_chan.freq_24;
4078 wpa_s->best_5_freq = data->best_chan.freq_5;
4079 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004080 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
4081 data->best_chan.freq_5,
4082 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004083 break;
4084 case EVENT_UNPROT_DEAUTH:
4085 wpa_supplicant_event_unprot_deauth(wpa_s,
4086 &data->unprot_deauth);
4087 break;
4088 case EVENT_UNPROT_DISASSOC:
4089 wpa_supplicant_event_unprot_disassoc(wpa_s,
4090 &data->unprot_disassoc);
4091 break;
4092 case EVENT_STATION_LOW_ACK:
4093#ifdef CONFIG_AP
4094 if (wpa_s->ap_iface && data)
4095 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
4096 data->low_ack.addr);
4097#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004098#ifdef CONFIG_TDLS
4099 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004100 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
4101 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004102#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103 break;
4104 case EVENT_IBSS_PEER_LOST:
4105#ifdef CONFIG_IBSS_RSN
4106 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
4107#endif /* CONFIG_IBSS_RSN */
4108 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004109 case EVENT_DRIVER_GTK_REKEY:
4110 if (os_memcmp(data->driver_gtk_rekey.bssid,
4111 wpa_s->bssid, ETH_ALEN))
4112 break;
4113 if (!wpa_s->wpa)
4114 break;
4115 wpa_sm_update_replay_ctr(wpa_s->wpa,
4116 data->driver_gtk_rekey.replay_ctr);
4117 break;
4118 case EVENT_SCHED_SCAN_STOPPED:
4119 wpa_s->sched_scanning = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004120 resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004121 wpa_supplicant_notify_scanning(wpa_s, 0);
4122
4123 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4124 break;
4125
4126 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004127 * If the driver stopped scanning without being requested to,
4128 * request a new scan to continue scanning for networks.
4129 */
4130 if (!wpa_s->sched_scan_stop_req &&
4131 wpa_s->wpa_state == WPA_SCANNING) {
4132 wpa_dbg(wpa_s, MSG_DEBUG,
4133 "Restart scanning after unexpected sched_scan stop event");
4134 wpa_supplicant_req_scan(wpa_s, 1, 0);
4135 break;
4136 }
4137
4138 wpa_s->sched_scan_stop_req = 0;
4139
4140 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08004141 * Start a new sched scan to continue searching for more SSIDs
4142 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004143 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07004144 if (wpa_s->sched_scan_timed_out) {
4145 wpa_supplicant_req_sched_scan(wpa_s);
4146 } else if (wpa_s->pno_sched_pending) {
4147 wpa_s->pno_sched_pending = 0;
4148 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004149 } else if (resched) {
4150 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08004151 }
4152
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004153 break;
4154 case EVENT_WPS_BUTTON_PUSHED:
4155#ifdef CONFIG_WPS
4156 wpas_wps_start_pbc(wpa_s, NULL, 0);
4157#endif /* CONFIG_WPS */
4158 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004159 case EVENT_AVOID_FREQUENCIES:
4160 wpa_supplicant_notify_avoid_freq(wpa_s, data);
4161 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004162 case EVENT_CONNECT_FAILED_REASON:
4163#ifdef CONFIG_AP
4164 if (!wpa_s->ap_iface || !data)
4165 break;
4166 hostapd_event_connect_failed_reason(
4167 wpa_s->ap_iface->bss[0],
4168 data->connect_failed_reason.addr,
4169 data->connect_failed_reason.code);
4170#endif /* CONFIG_AP */
4171 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004172 case EVENT_NEW_PEER_CANDIDATE:
4173#ifdef CONFIG_MESH
4174 if (!wpa_s->ifmsh || !data)
4175 break;
4176 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
4177 data->mesh_peer.ies,
4178 data->mesh_peer.ie_len);
4179#endif /* CONFIG_MESH */
4180 break;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004181 case EVENT_SURVEY:
4182#ifdef CONFIG_AP
4183 if (!wpa_s->ap_iface)
4184 break;
4185 hostapd_event_get_survey(wpa_s->ap_iface,
4186 &data->survey_results);
4187#endif /* CONFIG_AP */
4188 break;
4189 case EVENT_ACS_CHANNEL_SELECTED:
4190#ifdef CONFIG_ACS
4191 if (!wpa_s->ap_iface)
4192 break;
4193 hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
4194 &data->acs_selected_channels);
4195#endif /* CONFIG_ACS */
4196 break;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004197 case EVENT_P2P_LO_STOP:
4198#ifdef CONFIG_P2P
4199 wpa_s->p2p_lo_started = 0;
4200 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
4201 P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
4202 data->p2p_lo_stop.reason_code);
4203#endif /* CONFIG_P2P */
4204 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205 default:
4206 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
4207 break;
4208 }
4209}
Dmitry Shmidte4663042016-04-04 10:07:49 -07004210
4211
4212void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
4213 union wpa_event_data *data)
4214{
4215 struct wpa_supplicant *wpa_s;
4216
4217 if (event != EVENT_INTERFACE_STATUS)
4218 return;
4219
4220 wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
4221 if (wpa_s && wpa_s->driver->get_ifindex) {
4222 unsigned int ifindex;
4223
4224 ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
4225 if (ifindex != data->interface_status.ifindex) {
4226 wpa_dbg(wpa_s, MSG_DEBUG,
4227 "interface status ifindex %d mismatch (%d)",
4228 ifindex, data->interface_status.ifindex);
4229 return;
4230 }
4231 }
4232#ifdef CONFIG_MATCH_IFACE
4233 else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
4234 struct wpa_interface *wpa_i;
4235
4236 wpa_i = wpa_supplicant_match_iface(
4237 ctx, data->interface_status.ifname);
4238 if (!wpa_i)
4239 return;
4240 wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
4241 os_free(wpa_i);
4242 if (wpa_s)
4243 wpa_s->matched = 1;
4244 }
4245#endif /* CONFIG_MATCH_IFACE */
4246
4247 if (wpa_s)
4248 wpa_supplicant_event(wpa_s, event, data);
4249}