blob: 63cf77373518b2e0905eb5cc5d9937799681fb1b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Paul Stewart092955c2017-02-06 09:13:09 -08003 * Copyright (c) 2003-2017, 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"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070031#include "common/gas_server.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "crypto/random.h"
33#include "blacklist.h"
34#include "wpas_glue.h"
35#include "wps_supplicant.h"
36#include "ibss_rsn.h"
37#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "p2p_supplicant.h"
40#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070041#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "ap.h"
43#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080045#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070046#include "interworking.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080047#include "mesh.h"
48#include "mesh_mpm.h"
49#include "wmm_ac.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070050#include "dpp_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070051
52
Dmitry Shmidt34af3062013-07-11 10:46:32 -070053#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070054static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080055 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070056#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080057
58
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070059int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070060{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080061 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070062
63 if (ssid == NULL || ssid->disabled_until.sec == 0)
64 return 0;
65
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080066 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070067 if (ssid->disabled_until.sec > now.sec)
68 return ssid->disabled_until.sec - now.sec;
69
70 wpas_clear_temp_disabled(wpa_s, ssid, 0);
71
72 return 0;
73}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074
75
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080076#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070077/**
78 * wpas_reenabled_network_time - Time until first network is re-enabled
79 * @wpa_s: Pointer to wpa_supplicant data
80 * Returns: If all enabled networks are temporarily disabled, returns the time
81 * (in sec) until the first network is re-enabled. Otherwise returns 0.
82 *
83 * This function is used in case all enabled networks are temporarily disabled,
84 * in which case it returns the time (in sec) that the first network will be
85 * re-enabled. The function assumes that at least one network is enabled.
86 */
87static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
88{
89 struct wpa_ssid *ssid;
90 int disabled_for, res = 0;
91
92#ifdef CONFIG_INTERWORKING
93 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
94 wpa_s->conf->cred)
95 return 0;
96#endif /* CONFIG_INTERWORKING */
97
98 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
99 if (ssid->disabled)
100 continue;
101
102 disabled_for = wpas_temp_disabled(wpa_s, ssid);
103 if (!disabled_for)
104 return 0;
105
106 if (!res || disabled_for < res)
107 res = disabled_for;
108 }
109
110 return res;
111}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800112#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700113
114
115void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
116{
117 struct wpa_supplicant *wpa_s = eloop_ctx;
118
119 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
120 return;
121
122 wpa_dbg(wpa_s, MSG_DEBUG,
123 "Try to associate due to network getting re-enabled");
124 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
125 wpa_supplicant_cancel_sched_scan(wpa_s);
126 wpa_supplicant_req_scan(wpa_s, 0, 0);
127 }
128}
129
130
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800131static struct wpa_bss * wpa_supplicant_get_new_bss(
132 struct wpa_supplicant *wpa_s, const u8 *bssid)
133{
134 struct wpa_bss *bss = NULL;
135 struct wpa_ssid *ssid = wpa_s->current_ssid;
136
137 if (ssid->ssid_len > 0)
138 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
139 if (!bss)
140 bss = wpa_bss_get_bssid(wpa_s, bssid);
141
142 return bss;
143}
144
145
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700146static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
147{
148 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
149
150 if (!bss) {
151 wpa_supplicant_update_scan_results(wpa_s);
152
153 /* Get the BSS from the new scan results */
154 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
155 }
156
157 if (bss)
158 wpa_s->current_bss = bss;
159}
160
161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
163{
164 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700165 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700166 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700167 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700169 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
170 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700171
172 if (wpa_s->current_ssid->ssid_len == 0)
173 return 0; /* current profile still in use */
174 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
175 if (res < 0) {
176 wpa_msg(wpa_s, MSG_INFO,
177 "Failed to read SSID from driver");
178 return 0; /* try to use current profile */
179 }
180 drv_ssid_len = res;
181
182 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
183 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
184 drv_ssid_len) == 0)
185 return 0; /* current profile still in use */
186
187 wpa_msg(wpa_s, MSG_DEBUG,
188 "Driver-initiated BSS selection changed the SSID to %s",
189 wpa_ssid_txt(drv_ssid, drv_ssid_len));
190 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192
193 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
194 "information");
195 ssid = wpa_supplicant_get_ssid(wpa_s);
196 if (ssid == NULL) {
197 wpa_msg(wpa_s, MSG_INFO,
198 "No network configuration found for the current AP");
199 return -1;
200 }
201
Dmitry Shmidt04949592012-07-19 12:16:46 -0700202 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
204 return -1;
205 }
206
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800207 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
208 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
209 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
210 return -1;
211 }
212
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700213 res = wpas_temp_disabled(wpa_s, ssid);
214 if (res > 0) {
215 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
216 "disabled for %d second(s)", res);
217 return -1;
218 }
219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
221 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 u8 wpa_ie[80];
224 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800225 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
226 wpa_ie, &wpa_ie_len) < 0)
227 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 } else {
229 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
230 }
231
232 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
233 eapol_sm_invalidate_cached_session(wpa_s->eapol);
234 old_ssid = wpa_s->current_ssid;
235 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800236
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700237 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
240 wpa_supplicant_initiate_eapol(wpa_s);
241 if (old_ssid != wpa_s->current_ssid)
242 wpas_notify_network_changed(wpa_s);
243
244 return 0;
245}
246
247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800248void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249{
250 struct wpa_supplicant *wpa_s = eloop_ctx;
251
252 if (wpa_s->countermeasures) {
253 wpa_s->countermeasures = 0;
254 wpa_drv_set_countermeasures(wpa_s, 0);
255 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800256
257 /*
258 * It is possible that the device is sched scanning, which means
259 * that a connection attempt will be done only when we receive
260 * scan results. However, in this case, it would be preferable
261 * to scan and connect immediately, so cancel the sched_scan and
262 * issue a regular scan flow.
263 */
264 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 wpa_supplicant_req_scan(wpa_s, 0, 0);
266 }
267}
268
269
270void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
271{
272 int bssid_changed;
273
Dmitry Shmidt04949592012-07-19 12:16:46 -0700274 wnm_bss_keep_alive_deinit(wpa_s);
275
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276#ifdef CONFIG_IBSS_RSN
277 ibss_rsn_deinit(wpa_s->ibss_rsn);
278 wpa_s->ibss_rsn = NULL;
279#endif /* CONFIG_IBSS_RSN */
280
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700281#ifdef CONFIG_AP
282 wpa_supplicant_ap_deinit(wpa_s);
283#endif /* CONFIG_AP */
284
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700285#ifdef CONFIG_HS20
286 /* Clear possibly configured frame filters */
287 wpa_drv_configure_frame_filters(wpa_s, 0);
288#endif /* CONFIG_HS20 */
289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
291 return;
292
293 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
294 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
295 os_memset(wpa_s->bssid, 0, ETH_ALEN);
296 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800297 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 wpa_s->current_bss = NULL;
299 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 if (bssid_changed)
302 wpas_notify_bssid_changed(wpa_s);
303
304 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
305 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700306 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
307 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
308 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
310 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700312 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700313 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800314
315 wpas_rrm_reset(wpa_s);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800316 wpa_s->wnmsleep_used = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700317
318#ifdef CONFIG_TESTING_OPTIONS
319 wpa_s->last_tk_alg = WPA_ALG_NONE;
320 os_memset(wpa_s->last_tk, 0, sizeof(wpa_s->last_tk));
321#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322}
323
324
325static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
326{
327 struct wpa_ie_data ie;
328 int pmksa_set = -1;
329 size_t i;
330
331 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
332 ie.pmkid == NULL)
333 return;
334
335 for (i = 0; i < ie.num_pmkid; i++) {
336 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
337 ie.pmkid + i * PMKID_LEN,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700338 NULL, NULL, 0, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800340 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 break;
342 }
343 }
344
345 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
346 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
347}
348
349
350static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
351 union wpa_event_data *data)
352{
353 if (data == NULL) {
354 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
355 "event");
356 return;
357 }
358 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
359 " index=%d preauth=%d",
360 MAC2STR(data->pmkid_candidate.bssid),
361 data->pmkid_candidate.index,
362 data->pmkid_candidate.preauth);
363
364 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
365 data->pmkid_candidate.index,
366 data->pmkid_candidate.preauth);
367}
368
369
370static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
371{
372 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
373 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
374 return 0;
375
376#ifdef IEEE8021X_EAPOL
377 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
378 wpa_s->current_ssid &&
379 !(wpa_s->current_ssid->eapol_flags &
380 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
381 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
382 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
383 * plaintext or static WEP keys). */
384 return 0;
385 }
386#endif /* IEEE8021X_EAPOL */
387
388 return 1;
389}
390
391
392/**
393 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
394 * @wpa_s: pointer to wpa_supplicant data
395 * @ssid: Configuration data for the network
396 * Returns: 0 on success, -1 on failure
397 *
398 * This function is called when starting authentication with a network that is
399 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
400 */
401int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
402 struct wpa_ssid *ssid)
403{
404#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800405#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700406 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700408 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
409 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 return 0;
411
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700412 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 sim = 1;
414 aka = 1;
415 } else {
416 struct eap_method_type *eap = ssid->eap.eap_methods;
417 while (eap->vendor != EAP_VENDOR_IETF ||
418 eap->method != EAP_TYPE_NONE) {
419 if (eap->vendor == EAP_VENDOR_IETF) {
420 if (eap->method == EAP_TYPE_SIM)
421 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700422 else if (eap->method == EAP_TYPE_AKA ||
423 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 aka = 1;
425 }
426 eap++;
427 }
428 }
429
430 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
431 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700432 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
433 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
434 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 aka = 0;
436
437 if (!sim && !aka) {
438 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
439 "use SIM, but neither EAP-SIM nor EAP-AKA are "
440 "enabled");
441 return 0;
442 }
443
444 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
445 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700447 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 if (wpa_s->scard == NULL) {
449 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
450 "(pcsc-lite)");
451 return -1;
452 }
453 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
454 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800455#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456#endif /* IEEE8021X_EAPOL */
457
458 return 0;
459}
460
461
462#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700463
464static int has_wep_key(struct wpa_ssid *ssid)
465{
466 int i;
467
468 for (i = 0; i < NUM_WEP_KEYS; i++) {
469 if (ssid->wep_key_len[i])
470 return 1;
471 }
472
473 return 0;
474}
475
476
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700477static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 struct wpa_ssid *ssid)
479{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700480 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481
482 if (ssid->mixed_cell)
483 return 1;
484
485#ifdef CONFIG_WPS
486 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
487 return 1;
488#endif /* CONFIG_WPS */
489
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700490 if (has_wep_key(ssid))
491 privacy = 1;
492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493#ifdef IEEE8021X_EAPOL
494 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
495 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
496 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
497 privacy = 1;
498#endif /* IEEE8021X_EAPOL */
499
Jouni Malinen75ecf522011-06-27 15:19:46 -0700500 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
501 privacy = 1;
502
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800503 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
504 privacy = 1;
505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 if (bss->caps & IEEE80211_CAP_PRIVACY)
507 return privacy;
508 return !privacy;
509}
510
511
512static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
513 struct wpa_ssid *ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800514 struct wpa_bss *bss, int debug_print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515{
516 struct wpa_ie_data ie;
517 int proto_match = 0;
518 const u8 *rsn_ie, *wpa_ie;
519 int ret;
520 int wep_ok;
521
522 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
523 if (ret >= 0)
524 return ret;
525
526 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
527 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
528 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
529 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
530 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
531
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700532 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
534 proto_match++;
535
536 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800537 if (debug_print)
538 wpa_dbg(wpa_s, MSG_DEBUG,
539 " skip RSN IE - parse failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 break;
541 }
542
543 if (wep_ok &&
544 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
545 {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800546 if (debug_print)
547 wpa_dbg(wpa_s, MSG_DEBUG,
548 " selected based on TSN in RSN IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 return 1;
550 }
551
552 if (!(ie.proto & ssid->proto)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800553 if (debug_print)
554 wpa_dbg(wpa_s, MSG_DEBUG,
555 " skip RSN IE - proto mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 break;
557 }
558
559 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800560 if (debug_print)
561 wpa_dbg(wpa_s, MSG_DEBUG,
562 " skip RSN IE - PTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 break;
564 }
565
566 if (!(ie.group_cipher & ssid->group_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800567 if (debug_print)
568 wpa_dbg(wpa_s, MSG_DEBUG,
569 " skip RSN IE - GTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 break;
571 }
572
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700573 if (ssid->group_mgmt_cipher &&
574 !(ie.mgmt_group_cipher & ssid->group_mgmt_cipher)) {
575 if (debug_print)
576 wpa_dbg(wpa_s, MSG_DEBUG,
577 " skip RSN IE - group mgmt cipher mismatch");
578 break;
579 }
580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 if (!(ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800582 if (debug_print)
583 wpa_dbg(wpa_s, MSG_DEBUG,
584 " skip RSN IE - key mgmt mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 break;
586 }
587
588#ifdef CONFIG_IEEE80211W
589 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800590 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800591 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800592 if (debug_print)
593 wpa_dbg(wpa_s, MSG_DEBUG,
594 " skip RSN IE - no mgmt frame protection");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 break;
596 }
597#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800598 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
599 wpas_get_ssid_pmf(wpa_s, ssid) ==
600 NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800601 if (debug_print)
602 wpa_dbg(wpa_s, MSG_DEBUG,
603 " skip RSN IE - no mgmt frame protection enabled but AP requires it");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 break;
605 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800606#ifdef CONFIG_MBO
607 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
608 wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
609 wpas_get_ssid_pmf(wpa_s, ssid) !=
610 NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800611 if (debug_print)
612 wpa_dbg(wpa_s, MSG_DEBUG,
613 " skip RSN IE - no mgmt frame protection enabled on MBO AP");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800614 break;
615 }
616#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800618 if (debug_print)
619 wpa_dbg(wpa_s, MSG_DEBUG,
620 " selected based on RSN IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 return 1;
622 }
623
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700624#ifdef CONFIG_IEEE80211W
625 if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800626 if (debug_print)
627 wpa_dbg(wpa_s, MSG_DEBUG,
628 " skip - MFP Required but network not MFP Capable");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700629 return 0;
630 }
631#endif /* CONFIG_IEEE80211W */
632
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700633 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
635 proto_match++;
636
637 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800638 if (debug_print)
639 wpa_dbg(wpa_s, MSG_DEBUG,
640 " skip WPA IE - parse failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 break;
642 }
643
644 if (wep_ok &&
645 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
646 {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800647 if (debug_print)
648 wpa_dbg(wpa_s, MSG_DEBUG,
649 " selected based on TSN in WPA IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650 return 1;
651 }
652
653 if (!(ie.proto & ssid->proto)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800654 if (debug_print)
655 wpa_dbg(wpa_s, MSG_DEBUG,
656 " skip WPA IE - proto mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657 break;
658 }
659
660 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800661 if (debug_print)
662 wpa_dbg(wpa_s, MSG_DEBUG,
663 " skip WPA IE - PTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 break;
665 }
666
667 if (!(ie.group_cipher & ssid->group_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800668 if (debug_print)
669 wpa_dbg(wpa_s, MSG_DEBUG,
670 " skip WPA IE - GTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 break;
672 }
673
674 if (!(ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800675 if (debug_print)
676 wpa_dbg(wpa_s, MSG_DEBUG,
677 " skip WPA IE - key mgmt mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 break;
679 }
680
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800681 if (debug_print)
682 wpa_dbg(wpa_s, MSG_DEBUG,
683 " selected based on WPA IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 return 1;
685 }
686
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700687 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
688 !rsn_ie) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800689 if (debug_print)
690 wpa_dbg(wpa_s, MSG_DEBUG,
691 " allow for non-WPA IEEE 802.1X");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700692 return 1;
693 }
694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
696 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800697 if (debug_print)
698 wpa_dbg(wpa_s, MSG_DEBUG,
699 " skip - no WPA/RSN proto match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 return 0;
701 }
702
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800703 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
704 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800705 if (debug_print)
706 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800707 return 1;
708 }
709
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800711 if (debug_print)
712 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 return 1;
714 }
715
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800716 if (debug_print)
717 wpa_dbg(wpa_s, MSG_DEBUG,
718 " reject due to mismatch with WPA/WPA2");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719
720 return 0;
721}
722
723
724static int freq_allowed(int *freqs, int freq)
725{
726 int i;
727
728 if (freqs == NULL)
729 return 1;
730
731 for (i = 0; freqs[i]; i++)
732 if (freqs[i] == freq)
733 return 1;
734 return 0;
735}
736
737
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800738static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
739 int debug_print)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800740{
741 const struct hostapd_hw_modes *mode = NULL, *modes;
742 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
743 const u8 *rate_ie;
744 int i, j, k;
745
746 if (bss->freq == 0)
747 return 1; /* Cannot do matching without knowing band */
748
749 modes = wpa_s->hw.modes;
750 if (modes == NULL) {
751 /*
752 * The driver does not provide any additional information
753 * about the utilized hardware, so allow the connection attempt
754 * to continue.
755 */
756 return 1;
757 }
758
759 for (i = 0; i < wpa_s->hw.num_modes; i++) {
760 for (j = 0; j < modes[i].num_channels; j++) {
761 int freq = modes[i].channels[j].freq;
762 if (freq == bss->freq) {
763 if (mode &&
764 mode->mode == HOSTAPD_MODE_IEEE80211G)
765 break; /* do not allow 802.11b replace
766 * 802.11g */
767 mode = &modes[i];
768 break;
769 }
770 }
771 }
772
773 if (mode == NULL)
774 return 0;
775
776 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700777 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 if (rate_ie == NULL)
779 continue;
780
781 for (j = 2; j < rate_ie[1] + 2; j++) {
782 int flagged = !!(rate_ie[j] & 0x80);
783 int r = (rate_ie[j] & 0x7f) * 5;
784
785 /*
786 * IEEE Std 802.11n-2009 7.3.2.2:
787 * The new BSS Membership selector value is encoded
788 * like a legacy basic rate, but it is not a rate and
789 * only indicates if the BSS members are required to
790 * support the mandatory features of Clause 20 [HT PHY]
791 * in order to join the BSS.
792 */
793 if (flagged && ((rate_ie[j] & 0x7f) ==
794 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
795 if (!ht_supported(mode)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800796 if (debug_print)
797 wpa_dbg(wpa_s, MSG_DEBUG,
798 " hardware does not support HT PHY");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799 return 0;
800 }
801 continue;
802 }
803
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700804 /* There's also a VHT selector for 802.11ac */
805 if (flagged && ((rate_ie[j] & 0x7f) ==
806 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
807 if (!vht_supported(mode)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800808 if (debug_print)
809 wpa_dbg(wpa_s, MSG_DEBUG,
810 " hardware does not support VHT PHY");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700811 return 0;
812 }
813 continue;
814 }
815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816 if (!flagged)
817 continue;
818
819 /* check for legacy basic rates */
820 for (k = 0; k < mode->num_rates; k++) {
821 if (mode->rates[k] == r)
822 break;
823 }
824 if (k == mode->num_rates) {
825 /*
826 * IEEE Std 802.11-2007 7.3.2.2 demands that in
827 * order to join a BSS all required rates
828 * have to be supported by the hardware.
829 */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800830 if (debug_print)
831 wpa_dbg(wpa_s, MSG_DEBUG,
832 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
833 r / 10, r % 10,
834 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800835 return 0;
836 }
837 }
838 }
839
840 return 1;
841}
842
843
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800844/*
845 * Test whether BSS is in an ESS.
846 * This is done differently in DMG (60 GHz) and non-DMG bands
847 */
848static int bss_is_ess(struct wpa_bss *bss)
849{
850 if (bss_is_dmg(bss)) {
851 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
852 IEEE80211_CAP_DMG_AP;
853 }
854
855 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
856 IEEE80211_CAP_ESS);
857}
858
859
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800860static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
861{
862 size_t i;
863
864 for (i = 0; i < ETH_ALEN; i++) {
865 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
866 return 0;
867 }
868 return 1;
869}
870
871
872static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
873{
874 size_t i;
875
876 for (i = 0; i < num; i++) {
877 const u8 *a = list + i * ETH_ALEN * 2;
878 const u8 *m = a + ETH_ALEN;
879
880 if (match_mac_mask(a, addr, m))
881 return 1;
882 }
883 return 0;
884}
885
886
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700887static void owe_trans_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
888 const u8 **ret_ssid, size_t *ret_ssid_len)
889{
890#ifdef CONFIG_OWE
891 const u8 *owe, *pos, *end, *bssid;
892 u8 ssid_len;
893 struct wpa_bss *open_bss;
894
895 owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
896 if (!owe || !wpa_bss_get_ie(bss, WLAN_EID_RSN))
897 return;
898
899 pos = owe + 6;
900 end = owe + 2 + owe[1];
901
902 if (end - pos < ETH_ALEN + 1)
903 return;
904 bssid = pos;
905 pos += ETH_ALEN;
906 ssid_len = *pos++;
907 if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
908 return;
909
910 /* Match the profile SSID against the OWE transition mode SSID on the
911 * open network. */
912 wpa_dbg(wpa_s, MSG_DEBUG, "OWE: transition mode BSSID: " MACSTR
913 " SSID: %s", MAC2STR(bssid), wpa_ssid_txt(pos, ssid_len));
914 *ret_ssid = pos;
915 *ret_ssid_len = ssid_len;
916
917 if (bss->ssid_len > 0)
918 return;
919
920 open_bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
921 if (!open_bss)
922 return;
923 if (ssid_len != open_bss->ssid_len ||
924 os_memcmp(pos, open_bss->ssid, ssid_len) != 0) {
925 wpa_dbg(wpa_s, MSG_DEBUG,
926 "OWE: transition mode SSID mismatch: %s",
927 wpa_ssid_txt(open_bss->ssid, open_bss->ssid_len));
928 return;
929 }
930
931 owe = wpa_bss_get_vendor_ie(open_bss, OWE_IE_VENDOR_TYPE);
932 if (!owe || wpa_bss_get_ie(open_bss, WLAN_EID_RSN)) {
933 wpa_dbg(wpa_s, MSG_DEBUG,
934 "OWE: transition mode open BSS unexpected info");
935 return;
936 }
937
938 pos = owe + 6;
939 end = owe + 2 + owe[1];
940
941 if (end - pos < ETH_ALEN + 1)
942 return;
943 if (os_memcmp(pos, bss->bssid, ETH_ALEN) != 0) {
944 wpa_dbg(wpa_s, MSG_DEBUG,
945 "OWE: transition mode BSSID mismatch: " MACSTR,
946 MAC2STR(pos));
947 return;
948 }
949 pos += ETH_ALEN;
950 ssid_len = *pos++;
951 if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
952 return;
953 wpa_dbg(wpa_s, MSG_DEBUG, "OWE: learned transition mode OWE SSID: %s",
954 wpa_ssid_txt(pos, ssid_len));
955 os_memcpy(bss->ssid, pos, ssid_len);
956 bss->ssid_len = ssid_len;
957#endif /* CONFIG_OWE */
958}
959
960
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800961struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
962 int i, struct wpa_bss *bss,
963 struct wpa_ssid *group,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800964 int only_first_ssid, int debug_print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700966 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 int wpa;
968 struct wpa_blacklist *e;
969 const u8 *ie;
970 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800971 int osen;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800972#ifdef CONFIG_MBO
973 const u8 *assoc_disallow;
974#endif /* CONFIG_MBO */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700975 const u8 *match_ssid;
976 size_t match_ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700978 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_ie_len = ie ? ie[1] : 0;
980
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700981 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 rsn_ie_len = ie ? ie[1] : 0;
983
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800984 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
985 osen = ie != NULL;
986
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800987 if (debug_print) {
988 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
989 " ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
990 i, MAC2STR(bss->bssid),
991 wpa_ssid_txt(bss->ssid, bss->ssid_len),
992 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
993 bss->freq,
994 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
995 " wps" : "",
996 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
997 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
998 ? " p2p" : "",
999 osen ? " osen=1" : "");
1000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001
1002 e = wpa_blacklist_get(wpa_s, bss->bssid);
1003 if (e) {
1004 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 /*
1007 * When only a single network is enabled, we can
1008 * trigger blacklisting on the first failure. This
1009 * should not be done with multiple enabled networks to
1010 * avoid getting forced to move into a worse ESS on
1011 * single error if there are no other BSSes of the
1012 * current ESS.
1013 */
1014 limit = 0;
1015 }
1016 if (e->count > limit) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001017 if (debug_print) {
1018 wpa_dbg(wpa_s, MSG_DEBUG,
1019 " skip - blacklisted (count=%d limit=%d)",
1020 e->count, limit);
1021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 return NULL;
1023 }
1024 }
1025
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001026 match_ssid = bss->ssid;
1027 match_ssid_len = bss->ssid_len;
1028 owe_trans_ssid(wpa_s, bss, &match_ssid, &match_ssid_len);
1029
1030 if (match_ssid_len == 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001031 if (debug_print)
1032 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 return NULL;
1034 }
1035
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001036 if (disallowed_bssid(wpa_s, bss->bssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001037 if (debug_print)
1038 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001039 return NULL;
1040 }
1041
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001042 if (disallowed_ssid(wpa_s, match_ssid, match_ssid_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001043 if (debug_print)
1044 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001045 return NULL;
1046 }
1047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
1049
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001050 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001052 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053
Dmitry Shmidt04949592012-07-19 12:16:46 -07001054 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001055 if (debug_print)
1056 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 continue;
1058 }
1059
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001060 res = wpas_temp_disabled(wpa_s, ssid);
1061 if (res > 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001062 if (debug_print)
1063 wpa_dbg(wpa_s, MSG_DEBUG,
1064 " skip - disabled temporarily for %d second(s)",
1065 res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001066 continue;
1067 }
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069#ifdef CONFIG_WPS
1070 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001071 if (debug_print)
1072 wpa_dbg(wpa_s, MSG_DEBUG,
1073 " skip - blacklisted (WPS)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 continue;
1075 }
1076
1077 if (wpa && ssid->ssid_len == 0 &&
1078 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
1079 check_ssid = 0;
1080
1081 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1082 /* Only allow wildcard SSID match if an AP
1083 * advertises active WPS operation that matches
1084 * with our mode. */
1085 check_ssid = 1;
1086 if (ssid->ssid_len == 0 &&
1087 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
1088 check_ssid = 0;
1089 }
1090#endif /* CONFIG_WPS */
1091
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001092 if (ssid->bssid_set && ssid->ssid_len == 0 &&
1093 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
1094 check_ssid = 0;
1095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 if (check_ssid &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001097 (match_ssid_len != ssid->ssid_len ||
1098 os_memcmp(match_ssid, ssid->ssid, match_ssid_len) != 0)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001099 if (debug_print)
1100 wpa_dbg(wpa_s, MSG_DEBUG,
1101 " skip - SSID mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 continue;
1103 }
1104
1105 if (ssid->bssid_set &&
1106 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001107 if (debug_print)
1108 wpa_dbg(wpa_s, MSG_DEBUG,
1109 " skip - BSSID mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 continue;
1111 }
1112
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001113 /* check blacklist */
1114 if (ssid->num_bssid_blacklist &&
1115 addr_in_list(bss->bssid, ssid->bssid_blacklist,
1116 ssid->num_bssid_blacklist)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001117 if (debug_print)
1118 wpa_dbg(wpa_s, MSG_DEBUG,
1119 " skip - BSSID blacklisted");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001120 continue;
1121 }
1122
1123 /* if there is a whitelist, only accept those APs */
1124 if (ssid->num_bssid_whitelist &&
1125 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
1126 ssid->num_bssid_whitelist)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001127 if (debug_print)
1128 wpa_dbg(wpa_s, MSG_DEBUG,
1129 " skip - BSSID not in whitelist");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001130 continue;
1131 }
1132
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001133 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss,
1134 debug_print))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 continue;
1136
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001137 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
1139 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
1140 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001141 if (debug_print)
1142 wpa_dbg(wpa_s, MSG_DEBUG,
1143 " skip - non-WPA network not allowed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 continue;
1145 }
1146
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001147 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
1148 has_wep_key(ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001149 if (debug_print)
1150 wpa_dbg(wpa_s, MSG_DEBUG,
1151 " skip - ignore WPA/WPA2 AP for WEP network block");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001152 continue;
1153 }
1154
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001155 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001156 if (debug_print)
1157 wpa_dbg(wpa_s, MSG_DEBUG,
1158 " skip - non-OSEN network not allowed");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001159 continue;
1160 }
1161
Jouni Malinen75ecf522011-06-27 15:19:46 -07001162 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001163 if (debug_print)
1164 wpa_dbg(wpa_s, MSG_DEBUG,
1165 " skip - privacy mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 continue;
1167 }
1168
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001169 if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
1170 !bss_is_pbss(bss)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001171 if (debug_print)
1172 wpa_dbg(wpa_s, MSG_DEBUG,
1173 " skip - not ESS, PBSS, or MBSS");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001174 continue;
1175 }
1176
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001177 if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001178 if (debug_print)
1179 wpa_dbg(wpa_s, MSG_DEBUG,
1180 " skip - PBSS mismatch (ssid %d bss %d)",
1181 ssid->pbss, bss_is_pbss(bss));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 continue;
1183 }
1184
1185 if (!freq_allowed(ssid->freq_list, bss->freq)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001186 if (debug_print)
1187 wpa_dbg(wpa_s, MSG_DEBUG,
1188 " skip - frequency not allowed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 continue;
1190 }
1191
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001192#ifdef CONFIG_MESH
1193 if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
1194 ssid->frequency != bss->freq) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001195 if (debug_print)
1196 wpa_dbg(wpa_s, MSG_DEBUG,
1197 " skip - frequency not allowed (mesh)");
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001198 continue;
1199 }
1200#endif /* CONFIG_MESH */
1201
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001202 if (!rate_match(wpa_s, bss, debug_print)) {
1203 if (debug_print)
1204 wpa_dbg(wpa_s, MSG_DEBUG,
1205 " skip - rate sets do not match");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001206 continue;
1207 }
1208
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001209#ifndef CONFIG_IBSS_RSN
1210 if (ssid->mode == WPAS_MODE_IBSS &&
1211 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1212 WPA_KEY_MGMT_WPA_NONE))) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001213 if (debug_print)
1214 wpa_dbg(wpa_s, MSG_DEBUG,
1215 " skip - IBSS RSN not supported in the build");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001216 continue;
1217 }
1218#endif /* !CONFIG_IBSS_RSN */
1219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001221 if (ssid->p2p_group &&
1222 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1223 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001224 if (debug_print)
1225 wpa_dbg(wpa_s, MSG_DEBUG,
1226 " skip - no P2P IE seen");
Dmitry Shmidt96571392013-10-14 12:54:46 -07001227 continue;
1228 }
1229
Dmitry Shmidt54605472013-11-08 11:10:19 -08001230 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1231 struct wpabuf *p2p_ie;
1232 u8 dev_addr[ETH_ALEN];
1233
1234 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1235 if (ie == NULL) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001236 if (debug_print)
1237 wpa_dbg(wpa_s, MSG_DEBUG,
1238 " skip - no P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001239 continue;
1240 }
1241 p2p_ie = wpa_bss_get_vendor_ie_multi(
1242 bss, P2P_IE_VENDOR_TYPE);
1243 if (p2p_ie == NULL) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001244 if (debug_print)
1245 wpa_dbg(wpa_s, MSG_DEBUG,
1246 " skip - could not fetch P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001247 continue;
1248 }
1249
1250 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1251 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1252 ETH_ALEN) != 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001253 if (debug_print)
1254 wpa_dbg(wpa_s, MSG_DEBUG,
1255 " skip - no matching GO P2P Device Address in P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001256 wpabuf_free(p2p_ie);
1257 continue;
1258 }
1259 wpabuf_free(p2p_ie);
1260 }
1261
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 /*
1263 * TODO: skip the AP if its P2P IE has Group Formation
1264 * bit set in the P2P Group Capability Bitmap and we
1265 * are not in Group Formation with that device.
1266 */
1267#endif /* CONFIG_P2P */
1268
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001269 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1270 {
1271 struct os_reltime diff;
1272
1273 os_reltime_sub(&wpa_s->scan_min_time,
1274 &bss->last_update, &diff);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001275 if (debug_print)
1276 wpa_dbg(wpa_s, MSG_DEBUG,
1277 " skip - scan result not recent enough (%u.%06u seconds too old)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001278 (unsigned int) diff.sec,
1279 (unsigned int) diff.usec);
1280 continue;
1281 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001282#ifdef CONFIG_MBO
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001283#ifdef CONFIG_TESTING_OPTIONS
1284 if (wpa_s->ignore_assoc_disallow)
1285 goto skip_assoc_disallow;
1286#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001287 assoc_disallow = wpas_mbo_get_bss_attr(
1288 bss, MBO_ATTR_ID_ASSOC_DISALLOW);
1289 if (assoc_disallow && assoc_disallow[1] >= 1) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001290 if (debug_print)
1291 wpa_dbg(wpa_s, MSG_DEBUG,
1292 " skip - MBO association disallowed (reason %u)",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001293 assoc_disallow[2]);
1294 continue;
1295 }
1296
1297 if (wpa_is_bss_tmp_disallowed(wpa_s, bss->bssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001298 if (debug_print)
1299 wpa_dbg(wpa_s, MSG_DEBUG,
1300 " skip - MBO retry delay has not passed yet");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001301 continue;
1302 }
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001303#ifdef CONFIG_TESTING_OPTIONS
1304 skip_assoc_disallow:
1305#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001306#endif /* CONFIG_MBO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001307
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001308#ifdef CONFIG_DPP
1309 if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
1310 !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid) &&
1311 (!ssid->dpp_connector ||
1312 !ssid->dpp_netaccesskey ||
1313 !ssid->dpp_csign)) {
1314 if (debug_print)
1315 wpa_dbg(wpa_s, MSG_DEBUG,
1316 " skip - no PMKSA entry for DPP");
1317 continue;
1318 }
1319#endif /* CONFIG_DPP */
1320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 /* Matching configuration found */
1322 return ssid;
1323 }
1324
1325 /* No matching configuration found */
1326 return NULL;
1327}
1328
1329
1330static struct wpa_bss *
1331wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001333 struct wpa_ssid **selected_ssid,
1334 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001336 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001338 if (wpa_s->current_ssid) {
1339 struct wpa_ssid *ssid;
1340
1341 wpa_dbg(wpa_s, MSG_DEBUG,
1342 "Scan results matching the currently selected network");
1343 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1344 struct wpa_bss *bss = wpa_s->last_scan_res[i];
1345
1346 ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1347 only_first_ssid, 0);
1348 if (ssid != wpa_s->current_ssid)
1349 continue;
1350 wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
1351 " freq=%d level=%d snr=%d est_throughput=%u",
1352 i, MAC2STR(bss->bssid), bss->freq, bss->level,
1353 bss->snr, bss->est_throughput);
1354 }
1355 }
1356
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001357 if (only_first_ssid)
1358 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1359 group->id);
1360 else
1361 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1362 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001364 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1365 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001366 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001367 only_first_ssid, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 if (!*selected_ssid)
1369 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1371 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001372 MAC2STR(bss->bssid),
1373 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1374 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 }
1376
1377 return NULL;
1378}
1379
1380
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001381struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1382 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383{
1384 struct wpa_bss *selected = NULL;
1385 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001386 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001387 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001389 if (wpa_s->last_scan_res == NULL ||
1390 wpa_s->last_scan_res_used == 0)
1391 return NULL; /* no scan results from last update */
1392
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001393 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001394 /* check that next_ssid is still valid */
1395 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1396 if (ssid == wpa_s->next_ssid)
1397 break;
1398 }
1399 next_ssid = ssid;
1400 wpa_s->next_ssid = NULL;
1401 }
1402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001404 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1405 if (next_ssid && next_ssid->priority ==
1406 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001407 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001408 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001409 if (selected)
1410 break;
1411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001413 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001414 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 if (selected)
1416 break;
1417 }
1418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001419 if (selected == NULL && wpa_s->blacklist &&
1420 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1422 "blacklist and try again");
1423 wpa_blacklist_clear(wpa_s);
1424 wpa_s->blacklist_cleared++;
1425 } else if (selected == NULL)
1426 break;
1427 }
1428
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001429 ssid = *selected_ssid;
1430 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1431 !ssid->passphrase && !ssid->ext_psk) {
1432 const char *field_name, *txt = NULL;
1433
1434 wpa_dbg(wpa_s, MSG_DEBUG,
1435 "PSK/passphrase not yet available for the selected network");
1436
1437 wpas_notify_network_request(wpa_s, ssid,
1438 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1439
1440 field_name = wpa_supplicant_ctrl_req_to_string(
1441 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1442 if (field_name == NULL)
1443 return NULL;
1444
1445 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1446
1447 selected = NULL;
1448 }
1449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 return selected;
1451}
1452
1453
1454static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1455 int timeout_sec, int timeout_usec)
1456{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001457 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 /*
1459 * No networks are enabled; short-circuit request so
1460 * we don't wait timeout seconds before transitioning
1461 * to INACTIVE state.
1462 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001463 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1464 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1466 return;
1467 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001468
1469 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1471}
1472
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001473
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001474int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001475 struct wpa_bss *selected,
1476 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477{
1478 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1479 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1480 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001481 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001483 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1484 wpa_s->p2p_in_provisioning) {
1485 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1486 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001487 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489#endif /* CONFIG_P2P */
1490
1491#ifdef CONFIG_WPS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001492 wpas_wps_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493 wpas_wps_cancel(wpa_s);
1494#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 }
1497
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001498 wpa_msg(wpa_s, MSG_DEBUG,
1499 "Considering connect request: reassociate: %d selected: "
1500 MACSTR " bssid: " MACSTR " pending: " MACSTR
1501 " wpa_state: %s ssid=%p current_ssid=%p",
1502 wpa_s->reassociate, MAC2STR(selected->bssid),
1503 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1504 wpa_supplicant_state_txt(wpa_s->wpa_state),
1505 ssid, wpa_s->current_ssid);
1506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 /*
1508 * Do not trigger new association unless the BSSID has changed or if
1509 * reassociation is requested. If we are in process of associating with
1510 * the selected BSSID, do not trigger new attempt.
1511 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001512 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1514 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1515 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001516 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1517 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1518 0) ||
1519 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1520 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001521 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1522 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001523 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001525 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1526 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 wpa_supplicant_associate(wpa_s, selected, ssid);
1528 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001529 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1530 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001532
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001533 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534}
1535
1536
1537static struct wpa_ssid *
1538wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1539{
1540 int prio;
1541 struct wpa_ssid *ssid;
1542
1543 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1544 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1545 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001546 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 continue;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001548#ifndef CONFIG_IBSS_RSN
1549 if (ssid->mode == WPAS_MODE_IBSS &&
1550 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1551 WPA_KEY_MGMT_WPA_NONE))) {
1552 wpa_msg(wpa_s, MSG_INFO,
1553 "IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
1554 wpa_ssid_txt(ssid->ssid,
1555 ssid->ssid_len));
1556 continue;
1557 }
1558#endif /* !CONFIG_IBSS_RSN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001560 ssid->mode == IEEE80211_MODE_AP ||
1561 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 return ssid;
1563 }
1564 }
1565 return NULL;
1566}
1567
1568
1569/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1570 * on BSS added and BSS changed events */
1571static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001572 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001574 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575
1576 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1577 return;
1578
Jouni Malinen87fd2792011-05-16 18:35:42 +03001579 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581
Jouni Malinen87fd2792011-05-16 18:35:42 +03001582 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 if (ssid == NULL)
1584 continue;
1585
Jouni Malinen87fd2792011-05-16 18:35:42 +03001586 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 if (rsn == NULL)
1588 continue;
1589
Jouni Malinen87fd2792011-05-16 18:35:42 +03001590 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 }
1592
1593}
1594
1595
1596static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1597 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001598 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001600 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001601#ifndef CONFIG_NO_ROAMING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001602 int min_diff, diff;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001603 int to_5ghz;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001604 int cur_est, sel_est;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001605#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606
1607 if (wpa_s->reassociate)
1608 return 1; /* explicit request to reassociate */
1609 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1610 return 1; /* we are not associated; continue */
1611 if (wpa_s->current_ssid == NULL)
1612 return 1; /* unknown current SSID */
1613 if (wpa_s->current_ssid != ssid)
1614 return 1; /* different network block */
1615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001616 if (wpas_driver_bss_selection(wpa_s))
1617 return 0; /* Driver-based roaming */
1618
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001619 if (wpa_s->current_ssid->ssid)
1620 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1621 wpa_s->current_ssid->ssid,
1622 wpa_s->current_ssid->ssid_len);
1623 if (!current_bss)
1624 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625
1626 if (!current_bss)
1627 return 1; /* current BSS not seen in scan results */
1628
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001629 if (current_bss == selected)
1630 return 0;
1631
1632 if (selected->last_update_idx > current_bss->last_update_idx)
1633 return 1; /* current BSS not seen in the last scan */
1634
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001635#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001637 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001638 " freq=%d level=%d snr=%d est_throughput=%u",
1639 MAC2STR(current_bss->bssid),
1640 current_bss->freq, current_bss->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001641 current_bss->snr, current_bss->est_throughput);
1642 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001643 " freq=%d level=%d snr=%d est_throughput=%u",
1644 MAC2STR(selected->bssid), selected->freq, selected->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001645 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646
1647 if (wpa_s->current_ssid->bssid_set &&
1648 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1649 0) {
1650 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1651 "has preferred BSSID");
1652 return 1;
1653 }
1654
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001655 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1656 wpa_dbg(wpa_s, MSG_DEBUG,
1657 "Allow reassociation - selected BSS has better estimated throughput");
1658 return 1;
1659 }
1660
Dmitry Shmidte4663042016-04-04 10:07:49 -07001661 to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
1662
1663 if (current_bss->level < 0 &&
1664 current_bss->level > selected->level + to_5ghz * 2) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001665 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1666 "signal level");
1667 return 0;
1668 }
1669
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001670 if (current_bss->est_throughput > selected->est_throughput + 5000) {
1671 wpa_dbg(wpa_s, MSG_DEBUG,
1672 "Skip roam - Current BSS has better estimated throughput");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001673 return 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001674 }
1675
1676 cur_est = current_bss->est_throughput;
1677 sel_est = selected->est_throughput;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 min_diff = 2;
1679 if (current_bss->level < 0) {
1680 if (current_bss->level < -85)
1681 min_diff = 1;
1682 else if (current_bss->level < -80)
1683 min_diff = 2;
1684 else if (current_bss->level < -75)
1685 min_diff = 3;
1686 else if (current_bss->level < -70)
1687 min_diff = 4;
1688 else
1689 min_diff = 5;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001690 if (cur_est > sel_est * 1.5)
1691 min_diff += 10;
1692 else if (cur_est > sel_est * 1.2)
1693 min_diff += 5;
1694 else if (cur_est > sel_est * 1.1)
1695 min_diff += 2;
1696 else if (cur_est > sel_est)
1697 min_diff++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001699 if (to_5ghz) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001700 int reduce = 2;
1701
Dmitry Shmidte4663042016-04-04 10:07:49 -07001702 /* Make it easier to move to 5 GHz band */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001703 if (sel_est > cur_est * 1.5)
1704 reduce = 5;
1705 else if (sel_est > cur_est * 1.2)
1706 reduce = 4;
1707 else if (sel_est > cur_est * 1.1)
1708 reduce = 3;
1709
1710 if (min_diff > reduce)
1711 min_diff -= reduce;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001712 else
1713 min_diff = 0;
1714 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001715 diff = abs(current_bss->level - selected->level);
1716 if (diff < min_diff) {
1717 wpa_dbg(wpa_s, MSG_DEBUG,
1718 "Skip roam - too small difference in signal level (%d < %d)",
1719 diff, min_diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 return 0;
1721 }
1722
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001723 wpa_dbg(wpa_s, MSG_DEBUG,
1724 "Allow reassociation due to difference in signal level (%d >= %d)",
1725 diff, min_diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001727#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001728 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001729#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730}
1731
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001732
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001733/*
1734 * Return a negative value if no scan results could be fetched or if scan
1735 * results should not be shared with other virtual interfaces.
1736 * Return 0 if scan results were fetched and may be shared with other
1737 * interfaces.
1738 * Return 1 if scan results may be shared with other virtual interfaces but may
1739 * not trigger any operations.
1740 * Return 2 if the interface was removed and cannot be used.
1741 */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001742static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001743 union wpa_event_data *data,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001744 int own_request, int update_only)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001746 struct wpa_scan_results *scan_res = NULL;
1747 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001749#ifndef CONFIG_NO_RANDOM_POOL
1750 size_t i, num;
1751#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752
1753#ifdef CONFIG_AP
1754 if (wpa_s->ap_iface)
1755 ap = 1;
1756#endif /* CONFIG_AP */
1757
1758 wpa_supplicant_notify_scanning(wpa_s, 0);
1759
1760 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1761 data ? &data->scan_info :
1762 NULL, 1);
1763 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001764 if (wpa_s->conf->ap_scan == 2 || ap ||
1765 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001767 if (!own_request)
1768 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001769 if (data && data->scan_info.external_scan)
1770 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1772 "scanning again");
1773 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001774 ret = -1;
1775 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 }
1777
1778#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779 num = scan_res->num;
1780 if (num > 10)
1781 num = 10;
1782 for (i = 0; i < num; i++) {
1783 u8 buf[5];
1784 struct wpa_scan_res *res = scan_res->res[i];
1785 buf[0] = res->bssid[5];
1786 buf[1] = res->qual & 0xff;
1787 buf[2] = res->noise & 0xff;
1788 buf[3] = res->level & 0xff;
1789 buf[4] = res->tsf & 0xff;
1790 random_add_randomness(buf, sizeof(buf));
1791 }
1792#endif /* CONFIG_NO_RANDOM_POOL */
1793
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001794 if (update_only) {
1795 ret = 1;
1796 goto scan_work_done;
1797 }
1798
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001799 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001800 !(data && data->scan_info.external_scan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1802 struct wpa_scan_results *scan_res);
1803
1804 scan_res_handler = wpa_s->scan_res_handler;
1805 wpa_s->scan_res_handler = NULL;
1806 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001807 ret = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001808 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 }
1810
1811 if (ap) {
1812 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1813#ifdef CONFIG_AP
1814 if (wpa_s->ap_iface->scan_cb)
1815 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1816#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001817 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001819
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001820 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001821 wpa_s->own_scan_running,
1822 data ? data->scan_info.external_scan : 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001823 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001824 wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1825 own_request && !(data && data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001826 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1827 wpa_s->manual_scan_id);
1828 wpa_s->manual_scan_use_id = 0;
1829 } else {
1830 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1831 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001832 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833
1834 wpas_notify_scan_done(wpa_s, 1);
1835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001836 if (data && data->scan_info.external_scan) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001837 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 -07001838 wpa_scan_results_free(scan_res);
1839 return 0;
1840 }
1841
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001842 if (wnm_scan_process(wpa_s, 1) > 0)
1843 goto scan_work_done;
1844
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001845 if (sme_proc_obss_scan(wpa_s) > 0)
1846 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847
Dmitry Shmidt29333592017-01-09 12:27:11 -08001848 if (own_request &&
1849 wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
1850 goto scan_work_done;
1851
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001852 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1853 goto scan_work_done;
1854
1855 if (autoscan_notify_scan(wpa_s, scan_res))
1856 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001858 if (wpa_s->disconnected) {
1859 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001860 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861 }
1862
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001864 bgscan_notify_scan(wpa_s, scan_res) == 1)
1865 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001866
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001867 wpas_wps_update_ap_info(wpa_s, scan_res);
1868
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001869 if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
1870 wpa_s->wpa_state < WPA_COMPLETED)
1871 goto scan_work_done;
1872
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001873 wpa_scan_results_free(scan_res);
1874
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001875 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001876 struct wpa_radio_work *work = wpa_s->scan_work;
1877 wpa_s->scan_work = NULL;
1878 radio_work_done(work);
1879 }
1880
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001881 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001882
1883scan_work_done:
1884 wpa_scan_results_free(scan_res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001885 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001886 struct wpa_radio_work *work = wpa_s->scan_work;
1887 wpa_s->scan_work = NULL;
1888 radio_work_done(work);
1889 }
1890 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001891}
1892
1893
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001894static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001895 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001896{
1897 struct wpa_bss *selected;
1898 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001899 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1900
1901 if (time_to_reenable > 0) {
1902 wpa_dbg(wpa_s, MSG_DEBUG,
1903 "Postpone network selection by %d seconds since all networks are disabled",
1904 time_to_reenable);
1905 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1906 eloop_register_timeout(time_to_reenable, 0,
1907 wpas_network_reenabled, wpa_s, NULL);
1908 return 0;
1909 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001910
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001911 if (wpa_s->p2p_mgmt)
1912 return 0; /* no normal connection on p2p_mgmt interface */
1913
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001914 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001916#ifdef CONFIG_MESH
1917 if (wpa_s->ifmsh) {
1918 wpa_msg(wpa_s, MSG_INFO,
1919 "Avoiding join because we already joined a mesh group");
1920 return 0;
1921 }
1922#endif /* CONFIG_MESH */
1923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 if (selected) {
1925 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001926 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001927 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001928 if (new_scan)
1929 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001931 }
1932
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001933 if (ssid != wpa_s->current_ssid &&
1934 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1935 wpa_s->own_disconnect_req = 1;
1936 wpa_supplicant_deauthenticate(
1937 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1938 }
1939
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001940 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001941 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001942 return -1;
1943 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001944 if (new_scan)
1945 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001946 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001947 * Do not allow other virtual radios to trigger operations based
1948 * on these scan results since we do not want them to start
1949 * other associations at the same time.
Jouni Malinen89ca7022012-09-14 13:03:12 -07001950 */
1951 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1954 ssid = wpa_supplicant_pick_new_network(wpa_s);
1955 if (ssid) {
1956 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1957 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001958 if (new_scan)
1959 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001960 } else if (own_request) {
1961 /*
1962 * No SSID found. If SCAN results are as a result of
1963 * own scan request and not due to a scan request on
1964 * another shared interface, try another scan.
1965 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 int timeout_sec = wpa_s->scan_interval;
1967 int timeout_usec = 0;
1968#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001969 int res;
1970
1971 res = wpas_p2p_scan_no_go_seen(wpa_s);
1972 if (res == 2)
1973 return 2;
1974 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001975 return 0;
1976
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001977 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001978 wpa_s->show_group_started ||
1979 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 /*
1981 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001982 * state and during P2P join-a-group operation
1983 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 */
1985 timeout_sec = 0;
1986 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001987 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1988 timeout_usec);
1989 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 }
1991#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001992#ifdef CONFIG_INTERWORKING
1993 if (wpa_s->conf->auto_interworking &&
1994 wpa_s->conf->interworking &&
1995 wpa_s->conf->cred) {
1996 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1997 "start ANQP fetch since no matching "
1998 "networks found");
1999 wpa_s->network_select = 1;
2000 wpa_s->auto_network_select = 1;
2001 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07002002 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002003 }
2004#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002005#ifdef CONFIG_WPS
2006 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
2007 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
2008 timeout_sec = 0;
2009 timeout_usec = 500000;
2010 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2011 timeout_usec);
2012 return 0;
2013 }
2014#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002015 if (wpa_supplicant_req_sched_scan(wpa_s))
2016 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
2017 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07002018
2019 wpa_msg_ctrl(wpa_s, MSG_INFO,
2020 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 }
2022 }
2023 return 0;
2024}
2025
2026
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002027static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
2028 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002031 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07002032
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002033 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002034 if (res == 2) {
2035 /*
2036 * Interface may have been removed, so must not dereference
2037 * wpa_s after this.
2038 */
2039 return 1;
2040 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002041
2042 if (res < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 /*
2044 * If no scan results could be fetched, then no need to
2045 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07002046 * this scan. Similarly, if scan results started a new operation on this
2047 * interface, do not notify other interfaces to avoid concurrent
2048 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002050 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 }
2052
2053 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002054 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 * so, they get updated with this same scan info.
2056 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002057 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2058 radio_list) {
2059 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
2061 "sibling", ifs->ifname);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002062 res = _wpa_supplicant_event_scan_results(ifs, data, 0,
2063 res > 0);
2064 if (res < 0)
2065 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 }
2067 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002068
2069 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070}
2071
2072#endif /* CONFIG_NO_SCAN_PROCESSING */
2073
2074
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002075int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
2076{
2077#ifdef CONFIG_NO_SCAN_PROCESSING
2078 return -1;
2079#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002080 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002081
Dmitry Shmidt29333592017-01-09 12:27:11 -08002082 wpa_s->ignore_post_flush_scan_res = 0;
2083
Dmitry Shmidt41712582015-06-29 11:02:15 -07002084 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002085 return -1;
2086
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002087 os_get_reltime(&now);
2088 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002089 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
2090 return -1;
2091 }
2092
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002093 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002094#endif /* CONFIG_NO_SCAN_PROCESSING */
2095}
2096
Dmitry Shmidt04949592012-07-19 12:16:46 -07002097#ifdef CONFIG_WNM
2098
2099static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
2100{
2101 struct wpa_supplicant *wpa_s = eloop_ctx;
2102
2103 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2104 return;
2105
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002106 if (!wpa_s->no_keep_alive) {
2107 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
2108 MAC2STR(wpa_s->bssid));
2109 /* TODO: could skip this if normal data traffic has been sent */
2110 /* TODO: Consider using some more appropriate data frame for
2111 * this */
2112 if (wpa_s->l2)
2113 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
2114 (u8 *) "", 0);
2115 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002116
2117#ifdef CONFIG_SME
2118 if (wpa_s->sme.bss_max_idle_period) {
2119 unsigned int msec;
2120 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
2121 if (msec > 100)
2122 msec -= 100;
2123 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2124 wnm_bss_keep_alive, wpa_s, NULL);
2125 }
2126#endif /* CONFIG_SME */
2127}
2128
2129
2130static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
2131 const u8 *ies, size_t ies_len)
2132{
2133 struct ieee802_11_elems elems;
2134
2135 if (ies == NULL)
2136 return;
2137
2138 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2139 return;
2140
2141#ifdef CONFIG_SME
2142 if (elems.bss_max_idle_period) {
2143 unsigned int msec;
2144 wpa_s->sme.bss_max_idle_period =
2145 WPA_GET_LE16(elems.bss_max_idle_period);
2146 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
2147 "TU)%s", wpa_s->sme.bss_max_idle_period,
2148 (elems.bss_max_idle_period[2] & 0x01) ?
2149 " (protected keep-live required)" : "");
2150 if (wpa_s->sme.bss_max_idle_period == 0)
2151 wpa_s->sme.bss_max_idle_period = 1;
2152 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
2153 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2154 /* msec times 1000 */
2155 msec = wpa_s->sme.bss_max_idle_period * 1024;
2156 if (msec > 100)
2157 msec -= 100;
2158 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2159 wnm_bss_keep_alive, wpa_s,
2160 NULL);
2161 }
2162 }
2163#endif /* CONFIG_SME */
2164}
2165
2166#endif /* CONFIG_WNM */
2167
2168
2169void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
2170{
2171#ifdef CONFIG_WNM
2172 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2173#endif /* CONFIG_WNM */
2174}
2175
2176
Dmitry Shmidt051af732013-10-22 13:52:46 -07002177#ifdef CONFIG_INTERWORKING
2178
2179static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
2180 size_t len)
2181{
2182 int res;
2183
2184 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
2185 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
2186 if (res) {
2187 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
2188 }
2189
2190 return res;
2191}
2192
2193
2194static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
2195 const u8 *ies, size_t ies_len)
2196{
2197 struct ieee802_11_elems elems;
2198
2199 if (ies == NULL)
2200 return;
2201
2202 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2203 return;
2204
2205 if (elems.qos_map_set) {
2206 wpas_qos_map_set(wpa_s, elems.qos_map_set,
2207 elems.qos_map_set_len);
2208 }
2209}
2210
2211#endif /* CONFIG_INTERWORKING */
2212
2213
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002214#ifdef CONFIG_FST
2215static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
2216 const u8 *ie, size_t ie_len)
2217{
2218 struct mb_ies_info mb_ies;
2219
2220 if (!ie || !ie_len || !wpa_s->fst)
2221 return -ENOENT;
2222
2223 os_memset(&mb_ies, 0, sizeof(mb_ies));
2224
2225 while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
2226 size_t len;
2227
2228 len = 2 + ie[1];
2229 if (len > ie_len) {
2230 wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
2231 ie, ie_len);
2232 break;
2233 }
2234
2235 if (ie[0] == WLAN_EID_MULTI_BAND) {
2236 wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
2237 (unsigned int) len);
2238 mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
2239 mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
2240 mb_ies.nof_ies++;
2241 }
2242
2243 ie_len -= len;
2244 ie += len;
2245 }
2246
2247 if (mb_ies.nof_ies > 0) {
2248 wpabuf_free(wpa_s->received_mb_ies);
2249 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2250 return 0;
2251 }
2252
2253 return -ENOENT;
2254}
2255#endif /* CONFIG_FST */
2256
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
2259 union wpa_event_data *data)
2260{
2261 int l, len, found = 0, wpa_found, rsn_found;
2262 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002263#ifdef CONFIG_IEEE80211R
2264 u8 bssid[ETH_ALEN];
2265#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
2267 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
2268 if (data->assoc_info.req_ies)
2269 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
2270 data->assoc_info.req_ies_len);
2271 if (data->assoc_info.resp_ies) {
2272 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
2273 data->assoc_info.resp_ies_len);
2274#ifdef CONFIG_TDLS
2275 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
2276 data->assoc_info.resp_ies_len);
2277#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002278#ifdef CONFIG_WNM
2279 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2280 data->assoc_info.resp_ies_len);
2281#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002282#ifdef CONFIG_INTERWORKING
2283 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2284 data->assoc_info.resp_ies_len);
2285#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 }
2287 if (data->assoc_info.beacon_ies)
2288 wpa_hexdump(MSG_DEBUG, "beacon_ies",
2289 data->assoc_info.beacon_ies,
2290 data->assoc_info.beacon_ies_len);
2291 if (data->assoc_info.freq)
2292 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
2293 data->assoc_info.freq);
2294
2295 p = data->assoc_info.req_ies;
2296 l = data->assoc_info.req_ies_len;
2297
2298 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
2299 while (p && l >= 2) {
2300 len = p[1] + 2;
2301 if (len > l) {
2302 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2303 p, l);
2304 break;
2305 }
2306 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2307 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
Dmitry Shmidte4663042016-04-04 10:07:49 -07002308 (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
2309 (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
2311 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
2312 break;
2313 found = 1;
2314 wpa_find_assoc_pmkid(wpa_s);
2315 break;
2316 }
2317 l -= len;
2318 p += len;
2319 }
2320 if (!found && data->assoc_info.req_ies)
2321 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2322
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002323#ifdef CONFIG_FILS
2324#ifdef CONFIG_SME
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002325 if ((wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
2326 wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002327 (!data->assoc_info.resp_frame ||
2328 fils_process_assoc_resp(wpa_s->wpa,
2329 data->assoc_info.resp_frame,
2330 data->assoc_info.resp_frame_len) < 0)) {
2331 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2332 return -1;
2333 }
2334#endif /* CONFIG_SME */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002335
2336 /* Additional processing for FILS when SME is in driver */
2337 if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
2338 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2339 wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002340#endif /* CONFIG_FILS */
2341
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002342#ifdef CONFIG_OWE
2343 if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
2344 (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2345 owe_process_assoc_resp(wpa_s->wpa, bssid,
2346 data->assoc_info.resp_ies,
2347 data->assoc_info.resp_ies_len) < 0)) {
2348 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2349 return -1;
2350 }
2351#endif /* CONFIG_OWE */
2352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353#ifdef CONFIG_IEEE80211R
2354#ifdef CONFIG_SME
2355 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2357 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2358 data->assoc_info.resp_ies,
2359 data->assoc_info.resp_ies_len,
2360 bssid) < 0) {
2361 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2362 "Reassociation Response failed");
2363 wpa_supplicant_deauthenticate(
2364 wpa_s, WLAN_REASON_INVALID_IE);
2365 return -1;
2366 }
2367 }
2368
2369 p = data->assoc_info.resp_ies;
2370 l = data->assoc_info.resp_ies_len;
2371
2372#ifdef CONFIG_WPS_STRICT
2373 if (p && wpa_s->current_ssid &&
2374 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
2375 struct wpabuf *wps;
2376 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
2377 if (wps == NULL) {
2378 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
2379 "include WPS IE in (Re)Association Response");
2380 return -1;
2381 }
2382
2383 if (wps_validate_assoc_resp(wps) < 0) {
2384 wpabuf_free(wps);
2385 wpa_supplicant_deauthenticate(
2386 wpa_s, WLAN_REASON_INVALID_IE);
2387 return -1;
2388 }
2389 wpabuf_free(wps);
2390 }
2391#endif /* CONFIG_WPS_STRICT */
2392
2393 /* Go through the IEs and make a copy of the MDIE, if present. */
2394 while (p && l >= 2) {
2395 len = p[1] + 2;
2396 if (len > l) {
2397 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2398 p, l);
2399 break;
2400 }
2401 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
2402 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
2403 wpa_s->sme.ft_used = 1;
2404 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
2405 MOBILITY_DOMAIN_ID_LEN);
2406 break;
2407 }
2408 l -= len;
2409 p += len;
2410 }
2411#endif /* CONFIG_SME */
2412
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002413 /* Process FT when SME is in the driver */
2414 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2415 wpa_ft_is_completed(wpa_s->wpa)) {
2416 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2417 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2418 data->assoc_info.resp_ies,
2419 data->assoc_info.resp_ies_len,
2420 bssid) < 0) {
2421 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2422 "Reassociation Response failed");
2423 wpa_supplicant_deauthenticate(
2424 wpa_s, WLAN_REASON_INVALID_IE);
2425 return -1;
2426 }
2427 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
2428 }
2429
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
2431 data->assoc_info.resp_ies_len);
2432#endif /* CONFIG_IEEE80211R */
2433
2434 /* WPA/RSN IE from Beacon/ProbeResp */
2435 p = data->assoc_info.beacon_ies;
2436 l = data->assoc_info.beacon_ies_len;
2437
2438 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
2439 */
2440 wpa_found = rsn_found = 0;
2441 while (p && l >= 2) {
2442 len = p[1] + 2;
2443 if (len > l) {
2444 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
2445 p, l);
2446 break;
2447 }
2448 if (!wpa_found &&
2449 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2450 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2451 wpa_found = 1;
2452 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2453 }
2454
2455 if (!rsn_found &&
2456 p[0] == WLAN_EID_RSN && p[1] >= 2) {
2457 rsn_found = 1;
2458 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2459 }
2460
2461 l -= len;
2462 p += len;
2463 }
2464
2465 if (!wpa_found && data->assoc_info.beacon_ies)
2466 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2467 if (!rsn_found && data->assoc_info.beacon_ies)
2468 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2469 if (wpa_found || rsn_found)
2470 wpa_s->ap_ies_from_associnfo = 1;
2471
Jouni Malinen87fd2792011-05-16 18:35:42 +03002472 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2473 wpa_s->assoc_freq != data->assoc_info.freq) {
2474 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2475 "%u to %u MHz",
2476 wpa_s->assoc_freq, data->assoc_info.freq);
2477 wpa_supplicant_update_scan_results(wpa_s);
2478 }
2479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 wpa_s->assoc_freq = data->assoc_info.freq;
2481
2482 return 0;
2483}
2484
2485
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002486static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2487{
2488 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2489
2490 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2491 return -1;
2492
2493 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2494 return 0;
2495
2496 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2497 WPA_IE_VENDOR_TYPE);
2498 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2499
2500 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2501 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2502 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2503 bss_rsn ? 2 + bss_rsn[1] : 0))
2504 return -1;
2505
2506 return 0;
2507}
2508
2509
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002510static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
2511 union wpa_event_data *data)
2512{
2513#ifdef CONFIG_FST
2514 struct assoc_info *ai = data ? &data->assoc_info : NULL;
2515 struct wpa_bss *bss = wpa_s->current_bss;
2516 const u8 *ieprb, *iebcn;
2517
2518 wpabuf_free(wpa_s->received_mb_ies);
2519 wpa_s->received_mb_ies = NULL;
2520
2521 if (ai &&
2522 !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
2523 wpa_printf(MSG_DEBUG,
2524 "FST: MB IEs updated from Association Response frame");
2525 return;
2526 }
2527
2528 if (ai &&
2529 !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
2530 wpa_printf(MSG_DEBUG,
2531 "FST: MB IEs updated from association event Beacon IEs");
2532 return;
2533 }
2534
2535 if (!bss)
2536 return;
2537
2538 ieprb = (const u8 *) (bss + 1);
2539 iebcn = ieprb + bss->ie_len;
2540
2541 if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
2542 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
2543 else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
2544 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
2545#endif /* CONFIG_FST */
2546}
2547
2548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2550 union wpa_event_data *data)
2551{
2552 u8 bssid[ETH_ALEN];
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002553 int ft_completed, already_authorized;
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002554 int new_bss = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555
2556#ifdef CONFIG_AP
2557 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002558 if (!data)
2559 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002560 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2561 data->assoc_info.addr,
2562 data->assoc_info.req_ies,
2563 data->assoc_info.req_ies_len,
2564 data->assoc_info.reassoc);
2565 return;
2566 }
2567#endif /* CONFIG_AP */
2568
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002569 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2572 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2573 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002574 /*
2575 * FILS authentication can share the same mechanism to mark the
2576 * connection fully authenticated, so set ft_completed also based on
2577 * FILS result.
2578 */
2579 if (!ft_completed)
2580 ft_completed = wpa_fils_is_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002582 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2583 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002584 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002585 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2586 return;
2587 }
2588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002590 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2592 MACSTR, MAC2STR(bssid));
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002593 new_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002594 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2596 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002597 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598
2599 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2600 wpa_clear_keys(wpa_s, bssid);
2601 }
2602 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002603 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2605 return;
2606 }
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002607 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002608
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002609 if (wpa_s->conf->ap_scan == 1 &&
2610 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002611 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2612 wpa_msg(wpa_s, MSG_WARNING,
2613 "WPA/RSN IEs not updated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614 }
2615
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002616 wpas_fst_update_mb_assoc(wpa_s, data);
2617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618#ifdef CONFIG_SME
2619 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2620 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002621 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622#endif /* CONFIG_SME */
2623
2624 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2625 if (wpa_s->current_ssid) {
2626 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2627 * initialized before association, but for other modes,
2628 * initialize PC/SC here, if the current configuration needs
2629 * smartcard or SIM/USIM. */
2630 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2631 }
2632 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2633 if (wpa_s->l2)
2634 l2_packet_notify_auth_start(wpa_s->l2);
2635
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002636 already_authorized = data && data->assoc_info.authorized;
2637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002638 /*
2639 * Set portEnabled first to FALSE in order to get EAP state machine out
2640 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2641 * state machine may transit to AUTHENTICATING state based on obsolete
2642 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2643 * AUTHENTICATED without ever giving chance to EAP state machine to
2644 * reset the state.
2645 */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002646 if (!ft_completed && !already_authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2648 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2649 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002650 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2651 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
2652 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002653 already_authorized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2655 /* 802.1X::portControl = Auto */
2656 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2657 wpa_s->eapol_received = 0;
2658 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2659 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2660 (wpa_s->current_ssid &&
2661 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002662 if (wpa_s->current_ssid &&
2663 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002664 (wpa_s->drv_flags &
2665 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2666 /*
2667 * Set the key after having received joined-IBSS event
2668 * from the driver.
2669 */
2670 wpa_supplicant_set_wpa_none_key(wpa_s,
2671 wpa_s->current_ssid);
2672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 wpa_supplicant_cancel_auth_timeout(wpa_s);
2674 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2675 } else if (!ft_completed) {
2676 /* Timeout for receiving the first EAPOL packet */
2677 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2678 }
2679 wpa_supplicant_cancel_scan(wpa_s);
2680
2681 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2682 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2683 /*
2684 * We are done; the driver will take care of RSN 4-way
2685 * handshake.
2686 */
2687 wpa_supplicant_cancel_auth_timeout(wpa_s);
2688 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2689 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2690 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2691 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2692 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2693 /*
2694 * The driver will take care of RSN 4-way handshake, so we need
2695 * to allow EAPOL supplicant to complete its work without
2696 * waiting for WPA supplicant.
2697 */
2698 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2699 } else if (ft_completed) {
2700 /*
2701 * FT protocol completed - make sure EAPOL state machine ends
2702 * up in authenticated.
2703 */
2704 wpa_supplicant_cancel_auth_timeout(wpa_s);
2705 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2706 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2707 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2708 }
2709
Jouni Malinena05074c2012-12-21 21:35:35 +02002710 wpa_s->last_eapol_matches_bssid = 0;
2711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002713 struct os_reltime now, age;
2714 os_get_reltime(&now);
2715 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Paul Stewart092955c2017-02-06 09:13:09 -08002716 if (age.sec == 0 && age.usec < 200000 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2718 0) {
2719 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2720 "frame that was received just before "
2721 "association notification");
2722 wpa_supplicant_rx_eapol(
2723 wpa_s, wpa_s->pending_eapol_rx_src,
2724 wpabuf_head(wpa_s->pending_eapol_rx),
2725 wpabuf_len(wpa_s->pending_eapol_rx));
2726 }
2727 wpabuf_free(wpa_s->pending_eapol_rx);
2728 wpa_s->pending_eapol_rx = NULL;
2729 }
2730
2731 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2732 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002733 wpa_s->current_ssid &&
2734 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 /* Set static WEP keys again */
2736 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2737 }
2738
2739#ifdef CONFIG_IBSS_RSN
2740 if (wpa_s->current_ssid &&
2741 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2742 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2743 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2744 wpa_s->ibss_rsn == NULL) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002745 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 if (!wpa_s->ibss_rsn) {
2747 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2748 wpa_supplicant_deauthenticate(
2749 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2750 return;
2751 }
2752
2753 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2754 }
2755#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002756
2757 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002758
2759 if (data) {
2760 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2761 data->assoc_info.resp_ies_len,
2762 &data->assoc_info.wmm_params);
2763
2764 if (wpa_s->reassoc_same_bss)
2765 wmm_ac_restore_tspecs(wpa_s);
2766 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002767
2768#ifdef CONFIG_FILS
2769 if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
2770 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid);
2771 const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
2772
2773 if (fils_cache_id)
2774 wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
2775 }
2776#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777}
2778
2779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002780static int disconnect_reason_recoverable(u16 reason_code)
2781{
2782 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2783 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2784 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2785}
2786
2787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002789 u16 reason_code,
2790 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791{
2792 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002793
2794 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2795 /*
2796 * At least Host AP driver and a Prism3 card seemed to be
2797 * generating streams of disconnected events when configuring
2798 * IBSS for WPA-None. Ignore them for now.
2799 */
2800 return;
2801 }
2802
2803 bssid = wpa_s->bssid;
2804 if (is_zero_ether_addr(bssid))
2805 bssid = wpa_s->pending_bssid;
2806
2807 if (!is_zero_ether_addr(bssid) ||
2808 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2809 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2810 " reason=%d%s",
2811 MAC2STR(bssid), reason_code,
2812 locally_generated ? " locally_generated=1" : "");
2813 }
2814}
2815
2816
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002817static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2818 int locally_generated)
2819{
2820 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2821 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2822 return 0; /* Not in 4-way handshake with PSK */
2823
2824 /*
2825 * It looks like connection was lost while trying to go through PSK
2826 * 4-way handshake. Filter out known disconnection cases that are caused
2827 * by something else than PSK mismatch to avoid confusing reports.
2828 */
2829
2830 if (locally_generated) {
2831 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2832 return 0;
2833 }
2834
2835 return 1;
2836}
2837
2838
Jouni Malinen2b89da82012-08-31 22:04:41 +03002839static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2840 u16 reason_code,
2841 int locally_generated)
2842{
2843 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844 int authenticating;
2845 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002846 struct wpa_bss *fast_reconnect = NULL;
2847 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002848 struct wpa_ssid *last_ssid;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002849 struct wpa_bss *curr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850
2851 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2852 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2853
2854 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2855 /*
2856 * At least Host AP driver and a Prism3 card seemed to be
2857 * generating streams of disconnected events when configuring
2858 * IBSS for WPA-None. Ignore them for now.
2859 */
2860 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2861 "IBSS/WPA-None mode");
2862 return;
2863 }
2864
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002865 if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
2866 reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
2867 locally_generated)
2868 /*
2869 * Remove the inactive AP (which is probably out of range) from
2870 * the BSS list after marking disassociation. In particular
2871 * mac80211-based drivers use the
2872 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
2873 * locally generated disconnection events for cases where the
2874 * AP does not reply anymore.
2875 */
2876 curr = wpa_s->current_bss;
2877
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002878 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2880 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002881 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2882 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002883 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002885 if (!wpa_s->disconnected &&
2886 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002887 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002888 wpas_wps_searching(wpa_s) ||
2889 wpas_wps_reenable_networks_pending(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002890 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002891 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002892 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002893 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002894 wpa_s->wpa_state);
2895 if (wpa_s->wpa_state == WPA_COMPLETED &&
2896 wpa_s->current_ssid &&
2897 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002898 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002899 disconnect_reason_recoverable(reason_code)) {
2900 /*
2901 * It looks like the AP has dropped association with
2902 * us, but could allow us to get back in. Try to
2903 * reconnect to the same BSS without full scan to save
2904 * time for some common cases.
2905 */
2906 fast_reconnect = wpa_s->current_bss;
2907 fast_reconnect_ssid = wpa_s->current_ssid;
2908 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002909 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002910 else
2911 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2912 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002914 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 "try to re-connect");
2916 wpa_s->reassociate = 0;
2917 wpa_s->disconnected = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002918 if (!wpa_s->pno)
2919 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 }
2921 bssid = wpa_s->bssid;
2922 if (is_zero_ether_addr(bssid))
2923 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002924 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2925 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002927 if (locally_generated)
2928 wpa_s->disconnect_reason = -reason_code;
2929 else
2930 wpa_s->disconnect_reason = reason_code;
2931 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2933 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 wpa_clear_keys(wpa_s, wpa_s->bssid);
2935 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002936 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 wpa_supplicant_mark_disassoc(wpa_s);
2938
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002939 if (curr)
2940 wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
2941
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002942 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002944 wpa_s->current_ssid = last_ssid;
2945 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002946
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002947 if (fast_reconnect &&
2948 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2949 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2950 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2951 fast_reconnect->ssid_len) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002952 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
2953 !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect->bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002954#ifndef CONFIG_NO_SCAN_PROCESSING
2955 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2956 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2957 fast_reconnect_ssid) < 0) {
2958 /* Recover through full scan */
2959 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2960 }
2961#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002962 } else if (fast_reconnect) {
2963 /*
2964 * Could not reconnect to the same BSS due to network being
2965 * disabled. Use a new scan to match the alternative behavior
2966 * above, i.e., to continue automatic reconnection attempt in a
2967 * way that enforces disabled network rules.
2968 */
2969 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971}
2972
2973
2974#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002975void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976{
2977 struct wpa_supplicant *wpa_s = eloop_ctx;
2978
2979 if (!wpa_s->pending_mic_error_report)
2980 return;
2981
2982 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2983 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2984 wpa_s->pending_mic_error_report = 0;
2985}
2986#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2987
2988
2989static void
2990wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2991 union wpa_event_data *data)
2992{
2993 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002994 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995
2996 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2997 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002998 os_get_reltime(&t);
2999 if ((wpa_s->last_michael_mic_error.sec &&
3000 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 wpa_s->pending_mic_error_report) {
3002 if (wpa_s->pending_mic_error_report) {
3003 /*
3004 * Send the pending MIC error report immediately since
3005 * we are going to start countermeasures and AP better
3006 * do the same.
3007 */
3008 wpa_sm_key_request(wpa_s->wpa, 1,
3009 wpa_s->pending_mic_error_pairwise);
3010 }
3011
3012 /* Send the new MIC error report immediately since we are going
3013 * to start countermeasures and AP better do the same.
3014 */
3015 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3016
3017 /* initialize countermeasures */
3018 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003019
3020 wpa_blacklist_add(wpa_s, wpa_s->bssid);
3021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
3023
3024 /*
3025 * Need to wait for completion of request frame. We do not get
3026 * any callback for the message completion, so just wait a
3027 * short while and hope for the best. */
3028 os_sleep(0, 10000);
3029
3030 wpa_drv_set_countermeasures(wpa_s, 1);
3031 wpa_supplicant_deauthenticate(wpa_s,
3032 WLAN_REASON_MICHAEL_MIC_FAILURE);
3033 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
3034 wpa_s, NULL);
3035 eloop_register_timeout(60, 0,
3036 wpa_supplicant_stop_countermeasures,
3037 wpa_s, NULL);
3038 /* TODO: mark the AP rejected for 60 second. STA is
3039 * allowed to associate with another AP.. */
3040 } else {
3041#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
3042 if (wpa_s->mic_errors_seen) {
3043 /*
3044 * Reduce the effectiveness of Michael MIC error
3045 * reports as a means for attacking against TKIP if
3046 * more than one MIC failure is noticed with the same
3047 * PTK. We delay the transmission of the reports by a
3048 * random time between 0 and 60 seconds in order to
3049 * force the attacker wait 60 seconds before getting
3050 * the information on whether a frame resulted in a MIC
3051 * failure.
3052 */
3053 u8 rval[4];
3054 int sec;
3055
3056 if (os_get_random(rval, sizeof(rval)) < 0)
3057 sec = os_random() % 60;
3058 else
3059 sec = WPA_GET_BE32(rval) % 60;
3060 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
3061 "report %d seconds", sec);
3062 wpa_s->pending_mic_error_report = 1;
3063 wpa_s->pending_mic_error_pairwise = pairwise;
3064 eloop_cancel_timeout(
3065 wpa_supplicant_delayed_mic_error_report,
3066 wpa_s, NULL);
3067 eloop_register_timeout(
3068 sec, os_random() % 1000000,
3069 wpa_supplicant_delayed_mic_error_report,
3070 wpa_s, NULL);
3071 } else {
3072 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3073 }
3074#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
3075 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
3076#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
3077 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003078 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 wpa_s->mic_errors_seen++;
3080}
3081
3082
3083#ifdef CONFIG_TERMINATE_ONLASTIF
3084static int any_interfaces(struct wpa_supplicant *head)
3085{
3086 struct wpa_supplicant *wpa_s;
3087
3088 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
3089 if (!wpa_s->interface_removed)
3090 return 1;
3091 return 0;
3092}
3093#endif /* CONFIG_TERMINATE_ONLASTIF */
3094
3095
3096static void
3097wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
3098 union wpa_event_data *data)
3099{
3100 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
3101 return;
3102
3103 switch (data->interface_status.ievent) {
3104 case EVENT_INTERFACE_ADDED:
3105 if (!wpa_s->interface_removed)
3106 break;
3107 wpa_s->interface_removed = 0;
3108 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
3109 if (wpa_supplicant_driver_init(wpa_s) < 0) {
3110 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
3111 "driver after interface was added");
3112 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003113
3114#ifdef CONFIG_P2P
3115 if (!wpa_s->global->p2p &&
3116 !wpa_s->global->p2p_disabled &&
3117 !wpa_s->conf->p2p_disabled &&
3118 (wpa_s->drv_flags &
3119 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3120 wpas_p2p_add_p2pdev_interface(
3121 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
3122 wpa_printf(MSG_INFO,
3123 "P2P: Failed to enable P2P Device interface");
3124 /* Try to continue without. P2P will be disabled. */
3125 }
3126#endif /* CONFIG_P2P */
3127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 break;
3129 case EVENT_INTERFACE_REMOVED:
3130 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
3131 wpa_s->interface_removed = 1;
3132 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003133 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 l2_packet_deinit(wpa_s->l2);
3135 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003136
3137#ifdef CONFIG_P2P
3138 if (wpa_s->global->p2p &&
3139 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
3140 (wpa_s->drv_flags &
3141 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
3142 wpa_dbg(wpa_s, MSG_DEBUG,
3143 "Removing P2P Device interface");
3144 wpa_supplicant_remove_iface(
3145 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
3146 0);
3147 wpa_s->global->p2p_init_wpa_s = NULL;
3148 }
3149#endif /* CONFIG_P2P */
3150
Dmitry Shmidte4663042016-04-04 10:07:49 -07003151#ifdef CONFIG_MATCH_IFACE
3152 if (wpa_s->matched) {
3153 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
3154 break;
3155 }
3156#endif /* CONFIG_MATCH_IFACE */
3157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158#ifdef CONFIG_TERMINATE_ONLASTIF
3159 /* check if last interface */
3160 if (!any_interfaces(wpa_s->global->ifaces))
3161 eloop_terminate();
3162#endif /* CONFIG_TERMINATE_ONLASTIF */
3163 break;
3164 }
3165}
3166
3167
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168#ifdef CONFIG_TDLS
3169static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
3170 union wpa_event_data *data)
3171{
3172 if (data == NULL)
3173 return;
3174 switch (data->tdls.oper) {
3175 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003176 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
3177 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3178 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
3179 else
3180 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 break;
3182 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03003183 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3184 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
3185 data->tdls.reason_code);
3186 else
3187 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
3188 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003190 case TDLS_REQUEST_DISCOVER:
3191 wpa_tdls_send_discovery_request(wpa_s->wpa,
3192 data->tdls.peer);
3193 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 }
3195}
3196#endif /* CONFIG_TDLS */
3197
3198
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003199#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003200static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
3201 union wpa_event_data *data)
3202{
3203 if (data == NULL)
3204 return;
3205 switch (data->wnm.oper) {
3206 case WNM_OPER_SLEEP:
3207 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
3208 "(action=%d, intval=%d)",
3209 data->wnm.sleep_action, data->wnm.sleep_intval);
3210 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003211 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003212 break;
3213 }
3214}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003215#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003216
3217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218#ifdef CONFIG_IEEE80211R
3219static void
3220wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
3221 union wpa_event_data *data)
3222{
3223 if (data == NULL)
3224 return;
3225
3226 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
3227 data->ft_ies.ies_len,
3228 data->ft_ies.ft_action,
3229 data->ft_ies.target_ap,
3230 data->ft_ies.ric_ies,
3231 data->ft_ies.ric_ies_len) < 0) {
3232 /* TODO: prevent MLME/driver from trying to associate? */
3233 }
3234}
3235#endif /* CONFIG_IEEE80211R */
3236
3237
3238#ifdef CONFIG_IBSS_RSN
3239static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
3240 union wpa_event_data *data)
3241{
3242 struct wpa_ssid *ssid;
3243 if (wpa_s->wpa_state < WPA_ASSOCIATED)
3244 return;
3245 if (data == NULL)
3246 return;
3247 ssid = wpa_s->current_ssid;
3248 if (ssid == NULL)
3249 return;
3250 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3251 return;
3252
3253 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
3254}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003255
3256
3257static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
3258 union wpa_event_data *data)
3259{
3260 struct wpa_ssid *ssid = wpa_s->current_ssid;
3261
3262 if (ssid == NULL)
3263 return;
3264
3265 /* check if the ssid is correctly configured as IBSS/RSN */
3266 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3267 return;
3268
3269 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
3270 data->rx_mgmt.frame_len);
3271}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272#endif /* CONFIG_IBSS_RSN */
3273
3274
3275#ifdef CONFIG_IEEE80211R
3276static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
3277 size_t len)
3278{
3279 const u8 *sta_addr, *target_ap_addr;
3280 u16 status;
3281
3282 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
3283 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
3284 return; /* only SME case supported for now */
3285 if (len < 1 + 2 * ETH_ALEN + 2)
3286 return;
3287 if (data[0] != 2)
3288 return; /* Only FT Action Response is supported for now */
3289 sta_addr = data + 1;
3290 target_ap_addr = data + 1 + ETH_ALEN;
3291 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
3292 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
3293 MACSTR " TargetAP " MACSTR " status %u",
3294 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
3295
3296 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
3297 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
3298 " in FT Action Response", MAC2STR(sta_addr));
3299 return;
3300 }
3301
3302 if (status) {
3303 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
3304 "failure (status code %d)", status);
3305 /* TODO: report error to FT code(?) */
3306 return;
3307 }
3308
3309 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
3310 len - (1 + 2 * ETH_ALEN + 2), 1,
3311 target_ap_addr, NULL, 0) < 0)
3312 return;
3313
3314#ifdef CONFIG_SME
3315 {
3316 struct wpa_bss *bss;
3317 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
3318 if (bss)
3319 wpa_s->sme.freq = bss->freq;
3320 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
3321 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
3322 WLAN_AUTH_FT);
3323 }
3324#endif /* CONFIG_SME */
3325}
3326#endif /* CONFIG_IEEE80211R */
3327
3328
3329static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
3330 struct unprot_deauth *e)
3331{
3332#ifdef CONFIG_IEEE80211W
3333 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
3334 "dropped: " MACSTR " -> " MACSTR
3335 " (reason code %u)",
3336 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3337 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3338#endif /* CONFIG_IEEE80211W */
3339}
3340
3341
3342static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
3343 struct unprot_disassoc *e)
3344{
3345#ifdef CONFIG_IEEE80211W
3346 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
3347 "dropped: " MACSTR " -> " MACSTR
3348 " (reason code %u)",
3349 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3350 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3351#endif /* CONFIG_IEEE80211W */
3352}
3353
3354
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003355static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
3356 u16 reason_code, int locally_generated,
3357 const u8 *ie, size_t ie_len, int deauth)
3358{
3359#ifdef CONFIG_AP
3360 if (wpa_s->ap_iface && addr) {
3361 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
3362 return;
3363 }
3364
3365 if (wpa_s->ap_iface) {
3366 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
3367 return;
3368 }
3369#endif /* CONFIG_AP */
3370
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003371 if (!locally_generated)
3372 wpa_s->own_disconnect_req = 0;
3373
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003374 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
3375
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003376 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
3377 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
3378 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
3379 eapol_sm_failed(wpa_s->eapol))) &&
3380 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003381 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003382
3383#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003384 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003385 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
3386 locally_generated) > 0) {
3387 /*
3388 * The interface was removed, so cannot continue
3389 * processing any additional operations after this.
3390 */
3391 return;
3392 }
3393 }
3394#endif /* CONFIG_P2P */
3395
3396 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
3397 locally_generated);
3398}
3399
3400
3401static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
3402 struct disassoc_info *info)
3403{
3404 u16 reason_code = 0;
3405 int locally_generated = 0;
3406 const u8 *addr = NULL;
3407 const u8 *ie = NULL;
3408 size_t ie_len = 0;
3409
3410 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
3411
3412 if (info) {
3413 addr = info->addr;
3414 ie = info->ie;
3415 ie_len = info->ie_len;
3416 reason_code = info->reason_code;
3417 locally_generated = info->locally_generated;
3418 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
3419 locally_generated ? " (locally generated)" : "");
3420 if (addr)
3421 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3422 MAC2STR(addr));
3423 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
3424 ie, ie_len);
3425 }
3426
3427#ifdef CONFIG_AP
3428 if (wpa_s->ap_iface && info && info->addr) {
3429 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
3430 return;
3431 }
3432
3433 if (wpa_s->ap_iface) {
3434 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
3435 return;
3436 }
3437#endif /* CONFIG_AP */
3438
3439#ifdef CONFIG_P2P
3440 if (info) {
3441 wpas_p2p_disassoc_notif(
3442 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
3443 locally_generated);
3444 }
3445#endif /* CONFIG_P2P */
3446
3447 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3448 sme_event_disassoc(wpa_s, info);
3449
3450 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
3451 ie, ie_len, 0);
3452}
3453
3454
3455static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
3456 struct deauth_info *info)
3457{
3458 u16 reason_code = 0;
3459 int locally_generated = 0;
3460 const u8 *addr = NULL;
3461 const u8 *ie = NULL;
3462 size_t ie_len = 0;
3463
3464 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
3465
3466 if (info) {
3467 addr = info->addr;
3468 ie = info->ie;
3469 ie_len = info->ie_len;
3470 reason_code = info->reason_code;
3471 locally_generated = info->locally_generated;
3472 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
3473 reason_code,
3474 locally_generated ? " (locally generated)" : "");
3475 if (addr) {
3476 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3477 MAC2STR(addr));
3478 }
3479 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
3480 ie, ie_len);
3481 }
3482
3483 wpa_reset_ft_completed(wpa_s->wpa);
3484
3485 wpas_event_disconnect(wpa_s, addr, reason_code,
3486 locally_generated, ie, ie_len, 1);
3487}
3488
3489
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003490static const char * reg_init_str(enum reg_change_initiator init)
3491{
3492 switch (init) {
3493 case REGDOM_SET_BY_CORE:
3494 return "CORE";
3495 case REGDOM_SET_BY_USER:
3496 return "USER";
3497 case REGDOM_SET_BY_DRIVER:
3498 return "DRIVER";
3499 case REGDOM_SET_BY_COUNTRY_IE:
3500 return "COUNTRY_IE";
3501 case REGDOM_BEACON_HINT:
3502 return "BEACON_HINT";
3503 }
3504 return "?";
3505}
3506
3507
3508static const char * reg_type_str(enum reg_type type)
3509{
3510 switch (type) {
3511 case REGDOM_TYPE_UNKNOWN:
3512 return "UNKNOWN";
3513 case REGDOM_TYPE_COUNTRY:
3514 return "COUNTRY";
3515 case REGDOM_TYPE_WORLD:
3516 return "WORLD";
3517 case REGDOM_TYPE_CUSTOM_WORLD:
3518 return "CUSTOM_WORLD";
3519 case REGDOM_TYPE_INTERSECTION:
3520 return "INTERSECTION";
3521 }
3522 return "?";
3523}
3524
3525
3526static void wpa_supplicant_update_channel_list(
3527 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003528{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003529 struct wpa_supplicant *ifs;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003530 u8 dfs_domain;
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003531
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003532 /*
3533 * To allow backwards compatibility with higher level layers that
3534 * assumed the REGDOM_CHANGE event is sent over the initially added
3535 * interface. Find the highest parent of this interface and use it to
3536 * send the event.
3537 */
3538 for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
3539 ;
3540
3541 wpa_msg(ifs, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003542 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003543 info->alpha2[0] ? " alpha2=" : "",
3544 info->alpha2[0] ? info->alpha2 : "");
3545
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003546 if (wpa_s->drv_priv == NULL)
3547 return; /* Ignore event during drv initialization */
3548
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003549 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3550 radio_list) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003551 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3552 ifs->ifname);
3553 free_hw_features(ifs);
3554 ifs->hw.modes = wpa_drv_get_hw_feature_data(
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003555 ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003556
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003557 /* Restart PNO/sched_scan with updated channel list */
3558 if (ifs->pno) {
3559 wpas_stop_pno(ifs);
3560 wpas_start_pno(ifs);
3561 } else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
3562 wpa_dbg(ifs, MSG_DEBUG,
3563 "Channel list changed - restart sched_scan");
3564 wpas_scan_restart_sched_scan(ifs);
3565 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003566 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003567
3568 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003569}
3570
3571
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003572static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003573 const u8 *frame, size_t len, int freq,
3574 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003575{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003576 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003577 const u8 *payload;
3578 size_t plen;
3579 u8 category;
3580
3581 if (len < IEEE80211_HDRLEN + 2)
3582 return;
3583
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003584 mgmt = (const struct ieee80211_mgmt *) frame;
3585 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003586 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003587 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003588
3589 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3590 " Category=%u DataLen=%d freq=%d MHz",
3591 MAC2STR(mgmt->sa), category, (int) plen, freq);
3592
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003593 if (category == WLAN_ACTION_WMM) {
3594 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3595 return;
3596 }
3597
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003598#ifdef CONFIG_IEEE80211R
3599 if (category == WLAN_ACTION_FT) {
3600 ft_rx_action(wpa_s, payload, plen);
3601 return;
3602 }
3603#endif /* CONFIG_IEEE80211R */
3604
3605#ifdef CONFIG_IEEE80211W
3606#ifdef CONFIG_SME
3607 if (category == WLAN_ACTION_SA_QUERY) {
3608 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3609 return;
3610 }
3611#endif /* CONFIG_SME */
3612#endif /* CONFIG_IEEE80211W */
3613
3614#ifdef CONFIG_WNM
3615 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3616 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3617 return;
3618 }
3619#endif /* CONFIG_WNM */
3620
3621#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003622 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3623 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003624 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003625 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003626 payload, plen, freq) == 0)
3627 return;
3628#endif /* CONFIG_GAS */
3629
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003630#ifdef CONFIG_GAS_SERVER
3631 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3632 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
3633 gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
3634 mgmt->u.action.category,
3635 payload, plen, freq) == 0)
3636 return;
3637#endif /* CONFIG_GAS_SERVER */
3638
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003639#ifdef CONFIG_TDLS
3640 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3641 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3642 wpa_dbg(wpa_s, MSG_DEBUG,
3643 "TDLS: Received Discovery Response from " MACSTR,
3644 MAC2STR(mgmt->sa));
3645 return;
3646 }
3647#endif /* CONFIG_TDLS */
3648
3649#ifdef CONFIG_INTERWORKING
3650 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3651 payload[0] == QOS_QOS_MAP_CONFIG) {
3652 const u8 *pos = payload + 1;
3653 size_t qlen = plen - 1;
3654 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3655 MACSTR, MAC2STR(mgmt->sa));
3656 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3657 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3658 pos[1] <= qlen - 2 && pos[1] >= 16)
3659 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3660 return;
3661 }
3662#endif /* CONFIG_INTERWORKING */
3663
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003664 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003665 payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
3666 wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003667 mgmt->da,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003668 payload + 1,
3669 plen - 1);
3670 return;
3671 }
3672
3673 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003674 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3675 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3676 return;
3677 }
3678
3679 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3680 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3681 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3682 payload + 1, plen - 1,
3683 rssi);
3684 return;
3685 }
3686
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003687#ifdef CONFIG_FST
3688 if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3689 fst_rx_action(wpa_s->fst, mgmt, len);
3690 return;
3691 }
3692#endif /* CONFIG_FST */
3693
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003694#ifdef CONFIG_DPP
3695 if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
3696 payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
3697 WPA_GET_BE24(&payload[1]) == OUI_WFA &&
3698 payload[4] == DPP_OUI_TYPE) {
3699 payload++;
3700 plen--;
3701 wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
3702 return;
3703 }
3704#endif /* CONFIG_DPP */
3705
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003706 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3707 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003708 if (wpa_s->ifmsh)
3709 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003710}
3711
3712
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003713static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3714 union wpa_event_data *event)
3715{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003716 struct wpa_freq_range_list *list;
3717 char *str = NULL;
3718
3719 list = &event->freq_range;
3720
3721 if (list->num)
3722 str = freq_range_list_str(list);
3723 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3724 str ? str : "");
3725
3726#ifdef CONFIG_P2P
3727 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3728 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3729 __func__);
3730 } else {
3731 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003733 /*
3734 * The update channel flow will also take care of moving a GO
3735 * from the unsafe frequency if needed.
3736 */
3737 wpas_p2p_update_channel_list(wpa_s,
3738 WPAS_P2P_CHANNEL_UPDATE_AVOID);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003739 }
3740#endif /* CONFIG_P2P */
3741
3742 os_free(str);
3743}
3744
3745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003746static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3747 union wpa_event_data *data)
3748{
3749 wpa_dbg(wpa_s, MSG_DEBUG,
3750 "Connection authorized by device, previous state %d",
3751 wpa_s->wpa_state);
3752 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3753 wpa_supplicant_cancel_auth_timeout(wpa_s);
3754 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3755 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3756 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3757 }
3758 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3759 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003760 data->assoc_info.ptk_kck_len,
3761 data->assoc_info.ptk_kek,
3762 data->assoc_info.ptk_kek_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003763#ifdef CONFIG_FILS
3764 if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
3765 struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
3766 const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
3767
3768 /* Update ERP next sequence number */
3769 eapol_sm_update_erp_next_seq_num(
3770 wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
3771
3772 if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
3773 /* Add the new PMK and PMKID to the PMKSA cache */
3774 wpa_sm_pmksa_cache_add(wpa_s->wpa,
3775 data->assoc_info.fils_pmk,
3776 data->assoc_info.fils_pmk_len,
3777 data->assoc_info.fils_pmkid,
3778 wpa_s->bssid, fils_cache_id);
3779 } else if (data->assoc_info.fils_pmkid) {
3780 /* Update the current PMKSA used for this connection */
3781 pmksa_cache_set_current(wpa_s->wpa,
3782 data->assoc_info.fils_pmkid,
3783 NULL, NULL, 0, NULL);
3784 }
3785 }
3786#endif /* CONFIG_FILS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003787}
3788
3789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003790void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3791 union wpa_event_data *data)
3792{
3793 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003794 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795
3796 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3797 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003798 event != EVENT_INTERFACE_STATUS &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003799 event != EVENT_SCAN_RESULTS &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 event != EVENT_SCHED_SCAN_STOPPED) {
3801 wpa_dbg(wpa_s, MSG_DEBUG,
3802 "Ignore event %s (%d) while interface is disabled",
3803 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 return;
3805 }
3806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003807#ifndef CONFIG_NO_STDOUT_DEBUG
3808{
3809 int level = MSG_DEBUG;
3810
Dmitry Shmidt04949592012-07-19 12:16:46 -07003811 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003812 const struct ieee80211_hdr *hdr;
3813 u16 fc;
3814 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3815 fc = le_to_host16(hdr->frame_control);
3816 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3817 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3818 level = MSG_EXCESSIVE;
3819 }
3820
3821 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3822 event_to_string(event), event);
3823}
3824#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825
3826 switch (event) {
3827 case EVENT_AUTH:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003828#ifdef CONFIG_FST
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003829 if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
3830 data->auth.ies_len))
3831 wpa_printf(MSG_DEBUG,
3832 "FST: MB IEs updated from auth IE");
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003833#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 sme_event_auth(wpa_s, data);
3835 break;
3836 case EVENT_ASSOC:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003837#ifdef CONFIG_TESTING_OPTIONS
3838 if (wpa_s->ignore_auth_resp) {
3839 wpa_printf(MSG_INFO,
3840 "EVENT_ASSOC - ignore_auth_resp active!");
3841 break;
3842 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003843 if (wpa_s->testing_resend_assoc) {
3844 wpa_printf(MSG_INFO,
3845 "EVENT_DEAUTH - testing_resend_assoc");
3846 break;
3847 }
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003848#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003850 if (data &&
3851 (data->assoc_info.authorized ||
3852 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
3853 wpa_fils_is_completed(wpa_s->wpa))))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003854 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003855 if (data) {
3856 wpa_msg(wpa_s, MSG_INFO,
3857 WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
3858 data->assoc_info.subnet_status);
3859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 break;
3861 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003862 wpas_event_disassoc(wpa_s,
3863 data ? &data->disassoc_info : NULL);
3864 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865 case EVENT_DEAUTH:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003866#ifdef CONFIG_TESTING_OPTIONS
3867 if (wpa_s->ignore_auth_resp) {
3868 wpa_printf(MSG_INFO,
3869 "EVENT_DEAUTH - ignore_auth_resp active!");
3870 break;
3871 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003872 if (wpa_s->testing_resend_assoc) {
3873 wpa_printf(MSG_INFO,
3874 "EVENT_DEAUTH - testing_resend_assoc");
3875 break;
3876 }
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003877#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003878 wpas_event_deauth(wpa_s,
3879 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 break;
3881 case EVENT_MICHAEL_MIC_FAILURE:
3882 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3883 break;
3884#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003885 case EVENT_SCAN_STARTED:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003886 if (wpa_s->own_scan_requested ||
3887 (data && !data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003888 struct os_reltime diff;
3889
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003890 os_get_reltime(&wpa_s->scan_start_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003891 os_reltime_sub(&wpa_s->scan_start_time,
3892 &wpa_s->scan_trigger_time, &diff);
3893 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3894 diff.sec, diff.usec);
3895 wpa_s->own_scan_requested = 0;
3896 wpa_s->own_scan_running = 1;
3897 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3898 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003899 wpa_msg_ctrl(wpa_s, MSG_INFO,
3900 WPA_EVENT_SCAN_STARTED "id=%u",
3901 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003902 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003903 wpa_msg_ctrl(wpa_s, MSG_INFO,
3904 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003905 }
3906 } else {
3907 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003908 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003909 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003910 }
3911 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 case EVENT_SCAN_RESULTS:
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003913 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3914 wpa_s->scan_res_handler = NULL;
3915 wpa_s->own_scan_running = 0;
3916 wpa_s->radio->external_scan_running = 0;
3917 wpa_s->last_scan_req = NORMAL_SCAN_REQ;
3918 break;
3919 }
3920
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003921 if (!(data && data->scan_info.external_scan) &&
3922 os_reltime_initialized(&wpa_s->scan_start_time)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003923 struct os_reltime now, diff;
3924 os_get_reltime(&now);
3925 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3926 wpa_s->scan_start_time.sec = 0;
3927 wpa_s->scan_start_time.usec = 0;
3928 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3929 diff.sec, diff.usec);
3930 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003931 if (wpa_supplicant_event_scan_results(wpa_s, data))
3932 break; /* interface may have been removed */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003933 if (!(data && data->scan_info.external_scan))
3934 wpa_s->own_scan_running = 0;
3935 if (data && data->scan_info.nl_scan_event)
3936 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003937 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003938 break;
3939#endif /* CONFIG_NO_SCAN_PROCESSING */
3940 case EVENT_ASSOCINFO:
3941 wpa_supplicant_event_associnfo(wpa_s, data);
3942 break;
3943 case EVENT_INTERFACE_STATUS:
3944 wpa_supplicant_event_interface_status(wpa_s, data);
3945 break;
3946 case EVENT_PMKID_CANDIDATE:
3947 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3948 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003949#ifdef CONFIG_TDLS
3950 case EVENT_TDLS:
3951 wpa_supplicant_event_tdls(wpa_s, data);
3952 break;
3953#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003954#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003955 case EVENT_WNM:
3956 wpa_supplicant_event_wnm(wpa_s, data);
3957 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003958#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959#ifdef CONFIG_IEEE80211R
3960 case EVENT_FT_RESPONSE:
3961 wpa_supplicant_event_ft_response(wpa_s, data);
3962 break;
3963#endif /* CONFIG_IEEE80211R */
3964#ifdef CONFIG_IBSS_RSN
3965 case EVENT_IBSS_RSN_START:
3966 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3967 break;
3968#endif /* CONFIG_IBSS_RSN */
3969 case EVENT_ASSOC_REJECT:
3970 if (data->assoc_reject.bssid)
3971 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003972 "bssid=" MACSTR " status_code=%u%s%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 MAC2STR(data->assoc_reject.bssid),
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003974 data->assoc_reject.status_code,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003975 data->assoc_reject.timed_out ? " timeout" : "",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003976 data->assoc_reject.timeout_reason ? "=" : "",
3977 data->assoc_reject.timeout_reason ?
3978 data->assoc_reject.timeout_reason : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 else
3980 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003981 "status_code=%u%s%s%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003982 data->assoc_reject.status_code,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003983 data->assoc_reject.timed_out ? " timeout" : "",
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003984 data->assoc_reject.timeout_reason ? "=" : "",
3985 data->assoc_reject.timeout_reason ?
3986 data->assoc_reject.timeout_reason : "");
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003987 wpa_s->assoc_status_code = data->assoc_reject.status_code;
Roshan Pius807bc582017-03-07 14:59:48 -08003988 wpa_s->assoc_timed_out = data->assoc_reject.timed_out;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003989 wpas_notify_assoc_status_code(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3991 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003992 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003993 const u8 *bssid = data->assoc_reject.bssid;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003994
3995#ifdef CONFIG_FILS
3996 /* Update ERP next sequence number */
3997 if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS)
3998 eapol_sm_update_erp_next_seq_num(
3999 wpa_s->eapol,
4000 data->assoc_reject.fils_erp_next_seq_num);
4001#endif /* CONFIG_FILS */
4002
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004003 if (bssid == NULL || is_zero_ether_addr(bssid))
4004 bssid = wpa_s->pending_bssid;
4005 wpas_connection_failed(wpa_s, bssid);
4006 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004007 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 break;
4009 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004010 /* It is possible to get this event from earlier connection */
4011 if (wpa_s->current_ssid &&
4012 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
4013 wpa_dbg(wpa_s, MSG_DEBUG,
4014 "Ignore AUTH_TIMED_OUT in mesh configuration");
4015 break;
4016 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
4018 sme_event_auth_timed_out(wpa_s, data);
4019 break;
4020 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004021 /* It is possible to get this event from earlier connection */
4022 if (wpa_s->current_ssid &&
4023 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
4024 wpa_dbg(wpa_s, MSG_DEBUG,
4025 "Ignore ASSOC_TIMED_OUT in mesh configuration");
4026 break;
4027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
4029 sme_event_assoc_timed_out(wpa_s, data);
4030 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031 case EVENT_TX_STATUS:
4032 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
4033 " type=%d stype=%d",
4034 MAC2STR(data->tx_status.dst),
4035 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004036#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004037 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004038#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
4040 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004041 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 wpa_s, data->tx_status.dst,
4043 data->tx_status.data,
4044 data->tx_status.data_len,
4045 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046 OFFCHANNEL_SEND_ACTION_SUCCESS :
4047 OFFCHANNEL_SEND_ACTION_NO_ACK);
4048#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 break;
4050 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004051#endif /* CONFIG_AP */
4052#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004053 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004054 MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 /*
4056 * Catch TX status events for Action frames we sent via group
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004057 * interface in GO mode, or via standalone AP interface.
4058 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
4059 * except when the primary interface is used as a GO interface
4060 * (for drivers which do not have group interface concurrency)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 */
4062 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
4063 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004064 os_memcmp(wpa_s->p2pdev->pending_action_dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004065 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004066 offchannel_send_action_tx_status(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07004067 wpa_s->p2pdev, data->tx_status.dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 data->tx_status.data,
4069 data->tx_status.data_len,
4070 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004071 OFFCHANNEL_SEND_ACTION_SUCCESS :
4072 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 break;
4074 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004075#endif /* CONFIG_OFFCHANNEL */
4076#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 switch (data->tx_status.type) {
4078 case WLAN_FC_TYPE_MGMT:
4079 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
4080 data->tx_status.data_len,
4081 data->tx_status.stype,
4082 data->tx_status.ack);
4083 break;
4084 case WLAN_FC_TYPE_DATA:
4085 ap_tx_status(wpa_s, data->tx_status.dst,
4086 data->tx_status.data,
4087 data->tx_status.data_len,
4088 data->tx_status.ack);
4089 break;
4090 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004091#endif /* CONFIG_AP */
4092 break;
4093#ifdef CONFIG_AP
4094 case EVENT_EAPOL_TX_STATUS:
4095 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
4096 data->eapol_tx_status.data,
4097 data->eapol_tx_status.data_len,
4098 data->eapol_tx_status.ack);
4099 break;
4100 case EVENT_DRIVER_CLIENT_POLL_OK:
4101 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 break;
4103 case EVENT_RX_FROM_UNKNOWN:
4104 if (wpa_s->ap_iface == NULL)
4105 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004106 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
4107 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004109 case EVENT_CH_SWITCH:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004110 if (!data || !wpa_s->current_ssid)
Dmitry Shmidt04949592012-07-19 12:16:46 -07004111 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004112
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07004113 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
4114 "freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
4115 data->ch_switch.freq,
4116 data->ch_switch.ht_enabled,
4117 data->ch_switch.ch_offset,
4118 channel_width_to_string(data->ch_switch.ch_width),
4119 data->ch_switch.cf1,
4120 data->ch_switch.cf2);
4121
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004122 wpa_s->assoc_freq = data->ch_switch.freq;
4123 wpa_s->current_ssid->frequency = data->ch_switch.freq;
4124
4125 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4126 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
4127 wpa_s->current_ssid->mode ==
4128 WPAS_MODE_P2P_GROUP_FORMATION) {
4129 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
4130 data->ch_switch.ht_enabled,
4131 data->ch_switch.ch_offset,
4132 data->ch_switch.ch_width,
4133 data->ch_switch.cf1,
4134 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004135 }
4136
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004137 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004138 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004139#ifdef NEED_AP_MLME
4140 case EVENT_DFS_RADAR_DETECTED:
4141 if (data)
4142 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
4143 break;
4144 case EVENT_DFS_CAC_STARTED:
4145 if (data)
4146 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
4147 break;
4148 case EVENT_DFS_CAC_FINISHED:
4149 if (data)
4150 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
4151 break;
4152 case EVENT_DFS_CAC_ABORTED:
4153 if (data)
4154 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
4155 break;
4156 case EVENT_DFS_NOP_FINISHED:
4157 if (data)
4158 wpas_event_dfs_cac_nop_finished(wpa_s,
4159 &data->dfs_event);
4160 break;
4161#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004162#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004163 case EVENT_RX_MGMT: {
4164 u16 fc, stype;
4165 const struct ieee80211_mgmt *mgmt;
4166
Dmitry Shmidt818ea482014-03-10 13:15:21 -07004167#ifdef CONFIG_TESTING_OPTIONS
4168 if (wpa_s->ext_mgmt_frame_handling) {
4169 struct rx_mgmt *rx = &data->rx_mgmt;
4170 size_t hex_len = 2 * rx->frame_len + 1;
4171 char *hex = os_malloc(hex_len);
4172 if (hex) {
4173 wpa_snprintf_hex(hex, hex_len,
4174 rx->frame, rx->frame_len);
4175 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
4176 rx->freq, rx->datarate, rx->ssi_signal,
4177 hex);
4178 os_free(hex);
4179 }
4180 break;
4181 }
4182#endif /* CONFIG_TESTING_OPTIONS */
4183
Dmitry Shmidt04949592012-07-19 12:16:46 -07004184 mgmt = (const struct ieee80211_mgmt *)
4185 data->rx_mgmt.frame;
4186 fc = le_to_host16(mgmt->frame_control);
4187 stype = WLAN_FC_GET_STYPE(fc);
4188
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004189#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004191#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004193 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07004194 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195 const u8 *src = mgmt->sa;
Dmitry Shmidte4663042016-04-04 10:07:49 -07004196 const u8 *ie;
4197 size_t ie_len;
4198
4199 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4200 ie_len = data->rx_mgmt.frame_len -
4201 IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004202 wpas_p2p_probe_req_rx(
4203 wpa_s, src, mgmt->da,
4204 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004205 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004206 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 break;
4208 }
4209#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004210#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004211 if (wpa_s->current_ssid &&
4212 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
4213 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004214 data->rx_mgmt.frame_len >= 30) {
4215 wpa_supplicant_event_ibss_auth(wpa_s, data);
4216 break;
4217 }
4218#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004219
4220 if (stype == WLAN_FC_STYPE_ACTION) {
4221 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07004222 wpa_s, data->rx_mgmt.frame,
4223 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004224 data->rx_mgmt.freq,
4225 data->rx_mgmt.ssi_signal);
4226 break;
4227 }
4228
4229 if (wpa_s->ifmsh) {
4230 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004231 break;
4232 }
4233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
4235 "management frame in non-AP mode");
4236 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004237#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004238 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004239
4240 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07004241 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
4242 const u8 *ie;
4243 size_t ie_len;
4244
4245 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4246 ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004247
4248 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
4249 mgmt->bssid, ie, ie_len,
4250 data->rx_mgmt.ssi_signal);
4251 }
4252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004254#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004255 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004257 case EVENT_RX_PROBE_REQ:
4258 if (data->rx_probe_req.sa == NULL ||
4259 data->rx_probe_req.ie == NULL)
4260 break;
4261#ifdef CONFIG_AP
4262 if (wpa_s->ap_iface) {
4263 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
4264 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004265 data->rx_probe_req.da,
4266 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004268 data->rx_probe_req.ie_len,
4269 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 break;
4271 }
4272#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004274 data->rx_probe_req.da,
4275 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004276 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004277 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004278 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004279 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004281 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004282#ifdef CONFIG_OFFCHANNEL
4283 offchannel_remain_on_channel_cb(
4284 wpa_s, data->remain_on_channel.freq,
4285 data->remain_on_channel.duration);
4286#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 wpas_p2p_remain_on_channel_cb(
4288 wpa_s, data->remain_on_channel.freq,
4289 data->remain_on_channel.duration);
4290 break;
4291 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004292#ifdef CONFIG_OFFCHANNEL
4293 offchannel_cancel_remain_on_channel_cb(
4294 wpa_s, data->remain_on_channel.freq);
4295#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296 wpas_p2p_cancel_remain_on_channel_cb(
4297 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004298#ifdef CONFIG_DPP
4299 wpas_dpp_cancel_remain_on_channel_cb(
4300 wpa_s, data->remain_on_channel.freq);
4301#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 case EVENT_EAPOL_RX:
4304 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
4305 data->eapol_rx.data,
4306 data->eapol_rx.data_len);
4307 break;
4308 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004309 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
4310 "above=%d signal=%d noise=%d txrate=%d",
4311 data->signal_change.above_threshold,
4312 data->signal_change.current_signal,
4313 data->signal_change.current_noise,
4314 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004315 wpa_bss_update_level(wpa_s->current_bss,
4316 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 bgscan_notify_signal_change(
4318 wpa_s, data->signal_change.above_threshold,
4319 data->signal_change.current_signal,
4320 data->signal_change.current_noise,
4321 data->signal_change.current_txrate);
4322 break;
4323 case EVENT_INTERFACE_ENABLED:
4324 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
4325 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004326 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004327 if (wpa_s->p2p_mgmt) {
4328 wpa_supplicant_set_state(wpa_s,
4329 WPA_DISCONNECTED);
4330 break;
4331 }
4332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333#ifdef CONFIG_AP
4334 if (!wpa_s->ap_iface) {
4335 wpa_supplicant_set_state(wpa_s,
4336 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004337 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 wpa_supplicant_req_scan(wpa_s, 0, 0);
4339 } else
4340 wpa_supplicant_set_state(wpa_s,
4341 WPA_COMPLETED);
4342#else /* CONFIG_AP */
4343 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4344 wpa_supplicant_req_scan(wpa_s, 0, 0);
4345#endif /* CONFIG_AP */
4346 }
4347 break;
4348 case EVENT_INTERFACE_DISABLED:
4349 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004350#ifdef CONFIG_P2P
4351 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
4352 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
4353 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
4354 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004355 * Mark interface disabled if this happens to end up not
4356 * being removed as a separate P2P group interface.
4357 */
4358 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4359 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004360 * The interface was externally disabled. Remove
4361 * it assuming an external entity will start a
4362 * new session if needed.
4363 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004364 if (wpa_s->current_ssid &&
4365 wpa_s->current_ssid->p2p_group)
4366 wpas_p2p_interface_unavailable(wpa_s);
4367 else
4368 wpas_p2p_disconnect(wpa_s);
4369 /*
4370 * wpa_s instance may have been freed, so must not use
4371 * it here anymore.
4372 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004373 break;
4374 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004375 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
4376 p2p_in_progress(wpa_s->global->p2p) > 1) {
4377 /* This radio work will be cancelled, so clear P2P
4378 * state as well.
4379 */
4380 p2p_stop_find(wpa_s->global->p2p);
4381 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004382#endif /* CONFIG_P2P */
4383
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004384 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4385 /*
4386 * Indicate disconnection to keep ctrl_iface events
4387 * consistent.
4388 */
4389 wpa_supplicant_event_disassoc(
4390 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
4391 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004392 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004393 wpa_bss_flush(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004394 radio_remove_works(wpa_s, NULL, 0);
4395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4397 break;
4398 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004399 wpa_supplicant_update_channel_list(
4400 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 break;
4402 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004403 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 break;
4405 case EVENT_BEST_CHANNEL:
4406 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
4407 "(%d %d %d)",
4408 data->best_chan.freq_24, data->best_chan.freq_5,
4409 data->best_chan.freq_overall);
4410 wpa_s->best_24_freq = data->best_chan.freq_24;
4411 wpa_s->best_5_freq = data->best_chan.freq_5;
4412 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
4414 data->best_chan.freq_5,
4415 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 break;
4417 case EVENT_UNPROT_DEAUTH:
4418 wpa_supplicant_event_unprot_deauth(wpa_s,
4419 &data->unprot_deauth);
4420 break;
4421 case EVENT_UNPROT_DISASSOC:
4422 wpa_supplicant_event_unprot_disassoc(wpa_s,
4423 &data->unprot_disassoc);
4424 break;
4425 case EVENT_STATION_LOW_ACK:
4426#ifdef CONFIG_AP
4427 if (wpa_s->ap_iface && data)
4428 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
4429 data->low_ack.addr);
4430#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004431#ifdef CONFIG_TDLS
4432 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004433 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
4434 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004435#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 break;
4437 case EVENT_IBSS_PEER_LOST:
4438#ifdef CONFIG_IBSS_RSN
4439 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
4440#endif /* CONFIG_IBSS_RSN */
4441 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004442 case EVENT_DRIVER_GTK_REKEY:
4443 if (os_memcmp(data->driver_gtk_rekey.bssid,
4444 wpa_s->bssid, ETH_ALEN))
4445 break;
4446 if (!wpa_s->wpa)
4447 break;
4448 wpa_sm_update_replay_ctr(wpa_s->wpa,
4449 data->driver_gtk_rekey.replay_ctr);
4450 break;
4451 case EVENT_SCHED_SCAN_STOPPED:
4452 wpa_s->sched_scanning = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004453 resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004454 wpa_supplicant_notify_scanning(wpa_s, 0);
4455
4456 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4457 break;
4458
4459 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004460 * If the driver stopped scanning without being requested to,
4461 * request a new scan to continue scanning for networks.
4462 */
4463 if (!wpa_s->sched_scan_stop_req &&
4464 wpa_s->wpa_state == WPA_SCANNING) {
4465 wpa_dbg(wpa_s, MSG_DEBUG,
4466 "Restart scanning after unexpected sched_scan stop event");
4467 wpa_supplicant_req_scan(wpa_s, 1, 0);
4468 break;
4469 }
4470
4471 wpa_s->sched_scan_stop_req = 0;
4472
4473 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08004474 * Start a new sched scan to continue searching for more SSIDs
4475 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004476 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07004477 if (wpa_s->sched_scan_timed_out) {
4478 wpa_supplicant_req_sched_scan(wpa_s);
4479 } else if (wpa_s->pno_sched_pending) {
4480 wpa_s->pno_sched_pending = 0;
4481 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004482 } else if (resched) {
4483 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08004484 }
4485
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486 break;
4487 case EVENT_WPS_BUTTON_PUSHED:
4488#ifdef CONFIG_WPS
4489 wpas_wps_start_pbc(wpa_s, NULL, 0);
4490#endif /* CONFIG_WPS */
4491 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004492 case EVENT_AVOID_FREQUENCIES:
4493 wpa_supplicant_notify_avoid_freq(wpa_s, data);
4494 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004495 case EVENT_CONNECT_FAILED_REASON:
4496#ifdef CONFIG_AP
4497 if (!wpa_s->ap_iface || !data)
4498 break;
4499 hostapd_event_connect_failed_reason(
4500 wpa_s->ap_iface->bss[0],
4501 data->connect_failed_reason.addr,
4502 data->connect_failed_reason.code);
4503#endif /* CONFIG_AP */
4504 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004505 case EVENT_NEW_PEER_CANDIDATE:
4506#ifdef CONFIG_MESH
4507 if (!wpa_s->ifmsh || !data)
4508 break;
4509 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
4510 data->mesh_peer.ies,
4511 data->mesh_peer.ie_len);
4512#endif /* CONFIG_MESH */
4513 break;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004514 case EVENT_SURVEY:
4515#ifdef CONFIG_AP
4516 if (!wpa_s->ap_iface)
4517 break;
4518 hostapd_event_get_survey(wpa_s->ap_iface,
4519 &data->survey_results);
4520#endif /* CONFIG_AP */
4521 break;
4522 case EVENT_ACS_CHANNEL_SELECTED:
Paul Stewart092955c2017-02-06 09:13:09 -08004523#ifdef CONFIG_AP
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004524#ifdef CONFIG_ACS
4525 if (!wpa_s->ap_iface)
4526 break;
4527 hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
4528 &data->acs_selected_channels);
4529#endif /* CONFIG_ACS */
Paul Stewart092955c2017-02-06 09:13:09 -08004530#endif /* CONFIG_AP */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004531 break;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004532 case EVENT_P2P_LO_STOP:
4533#ifdef CONFIG_P2P
4534 wpa_s->p2p_lo_started = 0;
4535 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
4536 P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
4537 data->p2p_lo_stop.reason_code);
4538#endif /* CONFIG_P2P */
4539 break;
Paul Stewart092955c2017-02-06 09:13:09 -08004540 case EVENT_BEACON_LOSS:
4541 if (!wpa_s->current_bss || !wpa_s->current_ssid)
4542 break;
4543 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
4544 bgscan_notify_beacon_loss(wpa_s);
4545 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004546 default:
4547 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
4548 break;
4549 }
4550}
Dmitry Shmidte4663042016-04-04 10:07:49 -07004551
4552
4553void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
4554 union wpa_event_data *data)
4555{
4556 struct wpa_supplicant *wpa_s;
4557
4558 if (event != EVENT_INTERFACE_STATUS)
4559 return;
4560
4561 wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
4562 if (wpa_s && wpa_s->driver->get_ifindex) {
4563 unsigned int ifindex;
4564
4565 ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
4566 if (ifindex != data->interface_status.ifindex) {
4567 wpa_dbg(wpa_s, MSG_DEBUG,
4568 "interface status ifindex %d mismatch (%d)",
4569 ifindex, data->interface_status.ifindex);
4570 return;
4571 }
4572 }
4573#ifdef CONFIG_MATCH_IFACE
4574 else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
4575 struct wpa_interface *wpa_i;
4576
4577 wpa_i = wpa_supplicant_match_iface(
4578 ctx, data->interface_status.ifname);
4579 if (!wpa_i)
4580 return;
4581 wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
4582 os_free(wpa_i);
4583 if (wpa_s)
4584 wpa_s->matched = 1;
4585 }
4586#endif /* CONFIG_MATCH_IFACE */
4587
4588 if (wpa_s)
4589 wpa_supplicant_event(wpa_s, event, data);
4590}