blob: 448615e401980343322ba272a70b3cde2ecf67c3 [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"
31#include "crypto/random.h"
32#include "blacklist.h"
33#include "wpas_glue.h"
34#include "wps_supplicant.h"
35#include "ibss_rsn.h"
36#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "p2p_supplicant.h"
39#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070040#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "ap.h"
42#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#include "interworking.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "mesh.h"
47#include "mesh_mpm.h"
48#include "wmm_ac.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049
50
Dmitry Shmidt34af3062013-07-11 10:46:32 -070051#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070052static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080053 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080055
56
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
58 struct wpa_ssid *ssid)
59{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070061
62 if (ssid == NULL || ssid->disabled_until.sec == 0)
63 return 0;
64
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080065 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 if (ssid->disabled_until.sec > now.sec)
67 return ssid->disabled_until.sec - now.sec;
68
69 wpas_clear_temp_disabled(wpa_s, ssid, 0);
70
71 return 0;
72}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
74
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -080075#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070076/**
77 * wpas_reenabled_network_time - Time until first network is re-enabled
78 * @wpa_s: Pointer to wpa_supplicant data
79 * Returns: If all enabled networks are temporarily disabled, returns the time
80 * (in sec) until the first network is re-enabled. Otherwise returns 0.
81 *
82 * This function is used in case all enabled networks are temporarily disabled,
83 * in which case it returns the time (in sec) that the first network will be
84 * re-enabled. The function assumes that at least one network is enabled.
85 */
86static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
87{
88 struct wpa_ssid *ssid;
89 int disabled_for, res = 0;
90
91#ifdef CONFIG_INTERWORKING
92 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
93 wpa_s->conf->cred)
94 return 0;
95#endif /* CONFIG_INTERWORKING */
96
97 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
98 if (ssid->disabled)
99 continue;
100
101 disabled_for = wpas_temp_disabled(wpa_s, ssid);
102 if (!disabled_for)
103 return 0;
104
105 if (!res || disabled_for < res)
106 res = disabled_for;
107 }
108
109 return res;
110}
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -0800111#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700112
113
114void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
115{
116 struct wpa_supplicant *wpa_s = eloop_ctx;
117
118 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
119 return;
120
121 wpa_dbg(wpa_s, MSG_DEBUG,
122 "Try to associate due to network getting re-enabled");
123 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
124 wpa_supplicant_cancel_sched_scan(wpa_s);
125 wpa_supplicant_req_scan(wpa_s, 0, 0);
126 }
127}
128
129
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800130static struct wpa_bss * wpa_supplicant_get_new_bss(
131 struct wpa_supplicant *wpa_s, const u8 *bssid)
132{
133 struct wpa_bss *bss = NULL;
134 struct wpa_ssid *ssid = wpa_s->current_ssid;
135
136 if (ssid->ssid_len > 0)
137 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
138 if (!bss)
139 bss = wpa_bss_get_bssid(wpa_s, bssid);
140
141 return bss;
142}
143
144
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700145static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
146{
147 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
148
149 if (!bss) {
150 wpa_supplicant_update_scan_results(wpa_s);
151
152 /* Get the BSS from the new scan results */
153 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
154 }
155
156 if (bss)
157 wpa_s->current_bss = bss;
158}
159
160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
162{
163 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700164 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700165 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700167
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700168 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
169 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700170
171 if (wpa_s->current_ssid->ssid_len == 0)
172 return 0; /* current profile still in use */
173 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
174 if (res < 0) {
175 wpa_msg(wpa_s, MSG_INFO,
176 "Failed to read SSID from driver");
177 return 0; /* try to use current profile */
178 }
179 drv_ssid_len = res;
180
181 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
182 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
183 drv_ssid_len) == 0)
184 return 0; /* current profile still in use */
185
186 wpa_msg(wpa_s, MSG_DEBUG,
187 "Driver-initiated BSS selection changed the SSID to %s",
188 wpa_ssid_txt(drv_ssid, drv_ssid_len));
189 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191
192 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
193 "information");
194 ssid = wpa_supplicant_get_ssid(wpa_s);
195 if (ssid == NULL) {
196 wpa_msg(wpa_s, MSG_INFO,
197 "No network configuration found for the current AP");
198 return -1;
199 }
200
Dmitry Shmidt04949592012-07-19 12:16:46 -0700201 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
203 return -1;
204 }
205
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800206 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
207 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
208 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
209 return -1;
210 }
211
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700212 res = wpas_temp_disabled(wpa_s, ssid);
213 if (res > 0) {
214 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
215 "disabled for %d second(s)", res);
216 return -1;
217 }
218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
220 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 u8 wpa_ie[80];
223 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800224 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
225 wpa_ie, &wpa_ie_len) < 0)
226 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 } else {
228 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
229 }
230
231 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
232 eapol_sm_invalidate_cached_session(wpa_s->eapol);
233 old_ssid = wpa_s->current_ssid;
234 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800235
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700236 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
239 wpa_supplicant_initiate_eapol(wpa_s);
240 if (old_ssid != wpa_s->current_ssid)
241 wpas_notify_network_changed(wpa_s);
242
243 return 0;
244}
245
246
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248{
249 struct wpa_supplicant *wpa_s = eloop_ctx;
250
251 if (wpa_s->countermeasures) {
252 wpa_s->countermeasures = 0;
253 wpa_drv_set_countermeasures(wpa_s, 0);
254 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800255
256 /*
257 * It is possible that the device is sched scanning, which means
258 * that a connection attempt will be done only when we receive
259 * scan results. However, in this case, it would be preferable
260 * to scan and connect immediately, so cancel the sched_scan and
261 * issue a regular scan flow.
262 */
263 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264 wpa_supplicant_req_scan(wpa_s, 0, 0);
265 }
266}
267
268
269void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
270{
271 int bssid_changed;
272
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wnm_bss_keep_alive_deinit(wpa_s);
274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275#ifdef CONFIG_IBSS_RSN
276 ibss_rsn_deinit(wpa_s->ibss_rsn);
277 wpa_s->ibss_rsn = NULL;
278#endif /* CONFIG_IBSS_RSN */
279
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700280#ifdef CONFIG_AP
281 wpa_supplicant_ap_deinit(wpa_s);
282#endif /* CONFIG_AP */
283
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700284#ifdef CONFIG_HS20
285 /* Clear possibly configured frame filters */
286 wpa_drv_configure_frame_filters(wpa_s, 0);
287#endif /* CONFIG_HS20 */
288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
290 return;
291
292 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
293 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
294 os_memset(wpa_s->bssid, 0, ETH_ALEN);
295 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800296 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 wpa_s->current_bss = NULL;
298 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 if (bssid_changed)
301 wpas_notify_bssid_changed(wpa_s);
302
303 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
304 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
305 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
306 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
307 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700308 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700309 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800311
312 wpas_rrm_reset(wpa_s);
Dmitry Shmidtb70d0bb2015-11-16 10:43:06 -0800313 wpa_s->wnmsleep_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314}
315
316
317static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
318{
319 struct wpa_ie_data ie;
320 int pmksa_set = -1;
321 size_t i;
322
323 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
324 ie.pmkid == NULL)
325 return;
326
327 for (i = 0; i < ie.num_pmkid; i++) {
328 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
329 ie.pmkid + i * PMKID_LEN,
330 NULL, NULL, 0);
331 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800332 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 break;
334 }
335 }
336
337 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
338 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
339}
340
341
342static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
343 union wpa_event_data *data)
344{
345 if (data == NULL) {
346 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
347 "event");
348 return;
349 }
350 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
351 " index=%d preauth=%d",
352 MAC2STR(data->pmkid_candidate.bssid),
353 data->pmkid_candidate.index,
354 data->pmkid_candidate.preauth);
355
356 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
357 data->pmkid_candidate.index,
358 data->pmkid_candidate.preauth);
359}
360
361
362static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
363{
364 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
365 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
366 return 0;
367
368#ifdef IEEE8021X_EAPOL
369 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
370 wpa_s->current_ssid &&
371 !(wpa_s->current_ssid->eapol_flags &
372 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
373 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
374 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
375 * plaintext or static WEP keys). */
376 return 0;
377 }
378#endif /* IEEE8021X_EAPOL */
379
380 return 1;
381}
382
383
384/**
385 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
386 * @wpa_s: pointer to wpa_supplicant data
387 * @ssid: Configuration data for the network
388 * Returns: 0 on success, -1 on failure
389 *
390 * This function is called when starting authentication with a network that is
391 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
392 */
393int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
394 struct wpa_ssid *ssid)
395{
396#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800397#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700398 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700400 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
401 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 return 0;
403
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700404 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 sim = 1;
406 aka = 1;
407 } else {
408 struct eap_method_type *eap = ssid->eap.eap_methods;
409 while (eap->vendor != EAP_VENDOR_IETF ||
410 eap->method != EAP_TYPE_NONE) {
411 if (eap->vendor == EAP_VENDOR_IETF) {
412 if (eap->method == EAP_TYPE_SIM)
413 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700414 else if (eap->method == EAP_TYPE_AKA ||
415 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 aka = 1;
417 }
418 eap++;
419 }
420 }
421
422 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
423 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700424 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
425 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
426 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 aka = 0;
428
429 if (!sim && !aka) {
430 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
431 "use SIM, but neither EAP-SIM nor EAP-AKA are "
432 "enabled");
433 return 0;
434 }
435
436 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
437 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700439 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 if (wpa_s->scard == NULL) {
441 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
442 "(pcsc-lite)");
443 return -1;
444 }
445 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
446 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800447#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448#endif /* IEEE8021X_EAPOL */
449
450 return 0;
451}
452
453
454#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700455
456static int has_wep_key(struct wpa_ssid *ssid)
457{
458 int i;
459
460 for (i = 0; i < NUM_WEP_KEYS; i++) {
461 if (ssid->wep_key_len[i])
462 return 1;
463 }
464
465 return 0;
466}
467
468
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700469static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 struct wpa_ssid *ssid)
471{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700472 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473
474 if (ssid->mixed_cell)
475 return 1;
476
477#ifdef CONFIG_WPS
478 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
479 return 1;
480#endif /* CONFIG_WPS */
481
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700482 if (has_wep_key(ssid))
483 privacy = 1;
484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485#ifdef IEEE8021X_EAPOL
486 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
487 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
488 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
489 privacy = 1;
490#endif /* IEEE8021X_EAPOL */
491
Jouni Malinen75ecf522011-06-27 15:19:46 -0700492 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
493 privacy = 1;
494
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800495 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
496 privacy = 1;
497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 if (bss->caps & IEEE80211_CAP_PRIVACY)
499 return privacy;
500 return !privacy;
501}
502
503
504static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
505 struct wpa_ssid *ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800506 struct wpa_bss *bss, int debug_print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507{
508 struct wpa_ie_data ie;
509 int proto_match = 0;
510 const u8 *rsn_ie, *wpa_ie;
511 int ret;
512 int wep_ok;
513
514 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
515 if (ret >= 0)
516 return ret;
517
518 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
519 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
520 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
521 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
522 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
523
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700524 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
526 proto_match++;
527
528 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800529 if (debug_print)
530 wpa_dbg(wpa_s, MSG_DEBUG,
531 " skip RSN IE - parse failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 break;
533 }
534
535 if (wep_ok &&
536 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
537 {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800538 if (debug_print)
539 wpa_dbg(wpa_s, MSG_DEBUG,
540 " selected based on TSN in RSN IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 return 1;
542 }
543
544 if (!(ie.proto & ssid->proto)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800545 if (debug_print)
546 wpa_dbg(wpa_s, MSG_DEBUG,
547 " skip RSN IE - proto mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 break;
549 }
550
551 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800552 if (debug_print)
553 wpa_dbg(wpa_s, MSG_DEBUG,
554 " skip RSN IE - PTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 break;
556 }
557
558 if (!(ie.group_cipher & ssid->group_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800559 if (debug_print)
560 wpa_dbg(wpa_s, MSG_DEBUG,
561 " skip RSN IE - GTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 break;
563 }
564
565 if (!(ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800566 if (debug_print)
567 wpa_dbg(wpa_s, MSG_DEBUG,
568 " skip RSN IE - key mgmt mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 break;
570 }
571
572#ifdef CONFIG_IEEE80211W
573 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800574 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800575 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800576 if (debug_print)
577 wpa_dbg(wpa_s, MSG_DEBUG,
578 " skip RSN IE - no mgmt frame protection");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 break;
580 }
581#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800582 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
583 wpas_get_ssid_pmf(wpa_s, ssid) ==
584 NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800585 if (debug_print)
586 wpa_dbg(wpa_s, MSG_DEBUG,
587 " skip RSN IE - no mgmt frame protection enabled but AP requires it");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800588 break;
589 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800590#ifdef CONFIG_MBO
591 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
592 wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
593 wpas_get_ssid_pmf(wpa_s, ssid) !=
594 NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800595 if (debug_print)
596 wpa_dbg(wpa_s, MSG_DEBUG,
597 " skip RSN IE - no mgmt frame protection enabled on MBO AP");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800598 break;
599 }
600#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800602 if (debug_print)
603 wpa_dbg(wpa_s, MSG_DEBUG,
604 " selected based on RSN IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 return 1;
606 }
607
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700608#ifdef CONFIG_IEEE80211W
609 if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800610 if (debug_print)
611 wpa_dbg(wpa_s, MSG_DEBUG,
612 " skip - MFP Required but network not MFP Capable");
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700613 return 0;
614 }
615#endif /* CONFIG_IEEE80211W */
616
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700617 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
619 proto_match++;
620
621 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800622 if (debug_print)
623 wpa_dbg(wpa_s, MSG_DEBUG,
624 " skip WPA IE - parse failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 break;
626 }
627
628 if (wep_ok &&
629 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
630 {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800631 if (debug_print)
632 wpa_dbg(wpa_s, MSG_DEBUG,
633 " selected based on TSN in WPA IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 return 1;
635 }
636
637 if (!(ie.proto & ssid->proto)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800638 if (debug_print)
639 wpa_dbg(wpa_s, MSG_DEBUG,
640 " skip WPA IE - proto mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 break;
642 }
643
644 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800645 if (debug_print)
646 wpa_dbg(wpa_s, MSG_DEBUG,
647 " skip WPA IE - PTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648 break;
649 }
650
651 if (!(ie.group_cipher & ssid->group_cipher)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800652 if (debug_print)
653 wpa_dbg(wpa_s, MSG_DEBUG,
654 " skip WPA IE - GTK cipher mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 break;
656 }
657
658 if (!(ie.key_mgmt & ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800659 if (debug_print)
660 wpa_dbg(wpa_s, MSG_DEBUG,
661 " skip WPA IE - key mgmt mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700662 break;
663 }
664
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800665 if (debug_print)
666 wpa_dbg(wpa_s, MSG_DEBUG,
667 " selected based on WPA IE");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 return 1;
669 }
670
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700671 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
672 !rsn_ie) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800673 if (debug_print)
674 wpa_dbg(wpa_s, MSG_DEBUG,
675 " allow for non-WPA IEEE 802.1X");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700676 return 1;
677 }
678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
680 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800681 if (debug_print)
682 wpa_dbg(wpa_s, MSG_DEBUG,
683 " skip - no WPA/RSN proto match");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 return 0;
685 }
686
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800687 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
688 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800689 if (debug_print)
690 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800691 return 1;
692 }
693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800695 if (debug_print)
696 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 return 1;
698 }
699
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800700 if (debug_print)
701 wpa_dbg(wpa_s, MSG_DEBUG,
702 " reject due to mismatch with WPA/WPA2");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703
704 return 0;
705}
706
707
708static int freq_allowed(int *freqs, int freq)
709{
710 int i;
711
712 if (freqs == NULL)
713 return 1;
714
715 for (i = 0; freqs[i]; i++)
716 if (freqs[i] == freq)
717 return 1;
718 return 0;
719}
720
721
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800722static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
723 int debug_print)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800724{
725 const struct hostapd_hw_modes *mode = NULL, *modes;
726 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
727 const u8 *rate_ie;
728 int i, j, k;
729
730 if (bss->freq == 0)
731 return 1; /* Cannot do matching without knowing band */
732
733 modes = wpa_s->hw.modes;
734 if (modes == NULL) {
735 /*
736 * The driver does not provide any additional information
737 * about the utilized hardware, so allow the connection attempt
738 * to continue.
739 */
740 return 1;
741 }
742
743 for (i = 0; i < wpa_s->hw.num_modes; i++) {
744 for (j = 0; j < modes[i].num_channels; j++) {
745 int freq = modes[i].channels[j].freq;
746 if (freq == bss->freq) {
747 if (mode &&
748 mode->mode == HOSTAPD_MODE_IEEE80211G)
749 break; /* do not allow 802.11b replace
750 * 802.11g */
751 mode = &modes[i];
752 break;
753 }
754 }
755 }
756
757 if (mode == NULL)
758 return 0;
759
760 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700761 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800762 if (rate_ie == NULL)
763 continue;
764
765 for (j = 2; j < rate_ie[1] + 2; j++) {
766 int flagged = !!(rate_ie[j] & 0x80);
767 int r = (rate_ie[j] & 0x7f) * 5;
768
769 /*
770 * IEEE Std 802.11n-2009 7.3.2.2:
771 * The new BSS Membership selector value is encoded
772 * like a legacy basic rate, but it is not a rate and
773 * only indicates if the BSS members are required to
774 * support the mandatory features of Clause 20 [HT PHY]
775 * in order to join the BSS.
776 */
777 if (flagged && ((rate_ie[j] & 0x7f) ==
778 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
779 if (!ht_supported(mode)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800780 if (debug_print)
781 wpa_dbg(wpa_s, MSG_DEBUG,
782 " hardware does not support HT PHY");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783 return 0;
784 }
785 continue;
786 }
787
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700788 /* There's also a VHT selector for 802.11ac */
789 if (flagged && ((rate_ie[j] & 0x7f) ==
790 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
791 if (!vht_supported(mode)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800792 if (debug_print)
793 wpa_dbg(wpa_s, MSG_DEBUG,
794 " hardware does not support VHT PHY");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700795 return 0;
796 }
797 continue;
798 }
799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 if (!flagged)
801 continue;
802
803 /* check for legacy basic rates */
804 for (k = 0; k < mode->num_rates; k++) {
805 if (mode->rates[k] == r)
806 break;
807 }
808 if (k == mode->num_rates) {
809 /*
810 * IEEE Std 802.11-2007 7.3.2.2 demands that in
811 * order to join a BSS all required rates
812 * have to be supported by the hardware.
813 */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800814 if (debug_print)
815 wpa_dbg(wpa_s, MSG_DEBUG,
816 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
817 r / 10, r % 10,
818 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800819 return 0;
820 }
821 }
822 }
823
824 return 1;
825}
826
827
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800828/*
829 * Test whether BSS is in an ESS.
830 * This is done differently in DMG (60 GHz) and non-DMG bands
831 */
832static int bss_is_ess(struct wpa_bss *bss)
833{
834 if (bss_is_dmg(bss)) {
835 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
836 IEEE80211_CAP_DMG_AP;
837 }
838
839 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
840 IEEE80211_CAP_ESS);
841}
842
843
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800844static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
845{
846 size_t i;
847
848 for (i = 0; i < ETH_ALEN; i++) {
849 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
850 return 0;
851 }
852 return 1;
853}
854
855
856static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
857{
858 size_t i;
859
860 for (i = 0; i < num; i++) {
861 const u8 *a = list + i * ETH_ALEN * 2;
862 const u8 *m = a + ETH_ALEN;
863
864 if (match_mac_mask(a, addr, m))
865 return 1;
866 }
867 return 0;
868}
869
870
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800871struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
872 int i, struct wpa_bss *bss,
873 struct wpa_ssid *group,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800874 int only_first_ssid, int debug_print)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700876 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 int wpa;
878 struct wpa_blacklist *e;
879 const u8 *ie;
880 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800881 int osen;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800882#ifdef CONFIG_MBO
883 const u8 *assoc_disallow;
884#endif /* CONFIG_MBO */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700886 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpa_ie_len = ie ? ie[1] : 0;
888
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700889 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 rsn_ie_len = ie ? ie[1] : 0;
891
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800892 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
893 osen = ie != NULL;
894
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800895 if (debug_print) {
896 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
897 " ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
898 i, MAC2STR(bss->bssid),
899 wpa_ssid_txt(bss->ssid, bss->ssid_len),
900 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
901 bss->freq,
902 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
903 " wps" : "",
904 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
905 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
906 ? " p2p" : "",
907 osen ? " osen=1" : "");
908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909
910 e = wpa_blacklist_get(wpa_s, bss->bssid);
911 if (e) {
912 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700913 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 /*
915 * When only a single network is enabled, we can
916 * trigger blacklisting on the first failure. This
917 * should not be done with multiple enabled networks to
918 * avoid getting forced to move into a worse ESS on
919 * single error if there are no other BSSes of the
920 * current ESS.
921 */
922 limit = 0;
923 }
924 if (e->count > limit) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800925 if (debug_print) {
926 wpa_dbg(wpa_s, MSG_DEBUG,
927 " skip - blacklisted (count=%d limit=%d)",
928 e->count, limit);
929 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 return NULL;
931 }
932 }
933
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700934 if (bss->ssid_len == 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800935 if (debug_print)
936 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 return NULL;
938 }
939
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800940 if (disallowed_bssid(wpa_s, bss->bssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800941 if (debug_print)
942 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800943 return NULL;
944 }
945
946 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800947 if (debug_print)
948 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800949 return NULL;
950 }
951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
953
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800954 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700956 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957
Dmitry Shmidt04949592012-07-19 12:16:46 -0700958 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800959 if (debug_print)
960 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 continue;
962 }
963
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700964 res = wpas_temp_disabled(wpa_s, ssid);
965 if (res > 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800966 if (debug_print)
967 wpa_dbg(wpa_s, MSG_DEBUG,
968 " skip - disabled temporarily for %d second(s)",
969 res);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700970 continue;
971 }
972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973#ifdef CONFIG_WPS
974 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800975 if (debug_print)
976 wpa_dbg(wpa_s, MSG_DEBUG,
977 " skip - blacklisted (WPS)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 continue;
979 }
980
981 if (wpa && ssid->ssid_len == 0 &&
982 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
983 check_ssid = 0;
984
985 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
986 /* Only allow wildcard SSID match if an AP
987 * advertises active WPS operation that matches
988 * with our mode. */
989 check_ssid = 1;
990 if (ssid->ssid_len == 0 &&
991 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
992 check_ssid = 0;
993 }
994#endif /* CONFIG_WPS */
995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800996 if (ssid->bssid_set && ssid->ssid_len == 0 &&
997 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
998 check_ssid = 0;
999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001001 (bss->ssid_len != ssid->ssid_len ||
1002 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001003 if (debug_print)
1004 wpa_dbg(wpa_s, MSG_DEBUG,
1005 " skip - SSID mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 continue;
1007 }
1008
1009 if (ssid->bssid_set &&
1010 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001011 if (debug_print)
1012 wpa_dbg(wpa_s, MSG_DEBUG,
1013 " skip - BSSID mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 continue;
1015 }
1016
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001017 /* check blacklist */
1018 if (ssid->num_bssid_blacklist &&
1019 addr_in_list(bss->bssid, ssid->bssid_blacklist,
1020 ssid->num_bssid_blacklist)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001021 if (debug_print)
1022 wpa_dbg(wpa_s, MSG_DEBUG,
1023 " skip - BSSID blacklisted");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001024 continue;
1025 }
1026
1027 /* if there is a whitelist, only accept those APs */
1028 if (ssid->num_bssid_whitelist &&
1029 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
1030 ssid->num_bssid_whitelist)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001031 if (debug_print)
1032 wpa_dbg(wpa_s, MSG_DEBUG,
1033 " skip - BSSID not in whitelist");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001034 continue;
1035 }
1036
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001037 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss,
1038 debug_print))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 continue;
1040
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001041 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
1043 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
1044 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001045 if (debug_print)
1046 wpa_dbg(wpa_s, MSG_DEBUG,
1047 " skip - non-WPA network not allowed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 continue;
1049 }
1050
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001051 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
1052 has_wep_key(ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001053 if (debug_print)
1054 wpa_dbg(wpa_s, MSG_DEBUG,
1055 " skip - ignore WPA/WPA2 AP for WEP network block");
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001056 continue;
1057 }
1058
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001059 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001060 if (debug_print)
1061 wpa_dbg(wpa_s, MSG_DEBUG,
1062 " skip - non-OSEN network not allowed");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001063 continue;
1064 }
1065
Jouni Malinen75ecf522011-06-27 15:19:46 -07001066 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001067 if (debug_print)
1068 wpa_dbg(wpa_s, MSG_DEBUG,
1069 " skip - privacy mismatch");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 continue;
1071 }
1072
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001073 if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
1074 !bss_is_pbss(bss)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001075 if (debug_print)
1076 wpa_dbg(wpa_s, MSG_DEBUG,
1077 " skip - not ESS, PBSS, or MBSS");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001078 continue;
1079 }
1080
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001081 if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001082 if (debug_print)
1083 wpa_dbg(wpa_s, MSG_DEBUG,
1084 " skip - PBSS mismatch (ssid %d bss %d)",
1085 ssid->pbss, bss_is_pbss(bss));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086 continue;
1087 }
1088
1089 if (!freq_allowed(ssid->freq_list, bss->freq)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001090 if (debug_print)
1091 wpa_dbg(wpa_s, MSG_DEBUG,
1092 " skip - frequency not allowed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 continue;
1094 }
1095
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001096#ifdef CONFIG_MESH
1097 if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
1098 ssid->frequency != bss->freq) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001099 if (debug_print)
1100 wpa_dbg(wpa_s, MSG_DEBUG,
1101 " skip - frequency not allowed (mesh)");
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001102 continue;
1103 }
1104#endif /* CONFIG_MESH */
1105
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001106 if (!rate_match(wpa_s, bss, debug_print)) {
1107 if (debug_print)
1108 wpa_dbg(wpa_s, MSG_DEBUG,
1109 " skip - rate sets do not match");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001110 continue;
1111 }
1112
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001113#ifndef CONFIG_IBSS_RSN
1114 if (ssid->mode == WPAS_MODE_IBSS &&
1115 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1116 WPA_KEY_MGMT_WPA_NONE))) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001117 if (debug_print)
1118 wpa_dbg(wpa_s, MSG_DEBUG,
1119 " skip - IBSS RSN not supported in the build");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001120 continue;
1121 }
1122#endif /* !CONFIG_IBSS_RSN */
1123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001125 if (ssid->p2p_group &&
1126 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1127 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001128 if (debug_print)
1129 wpa_dbg(wpa_s, MSG_DEBUG,
1130 " skip - no P2P IE seen");
Dmitry Shmidt96571392013-10-14 12:54:46 -07001131 continue;
1132 }
1133
Dmitry Shmidt54605472013-11-08 11:10:19 -08001134 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1135 struct wpabuf *p2p_ie;
1136 u8 dev_addr[ETH_ALEN];
1137
1138 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1139 if (ie == NULL) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001140 if (debug_print)
1141 wpa_dbg(wpa_s, MSG_DEBUG,
1142 " skip - no P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001143 continue;
1144 }
1145 p2p_ie = wpa_bss_get_vendor_ie_multi(
1146 bss, P2P_IE_VENDOR_TYPE);
1147 if (p2p_ie == NULL) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001148 if (debug_print)
1149 wpa_dbg(wpa_s, MSG_DEBUG,
1150 " skip - could not fetch P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001151 continue;
1152 }
1153
1154 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1155 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1156 ETH_ALEN) != 0) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001157 if (debug_print)
1158 wpa_dbg(wpa_s, MSG_DEBUG,
1159 " skip - no matching GO P2P Device Address in P2P element");
Dmitry Shmidt54605472013-11-08 11:10:19 -08001160 wpabuf_free(p2p_ie);
1161 continue;
1162 }
1163 wpabuf_free(p2p_ie);
1164 }
1165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 /*
1167 * TODO: skip the AP if its P2P IE has Group Formation
1168 * bit set in the P2P Group Capability Bitmap and we
1169 * are not in Group Formation with that device.
1170 */
1171#endif /* CONFIG_P2P */
1172
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001173 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1174 {
1175 struct os_reltime diff;
1176
1177 os_reltime_sub(&wpa_s->scan_min_time,
1178 &bss->last_update, &diff);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001179 if (debug_print)
1180 wpa_dbg(wpa_s, MSG_DEBUG,
1181 " skip - scan result not recent enough (%u.%06u seconds too old)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001182 (unsigned int) diff.sec,
1183 (unsigned int) diff.usec);
1184 continue;
1185 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001186#ifdef CONFIG_MBO
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001187#ifdef CONFIG_TESTING_OPTIONS
1188 if (wpa_s->ignore_assoc_disallow)
1189 goto skip_assoc_disallow;
1190#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001191 assoc_disallow = wpas_mbo_get_bss_attr(
1192 bss, MBO_ATTR_ID_ASSOC_DISALLOW);
1193 if (assoc_disallow && assoc_disallow[1] >= 1) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001194 if (debug_print)
1195 wpa_dbg(wpa_s, MSG_DEBUG,
1196 " skip - MBO association disallowed (reason %u)",
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001197 assoc_disallow[2]);
1198 continue;
1199 }
1200
1201 if (wpa_is_bss_tmp_disallowed(wpa_s, bss->bssid)) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001202 if (debug_print)
1203 wpa_dbg(wpa_s, MSG_DEBUG,
1204 " skip - MBO retry delay has not passed yet");
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001205 continue;
1206 }
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001207#ifdef CONFIG_TESTING_OPTIONS
1208 skip_assoc_disallow:
1209#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001210#endif /* CONFIG_MBO */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 /* Matching configuration found */
1213 return ssid;
1214 }
1215
1216 /* No matching configuration found */
1217 return NULL;
1218}
1219
1220
1221static struct wpa_bss *
1222wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001224 struct wpa_ssid **selected_ssid,
1225 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001227 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001229 if (wpa_s->current_ssid) {
1230 struct wpa_ssid *ssid;
1231
1232 wpa_dbg(wpa_s, MSG_DEBUG,
1233 "Scan results matching the currently selected network");
1234 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1235 struct wpa_bss *bss = wpa_s->last_scan_res[i];
1236
1237 ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1238 only_first_ssid, 0);
1239 if (ssid != wpa_s->current_ssid)
1240 continue;
1241 wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
1242 " freq=%d level=%d snr=%d est_throughput=%u",
1243 i, MAC2STR(bss->bssid), bss->freq, bss->level,
1244 bss->snr, bss->est_throughput);
1245 }
1246 }
1247
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001248 if (only_first_ssid)
1249 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1250 group->id);
1251 else
1252 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1253 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001255 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1256 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001257 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001258 only_first_ssid, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 if (!*selected_ssid)
1260 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1262 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001263 MAC2STR(bss->bssid),
1264 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1265 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 }
1267
1268 return NULL;
1269}
1270
1271
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001272struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1273 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274{
1275 struct wpa_bss *selected = NULL;
1276 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001277 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001278 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001280 if (wpa_s->last_scan_res == NULL ||
1281 wpa_s->last_scan_res_used == 0)
1282 return NULL; /* no scan results from last update */
1283
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001284 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001285 /* check that next_ssid is still valid */
1286 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1287 if (ssid == wpa_s->next_ssid)
1288 break;
1289 }
1290 next_ssid = ssid;
1291 wpa_s->next_ssid = NULL;
1292 }
1293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001295 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1296 if (next_ssid && next_ssid->priority ==
1297 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001298 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001299 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001300 if (selected)
1301 break;
1302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001304 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001305 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 if (selected)
1307 break;
1308 }
1309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001310 if (selected == NULL && wpa_s->blacklist &&
1311 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1313 "blacklist and try again");
1314 wpa_blacklist_clear(wpa_s);
1315 wpa_s->blacklist_cleared++;
1316 } else if (selected == NULL)
1317 break;
1318 }
1319
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001320 ssid = *selected_ssid;
1321 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1322 !ssid->passphrase && !ssid->ext_psk) {
1323 const char *field_name, *txt = NULL;
1324
1325 wpa_dbg(wpa_s, MSG_DEBUG,
1326 "PSK/passphrase not yet available for the selected network");
1327
1328 wpas_notify_network_request(wpa_s, ssid,
1329 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1330
1331 field_name = wpa_supplicant_ctrl_req_to_string(
1332 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1333 if (field_name == NULL)
1334 return NULL;
1335
1336 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1337
1338 selected = NULL;
1339 }
1340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 return selected;
1342}
1343
1344
1345static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1346 int timeout_sec, int timeout_usec)
1347{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001348 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 /*
1350 * No networks are enabled; short-circuit request so
1351 * we don't wait timeout seconds before transitioning
1352 * to INACTIVE state.
1353 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001354 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1355 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1357 return;
1358 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001359
1360 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001361 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1362}
1363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001364
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001365int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001366 struct wpa_bss *selected,
1367 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368{
1369 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1370 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1371 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001372 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001374 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1375 wpa_s->p2p_in_provisioning) {
1376 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1377 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001378 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001379 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380#endif /* CONFIG_P2P */
1381
1382#ifdef CONFIG_WPS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001383 wpas_wps_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 wpas_wps_cancel(wpa_s);
1385#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001386 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 }
1388
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001389 wpa_msg(wpa_s, MSG_DEBUG,
1390 "Considering connect request: reassociate: %d selected: "
1391 MACSTR " bssid: " MACSTR " pending: " MACSTR
1392 " wpa_state: %s ssid=%p current_ssid=%p",
1393 wpa_s->reassociate, MAC2STR(selected->bssid),
1394 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1395 wpa_supplicant_state_txt(wpa_s->wpa_state),
1396 ssid, wpa_s->current_ssid);
1397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 /*
1399 * Do not trigger new association unless the BSSID has changed or if
1400 * reassociation is requested. If we are in process of associating with
1401 * the selected BSSID, do not trigger new attempt.
1402 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001403 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1405 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1406 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001407 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1408 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1409 0) ||
1410 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1411 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1413 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001414 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001416 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1417 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 wpa_supplicant_associate(wpa_s, selected, ssid);
1419 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001420 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1421 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001423
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001424 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425}
1426
1427
1428static struct wpa_ssid *
1429wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1430{
1431 int prio;
1432 struct wpa_ssid *ssid;
1433
1434 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1435 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1436 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001437 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 continue;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001439#ifndef CONFIG_IBSS_RSN
1440 if (ssid->mode == WPAS_MODE_IBSS &&
1441 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
1442 WPA_KEY_MGMT_WPA_NONE))) {
1443 wpa_msg(wpa_s, MSG_INFO,
1444 "IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
1445 wpa_ssid_txt(ssid->ssid,
1446 ssid->ssid_len));
1447 continue;
1448 }
1449#endif /* !CONFIG_IBSS_RSN */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001451 ssid->mode == IEEE80211_MODE_AP ||
1452 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001453 return ssid;
1454 }
1455 }
1456 return NULL;
1457}
1458
1459
1460/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1461 * on BSS added and BSS changed events */
1462static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001463 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001465 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466
1467 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1468 return;
1469
Jouni Malinen87fd2792011-05-16 18:35:42 +03001470 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472
Jouni Malinen87fd2792011-05-16 18:35:42 +03001473 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 if (ssid == NULL)
1475 continue;
1476
Jouni Malinen87fd2792011-05-16 18:35:42 +03001477 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (rsn == NULL)
1479 continue;
1480
Jouni Malinen87fd2792011-05-16 18:35:42 +03001481 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 }
1483
1484}
1485
1486
1487static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1488 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001489 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001491 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001492#ifndef CONFIG_NO_ROAMING
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001493 int min_diff, diff;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001494 int to_5ghz;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001495 int cur_est, sel_est;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001496#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497
1498 if (wpa_s->reassociate)
1499 return 1; /* explicit request to reassociate */
1500 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1501 return 1; /* we are not associated; continue */
1502 if (wpa_s->current_ssid == NULL)
1503 return 1; /* unknown current SSID */
1504 if (wpa_s->current_ssid != ssid)
1505 return 1; /* different network block */
1506
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 if (wpas_driver_bss_selection(wpa_s))
1508 return 0; /* Driver-based roaming */
1509
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001510 if (wpa_s->current_ssid->ssid)
1511 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1512 wpa_s->current_ssid->ssid,
1513 wpa_s->current_ssid->ssid_len);
1514 if (!current_bss)
1515 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516
1517 if (!current_bss)
1518 return 1; /* current BSS not seen in scan results */
1519
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001520 if (current_bss == selected)
1521 return 0;
1522
1523 if (selected->last_update_idx > current_bss->last_update_idx)
1524 return 1; /* current BSS not seen in the last scan */
1525
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001526#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001528 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001529 " freq=%d level=%d snr=%d est_throughput=%u",
1530 MAC2STR(current_bss->bssid),
1531 current_bss->freq, current_bss->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001532 current_bss->snr, current_bss->est_throughput);
1533 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001534 " freq=%d level=%d snr=%d est_throughput=%u",
1535 MAC2STR(selected->bssid), selected->freq, selected->level,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001536 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537
1538 if (wpa_s->current_ssid->bssid_set &&
1539 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1540 0) {
1541 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1542 "has preferred BSSID");
1543 return 1;
1544 }
1545
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001546 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1547 wpa_dbg(wpa_s, MSG_DEBUG,
1548 "Allow reassociation - selected BSS has better estimated throughput");
1549 return 1;
1550 }
1551
Dmitry Shmidte4663042016-04-04 10:07:49 -07001552 to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
1553
1554 if (current_bss->level < 0 &&
1555 current_bss->level > selected->level + to_5ghz * 2) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001556 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1557 "signal level");
1558 return 0;
1559 }
1560
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001561 if (current_bss->est_throughput > selected->est_throughput + 5000) {
1562 wpa_dbg(wpa_s, MSG_DEBUG,
1563 "Skip roam - Current BSS has better estimated throughput");
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001564 return 0;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001565 }
1566
1567 cur_est = current_bss->est_throughput;
1568 sel_est = selected->est_throughput;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 min_diff = 2;
1570 if (current_bss->level < 0) {
1571 if (current_bss->level < -85)
1572 min_diff = 1;
1573 else if (current_bss->level < -80)
1574 min_diff = 2;
1575 else if (current_bss->level < -75)
1576 min_diff = 3;
1577 else if (current_bss->level < -70)
1578 min_diff = 4;
1579 else
1580 min_diff = 5;
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001581 if (cur_est > sel_est * 1.5)
1582 min_diff += 10;
1583 else if (cur_est > sel_est * 1.2)
1584 min_diff += 5;
1585 else if (cur_est > sel_est * 1.1)
1586 min_diff += 2;
1587 else if (cur_est > sel_est)
1588 min_diff++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 }
Dmitry Shmidte4663042016-04-04 10:07:49 -07001590 if (to_5ghz) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001591 int reduce = 2;
1592
Dmitry Shmidte4663042016-04-04 10:07:49 -07001593 /* Make it easier to move to 5 GHz band */
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001594 if (sel_est > cur_est * 1.5)
1595 reduce = 5;
1596 else if (sel_est > cur_est * 1.2)
1597 reduce = 4;
1598 else if (sel_est > cur_est * 1.1)
1599 reduce = 3;
1600
1601 if (min_diff > reduce)
1602 min_diff -= reduce;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001603 else
1604 min_diff = 0;
1605 }
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001606 diff = abs(current_bss->level - selected->level);
1607 if (diff < min_diff) {
1608 wpa_dbg(wpa_s, MSG_DEBUG,
1609 "Skip roam - too small difference in signal level (%d < %d)",
1610 diff, min_diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611 return 0;
1612 }
1613
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001614 wpa_dbg(wpa_s, MSG_DEBUG,
1615 "Allow reassociation due to difference in signal level (%d >= %d)",
1616 diff, min_diff);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001618#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001619 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001620#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001621}
1622
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001623
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001624/*
1625 * Return a negative value if no scan results could be fetched or if scan
1626 * results should not be shared with other virtual interfaces.
1627 * Return 0 if scan results were fetched and may be shared with other
1628 * interfaces.
1629 * Return 1 if scan results may be shared with other virtual interfaces but may
1630 * not trigger any operations.
1631 * Return 2 if the interface was removed and cannot be used.
1632 */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001633static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001634 union wpa_event_data *data,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001635 int own_request, int update_only)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001637 struct wpa_scan_results *scan_res = NULL;
1638 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001640#ifndef CONFIG_NO_RANDOM_POOL
1641 size_t i, num;
1642#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643
1644#ifdef CONFIG_AP
1645 if (wpa_s->ap_iface)
1646 ap = 1;
1647#endif /* CONFIG_AP */
1648
1649 wpa_supplicant_notify_scanning(wpa_s, 0);
1650
1651 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1652 data ? &data->scan_info :
1653 NULL, 1);
1654 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001655 if (wpa_s->conf->ap_scan == 2 || ap ||
1656 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001658 if (!own_request)
1659 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001660 if (data && data->scan_info.external_scan)
1661 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1663 "scanning again");
1664 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001665 ret = -1;
1666 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 }
1668
1669#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 num = scan_res->num;
1671 if (num > 10)
1672 num = 10;
1673 for (i = 0; i < num; i++) {
1674 u8 buf[5];
1675 struct wpa_scan_res *res = scan_res->res[i];
1676 buf[0] = res->bssid[5];
1677 buf[1] = res->qual & 0xff;
1678 buf[2] = res->noise & 0xff;
1679 buf[3] = res->level & 0xff;
1680 buf[4] = res->tsf & 0xff;
1681 random_add_randomness(buf, sizeof(buf));
1682 }
1683#endif /* CONFIG_NO_RANDOM_POOL */
1684
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001685 if (update_only) {
1686 ret = 1;
1687 goto scan_work_done;
1688 }
1689
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001690 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001691 !(data && data->scan_info.external_scan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1693 struct wpa_scan_results *scan_res);
1694
1695 scan_res_handler = wpa_s->scan_res_handler;
1696 wpa_s->scan_res_handler = NULL;
1697 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001698 ret = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001699 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 }
1701
1702 if (ap) {
1703 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1704#ifdef CONFIG_AP
1705 if (wpa_s->ap_iface->scan_cb)
1706 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1707#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001708 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001710
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001711 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001712 wpa_s->own_scan_running,
1713 data ? data->scan_info.external_scan : 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001714 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001715 wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1716 own_request && !(data && data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001717 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1718 wpa_s->manual_scan_id);
1719 wpa_s->manual_scan_use_id = 0;
1720 } else {
1721 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1722 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001723 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724
1725 wpas_notify_scan_done(wpa_s, 1);
1726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001727 if (data && data->scan_info.external_scan) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001728 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 -07001729 wpa_scan_results_free(scan_res);
1730 return 0;
1731 }
1732
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001733 if (wnm_scan_process(wpa_s, 1) > 0)
1734 goto scan_work_done;
1735
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001736 if (sme_proc_obss_scan(wpa_s) > 0)
1737 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738
Dmitry Shmidt29333592017-01-09 12:27:11 -08001739 if (own_request &&
1740 wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
1741 goto scan_work_done;
1742
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001743 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1744 goto scan_work_done;
1745
1746 if (autoscan_notify_scan(wpa_s, scan_res))
1747 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 if (wpa_s->disconnected) {
1750 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001751 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 }
1753
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001754 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001755 bgscan_notify_scan(wpa_s, scan_res) == 1)
1756 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001757
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001758 wpas_wps_update_ap_info(wpa_s, scan_res);
1759
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001760 if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
1761 wpa_s->wpa_state < WPA_COMPLETED)
1762 goto scan_work_done;
1763
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001764 wpa_scan_results_free(scan_res);
1765
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001766 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001767 struct wpa_radio_work *work = wpa_s->scan_work;
1768 wpa_s->scan_work = NULL;
1769 radio_work_done(work);
1770 }
1771
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001772 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001773
1774scan_work_done:
1775 wpa_scan_results_free(scan_res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001776 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001777 struct wpa_radio_work *work = wpa_s->scan_work;
1778 wpa_s->scan_work = NULL;
1779 radio_work_done(work);
1780 }
1781 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001782}
1783
1784
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001785static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001786 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001787{
1788 struct wpa_bss *selected;
1789 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001790 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1791
1792 if (time_to_reenable > 0) {
1793 wpa_dbg(wpa_s, MSG_DEBUG,
1794 "Postpone network selection by %d seconds since all networks are disabled",
1795 time_to_reenable);
1796 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1797 eloop_register_timeout(time_to_reenable, 0,
1798 wpas_network_reenabled, wpa_s, NULL);
1799 return 0;
1800 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001801
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001802 if (wpa_s->p2p_mgmt)
1803 return 0; /* no normal connection on p2p_mgmt interface */
1804
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001805 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07001807#ifdef CONFIG_MESH
1808 if (wpa_s->ifmsh) {
1809 wpa_msg(wpa_s, MSG_INFO,
1810 "Avoiding join because we already joined a mesh group");
1811 return 0;
1812 }
1813#endif /* CONFIG_MESH */
1814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 if (selected) {
1816 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001817 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001818 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001819 if (new_scan)
1820 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 }
1823
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001824 if (ssid != wpa_s->current_ssid &&
1825 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1826 wpa_s->own_disconnect_req = 1;
1827 wpa_supplicant_deauthenticate(
1828 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1829 }
1830
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001831 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001832 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001833 return -1;
1834 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001835 if (new_scan)
1836 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001837 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001838 * Do not allow other virtual radios to trigger operations based
1839 * on these scan results since we do not want them to start
1840 * other associations at the same time.
Jouni Malinen89ca7022012-09-14 13:03:12 -07001841 */
1842 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1845 ssid = wpa_supplicant_pick_new_network(wpa_s);
1846 if (ssid) {
1847 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1848 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001849 if (new_scan)
1850 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001851 } else if (own_request) {
1852 /*
1853 * No SSID found. If SCAN results are as a result of
1854 * own scan request and not due to a scan request on
1855 * another shared interface, try another scan.
1856 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 int timeout_sec = wpa_s->scan_interval;
1858 int timeout_usec = 0;
1859#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001860 int res;
1861
1862 res = wpas_p2p_scan_no_go_seen(wpa_s);
1863 if (res == 2)
1864 return 2;
1865 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001866 return 0;
1867
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001868 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001869 wpa_s->show_group_started ||
1870 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 /*
1872 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001873 * state and during P2P join-a-group operation
1874 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 */
1876 timeout_sec = 0;
1877 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001878 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1879 timeout_usec);
1880 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001881 }
1882#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001883#ifdef CONFIG_INTERWORKING
1884 if (wpa_s->conf->auto_interworking &&
1885 wpa_s->conf->interworking &&
1886 wpa_s->conf->cred) {
1887 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1888 "start ANQP fetch since no matching "
1889 "networks found");
1890 wpa_s->network_select = 1;
1891 wpa_s->auto_network_select = 1;
1892 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001893 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001894 }
1895#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001896#ifdef CONFIG_WPS
1897 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1898 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1899 timeout_sec = 0;
1900 timeout_usec = 500000;
1901 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1902 timeout_usec);
1903 return 0;
1904 }
1905#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906 if (wpa_supplicant_req_sched_scan(wpa_s))
1907 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1908 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001909
1910 wpa_msg_ctrl(wpa_s, MSG_INFO,
1911 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 }
1913 }
1914 return 0;
1915}
1916
1917
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001918static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1919 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001922 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001923
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001924 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001925 if (res == 2) {
1926 /*
1927 * Interface may have been removed, so must not dereference
1928 * wpa_s after this.
1929 */
1930 return 1;
1931 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001932
1933 if (res < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 /*
1935 * If no scan results could be fetched, then no need to
1936 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001937 * this scan. Similarly, if scan results started a new operation on this
1938 * interface, do not notify other interfaces to avoid concurrent
1939 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001941 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 }
1943
1944 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001945 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 * so, they get updated with this same scan info.
1947 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001948 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1949 radio_list) {
1950 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1952 "sibling", ifs->ifname);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001953 res = _wpa_supplicant_event_scan_results(ifs, data, 0,
1954 res > 0);
1955 if (res < 0)
1956 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 }
1958 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001959
1960 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961}
1962
1963#endif /* CONFIG_NO_SCAN_PROCESSING */
1964
1965
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001966int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1967{
1968#ifdef CONFIG_NO_SCAN_PROCESSING
1969 return -1;
1970#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001971 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001972
Dmitry Shmidt29333592017-01-09 12:27:11 -08001973 wpa_s->ignore_post_flush_scan_res = 0;
1974
Dmitry Shmidt41712582015-06-29 11:02:15 -07001975 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001976 return -1;
1977
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001978 os_get_reltime(&now);
1979 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001980 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1981 return -1;
1982 }
1983
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001984 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001985#endif /* CONFIG_NO_SCAN_PROCESSING */
1986}
1987
Dmitry Shmidt04949592012-07-19 12:16:46 -07001988#ifdef CONFIG_WNM
1989
1990static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1991{
1992 struct wpa_supplicant *wpa_s = eloop_ctx;
1993
1994 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1995 return;
1996
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001997 if (!wpa_s->no_keep_alive) {
1998 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1999 MAC2STR(wpa_s->bssid));
2000 /* TODO: could skip this if normal data traffic has been sent */
2001 /* TODO: Consider using some more appropriate data frame for
2002 * this */
2003 if (wpa_s->l2)
2004 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
2005 (u8 *) "", 0);
2006 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002007
2008#ifdef CONFIG_SME
2009 if (wpa_s->sme.bss_max_idle_period) {
2010 unsigned int msec;
2011 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
2012 if (msec > 100)
2013 msec -= 100;
2014 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2015 wnm_bss_keep_alive, wpa_s, NULL);
2016 }
2017#endif /* CONFIG_SME */
2018}
2019
2020
2021static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
2022 const u8 *ies, size_t ies_len)
2023{
2024 struct ieee802_11_elems elems;
2025
2026 if (ies == NULL)
2027 return;
2028
2029 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2030 return;
2031
2032#ifdef CONFIG_SME
2033 if (elems.bss_max_idle_period) {
2034 unsigned int msec;
2035 wpa_s->sme.bss_max_idle_period =
2036 WPA_GET_LE16(elems.bss_max_idle_period);
2037 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
2038 "TU)%s", wpa_s->sme.bss_max_idle_period,
2039 (elems.bss_max_idle_period[2] & 0x01) ?
2040 " (protected keep-live required)" : "");
2041 if (wpa_s->sme.bss_max_idle_period == 0)
2042 wpa_s->sme.bss_max_idle_period = 1;
2043 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
2044 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2045 /* msec times 1000 */
2046 msec = wpa_s->sme.bss_max_idle_period * 1024;
2047 if (msec > 100)
2048 msec -= 100;
2049 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
2050 wnm_bss_keep_alive, wpa_s,
2051 NULL);
2052 }
2053 }
2054#endif /* CONFIG_SME */
2055}
2056
2057#endif /* CONFIG_WNM */
2058
2059
2060void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
2061{
2062#ifdef CONFIG_WNM
2063 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
2064#endif /* CONFIG_WNM */
2065}
2066
2067
Dmitry Shmidt051af732013-10-22 13:52:46 -07002068#ifdef CONFIG_INTERWORKING
2069
2070static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
2071 size_t len)
2072{
2073 int res;
2074
2075 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
2076 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
2077 if (res) {
2078 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
2079 }
2080
2081 return res;
2082}
2083
2084
2085static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
2086 const u8 *ies, size_t ies_len)
2087{
2088 struct ieee802_11_elems elems;
2089
2090 if (ies == NULL)
2091 return;
2092
2093 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
2094 return;
2095
2096 if (elems.qos_map_set) {
2097 wpas_qos_map_set(wpa_s, elems.qos_map_set,
2098 elems.qos_map_set_len);
2099 }
2100}
2101
2102#endif /* CONFIG_INTERWORKING */
2103
2104
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002105#ifdef CONFIG_FST
2106static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
2107 const u8 *ie, size_t ie_len)
2108{
2109 struct mb_ies_info mb_ies;
2110
2111 if (!ie || !ie_len || !wpa_s->fst)
2112 return -ENOENT;
2113
2114 os_memset(&mb_ies, 0, sizeof(mb_ies));
2115
2116 while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
2117 size_t len;
2118
2119 len = 2 + ie[1];
2120 if (len > ie_len) {
2121 wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
2122 ie, ie_len);
2123 break;
2124 }
2125
2126 if (ie[0] == WLAN_EID_MULTI_BAND) {
2127 wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
2128 (unsigned int) len);
2129 mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
2130 mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
2131 mb_ies.nof_ies++;
2132 }
2133
2134 ie_len -= len;
2135 ie += len;
2136 }
2137
2138 if (mb_ies.nof_ies > 0) {
2139 wpabuf_free(wpa_s->received_mb_ies);
2140 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2141 return 0;
2142 }
2143
2144 return -ENOENT;
2145}
2146#endif /* CONFIG_FST */
2147
2148
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
2150 union wpa_event_data *data)
2151{
2152 int l, len, found = 0, wpa_found, rsn_found;
2153 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002154#ifdef CONFIG_IEEE80211R
2155 u8 bssid[ETH_ALEN];
2156#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157
2158 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
2159 if (data->assoc_info.req_ies)
2160 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
2161 data->assoc_info.req_ies_len);
2162 if (data->assoc_info.resp_ies) {
2163 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
2164 data->assoc_info.resp_ies_len);
2165#ifdef CONFIG_TDLS
2166 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
2167 data->assoc_info.resp_ies_len);
2168#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002169#ifdef CONFIG_WNM
2170 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2171 data->assoc_info.resp_ies_len);
2172#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07002173#ifdef CONFIG_INTERWORKING
2174 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
2175 data->assoc_info.resp_ies_len);
2176#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 }
2178 if (data->assoc_info.beacon_ies)
2179 wpa_hexdump(MSG_DEBUG, "beacon_ies",
2180 data->assoc_info.beacon_ies,
2181 data->assoc_info.beacon_ies_len);
2182 if (data->assoc_info.freq)
2183 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
2184 data->assoc_info.freq);
2185
2186 p = data->assoc_info.req_ies;
2187 l = data->assoc_info.req_ies_len;
2188
2189 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
2190 while (p && l >= 2) {
2191 len = p[1] + 2;
2192 if (len > l) {
2193 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2194 p, l);
2195 break;
2196 }
2197 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2198 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
Dmitry Shmidte4663042016-04-04 10:07:49 -07002199 (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
2200 (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
2202 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
2203 break;
2204 found = 1;
2205 wpa_find_assoc_pmkid(wpa_s);
2206 break;
2207 }
2208 l -= len;
2209 p += len;
2210 }
2211 if (!found && data->assoc_info.req_ies)
2212 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2213
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002214#ifdef CONFIG_FILS
2215#ifdef CONFIG_SME
2216 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS &&
2217 (!data->assoc_info.resp_frame ||
2218 fils_process_assoc_resp(wpa_s->wpa,
2219 data->assoc_info.resp_frame,
2220 data->assoc_info.resp_frame_len) < 0)) {
2221 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
2222 return -1;
2223 }
2224#endif /* CONFIG_SME */
2225#endif /* CONFIG_FILS */
2226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227#ifdef CONFIG_IEEE80211R
2228#ifdef CONFIG_SME
2229 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2231 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2232 data->assoc_info.resp_ies,
2233 data->assoc_info.resp_ies_len,
2234 bssid) < 0) {
2235 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2236 "Reassociation Response failed");
2237 wpa_supplicant_deauthenticate(
2238 wpa_s, WLAN_REASON_INVALID_IE);
2239 return -1;
2240 }
2241 }
2242
2243 p = data->assoc_info.resp_ies;
2244 l = data->assoc_info.resp_ies_len;
2245
2246#ifdef CONFIG_WPS_STRICT
2247 if (p && wpa_s->current_ssid &&
2248 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
2249 struct wpabuf *wps;
2250 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
2251 if (wps == NULL) {
2252 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
2253 "include WPS IE in (Re)Association Response");
2254 return -1;
2255 }
2256
2257 if (wps_validate_assoc_resp(wps) < 0) {
2258 wpabuf_free(wps);
2259 wpa_supplicant_deauthenticate(
2260 wpa_s, WLAN_REASON_INVALID_IE);
2261 return -1;
2262 }
2263 wpabuf_free(wps);
2264 }
2265#endif /* CONFIG_WPS_STRICT */
2266
2267 /* Go through the IEs and make a copy of the MDIE, if present. */
2268 while (p && l >= 2) {
2269 len = p[1] + 2;
2270 if (len > l) {
2271 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
2272 p, l);
2273 break;
2274 }
2275 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
2276 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
2277 wpa_s->sme.ft_used = 1;
2278 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
2279 MOBILITY_DOMAIN_ID_LEN);
2280 break;
2281 }
2282 l -= len;
2283 p += len;
2284 }
2285#endif /* CONFIG_SME */
2286
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002287 /* Process FT when SME is in the driver */
2288 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
2289 wpa_ft_is_completed(wpa_s->wpa)) {
2290 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
2291 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
2292 data->assoc_info.resp_ies,
2293 data->assoc_info.resp_ies_len,
2294 bssid) < 0) {
2295 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
2296 "Reassociation Response failed");
2297 wpa_supplicant_deauthenticate(
2298 wpa_s, WLAN_REASON_INVALID_IE);
2299 return -1;
2300 }
2301 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
2302 }
2303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
2305 data->assoc_info.resp_ies_len);
2306#endif /* CONFIG_IEEE80211R */
2307
2308 /* WPA/RSN IE from Beacon/ProbeResp */
2309 p = data->assoc_info.beacon_ies;
2310 l = data->assoc_info.beacon_ies_len;
2311
2312 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
2313 */
2314 wpa_found = rsn_found = 0;
2315 while (p && l >= 2) {
2316 len = p[1] + 2;
2317 if (len > l) {
2318 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
2319 p, l);
2320 break;
2321 }
2322 if (!wpa_found &&
2323 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2324 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2325 wpa_found = 1;
2326 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2327 }
2328
2329 if (!rsn_found &&
2330 p[0] == WLAN_EID_RSN && p[1] >= 2) {
2331 rsn_found = 1;
2332 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2333 }
2334
2335 l -= len;
2336 p += len;
2337 }
2338
2339 if (!wpa_found && data->assoc_info.beacon_ies)
2340 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2341 if (!rsn_found && data->assoc_info.beacon_ies)
2342 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2343 if (wpa_found || rsn_found)
2344 wpa_s->ap_ies_from_associnfo = 1;
2345
Jouni Malinen87fd2792011-05-16 18:35:42 +03002346 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2347 wpa_s->assoc_freq != data->assoc_info.freq) {
2348 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2349 "%u to %u MHz",
2350 wpa_s->assoc_freq, data->assoc_info.freq);
2351 wpa_supplicant_update_scan_results(wpa_s);
2352 }
2353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 wpa_s->assoc_freq = data->assoc_info.freq;
2355
2356 return 0;
2357}
2358
2359
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002360static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2361{
2362 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2363
2364 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2365 return -1;
2366
2367 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2368 return 0;
2369
2370 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2371 WPA_IE_VENDOR_TYPE);
2372 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2373
2374 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2375 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2376 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2377 bss_rsn ? 2 + bss_rsn[1] : 0))
2378 return -1;
2379
2380 return 0;
2381}
2382
2383
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002384static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
2385 union wpa_event_data *data)
2386{
2387#ifdef CONFIG_FST
2388 struct assoc_info *ai = data ? &data->assoc_info : NULL;
2389 struct wpa_bss *bss = wpa_s->current_bss;
2390 const u8 *ieprb, *iebcn;
2391
2392 wpabuf_free(wpa_s->received_mb_ies);
2393 wpa_s->received_mb_ies = NULL;
2394
2395 if (ai &&
2396 !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
2397 wpa_printf(MSG_DEBUG,
2398 "FST: MB IEs updated from Association Response frame");
2399 return;
2400 }
2401
2402 if (ai &&
2403 !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
2404 wpa_printf(MSG_DEBUG,
2405 "FST: MB IEs updated from association event Beacon IEs");
2406 return;
2407 }
2408
2409 if (!bss)
2410 return;
2411
2412 ieprb = (const u8 *) (bss + 1);
2413 iebcn = ieprb + bss->ie_len;
2414
2415 if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
2416 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
2417 else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
2418 wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
2419#endif /* CONFIG_FST */
2420}
2421
2422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2424 union wpa_event_data *data)
2425{
2426 u8 bssid[ETH_ALEN];
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002427 int ft_completed, already_authorized;
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002428 int new_bss = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429
2430#ifdef CONFIG_AP
2431 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002432 if (!data)
2433 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2435 data->assoc_info.addr,
2436 data->assoc_info.req_ies,
2437 data->assoc_info.req_ies_len,
2438 data->assoc_info.reassoc);
2439 return;
2440 }
2441#endif /* CONFIG_AP */
2442
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002443 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2446 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2447 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002448 /*
2449 * FILS authentication can share the same mechanism to mark the
2450 * connection fully authenticated, so set ft_completed also based on
2451 * FILS result.
2452 */
2453 if (!ft_completed)
2454 ft_completed = wpa_fils_is_completed(wpa_s->wpa);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002456 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2457 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002458 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002459 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2460 return;
2461 }
2462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002464 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2466 MACSTR, MAC2STR(bssid));
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002467 new_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2470 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002471 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472
2473 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2474 wpa_clear_keys(wpa_s, bssid);
2475 }
2476 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002477 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2479 return;
2480 }
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002481 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002482
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002483 if (wpa_s->conf->ap_scan == 1 &&
2484 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002485 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2486 wpa_msg(wpa_s, MSG_WARNING,
2487 "WPA/RSN IEs not updated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 }
2489
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002490 wpas_fst_update_mb_assoc(wpa_s, data);
2491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492#ifdef CONFIG_SME
2493 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2494 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002495 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496#endif /* CONFIG_SME */
2497
2498 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2499 if (wpa_s->current_ssid) {
2500 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2501 * initialized before association, but for other modes,
2502 * initialize PC/SC here, if the current configuration needs
2503 * smartcard or SIM/USIM. */
2504 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2505 }
2506 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2507 if (wpa_s->l2)
2508 l2_packet_notify_auth_start(wpa_s->l2);
2509
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002510 already_authorized = data && data->assoc_info.authorized;
2511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 /*
2513 * Set portEnabled first to FALSE in order to get EAP state machine out
2514 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2515 * state machine may transit to AUTHENTICATING state based on obsolete
2516 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2517 * AUTHENTICATED without ever giving chance to EAP state machine to
2518 * reset the state.
2519 */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002520 if (!ft_completed && !already_authorized) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2522 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2523 }
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002524 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed ||
2525 already_authorized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2527 /* 802.1X::portControl = Auto */
2528 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2529 wpa_s->eapol_received = 0;
2530 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2531 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2532 (wpa_s->current_ssid &&
2533 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002534 if (wpa_s->current_ssid &&
2535 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002536 (wpa_s->drv_flags &
2537 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2538 /*
2539 * Set the key after having received joined-IBSS event
2540 * from the driver.
2541 */
2542 wpa_supplicant_set_wpa_none_key(wpa_s,
2543 wpa_s->current_ssid);
2544 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 wpa_supplicant_cancel_auth_timeout(wpa_s);
2546 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2547 } else if (!ft_completed) {
2548 /* Timeout for receiving the first EAPOL packet */
2549 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2550 }
2551 wpa_supplicant_cancel_scan(wpa_s);
2552
2553 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2554 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2555 /*
2556 * We are done; the driver will take care of RSN 4-way
2557 * handshake.
2558 */
2559 wpa_supplicant_cancel_auth_timeout(wpa_s);
2560 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2561 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2562 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2563 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2564 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2565 /*
2566 * The driver will take care of RSN 4-way handshake, so we need
2567 * to allow EAPOL supplicant to complete its work without
2568 * waiting for WPA supplicant.
2569 */
2570 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2571 } else if (ft_completed) {
2572 /*
2573 * FT protocol completed - make sure EAPOL state machine ends
2574 * up in authenticated.
2575 */
2576 wpa_supplicant_cancel_auth_timeout(wpa_s);
2577 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2578 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2579 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2580 }
2581
Jouni Malinena05074c2012-12-21 21:35:35 +02002582 wpa_s->last_eapol_matches_bssid = 0;
2583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002585 struct os_reltime now, age;
2586 os_get_reltime(&now);
2587 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Paul Stewart092955c2017-02-06 09:13:09 -08002588 if (age.sec == 0 && age.usec < 200000 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2590 0) {
2591 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2592 "frame that was received just before "
2593 "association notification");
2594 wpa_supplicant_rx_eapol(
2595 wpa_s, wpa_s->pending_eapol_rx_src,
2596 wpabuf_head(wpa_s->pending_eapol_rx),
2597 wpabuf_len(wpa_s->pending_eapol_rx));
2598 }
2599 wpabuf_free(wpa_s->pending_eapol_rx);
2600 wpa_s->pending_eapol_rx = NULL;
2601 }
2602
2603 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2604 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002605 wpa_s->current_ssid &&
2606 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 /* Set static WEP keys again */
2608 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2609 }
2610
2611#ifdef CONFIG_IBSS_RSN
2612 if (wpa_s->current_ssid &&
2613 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2614 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2615 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2616 wpa_s->ibss_rsn == NULL) {
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07002617 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 if (!wpa_s->ibss_rsn) {
2619 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2620 wpa_supplicant_deauthenticate(
2621 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2622 return;
2623 }
2624
2625 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2626 }
2627#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002628
2629 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002630
2631 if (data) {
2632 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2633 data->assoc_info.resp_ies_len,
2634 &data->assoc_info.wmm_params);
2635
2636 if (wpa_s->reassoc_same_bss)
2637 wmm_ac_restore_tspecs(wpa_s);
2638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639}
2640
2641
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642static int disconnect_reason_recoverable(u16 reason_code)
2643{
2644 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2645 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2646 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2647}
2648
2649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002650static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002651 u16 reason_code,
2652 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653{
2654 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002655
2656 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2657 /*
2658 * At least Host AP driver and a Prism3 card seemed to be
2659 * generating streams of disconnected events when configuring
2660 * IBSS for WPA-None. Ignore them for now.
2661 */
2662 return;
2663 }
2664
2665 bssid = wpa_s->bssid;
2666 if (is_zero_ether_addr(bssid))
2667 bssid = wpa_s->pending_bssid;
2668
2669 if (!is_zero_ether_addr(bssid) ||
2670 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2671 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2672 " reason=%d%s",
2673 MAC2STR(bssid), reason_code,
2674 locally_generated ? " locally_generated=1" : "");
2675 }
2676}
2677
2678
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002679static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2680 int locally_generated)
2681{
2682 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2683 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2684 return 0; /* Not in 4-way handshake with PSK */
2685
2686 /*
2687 * It looks like connection was lost while trying to go through PSK
2688 * 4-way handshake. Filter out known disconnection cases that are caused
2689 * by something else than PSK mismatch to avoid confusing reports.
2690 */
2691
2692 if (locally_generated) {
2693 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2694 return 0;
2695 }
2696
2697 return 1;
2698}
2699
2700
Jouni Malinen2b89da82012-08-31 22:04:41 +03002701static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2702 u16 reason_code,
2703 int locally_generated)
2704{
2705 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 int authenticating;
2707 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002708 struct wpa_bss *fast_reconnect = NULL;
2709 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002710 struct wpa_ssid *last_ssid;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002711 struct wpa_bss *curr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712
2713 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2714 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2715
2716 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2717 /*
2718 * At least Host AP driver and a Prism3 card seemed to be
2719 * generating streams of disconnected events when configuring
2720 * IBSS for WPA-None. Ignore them for now.
2721 */
2722 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2723 "IBSS/WPA-None mode");
2724 return;
2725 }
2726
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002727 if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
2728 reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
2729 locally_generated)
2730 /*
2731 * Remove the inactive AP (which is probably out of range) from
2732 * the BSS list after marking disassociation. In particular
2733 * mac80211-based drivers use the
2734 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
2735 * locally generated disconnection events for cases where the
2736 * AP does not reply anymore.
2737 */
2738 curr = wpa_s->current_bss;
2739
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002740 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002741 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2742 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002743 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2744 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002745 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002747 if (!wpa_s->disconnected &&
2748 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002749 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002750 wpas_wps_searching(wpa_s) ||
2751 wpas_wps_reenable_networks_pending(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002753 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002754 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002755 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002756 wpa_s->wpa_state);
2757 if (wpa_s->wpa_state == WPA_COMPLETED &&
2758 wpa_s->current_ssid &&
2759 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002760 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002761 disconnect_reason_recoverable(reason_code)) {
2762 /*
2763 * It looks like the AP has dropped association with
2764 * us, but could allow us to get back in. Try to
2765 * reconnect to the same BSS without full scan to save
2766 * time for some common cases.
2767 */
2768 fast_reconnect = wpa_s->current_bss;
2769 fast_reconnect_ssid = wpa_s->current_ssid;
2770 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002771 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002772 else
2773 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2774 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002776 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 "try to re-connect");
2778 wpa_s->reassociate = 0;
2779 wpa_s->disconnected = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002780 if (!wpa_s->pno)
2781 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 }
2783 bssid = wpa_s->bssid;
2784 if (is_zero_ether_addr(bssid))
2785 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002786 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2787 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002789 if (locally_generated)
2790 wpa_s->disconnect_reason = -reason_code;
2791 else
2792 wpa_s->disconnect_reason = reason_code;
2793 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002794 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2795 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 wpa_clear_keys(wpa_s, wpa_s->bssid);
2797 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002798 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 wpa_supplicant_mark_disassoc(wpa_s);
2800
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002801 if (curr)
2802 wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
2803
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002804 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002806 wpa_s->current_ssid = last_ssid;
2807 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002808
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002809 if (fast_reconnect &&
2810 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2811 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2812 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2813 fast_reconnect->ssid_len) &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002814 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
2815 !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect->bssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002816#ifndef CONFIG_NO_SCAN_PROCESSING
2817 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2818 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2819 fast_reconnect_ssid) < 0) {
2820 /* Recover through full scan */
2821 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2822 }
2823#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002824 } else if (fast_reconnect) {
2825 /*
2826 * Could not reconnect to the same BSS due to network being
2827 * disabled. Use a new scan to match the alternative behavior
2828 * above, i.e., to continue automatic reconnection attempt in a
2829 * way that enforces disabled network rules.
2830 */
2831 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002832 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833}
2834
2835
2836#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002837void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838{
2839 struct wpa_supplicant *wpa_s = eloop_ctx;
2840
2841 if (!wpa_s->pending_mic_error_report)
2842 return;
2843
2844 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2845 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2846 wpa_s->pending_mic_error_report = 0;
2847}
2848#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2849
2850
2851static void
2852wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2853 union wpa_event_data *data)
2854{
2855 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002856 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857
2858 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2859 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002860 os_get_reltime(&t);
2861 if ((wpa_s->last_michael_mic_error.sec &&
2862 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 wpa_s->pending_mic_error_report) {
2864 if (wpa_s->pending_mic_error_report) {
2865 /*
2866 * Send the pending MIC error report immediately since
2867 * we are going to start countermeasures and AP better
2868 * do the same.
2869 */
2870 wpa_sm_key_request(wpa_s->wpa, 1,
2871 wpa_s->pending_mic_error_pairwise);
2872 }
2873
2874 /* Send the new MIC error report immediately since we are going
2875 * to start countermeasures and AP better do the same.
2876 */
2877 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2878
2879 /* initialize countermeasures */
2880 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002881
2882 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2885
2886 /*
2887 * Need to wait for completion of request frame. We do not get
2888 * any callback for the message completion, so just wait a
2889 * short while and hope for the best. */
2890 os_sleep(0, 10000);
2891
2892 wpa_drv_set_countermeasures(wpa_s, 1);
2893 wpa_supplicant_deauthenticate(wpa_s,
2894 WLAN_REASON_MICHAEL_MIC_FAILURE);
2895 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2896 wpa_s, NULL);
2897 eloop_register_timeout(60, 0,
2898 wpa_supplicant_stop_countermeasures,
2899 wpa_s, NULL);
2900 /* TODO: mark the AP rejected for 60 second. STA is
2901 * allowed to associate with another AP.. */
2902 } else {
2903#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2904 if (wpa_s->mic_errors_seen) {
2905 /*
2906 * Reduce the effectiveness of Michael MIC error
2907 * reports as a means for attacking against TKIP if
2908 * more than one MIC failure is noticed with the same
2909 * PTK. We delay the transmission of the reports by a
2910 * random time between 0 and 60 seconds in order to
2911 * force the attacker wait 60 seconds before getting
2912 * the information on whether a frame resulted in a MIC
2913 * failure.
2914 */
2915 u8 rval[4];
2916 int sec;
2917
2918 if (os_get_random(rval, sizeof(rval)) < 0)
2919 sec = os_random() % 60;
2920 else
2921 sec = WPA_GET_BE32(rval) % 60;
2922 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2923 "report %d seconds", sec);
2924 wpa_s->pending_mic_error_report = 1;
2925 wpa_s->pending_mic_error_pairwise = pairwise;
2926 eloop_cancel_timeout(
2927 wpa_supplicant_delayed_mic_error_report,
2928 wpa_s, NULL);
2929 eloop_register_timeout(
2930 sec, os_random() % 1000000,
2931 wpa_supplicant_delayed_mic_error_report,
2932 wpa_s, NULL);
2933 } else {
2934 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2935 }
2936#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2937 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2938#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2939 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002940 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 wpa_s->mic_errors_seen++;
2942}
2943
2944
2945#ifdef CONFIG_TERMINATE_ONLASTIF
2946static int any_interfaces(struct wpa_supplicant *head)
2947{
2948 struct wpa_supplicant *wpa_s;
2949
2950 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2951 if (!wpa_s->interface_removed)
2952 return 1;
2953 return 0;
2954}
2955#endif /* CONFIG_TERMINATE_ONLASTIF */
2956
2957
2958static void
2959wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2960 union wpa_event_data *data)
2961{
2962 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2963 return;
2964
2965 switch (data->interface_status.ievent) {
2966 case EVENT_INTERFACE_ADDED:
2967 if (!wpa_s->interface_removed)
2968 break;
2969 wpa_s->interface_removed = 0;
2970 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2971 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2972 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2973 "driver after interface was added");
2974 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002975
2976#ifdef CONFIG_P2P
2977 if (!wpa_s->global->p2p &&
2978 !wpa_s->global->p2p_disabled &&
2979 !wpa_s->conf->p2p_disabled &&
2980 (wpa_s->drv_flags &
2981 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2982 wpas_p2p_add_p2pdev_interface(
2983 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2984 wpa_printf(MSG_INFO,
2985 "P2P: Failed to enable P2P Device interface");
2986 /* Try to continue without. P2P will be disabled. */
2987 }
2988#endif /* CONFIG_P2P */
2989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990 break;
2991 case EVENT_INTERFACE_REMOVED:
2992 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2993 wpa_s->interface_removed = 1;
2994 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002995 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 l2_packet_deinit(wpa_s->l2);
2997 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002998
2999#ifdef CONFIG_P2P
3000 if (wpa_s->global->p2p &&
3001 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
3002 (wpa_s->drv_flags &
3003 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
3004 wpa_dbg(wpa_s, MSG_DEBUG,
3005 "Removing P2P Device interface");
3006 wpa_supplicant_remove_iface(
3007 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
3008 0);
3009 wpa_s->global->p2p_init_wpa_s = NULL;
3010 }
3011#endif /* CONFIG_P2P */
3012
Dmitry Shmidte4663042016-04-04 10:07:49 -07003013#ifdef CONFIG_MATCH_IFACE
3014 if (wpa_s->matched) {
3015 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
3016 break;
3017 }
3018#endif /* CONFIG_MATCH_IFACE */
3019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020#ifdef CONFIG_TERMINATE_ONLASTIF
3021 /* check if last interface */
3022 if (!any_interfaces(wpa_s->global->ifaces))
3023 eloop_terminate();
3024#endif /* CONFIG_TERMINATE_ONLASTIF */
3025 break;
3026 }
3027}
3028
3029
3030#ifdef CONFIG_PEERKEY
3031static void
3032wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
3033 union wpa_event_data *data)
3034{
3035 if (data == NULL)
3036 return;
3037 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
3038}
3039#endif /* CONFIG_PEERKEY */
3040
3041
3042#ifdef CONFIG_TDLS
3043static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
3044 union wpa_event_data *data)
3045{
3046 if (data == NULL)
3047 return;
3048 switch (data->tdls.oper) {
3049 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08003050 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
3051 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3052 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
3053 else
3054 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 break;
3056 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03003057 if (wpa_tdls_is_external_setup(wpa_s->wpa))
3058 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
3059 data->tdls.reason_code);
3060 else
3061 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
3062 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003064 case TDLS_REQUEST_DISCOVER:
3065 wpa_tdls_send_discovery_request(wpa_s->wpa,
3066 data->tdls.peer);
3067 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 }
3069}
3070#endif /* CONFIG_TDLS */
3071
3072
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003073#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003074static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
3075 union wpa_event_data *data)
3076{
3077 if (data == NULL)
3078 return;
3079 switch (data->wnm.oper) {
3080 case WNM_OPER_SLEEP:
3081 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
3082 "(action=%d, intval=%d)",
3083 data->wnm.sleep_action, data->wnm.sleep_intval);
3084 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003085 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003086 break;
3087 }
3088}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003089#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003090
3091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092#ifdef CONFIG_IEEE80211R
3093static void
3094wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
3095 union wpa_event_data *data)
3096{
3097 if (data == NULL)
3098 return;
3099
3100 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
3101 data->ft_ies.ies_len,
3102 data->ft_ies.ft_action,
3103 data->ft_ies.target_ap,
3104 data->ft_ies.ric_ies,
3105 data->ft_ies.ric_ies_len) < 0) {
3106 /* TODO: prevent MLME/driver from trying to associate? */
3107 }
3108}
3109#endif /* CONFIG_IEEE80211R */
3110
3111
3112#ifdef CONFIG_IBSS_RSN
3113static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
3114 union wpa_event_data *data)
3115{
3116 struct wpa_ssid *ssid;
3117 if (wpa_s->wpa_state < WPA_ASSOCIATED)
3118 return;
3119 if (data == NULL)
3120 return;
3121 ssid = wpa_s->current_ssid;
3122 if (ssid == NULL)
3123 return;
3124 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3125 return;
3126
3127 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
3128}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003129
3130
3131static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
3132 union wpa_event_data *data)
3133{
3134 struct wpa_ssid *ssid = wpa_s->current_ssid;
3135
3136 if (ssid == NULL)
3137 return;
3138
3139 /* check if the ssid is correctly configured as IBSS/RSN */
3140 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
3141 return;
3142
3143 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
3144 data->rx_mgmt.frame_len);
3145}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146#endif /* CONFIG_IBSS_RSN */
3147
3148
3149#ifdef CONFIG_IEEE80211R
3150static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
3151 size_t len)
3152{
3153 const u8 *sta_addr, *target_ap_addr;
3154 u16 status;
3155
3156 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
3157 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
3158 return; /* only SME case supported for now */
3159 if (len < 1 + 2 * ETH_ALEN + 2)
3160 return;
3161 if (data[0] != 2)
3162 return; /* Only FT Action Response is supported for now */
3163 sta_addr = data + 1;
3164 target_ap_addr = data + 1 + ETH_ALEN;
3165 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
3166 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
3167 MACSTR " TargetAP " MACSTR " status %u",
3168 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
3169
3170 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
3171 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
3172 " in FT Action Response", MAC2STR(sta_addr));
3173 return;
3174 }
3175
3176 if (status) {
3177 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
3178 "failure (status code %d)", status);
3179 /* TODO: report error to FT code(?) */
3180 return;
3181 }
3182
3183 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
3184 len - (1 + 2 * ETH_ALEN + 2), 1,
3185 target_ap_addr, NULL, 0) < 0)
3186 return;
3187
3188#ifdef CONFIG_SME
3189 {
3190 struct wpa_bss *bss;
3191 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
3192 if (bss)
3193 wpa_s->sme.freq = bss->freq;
3194 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
3195 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
3196 WLAN_AUTH_FT);
3197 }
3198#endif /* CONFIG_SME */
3199}
3200#endif /* CONFIG_IEEE80211R */
3201
3202
3203static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
3204 struct unprot_deauth *e)
3205{
3206#ifdef CONFIG_IEEE80211W
3207 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
3208 "dropped: " MACSTR " -> " MACSTR
3209 " (reason code %u)",
3210 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3211 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3212#endif /* CONFIG_IEEE80211W */
3213}
3214
3215
3216static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
3217 struct unprot_disassoc *e)
3218{
3219#ifdef CONFIG_IEEE80211W
3220 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
3221 "dropped: " MACSTR " -> " MACSTR
3222 " (reason code %u)",
3223 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
3224 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
3225#endif /* CONFIG_IEEE80211W */
3226}
3227
3228
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003229static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
3230 u16 reason_code, int locally_generated,
3231 const u8 *ie, size_t ie_len, int deauth)
3232{
3233#ifdef CONFIG_AP
3234 if (wpa_s->ap_iface && addr) {
3235 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
3236 return;
3237 }
3238
3239 if (wpa_s->ap_iface) {
3240 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
3241 return;
3242 }
3243#endif /* CONFIG_AP */
3244
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003245 if (!locally_generated)
3246 wpa_s->own_disconnect_req = 0;
3247
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003248 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
3249
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003250 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
3251 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
3252 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
3253 eapol_sm_failed(wpa_s->eapol))) &&
3254 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003255 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003256
3257#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003258 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003259 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
3260 locally_generated) > 0) {
3261 /*
3262 * The interface was removed, so cannot continue
3263 * processing any additional operations after this.
3264 */
3265 return;
3266 }
3267 }
3268#endif /* CONFIG_P2P */
3269
3270 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
3271 locally_generated);
3272}
3273
3274
3275static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
3276 struct disassoc_info *info)
3277{
3278 u16 reason_code = 0;
3279 int locally_generated = 0;
3280 const u8 *addr = NULL;
3281 const u8 *ie = NULL;
3282 size_t ie_len = 0;
3283
3284 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
3285
3286 if (info) {
3287 addr = info->addr;
3288 ie = info->ie;
3289 ie_len = info->ie_len;
3290 reason_code = info->reason_code;
3291 locally_generated = info->locally_generated;
3292 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
3293 locally_generated ? " (locally generated)" : "");
3294 if (addr)
3295 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3296 MAC2STR(addr));
3297 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
3298 ie, ie_len);
3299 }
3300
3301#ifdef CONFIG_AP
3302 if (wpa_s->ap_iface && info && info->addr) {
3303 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
3304 return;
3305 }
3306
3307 if (wpa_s->ap_iface) {
3308 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
3309 return;
3310 }
3311#endif /* CONFIG_AP */
3312
3313#ifdef CONFIG_P2P
3314 if (info) {
3315 wpas_p2p_disassoc_notif(
3316 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
3317 locally_generated);
3318 }
3319#endif /* CONFIG_P2P */
3320
3321 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3322 sme_event_disassoc(wpa_s, info);
3323
3324 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
3325 ie, ie_len, 0);
3326}
3327
3328
3329static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
3330 struct deauth_info *info)
3331{
3332 u16 reason_code = 0;
3333 int locally_generated = 0;
3334 const u8 *addr = NULL;
3335 const u8 *ie = NULL;
3336 size_t ie_len = 0;
3337
3338 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
3339
3340 if (info) {
3341 addr = info->addr;
3342 ie = info->ie;
3343 ie_len = info->ie_len;
3344 reason_code = info->reason_code;
3345 locally_generated = info->locally_generated;
3346 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
3347 reason_code,
3348 locally_generated ? " (locally generated)" : "");
3349 if (addr) {
3350 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
3351 MAC2STR(addr));
3352 }
3353 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
3354 ie, ie_len);
3355 }
3356
3357 wpa_reset_ft_completed(wpa_s->wpa);
3358
3359 wpas_event_disconnect(wpa_s, addr, reason_code,
3360 locally_generated, ie, ie_len, 1);
3361}
3362
3363
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003364static const char * reg_init_str(enum reg_change_initiator init)
3365{
3366 switch (init) {
3367 case REGDOM_SET_BY_CORE:
3368 return "CORE";
3369 case REGDOM_SET_BY_USER:
3370 return "USER";
3371 case REGDOM_SET_BY_DRIVER:
3372 return "DRIVER";
3373 case REGDOM_SET_BY_COUNTRY_IE:
3374 return "COUNTRY_IE";
3375 case REGDOM_BEACON_HINT:
3376 return "BEACON_HINT";
3377 }
3378 return "?";
3379}
3380
3381
3382static const char * reg_type_str(enum reg_type type)
3383{
3384 switch (type) {
3385 case REGDOM_TYPE_UNKNOWN:
3386 return "UNKNOWN";
3387 case REGDOM_TYPE_COUNTRY:
3388 return "COUNTRY";
3389 case REGDOM_TYPE_WORLD:
3390 return "WORLD";
3391 case REGDOM_TYPE_CUSTOM_WORLD:
3392 return "CUSTOM_WORLD";
3393 case REGDOM_TYPE_INTERSECTION:
3394 return "INTERSECTION";
3395 }
3396 return "?";
3397}
3398
3399
3400static void wpa_supplicant_update_channel_list(
3401 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003402{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003403 struct wpa_supplicant *ifs;
3404
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003405 /*
3406 * To allow backwards compatibility with higher level layers that
3407 * assumed the REGDOM_CHANGE event is sent over the initially added
3408 * interface. Find the highest parent of this interface and use it to
3409 * send the event.
3410 */
3411 for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
3412 ;
3413
3414 wpa_msg(ifs, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003415 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003416 info->alpha2[0] ? " alpha2=" : "",
3417 info->alpha2[0] ? info->alpha2 : "");
3418
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003419 if (wpa_s->drv_priv == NULL)
3420 return; /* Ignore event during drv initialization */
3421
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003422 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3423 radio_list) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003424 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3425 ifs->ifname);
3426 free_hw_features(ifs);
3427 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3428 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003429
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003430 /* Restart PNO/sched_scan with updated channel list */
3431 if (ifs->pno) {
3432 wpas_stop_pno(ifs);
3433 wpas_start_pno(ifs);
3434 } else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
3435 wpa_dbg(ifs, MSG_DEBUG,
3436 "Channel list changed - restart sched_scan");
3437 wpas_scan_restart_sched_scan(ifs);
3438 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003439 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003440
3441 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003442}
3443
3444
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003445static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003446 const u8 *frame, size_t len, int freq,
3447 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003448{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003449 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003450 const u8 *payload;
3451 size_t plen;
3452 u8 category;
3453
3454 if (len < IEEE80211_HDRLEN + 2)
3455 return;
3456
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003457 mgmt = (const struct ieee80211_mgmt *) frame;
3458 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003459 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003460 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003461
3462 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3463 " Category=%u DataLen=%d freq=%d MHz",
3464 MAC2STR(mgmt->sa), category, (int) plen, freq);
3465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003466 if (category == WLAN_ACTION_WMM) {
3467 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3468 return;
3469 }
3470
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003471#ifdef CONFIG_IEEE80211R
3472 if (category == WLAN_ACTION_FT) {
3473 ft_rx_action(wpa_s, payload, plen);
3474 return;
3475 }
3476#endif /* CONFIG_IEEE80211R */
3477
3478#ifdef CONFIG_IEEE80211W
3479#ifdef CONFIG_SME
3480 if (category == WLAN_ACTION_SA_QUERY) {
3481 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3482 return;
3483 }
3484#endif /* CONFIG_SME */
3485#endif /* CONFIG_IEEE80211W */
3486
3487#ifdef CONFIG_WNM
3488 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3489 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3490 return;
3491 }
3492#endif /* CONFIG_WNM */
3493
3494#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003495 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3496 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003497 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003498 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003499 payload, plen, freq) == 0)
3500 return;
3501#endif /* CONFIG_GAS */
3502
3503#ifdef CONFIG_TDLS
3504 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3505 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3506 wpa_dbg(wpa_s, MSG_DEBUG,
3507 "TDLS: Received Discovery Response from " MACSTR,
3508 MAC2STR(mgmt->sa));
3509 return;
3510 }
3511#endif /* CONFIG_TDLS */
3512
3513#ifdef CONFIG_INTERWORKING
3514 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3515 payload[0] == QOS_QOS_MAP_CONFIG) {
3516 const u8 *pos = payload + 1;
3517 size_t qlen = plen - 1;
3518 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3519 MACSTR, MAC2STR(mgmt->sa));
3520 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3521 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3522 pos[1] <= qlen - 2 && pos[1] >= 16)
3523 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3524 return;
3525 }
3526#endif /* CONFIG_INTERWORKING */
3527
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003528 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003529 payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
3530 wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
3531 payload + 1,
3532 plen - 1);
3533 return;
3534 }
3535
3536 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003537 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3538 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3539 return;
3540 }
3541
3542 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3543 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3544 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3545 payload + 1, plen - 1,
3546 rssi);
3547 return;
3548 }
3549
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003550#ifdef CONFIG_FST
3551 if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3552 fst_rx_action(wpa_s->fst, mgmt, len);
3553 return;
3554 }
3555#endif /* CONFIG_FST */
3556
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003557 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3558 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003559 if (wpa_s->ifmsh)
3560 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003561}
3562
3563
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003564static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3565 union wpa_event_data *event)
3566{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003567 struct wpa_freq_range_list *list;
3568 char *str = NULL;
3569
3570 list = &event->freq_range;
3571
3572 if (list->num)
3573 str = freq_range_list_str(list);
3574 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3575 str ? str : "");
3576
3577#ifdef CONFIG_P2P
3578 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3579 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3580 __func__);
3581 } else {
3582 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003583
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003584 /*
3585 * The update channel flow will also take care of moving a GO
3586 * from the unsafe frequency if needed.
3587 */
3588 wpas_p2p_update_channel_list(wpa_s,
3589 WPAS_P2P_CHANNEL_UPDATE_AVOID);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003590 }
3591#endif /* CONFIG_P2P */
3592
3593 os_free(str);
3594}
3595
3596
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003597static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3598 union wpa_event_data *data)
3599{
3600 wpa_dbg(wpa_s, MSG_DEBUG,
3601 "Connection authorized by device, previous state %d",
3602 wpa_s->wpa_state);
3603 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3604 wpa_supplicant_cancel_auth_timeout(wpa_s);
3605 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3606 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3607 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3608 }
3609 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3610 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003611 data->assoc_info.ptk_kck_len,
3612 data->assoc_info.ptk_kek,
3613 data->assoc_info.ptk_kek_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003614}
3615
3616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003617void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3618 union wpa_event_data *data)
3619{
3620 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003621 char buf[100];
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003622 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623
3624 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3625 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003626 event != EVENT_INTERFACE_STATUS &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003627 event != EVENT_SCAN_RESULTS &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003628 event != EVENT_SCHED_SCAN_STOPPED) {
3629 wpa_dbg(wpa_s, MSG_DEBUG,
3630 "Ignore event %s (%d) while interface is disabled",
3631 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632 return;
3633 }
3634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635#ifndef CONFIG_NO_STDOUT_DEBUG
3636{
3637 int level = MSG_DEBUG;
3638
Dmitry Shmidt04949592012-07-19 12:16:46 -07003639 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003640 const struct ieee80211_hdr *hdr;
3641 u16 fc;
3642 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3643 fc = le_to_host16(hdr->frame_control);
3644 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3645 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3646 level = MSG_EXCESSIVE;
3647 }
3648
3649 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3650 event_to_string(event), event);
3651}
3652#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003653
3654 switch (event) {
3655 case EVENT_AUTH:
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003656#ifdef CONFIG_FST
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003657 if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
3658 data->auth.ies_len))
3659 wpa_printf(MSG_DEBUG,
3660 "FST: MB IEs updated from auth IE");
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003661#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 sme_event_auth(wpa_s, data);
3663 break;
3664 case EVENT_ASSOC:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003665#ifdef CONFIG_TESTING_OPTIONS
3666 if (wpa_s->ignore_auth_resp) {
3667 wpa_printf(MSG_INFO,
3668 "EVENT_ASSOC - ignore_auth_resp active!");
3669 break;
3670 }
3671#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003673 if (data && data->assoc_info.authorized)
3674 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003675 if (data) {
3676 wpa_msg(wpa_s, MSG_INFO,
3677 WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
3678 data->assoc_info.subnet_status);
3679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 break;
3681 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003682 wpas_event_disassoc(wpa_s,
3683 data ? &data->disassoc_info : NULL);
3684 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 case EVENT_DEAUTH:
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003686#ifdef CONFIG_TESTING_OPTIONS
3687 if (wpa_s->ignore_auth_resp) {
3688 wpa_printf(MSG_INFO,
3689 "EVENT_DEAUTH - ignore_auth_resp active!");
3690 break;
3691 }
3692#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003693 wpas_event_deauth(wpa_s,
3694 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 break;
3696 case EVENT_MICHAEL_MIC_FAILURE:
3697 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3698 break;
3699#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003700 case EVENT_SCAN_STARTED:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003701 if (wpa_s->own_scan_requested ||
3702 (data && !data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003703 struct os_reltime diff;
3704
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003705 os_get_reltime(&wpa_s->scan_start_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003706 os_reltime_sub(&wpa_s->scan_start_time,
3707 &wpa_s->scan_trigger_time, &diff);
3708 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3709 diff.sec, diff.usec);
3710 wpa_s->own_scan_requested = 0;
3711 wpa_s->own_scan_running = 1;
3712 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3713 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003714 wpa_msg_ctrl(wpa_s, MSG_INFO,
3715 WPA_EVENT_SCAN_STARTED "id=%u",
3716 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003717 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003718 wpa_msg_ctrl(wpa_s, MSG_INFO,
3719 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003720 }
3721 } else {
3722 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003723 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003724 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003725 }
3726 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727 case EVENT_SCAN_RESULTS:
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003728 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3729 wpa_s->scan_res_handler = NULL;
3730 wpa_s->own_scan_running = 0;
3731 wpa_s->radio->external_scan_running = 0;
3732 wpa_s->last_scan_req = NORMAL_SCAN_REQ;
3733 break;
3734 }
3735
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003736 if (!(data && data->scan_info.external_scan) &&
3737 os_reltime_initialized(&wpa_s->scan_start_time)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003738 struct os_reltime now, diff;
3739 os_get_reltime(&now);
3740 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3741 wpa_s->scan_start_time.sec = 0;
3742 wpa_s->scan_start_time.usec = 0;
3743 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3744 diff.sec, diff.usec);
3745 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003746 if (wpa_supplicant_event_scan_results(wpa_s, data))
3747 break; /* interface may have been removed */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003748 if (!(data && data->scan_info.external_scan))
3749 wpa_s->own_scan_running = 0;
3750 if (data && data->scan_info.nl_scan_event)
3751 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003752 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003753 break;
3754#endif /* CONFIG_NO_SCAN_PROCESSING */
3755 case EVENT_ASSOCINFO:
3756 wpa_supplicant_event_associnfo(wpa_s, data);
3757 break;
3758 case EVENT_INTERFACE_STATUS:
3759 wpa_supplicant_event_interface_status(wpa_s, data);
3760 break;
3761 case EVENT_PMKID_CANDIDATE:
3762 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3763 break;
3764#ifdef CONFIG_PEERKEY
3765 case EVENT_STKSTART:
3766 wpa_supplicant_event_stkstart(wpa_s, data);
3767 break;
3768#endif /* CONFIG_PEERKEY */
3769#ifdef CONFIG_TDLS
3770 case EVENT_TDLS:
3771 wpa_supplicant_event_tdls(wpa_s, data);
3772 break;
3773#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003774#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003775 case EVENT_WNM:
3776 wpa_supplicant_event_wnm(wpa_s, data);
3777 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003778#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779#ifdef CONFIG_IEEE80211R
3780 case EVENT_FT_RESPONSE:
3781 wpa_supplicant_event_ft_response(wpa_s, data);
3782 break;
3783#endif /* CONFIG_IEEE80211R */
3784#ifdef CONFIG_IBSS_RSN
3785 case EVENT_IBSS_RSN_START:
3786 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3787 break;
3788#endif /* CONFIG_IBSS_RSN */
3789 case EVENT_ASSOC_REJECT:
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003790 if (data->assoc_reject.timeout_reason)
3791 os_snprintf(buf, sizeof(buf), "=%s",
3792 data->assoc_reject.timeout_reason);
3793 else
3794 buf[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 if (data->assoc_reject.bssid)
3796 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003797 "bssid=" MACSTR " status_code=%u%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798 MAC2STR(data->assoc_reject.bssid),
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003799 data->assoc_reject.status_code,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003800 data->assoc_reject.timed_out ? " timeout" : "",
3801 buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003802 else
3803 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003804 "status_code=%u%s%s",
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07003805 data->assoc_reject.status_code,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003806 data->assoc_reject.timed_out ? " timeout" : "",
3807 buf);
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08003808 wpa_s->assoc_status_code = data->assoc_reject.status_code;
3809 wpas_notify_assoc_status_code(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3811 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003812 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003813 const u8 *bssid = data->assoc_reject.bssid;
3814 if (bssid == NULL || is_zero_ether_addr(bssid))
3815 bssid = wpa_s->pending_bssid;
3816 wpas_connection_failed(wpa_s, bssid);
3817 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003818 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003819 break;
3820 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003821 /* It is possible to get this event from earlier connection */
3822 if (wpa_s->current_ssid &&
3823 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3824 wpa_dbg(wpa_s, MSG_DEBUG,
3825 "Ignore AUTH_TIMED_OUT in mesh configuration");
3826 break;
3827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003828 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3829 sme_event_auth_timed_out(wpa_s, data);
3830 break;
3831 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003832 /* It is possible to get this event from earlier connection */
3833 if (wpa_s->current_ssid &&
3834 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3835 wpa_dbg(wpa_s, MSG_DEBUG,
3836 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3837 break;
3838 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3840 sme_event_assoc_timed_out(wpa_s, data);
3841 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003842 case EVENT_TX_STATUS:
3843 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3844 " type=%d stype=%d",
3845 MAC2STR(data->tx_status.dst),
3846 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003847#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003849#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3851 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003852 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003853 wpa_s, data->tx_status.dst,
3854 data->tx_status.data,
3855 data->tx_status.data_len,
3856 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003857 OFFCHANNEL_SEND_ACTION_SUCCESS :
3858 OFFCHANNEL_SEND_ACTION_NO_ACK);
3859#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 break;
3861 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003862#endif /* CONFIG_AP */
3863#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003865 MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866 /*
3867 * Catch TX status events for Action frames we sent via group
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003868 * interface in GO mode, or via standalone AP interface.
3869 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
3870 * except when the primary interface is used as a GO interface
3871 * (for drivers which do not have group interface concurrency)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 */
3873 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3874 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003875 os_memcmp(wpa_s->p2pdev->pending_action_dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003877 offchannel_send_action_tx_status(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003878 wpa_s->p2pdev, data->tx_status.dst,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 data->tx_status.data,
3880 data->tx_status.data_len,
3881 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003882 OFFCHANNEL_SEND_ACTION_SUCCESS :
3883 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 break;
3885 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003886#endif /* CONFIG_OFFCHANNEL */
3887#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 switch (data->tx_status.type) {
3889 case WLAN_FC_TYPE_MGMT:
3890 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3891 data->tx_status.data_len,
3892 data->tx_status.stype,
3893 data->tx_status.ack);
3894 break;
3895 case WLAN_FC_TYPE_DATA:
3896 ap_tx_status(wpa_s, data->tx_status.dst,
3897 data->tx_status.data,
3898 data->tx_status.data_len,
3899 data->tx_status.ack);
3900 break;
3901 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003902#endif /* CONFIG_AP */
3903 break;
3904#ifdef CONFIG_AP
3905 case EVENT_EAPOL_TX_STATUS:
3906 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3907 data->eapol_tx_status.data,
3908 data->eapol_tx_status.data_len,
3909 data->eapol_tx_status.ack);
3910 break;
3911 case EVENT_DRIVER_CLIENT_POLL_OK:
3912 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 break;
3914 case EVENT_RX_FROM_UNKNOWN:
3915 if (wpa_s->ap_iface == NULL)
3916 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3918 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003919 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003920 case EVENT_CH_SWITCH:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003921 if (!data || !wpa_s->current_ssid)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003922 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003923
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -07003924 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
3925 "freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
3926 data->ch_switch.freq,
3927 data->ch_switch.ht_enabled,
3928 data->ch_switch.ch_offset,
3929 channel_width_to_string(data->ch_switch.ch_width),
3930 data->ch_switch.cf1,
3931 data->ch_switch.cf2);
3932
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003933 wpa_s->assoc_freq = data->ch_switch.freq;
3934 wpa_s->current_ssid->frequency = data->ch_switch.freq;
3935
3936 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
3937 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
3938 wpa_s->current_ssid->mode ==
3939 WPAS_MODE_P2P_GROUP_FORMATION) {
3940 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3941 data->ch_switch.ht_enabled,
3942 data->ch_switch.ch_offset,
3943 data->ch_switch.ch_width,
3944 data->ch_switch.cf1,
3945 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003946 }
3947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003948 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003949 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003950#ifdef NEED_AP_MLME
3951 case EVENT_DFS_RADAR_DETECTED:
3952 if (data)
3953 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3954 break;
3955 case EVENT_DFS_CAC_STARTED:
3956 if (data)
3957 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3958 break;
3959 case EVENT_DFS_CAC_FINISHED:
3960 if (data)
3961 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3962 break;
3963 case EVENT_DFS_CAC_ABORTED:
3964 if (data)
3965 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3966 break;
3967 case EVENT_DFS_NOP_FINISHED:
3968 if (data)
3969 wpas_event_dfs_cac_nop_finished(wpa_s,
3970 &data->dfs_event);
3971 break;
3972#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003973#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003974 case EVENT_RX_MGMT: {
3975 u16 fc, stype;
3976 const struct ieee80211_mgmt *mgmt;
3977
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003978#ifdef CONFIG_TESTING_OPTIONS
3979 if (wpa_s->ext_mgmt_frame_handling) {
3980 struct rx_mgmt *rx = &data->rx_mgmt;
3981 size_t hex_len = 2 * rx->frame_len + 1;
3982 char *hex = os_malloc(hex_len);
3983 if (hex) {
3984 wpa_snprintf_hex(hex, hex_len,
3985 rx->frame, rx->frame_len);
3986 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3987 rx->freq, rx->datarate, rx->ssi_signal,
3988 hex);
3989 os_free(hex);
3990 }
3991 break;
3992 }
3993#endif /* CONFIG_TESTING_OPTIONS */
3994
Dmitry Shmidt04949592012-07-19 12:16:46 -07003995 mgmt = (const struct ieee80211_mgmt *)
3996 data->rx_mgmt.frame;
3997 fc = le_to_host16(mgmt->frame_control);
3998 stype = WLAN_FC_GET_STYPE(fc);
3999
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004000#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004002#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004004 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07004005 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 const u8 *src = mgmt->sa;
Dmitry Shmidte4663042016-04-04 10:07:49 -07004007 const u8 *ie;
4008 size_t ie_len;
4009
4010 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4011 ie_len = data->rx_mgmt.frame_len -
4012 IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004013 wpas_p2p_probe_req_rx(
4014 wpa_s, src, mgmt->da,
4015 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004016 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004017 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 break;
4019 }
4020#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004021#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004022 if (wpa_s->current_ssid &&
4023 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
4024 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004025 data->rx_mgmt.frame_len >= 30) {
4026 wpa_supplicant_event_ibss_auth(wpa_s, data);
4027 break;
4028 }
4029#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004030
4031 if (stype == WLAN_FC_STYPE_ACTION) {
4032 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07004033 wpa_s, data->rx_mgmt.frame,
4034 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004035 data->rx_mgmt.freq,
4036 data->rx_mgmt.ssi_signal);
4037 break;
4038 }
4039
4040 if (wpa_s->ifmsh) {
4041 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004042 break;
4043 }
4044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
4046 "management frame in non-AP mode");
4047 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004048#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004050
4051 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
Dmitry Shmidte4663042016-04-04 10:07:49 -07004052 data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
4053 const u8 *ie;
4054 size_t ie_len;
4055
4056 ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
4057 ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004058
4059 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
4060 mgmt->bssid, ie, ie_len,
4061 data->rx_mgmt.ssi_signal);
4062 }
4063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004065#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004068 case EVENT_RX_PROBE_REQ:
4069 if (data->rx_probe_req.sa == NULL ||
4070 data->rx_probe_req.ie == NULL)
4071 break;
4072#ifdef CONFIG_AP
4073 if (wpa_s->ap_iface) {
4074 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
4075 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004076 data->rx_probe_req.da,
4077 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004079 data->rx_probe_req.ie_len,
4080 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 break;
4082 }
4083#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004085 data->rx_probe_req.da,
4086 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004088 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004089 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004090 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004092 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004093#ifdef CONFIG_OFFCHANNEL
4094 offchannel_remain_on_channel_cb(
4095 wpa_s, data->remain_on_channel.freq,
4096 data->remain_on_channel.duration);
4097#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004098 wpas_p2p_remain_on_channel_cb(
4099 wpa_s, data->remain_on_channel.freq,
4100 data->remain_on_channel.duration);
4101 break;
4102 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004103#ifdef CONFIG_OFFCHANNEL
4104 offchannel_cancel_remain_on_channel_cb(
4105 wpa_s, data->remain_on_channel.freq);
4106#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107 wpas_p2p_cancel_remain_on_channel_cb(
4108 wpa_s, data->remain_on_channel.freq);
4109 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 case EVENT_EAPOL_RX:
4111 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
4112 data->eapol_rx.data,
4113 data->eapol_rx.data_len);
4114 break;
4115 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004116 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
4117 "above=%d signal=%d noise=%d txrate=%d",
4118 data->signal_change.above_threshold,
4119 data->signal_change.current_signal,
4120 data->signal_change.current_noise,
4121 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004122 wpa_bss_update_level(wpa_s->current_bss,
4123 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124 bgscan_notify_signal_change(
4125 wpa_s, data->signal_change.above_threshold,
4126 data->signal_change.current_signal,
4127 data->signal_change.current_noise,
4128 data->signal_change.current_txrate);
4129 break;
4130 case EVENT_INTERFACE_ENABLED:
4131 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
4132 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004133 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07004134 if (wpa_s->p2p_mgmt) {
4135 wpa_supplicant_set_state(wpa_s,
4136 WPA_DISCONNECTED);
4137 break;
4138 }
4139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140#ifdef CONFIG_AP
4141 if (!wpa_s->ap_iface) {
4142 wpa_supplicant_set_state(wpa_s,
4143 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004144 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 wpa_supplicant_req_scan(wpa_s, 0, 0);
4146 } else
4147 wpa_supplicant_set_state(wpa_s,
4148 WPA_COMPLETED);
4149#else /* CONFIG_AP */
4150 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
4151 wpa_supplicant_req_scan(wpa_s, 0, 0);
4152#endif /* CONFIG_AP */
4153 }
4154 break;
4155 case EVENT_INTERFACE_DISABLED:
4156 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004157#ifdef CONFIG_P2P
4158 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
4159 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
4160 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
4161 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004162 * Mark interface disabled if this happens to end up not
4163 * being removed as a separate P2P group interface.
4164 */
4165 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4166 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004167 * The interface was externally disabled. Remove
4168 * it assuming an external entity will start a
4169 * new session if needed.
4170 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004171 if (wpa_s->current_ssid &&
4172 wpa_s->current_ssid->p2p_group)
4173 wpas_p2p_interface_unavailable(wpa_s);
4174 else
4175 wpas_p2p_disconnect(wpa_s);
4176 /*
4177 * wpa_s instance may have been freed, so must not use
4178 * it here anymore.
4179 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004180 break;
4181 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004182 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
4183 p2p_in_progress(wpa_s->global->p2p) > 1) {
4184 /* This radio work will be cancelled, so clear P2P
4185 * state as well.
4186 */
4187 p2p_stop_find(wpa_s->global->p2p);
4188 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004189#endif /* CONFIG_P2P */
4190
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004191 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
4192 /*
4193 * Indicate disconnection to keep ctrl_iface events
4194 * consistent.
4195 */
4196 wpa_supplicant_event_disassoc(
4197 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
4198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07004200 wpa_bss_flush(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08004201 radio_remove_works(wpa_s, NULL, 0);
4202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004203 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
4204 break;
4205 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004206 wpa_supplicant_update_channel_list(
4207 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 break;
4209 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004210 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 break;
4212 case EVENT_BEST_CHANNEL:
4213 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
4214 "(%d %d %d)",
4215 data->best_chan.freq_24, data->best_chan.freq_5,
4216 data->best_chan.freq_overall);
4217 wpa_s->best_24_freq = data->best_chan.freq_24;
4218 wpa_s->best_5_freq = data->best_chan.freq_5;
4219 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004220 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
4221 data->best_chan.freq_5,
4222 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223 break;
4224 case EVENT_UNPROT_DEAUTH:
4225 wpa_supplicant_event_unprot_deauth(wpa_s,
4226 &data->unprot_deauth);
4227 break;
4228 case EVENT_UNPROT_DISASSOC:
4229 wpa_supplicant_event_unprot_disassoc(wpa_s,
4230 &data->unprot_disassoc);
4231 break;
4232 case EVENT_STATION_LOW_ACK:
4233#ifdef CONFIG_AP
4234 if (wpa_s->ap_iface && data)
4235 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
4236 data->low_ack.addr);
4237#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004238#ifdef CONFIG_TDLS
4239 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004240 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
4241 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004242#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 break;
4244 case EVENT_IBSS_PEER_LOST:
4245#ifdef CONFIG_IBSS_RSN
4246 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
4247#endif /* CONFIG_IBSS_RSN */
4248 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004249 case EVENT_DRIVER_GTK_REKEY:
4250 if (os_memcmp(data->driver_gtk_rekey.bssid,
4251 wpa_s->bssid, ETH_ALEN))
4252 break;
4253 if (!wpa_s->wpa)
4254 break;
4255 wpa_sm_update_replay_ctr(wpa_s->wpa,
4256 data->driver_gtk_rekey.replay_ctr);
4257 break;
4258 case EVENT_SCHED_SCAN_STOPPED:
4259 wpa_s->sched_scanning = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004260 resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004261 wpa_supplicant_notify_scanning(wpa_s, 0);
4262
4263 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
4264 break;
4265
4266 /*
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004267 * If the driver stopped scanning without being requested to,
4268 * request a new scan to continue scanning for networks.
4269 */
4270 if (!wpa_s->sched_scan_stop_req &&
4271 wpa_s->wpa_state == WPA_SCANNING) {
4272 wpa_dbg(wpa_s, MSG_DEBUG,
4273 "Restart scanning after unexpected sched_scan stop event");
4274 wpa_supplicant_req_scan(wpa_s, 1, 0);
4275 break;
4276 }
4277
4278 wpa_s->sched_scan_stop_req = 0;
4279
4280 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08004281 * Start a new sched scan to continue searching for more SSIDs
4282 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07004284 if (wpa_s->sched_scan_timed_out) {
4285 wpa_supplicant_req_sched_scan(wpa_s);
4286 } else if (wpa_s->pno_sched_pending) {
4287 wpa_s->pno_sched_pending = 0;
4288 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07004289 } else if (resched) {
4290 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08004291 }
4292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004293 break;
4294 case EVENT_WPS_BUTTON_PUSHED:
4295#ifdef CONFIG_WPS
4296 wpas_wps_start_pbc(wpa_s, NULL, 0);
4297#endif /* CONFIG_WPS */
4298 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004299 case EVENT_AVOID_FREQUENCIES:
4300 wpa_supplicant_notify_avoid_freq(wpa_s, data);
4301 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08004302 case EVENT_CONNECT_FAILED_REASON:
4303#ifdef CONFIG_AP
4304 if (!wpa_s->ap_iface || !data)
4305 break;
4306 hostapd_event_connect_failed_reason(
4307 wpa_s->ap_iface->bss[0],
4308 data->connect_failed_reason.addr,
4309 data->connect_failed_reason.code);
4310#endif /* CONFIG_AP */
4311 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004312 case EVENT_NEW_PEER_CANDIDATE:
4313#ifdef CONFIG_MESH
4314 if (!wpa_s->ifmsh || !data)
4315 break;
4316 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
4317 data->mesh_peer.ies,
4318 data->mesh_peer.ie_len);
4319#endif /* CONFIG_MESH */
4320 break;
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004321 case EVENT_SURVEY:
4322#ifdef CONFIG_AP
4323 if (!wpa_s->ap_iface)
4324 break;
4325 hostapd_event_get_survey(wpa_s->ap_iface,
4326 &data->survey_results);
4327#endif /* CONFIG_AP */
4328 break;
4329 case EVENT_ACS_CHANNEL_SELECTED:
Paul Stewart092955c2017-02-06 09:13:09 -08004330#ifdef CONFIG_AP
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004331#ifdef CONFIG_ACS
4332 if (!wpa_s->ap_iface)
4333 break;
4334 hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
4335 &data->acs_selected_channels);
4336#endif /* CONFIG_ACS */
Paul Stewart092955c2017-02-06 09:13:09 -08004337#endif /* CONFIG_AP */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -08004338 break;
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07004339 case EVENT_P2P_LO_STOP:
4340#ifdef CONFIG_P2P
4341 wpa_s->p2p_lo_started = 0;
4342 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
4343 P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
4344 data->p2p_lo_stop.reason_code);
4345#endif /* CONFIG_P2P */
4346 break;
Paul Stewart092955c2017-02-06 09:13:09 -08004347 case EVENT_BEACON_LOSS:
4348 if (!wpa_s->current_bss || !wpa_s->current_ssid)
4349 break;
4350 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
4351 bgscan_notify_beacon_loss(wpa_s);
4352 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353 default:
4354 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
4355 break;
4356 }
4357}
Dmitry Shmidte4663042016-04-04 10:07:49 -07004358
4359
4360void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
4361 union wpa_event_data *data)
4362{
4363 struct wpa_supplicant *wpa_s;
4364
4365 if (event != EVENT_INTERFACE_STATUS)
4366 return;
4367
4368 wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
4369 if (wpa_s && wpa_s->driver->get_ifindex) {
4370 unsigned int ifindex;
4371
4372 ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
4373 if (ifindex != data->interface_status.ifindex) {
4374 wpa_dbg(wpa_s, MSG_DEBUG,
4375 "interface status ifindex %d mismatch (%d)",
4376 ifindex, data->interface_status.ifindex);
4377 return;
4378 }
4379 }
4380#ifdef CONFIG_MATCH_IFACE
4381 else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
4382 struct wpa_interface *wpa_i;
4383
4384 wpa_i = wpa_supplicant_match_iface(
4385 ctx, data->interface_status.ifname);
4386 if (!wpa_i)
4387 return;
4388 wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
4389 os_free(wpa_i);
4390 if (wpa_s)
4391 wpa_s->matched = 1;
4392 }
4393#endif /* CONFIG_MATCH_IFACE */
4394
4395 if (wpa_s)
4396 wpa_supplicant_event(wpa_s, event, data);
4397}