blob: 8422170741aed0eec7bbf92a66ee903666142f59 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eapol_supp/eapol_supp_sm.h"
13#include "rsn_supp/wpa.h"
14#include "eloop.h"
15#include "config.h"
16#include "l2_packet/l2_packet.h"
17#include "wpa_supplicant_i.h"
18#include "driver_i.h"
19#include "pcsc_funcs.h"
20#include "rsn_supp/preauth.h"
21#include "rsn_supp/pmksa_cache.h"
22#include "common/wpa_ctrl.h"
23#include "eap_peer/eap.h"
24#include "ap/hostapd.h"
25#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080026#include "fst/fst.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070027#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "notify.h"
29#include "common/ieee802_11_defs.h"
30#include "common/ieee802_11_common.h"
31#include "crypto/random.h"
32#include "blacklist.h"
33#include "wpas_glue.h"
34#include "wps_supplicant.h"
35#include "ibss_rsn.h"
36#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038#include "p2p_supplicant.h"
39#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070040#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041#include "ap.h"
42#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080044#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#include "interworking.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080046#include "mesh.h"
47#include "mesh_mpm.h"
48#include "wmm_ac.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070049
50
Dmitry Shmidt34af3062013-07-11 10:46:32 -070051#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070052static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080053 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070054#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080055
56
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
58 struct wpa_ssid *ssid)
59{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080060 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070061
62 if (ssid == NULL || ssid->disabled_until.sec == 0)
63 return 0;
64
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080065 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070066 if (ssid->disabled_until.sec > now.sec)
67 return ssid->disabled_until.sec - now.sec;
68
69 wpas_clear_temp_disabled(wpa_s, ssid, 0);
70
71 return 0;
72}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073
74
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070075/**
76 * wpas_reenabled_network_time - Time until first network is re-enabled
77 * @wpa_s: Pointer to wpa_supplicant data
78 * Returns: If all enabled networks are temporarily disabled, returns the time
79 * (in sec) until the first network is re-enabled. Otherwise returns 0.
80 *
81 * This function is used in case all enabled networks are temporarily disabled,
82 * in which case it returns the time (in sec) that the first network will be
83 * re-enabled. The function assumes that at least one network is enabled.
84 */
85static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
86{
87 struct wpa_ssid *ssid;
88 int disabled_for, res = 0;
89
90#ifdef CONFIG_INTERWORKING
91 if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
92 wpa_s->conf->cred)
93 return 0;
94#endif /* CONFIG_INTERWORKING */
95
96 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
97 if (ssid->disabled)
98 continue;
99
100 disabled_for = wpas_temp_disabled(wpa_s, ssid);
101 if (!disabled_for)
102 return 0;
103
104 if (!res || disabled_for < res)
105 res = disabled_for;
106 }
107
108 return res;
109}
110
111
112void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
113{
114 struct wpa_supplicant *wpa_s = eloop_ctx;
115
116 if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
117 return;
118
119 wpa_dbg(wpa_s, MSG_DEBUG,
120 "Try to associate due to network getting re-enabled");
121 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
122 wpa_supplicant_cancel_sched_scan(wpa_s);
123 wpa_supplicant_req_scan(wpa_s, 0, 0);
124 }
125}
126
127
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800128static struct wpa_bss * wpa_supplicant_get_new_bss(
129 struct wpa_supplicant *wpa_s, const u8 *bssid)
130{
131 struct wpa_bss *bss = NULL;
132 struct wpa_ssid *ssid = wpa_s->current_ssid;
133
134 if (ssid->ssid_len > 0)
135 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
136 if (!bss)
137 bss = wpa_bss_get_bssid(wpa_s, bssid);
138
139 return bss;
140}
141
142
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700143static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
144{
145 struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
146
147 if (!bss) {
148 wpa_supplicant_update_scan_results(wpa_s);
149
150 /* Get the BSS from the new scan results */
151 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
152 }
153
154 if (bss)
155 wpa_s->current_bss = bss;
156}
157
158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
160{
161 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700162 u8 drv_ssid[SSID_MAX_LEN];
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700163 size_t drv_ssid_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700164 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700166 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
167 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700168
169 if (wpa_s->current_ssid->ssid_len == 0)
170 return 0; /* current profile still in use */
171 res = wpa_drv_get_ssid(wpa_s, drv_ssid);
172 if (res < 0) {
173 wpa_msg(wpa_s, MSG_INFO,
174 "Failed to read SSID from driver");
175 return 0; /* try to use current profile */
176 }
177 drv_ssid_len = res;
178
179 if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
180 os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
181 drv_ssid_len) == 0)
182 return 0; /* current profile still in use */
183
184 wpa_msg(wpa_s, MSG_DEBUG,
185 "Driver-initiated BSS selection changed the SSID to %s",
186 wpa_ssid_txt(drv_ssid, drv_ssid_len));
187 /* continue selecting a new network profile */
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189
190 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
191 "information");
192 ssid = wpa_supplicant_get_ssid(wpa_s);
193 if (ssid == NULL) {
194 wpa_msg(wpa_s, MSG_INFO,
195 "No network configuration found for the current AP");
196 return -1;
197 }
198
Dmitry Shmidt04949592012-07-19 12:16:46 -0700199 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
201 return -1;
202 }
203
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800204 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
205 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
206 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
207 return -1;
208 }
209
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700210 res = wpas_temp_disabled(wpa_s, ssid);
211 if (res > 0) {
212 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
213 "disabled for %d second(s)", res);
214 return -1;
215 }
216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
218 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800219 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 u8 wpa_ie[80];
221 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800222 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
223 wpa_ie, &wpa_ie_len) < 0)
224 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225 } else {
226 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
227 }
228
229 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
230 eapol_sm_invalidate_cached_session(wpa_s->eapol);
231 old_ssid = wpa_s->current_ssid;
232 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800233
Jouni Malinen5c879ee2014-08-18 11:04:56 -0700234 wpa_supplicant_update_current_bss(wpa_s);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
237 wpa_supplicant_initiate_eapol(wpa_s);
238 if (old_ssid != wpa_s->current_ssid)
239 wpas_notify_network_changed(wpa_s);
240
241 return 0;
242}
243
244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246{
247 struct wpa_supplicant *wpa_s = eloop_ctx;
248
249 if (wpa_s->countermeasures) {
250 wpa_s->countermeasures = 0;
251 wpa_drv_set_countermeasures(wpa_s, 0);
252 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800253
254 /*
255 * It is possible that the device is sched scanning, which means
256 * that a connection attempt will be done only when we receive
257 * scan results. However, in this case, it would be preferable
258 * to scan and connect immediately, so cancel the sched_scan and
259 * issue a regular scan flow.
260 */
261 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 wpa_supplicant_req_scan(wpa_s, 0, 0);
263 }
264}
265
266
267void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
268{
269 int bssid_changed;
270
Dmitry Shmidt04949592012-07-19 12:16:46 -0700271 wnm_bss_keep_alive_deinit(wpa_s);
272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273#ifdef CONFIG_IBSS_RSN
274 ibss_rsn_deinit(wpa_s->ibss_rsn);
275 wpa_s->ibss_rsn = NULL;
276#endif /* CONFIG_IBSS_RSN */
277
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700278#ifdef CONFIG_AP
279 wpa_supplicant_ap_deinit(wpa_s);
280#endif /* CONFIG_AP */
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
283 return;
284
285 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
286 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
287 os_memset(wpa_s->bssid, 0, ETH_ALEN);
288 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800289 sme_clear_on_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 wpa_s->current_bss = NULL;
291 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if (bssid_changed)
294 wpas_notify_bssid_changed(wpa_s);
295
296 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
297 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
298 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
299 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
300 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700301 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700302 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700303 wpa_s->key_mgmt = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800304
305 wpas_rrm_reset(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306}
307
308
309static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
310{
311 struct wpa_ie_data ie;
312 int pmksa_set = -1;
313 size_t i;
314
315 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
316 ie.pmkid == NULL)
317 return;
318
319 for (i = 0; i < ie.num_pmkid; i++) {
320 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
321 ie.pmkid + i * PMKID_LEN,
322 NULL, NULL, 0);
323 if (pmksa_set == 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800324 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 break;
326 }
327 }
328
329 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
330 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
331}
332
333
334static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
335 union wpa_event_data *data)
336{
337 if (data == NULL) {
338 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
339 "event");
340 return;
341 }
342 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
343 " index=%d preauth=%d",
344 MAC2STR(data->pmkid_candidate.bssid),
345 data->pmkid_candidate.index,
346 data->pmkid_candidate.preauth);
347
348 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
349 data->pmkid_candidate.index,
350 data->pmkid_candidate.preauth);
351}
352
353
354static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
355{
356 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
357 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
358 return 0;
359
360#ifdef IEEE8021X_EAPOL
361 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
362 wpa_s->current_ssid &&
363 !(wpa_s->current_ssid->eapol_flags &
364 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
365 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
366 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
367 * plaintext or static WEP keys). */
368 return 0;
369 }
370#endif /* IEEE8021X_EAPOL */
371
372 return 1;
373}
374
375
376/**
377 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
378 * @wpa_s: pointer to wpa_supplicant data
379 * @ssid: Configuration data for the network
380 * Returns: 0 on success, -1 on failure
381 *
382 * This function is called when starting authentication with a network that is
383 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
384 */
385int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
386 struct wpa_ssid *ssid)
387{
388#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700390 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700392 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
393 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 return 0;
395
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700396 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 sim = 1;
398 aka = 1;
399 } else {
400 struct eap_method_type *eap = ssid->eap.eap_methods;
401 while (eap->vendor != EAP_VENDOR_IETF ||
402 eap->method != EAP_TYPE_NONE) {
403 if (eap->vendor == EAP_VENDOR_IETF) {
404 if (eap->method == EAP_TYPE_SIM)
405 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700406 else if (eap->method == EAP_TYPE_AKA ||
407 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 aka = 1;
409 }
410 eap++;
411 }
412 }
413
414 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
415 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700416 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
417 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
418 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 aka = 0;
420
421 if (!sim && !aka) {
422 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
423 "use SIM, but neither EAP-SIM nor EAP-AKA are "
424 "enabled");
425 return 0;
426 }
427
428 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
429 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700431 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 if (wpa_s->scard == NULL) {
433 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
434 "(pcsc-lite)");
435 return -1;
436 }
437 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
438 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800439#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440#endif /* IEEE8021X_EAPOL */
441
442 return 0;
443}
444
445
446#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700447
448static int has_wep_key(struct wpa_ssid *ssid)
449{
450 int i;
451
452 for (i = 0; i < NUM_WEP_KEYS; i++) {
453 if (ssid->wep_key_len[i])
454 return 1;
455 }
456
457 return 0;
458}
459
460
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700461static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 struct wpa_ssid *ssid)
463{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700464 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465
466 if (ssid->mixed_cell)
467 return 1;
468
469#ifdef CONFIG_WPS
470 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
471 return 1;
472#endif /* CONFIG_WPS */
473
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700474 if (has_wep_key(ssid))
475 privacy = 1;
476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477#ifdef IEEE8021X_EAPOL
478 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
479 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
480 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
481 privacy = 1;
482#endif /* IEEE8021X_EAPOL */
483
Jouni Malinen75ecf522011-06-27 15:19:46 -0700484 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
485 privacy = 1;
486
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800487 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
488 privacy = 1;
489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 if (bss->caps & IEEE80211_CAP_PRIVACY)
491 return privacy;
492 return !privacy;
493}
494
495
496static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
497 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700498 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700499{
500 struct wpa_ie_data ie;
501 int proto_match = 0;
502 const u8 *rsn_ie, *wpa_ie;
503 int ret;
504 int wep_ok;
505
506 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
507 if (ret >= 0)
508 return ret;
509
510 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
511 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
512 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
513 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
514 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
515
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700516 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
518 proto_match++;
519
520 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
521 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
522 "failed");
523 break;
524 }
525
526 if (wep_ok &&
527 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
528 {
529 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
530 "in RSN IE");
531 return 1;
532 }
533
534 if (!(ie.proto & ssid->proto)) {
535 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
536 "mismatch");
537 break;
538 }
539
540 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
541 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
542 "cipher mismatch");
543 break;
544 }
545
546 if (!(ie.group_cipher & ssid->group_cipher)) {
547 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
548 "cipher mismatch");
549 break;
550 }
551
552 if (!(ie.key_mgmt & ssid->key_mgmt)) {
553 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
554 "mismatch");
555 break;
556 }
557
558#ifdef CONFIG_IEEE80211W
559 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800560 wpas_get_ssid_pmf(wpa_s, ssid) ==
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800561 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
563 "frame protection");
564 break;
565 }
566#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800567 if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
568 wpas_get_ssid_pmf(wpa_s, ssid) ==
569 NO_MGMT_FRAME_PROTECTION) {
570 wpa_dbg(wpa_s, MSG_DEBUG,
571 " skip RSN IE - no mgmt frame protection enabled but AP requires it");
572 break;
573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574
575 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
576 return 1;
577 }
578
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700579 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
581 proto_match++;
582
583 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
584 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
585 "failed");
586 break;
587 }
588
589 if (wep_ok &&
590 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
591 {
592 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
593 "in WPA IE");
594 return 1;
595 }
596
597 if (!(ie.proto & ssid->proto)) {
598 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
599 "mismatch");
600 break;
601 }
602
603 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
604 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
605 "cipher mismatch");
606 break;
607 }
608
609 if (!(ie.group_cipher & ssid->group_cipher)) {
610 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
611 "cipher mismatch");
612 break;
613 }
614
615 if (!(ie.key_mgmt & ssid->key_mgmt)) {
616 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
617 "mismatch");
618 break;
619 }
620
621 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
622 return 1;
623 }
624
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700625 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
626 !rsn_ie) {
627 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
628 return 1;
629 }
630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
632 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
633 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
634 return 0;
635 }
636
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800637 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
638 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
639 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
640 return 1;
641 }
642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
644 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
645 return 1;
646 }
647
648 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
649 "WPA/WPA2");
650
651 return 0;
652}
653
654
655static int freq_allowed(int *freqs, int freq)
656{
657 int i;
658
659 if (freqs == NULL)
660 return 1;
661
662 for (i = 0; freqs[i]; i++)
663 if (freqs[i] == freq)
664 return 1;
665 return 0;
666}
667
668
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700669static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800670{
671 const struct hostapd_hw_modes *mode = NULL, *modes;
672 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
673 const u8 *rate_ie;
674 int i, j, k;
675
676 if (bss->freq == 0)
677 return 1; /* Cannot do matching without knowing band */
678
679 modes = wpa_s->hw.modes;
680 if (modes == NULL) {
681 /*
682 * The driver does not provide any additional information
683 * about the utilized hardware, so allow the connection attempt
684 * to continue.
685 */
686 return 1;
687 }
688
689 for (i = 0; i < wpa_s->hw.num_modes; i++) {
690 for (j = 0; j < modes[i].num_channels; j++) {
691 int freq = modes[i].channels[j].freq;
692 if (freq == bss->freq) {
693 if (mode &&
694 mode->mode == HOSTAPD_MODE_IEEE80211G)
695 break; /* do not allow 802.11b replace
696 * 802.11g */
697 mode = &modes[i];
698 break;
699 }
700 }
701 }
702
703 if (mode == NULL)
704 return 0;
705
706 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700707 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800708 if (rate_ie == NULL)
709 continue;
710
711 for (j = 2; j < rate_ie[1] + 2; j++) {
712 int flagged = !!(rate_ie[j] & 0x80);
713 int r = (rate_ie[j] & 0x7f) * 5;
714
715 /*
716 * IEEE Std 802.11n-2009 7.3.2.2:
717 * The new BSS Membership selector value is encoded
718 * like a legacy basic rate, but it is not a rate and
719 * only indicates if the BSS members are required to
720 * support the mandatory features of Clause 20 [HT PHY]
721 * in order to join the BSS.
722 */
723 if (flagged && ((rate_ie[j] & 0x7f) ==
724 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
725 if (!ht_supported(mode)) {
726 wpa_dbg(wpa_s, MSG_DEBUG,
727 " hardware does not support "
728 "HT PHY");
729 return 0;
730 }
731 continue;
732 }
733
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700734 /* There's also a VHT selector for 802.11ac */
735 if (flagged && ((rate_ie[j] & 0x7f) ==
736 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
737 if (!vht_supported(mode)) {
738 wpa_dbg(wpa_s, MSG_DEBUG,
739 " hardware does not support "
740 "VHT PHY");
741 return 0;
742 }
743 continue;
744 }
745
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800746 if (!flagged)
747 continue;
748
749 /* check for legacy basic rates */
750 for (k = 0; k < mode->num_rates; k++) {
751 if (mode->rates[k] == r)
752 break;
753 }
754 if (k == mode->num_rates) {
755 /*
756 * IEEE Std 802.11-2007 7.3.2.2 demands that in
757 * order to join a BSS all required rates
758 * have to be supported by the hardware.
759 */
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -0700760 wpa_dbg(wpa_s, MSG_DEBUG,
761 " hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
762 r / 10, r % 10,
763 bss->freq, mode->mode, mode->num_rates);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 return 0;
765 }
766 }
767 }
768
769 return 1;
770}
771
772
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800773/*
774 * Test whether BSS is in an ESS.
775 * This is done differently in DMG (60 GHz) and non-DMG bands
776 */
777static int bss_is_ess(struct wpa_bss *bss)
778{
779 if (bss_is_dmg(bss)) {
780 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
781 IEEE80211_CAP_DMG_AP;
782 }
783
784 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
785 IEEE80211_CAP_ESS);
786}
787
788
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800789static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
790{
791 size_t i;
792
793 for (i = 0; i < ETH_ALEN; i++) {
794 if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
795 return 0;
796 }
797 return 1;
798}
799
800
801static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
802{
803 size_t i;
804
805 for (i = 0; i < num; i++) {
806 const u8 *a = list + i * ETH_ALEN * 2;
807 const u8 *m = a + ETH_ALEN;
808
809 if (match_mac_mask(a, addr, m))
810 return 1;
811 }
812 return 0;
813}
814
815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700817 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800818 struct wpa_ssid *group,
819 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700821 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 int wpa;
823 struct wpa_blacklist *e;
824 const u8 *ie;
825 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800826 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700828 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 wpa_ie_len = ie ? ie[1] : 0;
830
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700831 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 rsn_ie_len = ie ? ie[1] : 0;
833
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800834 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
835 osen = ie != NULL;
836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800838 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700839 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800840 wpa_ie_len, rsn_ie_len, bss->caps, bss->level, bss->freq,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700841 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
842 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
843 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800844 " p2p" : "",
845 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846
847 e = wpa_blacklist_get(wpa_s, bss->bssid);
848 if (e) {
849 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 /*
852 * When only a single network is enabled, we can
853 * trigger blacklisting on the first failure. This
854 * should not be done with multiple enabled networks to
855 * avoid getting forced to move into a worse ESS on
856 * single error if there are no other BSSes of the
857 * current ESS.
858 */
859 limit = 0;
860 }
861 if (e->count > limit) {
862 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
863 "(count=%d limit=%d)", e->count, limit);
864 return NULL;
865 }
866 }
867
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700868 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
870 return NULL;
871 }
872
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800873 if (disallowed_bssid(wpa_s, bss->bssid)) {
874 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
875 return NULL;
876 }
877
878 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
879 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
880 return NULL;
881 }
882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
884
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800885 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700887 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888
Dmitry Shmidt04949592012-07-19 12:16:46 -0700889 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
891 continue;
892 }
893
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700894 res = wpas_temp_disabled(wpa_s, ssid);
895 if (res > 0) {
896 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
897 "temporarily for %d second(s)", res);
898 continue;
899 }
900
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901#ifdef CONFIG_WPS
902 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
903 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
904 "(WPS)");
905 continue;
906 }
907
908 if (wpa && ssid->ssid_len == 0 &&
909 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
910 check_ssid = 0;
911
912 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
913 /* Only allow wildcard SSID match if an AP
914 * advertises active WPS operation that matches
915 * with our mode. */
916 check_ssid = 1;
917 if (ssid->ssid_len == 0 &&
918 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
919 check_ssid = 0;
920 }
921#endif /* CONFIG_WPS */
922
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800923 if (ssid->bssid_set && ssid->ssid_len == 0 &&
924 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
925 check_ssid = 0;
926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700928 (bss->ssid_len != ssid->ssid_len ||
929 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
931 continue;
932 }
933
934 if (ssid->bssid_set &&
935 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
936 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
937 continue;
938 }
939
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800940 /* check blacklist */
941 if (ssid->num_bssid_blacklist &&
942 addr_in_list(bss->bssid, ssid->bssid_blacklist,
943 ssid->num_bssid_blacklist)) {
944 wpa_dbg(wpa_s, MSG_DEBUG,
945 " skip - BSSID blacklisted");
946 continue;
947 }
948
949 /* if there is a whitelist, only accept those APs */
950 if (ssid->num_bssid_whitelist &&
951 !addr_in_list(bss->bssid, ssid->bssid_whitelist,
952 ssid->num_bssid_whitelist)) {
953 wpa_dbg(wpa_s, MSG_DEBUG,
954 " skip - BSSID not in whitelist");
955 continue;
956 }
957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
959 continue;
960
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800961 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
963 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
964 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
965 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
966 "not allowed");
967 continue;
968 }
969
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700970 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
971 has_wep_key(ssid)) {
972 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
973 continue;
974 }
975
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800976 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
977 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
978 "not allowed");
979 continue;
980 }
981
Jouni Malinen75ecf522011-06-27 15:19:46 -0700982 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
984 "mismatch");
985 continue;
986 }
987
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800988 if (!bss_is_ess(bss)) {
989 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 continue;
991 }
992
993 if (!freq_allowed(ssid->freq_list, bss->freq)) {
994 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
995 "allowed");
996 continue;
997 }
998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800999 if (!rate_match(wpa_s, bss)) {
1000 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
1001 "not match");
1002 continue;
1003 }
1004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -07001006 if (ssid->p2p_group &&
1007 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
1008 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
1009 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
1010 continue;
1011 }
1012
Dmitry Shmidt54605472013-11-08 11:10:19 -08001013 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
1014 struct wpabuf *p2p_ie;
1015 u8 dev_addr[ETH_ALEN];
1016
1017 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
1018 if (ie == NULL) {
1019 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
1020 continue;
1021 }
1022 p2p_ie = wpa_bss_get_vendor_ie_multi(
1023 bss, P2P_IE_VENDOR_TYPE);
1024 if (p2p_ie == NULL) {
1025 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
1026 continue;
1027 }
1028
1029 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
1030 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
1031 ETH_ALEN) != 0) {
1032 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
1033 wpabuf_free(p2p_ie);
1034 continue;
1035 }
1036 wpabuf_free(p2p_ie);
1037 }
1038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 /*
1040 * TODO: skip the AP if its P2P IE has Group Formation
1041 * bit set in the P2P Group Capability Bitmap and we
1042 * are not in Group Formation with that device.
1043 */
1044#endif /* CONFIG_P2P */
1045
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001046 if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
1047 {
1048 struct os_reltime diff;
1049
1050 os_reltime_sub(&wpa_s->scan_min_time,
1051 &bss->last_update, &diff);
1052 wpa_dbg(wpa_s, MSG_DEBUG,
1053 " skip - scan result not recent enough (%u.%06u seconds too old)",
1054 (unsigned int) diff.sec,
1055 (unsigned int) diff.usec);
1056 continue;
1057 }
1058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 /* Matching configuration found */
1060 return ssid;
1061 }
1062
1063 /* No matching configuration found */
1064 return NULL;
1065}
1066
1067
1068static struct wpa_bss *
1069wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001071 struct wpa_ssid **selected_ssid,
1072 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001073{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001074 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001076 if (only_first_ssid)
1077 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
1078 group->id);
1079 else
1080 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
1081 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001083 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1084 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001085 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
1086 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 if (!*selected_ssid)
1088 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
1090 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001091 MAC2STR(bss->bssid),
1092 wpa_ssid_txt(bss->ssid, bss->ssid_len));
1093 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094 }
1095
1096 return NULL;
1097}
1098
1099
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001100struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
1101 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102{
1103 struct wpa_bss *selected = NULL;
1104 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001105 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001106 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001108 if (wpa_s->last_scan_res == NULL ||
1109 wpa_s->last_scan_res_used == 0)
1110 return NULL; /* no scan results from last update */
1111
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001112 if (wpa_s->next_ssid) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001113 /* check that next_ssid is still valid */
1114 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1115 if (ssid == wpa_s->next_ssid)
1116 break;
1117 }
1118 next_ssid = ssid;
1119 wpa_s->next_ssid = NULL;
1120 }
1121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001123 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1124 if (next_ssid && next_ssid->priority ==
1125 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001126 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001127 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001128 if (selected)
1129 break;
1130 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001132 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001133 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 if (selected)
1135 break;
1136 }
1137
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001138 if (selected == NULL && wpa_s->blacklist &&
1139 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1141 "blacklist and try again");
1142 wpa_blacklist_clear(wpa_s);
1143 wpa_s->blacklist_cleared++;
1144 } else if (selected == NULL)
1145 break;
1146 }
1147
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001148 ssid = *selected_ssid;
1149 if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
1150 !ssid->passphrase && !ssid->ext_psk) {
1151 const char *field_name, *txt = NULL;
1152
1153 wpa_dbg(wpa_s, MSG_DEBUG,
1154 "PSK/passphrase not yet available for the selected network");
1155
1156 wpas_notify_network_request(wpa_s, ssid,
1157 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
1158
1159 field_name = wpa_supplicant_ctrl_req_to_string(
1160 WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
1161 if (field_name == NULL)
1162 return NULL;
1163
1164 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1165
1166 selected = NULL;
1167 }
1168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 return selected;
1170}
1171
1172
1173static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1174 int timeout_sec, int timeout_usec)
1175{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001176 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 /*
1178 * No networks are enabled; short-circuit request so
1179 * we don't wait timeout seconds before transitioning
1180 * to INACTIVE state.
1181 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001182 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1183 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1185 return;
1186 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001187
1188 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1190}
1191
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001192
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001193int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001194 struct wpa_bss *selected,
1195 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196{
1197 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1198 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1199 "PBC session overlap");
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07001200 wpas_notify_wps_event_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001202 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1203 wpa_s->p2p_in_provisioning) {
1204 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1205 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001206 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001207 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208#endif /* CONFIG_P2P */
1209
1210#ifdef CONFIG_WPS
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001211 wpas_wps_pbc_overlap(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001212 wpas_wps_cancel(wpa_s);
1213#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001214 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 }
1216
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001217 wpa_msg(wpa_s, MSG_DEBUG,
1218 "Considering connect request: reassociate: %d selected: "
1219 MACSTR " bssid: " MACSTR " pending: " MACSTR
1220 " wpa_state: %s ssid=%p current_ssid=%p",
1221 wpa_s->reassociate, MAC2STR(selected->bssid),
1222 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1223 wpa_supplicant_state_txt(wpa_s->wpa_state),
1224 ssid, wpa_s->current_ssid);
1225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 /*
1227 * Do not trigger new association unless the BSSID has changed or if
1228 * reassociation is requested. If we are in process of associating with
1229 * the selected BSSID, do not trigger new attempt.
1230 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1233 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1234 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001235 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1236 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1237 0) ||
1238 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1239 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1241 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001242 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001244 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1245 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 wpa_supplicant_associate(wpa_s, selected, ssid);
1247 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001248 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1249 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001252 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253}
1254
1255
1256static struct wpa_ssid *
1257wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1258{
1259 int prio;
1260 struct wpa_ssid *ssid;
1261
1262 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1263 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1264 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001265 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 continue;
1267 if (ssid->mode == IEEE80211_MODE_IBSS ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001268 ssid->mode == IEEE80211_MODE_AP ||
1269 ssid->mode == IEEE80211_MODE_MESH)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 return ssid;
1271 }
1272 }
1273 return NULL;
1274}
1275
1276
1277/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1278 * on BSS added and BSS changed events */
1279static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001280 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001282 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283
1284 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1285 return;
1286
Jouni Malinen87fd2792011-05-16 18:35:42 +03001287 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289
Jouni Malinen87fd2792011-05-16 18:35:42 +03001290 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 if (ssid == NULL)
1292 continue;
1293
Jouni Malinen87fd2792011-05-16 18:35:42 +03001294 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295 if (rsn == NULL)
1296 continue;
1297
Jouni Malinen87fd2792011-05-16 18:35:42 +03001298 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 }
1300
1301}
1302
1303
1304static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1305 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001306 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001308 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001309#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 int min_diff;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07001311#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
1313 if (wpa_s->reassociate)
1314 return 1; /* explicit request to reassociate */
1315 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1316 return 1; /* we are not associated; continue */
1317 if (wpa_s->current_ssid == NULL)
1318 return 1; /* unknown current SSID */
1319 if (wpa_s->current_ssid != ssid)
1320 return 1; /* different network block */
1321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 if (wpas_driver_bss_selection(wpa_s))
1323 return 0; /* Driver-based roaming */
1324
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001325 if (wpa_s->current_ssid->ssid)
1326 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1327 wpa_s->current_ssid->ssid,
1328 wpa_s->current_ssid->ssid_len);
1329 if (!current_bss)
1330 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
1332 if (!current_bss)
1333 return 1; /* current BSS not seen in scan results */
1334
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001335 if (current_bss == selected)
1336 return 0;
1337
1338 if (selected->last_update_idx > current_bss->last_update_idx)
1339 return 1; /* current BSS not seen in the last scan */
1340
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001341#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001343 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
1344 " level=%d snr=%d est_throughput=%u",
1345 MAC2STR(current_bss->bssid), current_bss->level,
1346 current_bss->snr, current_bss->est_throughput);
1347 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
1348 " level=%d snr=%d est_throughput=%u",
1349 MAC2STR(selected->bssid), selected->level,
1350 selected->snr, selected->est_throughput);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351
1352 if (wpa_s->current_ssid->bssid_set &&
1353 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1354 0) {
1355 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1356 "has preferred BSSID");
1357 return 1;
1358 }
1359
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001360 if (selected->est_throughput > current_bss->est_throughput + 5000) {
1361 wpa_dbg(wpa_s, MSG_DEBUG,
1362 "Allow reassociation - selected BSS has better estimated throughput");
1363 return 1;
1364 }
1365
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001366 if (current_bss->level < 0 && current_bss->level > selected->level) {
1367 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1368 "signal level");
1369 return 0;
1370 }
1371
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 min_diff = 2;
1373 if (current_bss->level < 0) {
1374 if (current_bss->level < -85)
1375 min_diff = 1;
1376 else if (current_bss->level < -80)
1377 min_diff = 2;
1378 else if (current_bss->level < -75)
1379 min_diff = 3;
1380 else if (current_bss->level < -70)
1381 min_diff = 4;
1382 else
1383 min_diff = 5;
1384 }
1385 if (abs(current_bss->level - selected->level) < min_diff) {
1386 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1387 "in signal level");
1388 return 0;
1389 }
1390
1391 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001392#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001393 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001394#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395}
1396
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001397
Jouni Malinen89ca7022012-09-14 13:03:12 -07001398/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001399 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001400static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001401 union wpa_event_data *data,
1402 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001403{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001404 struct wpa_scan_results *scan_res = NULL;
1405 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001407#ifndef CONFIG_NO_RANDOM_POOL
1408 size_t i, num;
1409#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410
1411#ifdef CONFIG_AP
1412 if (wpa_s->ap_iface)
1413 ap = 1;
1414#endif /* CONFIG_AP */
1415
1416 wpa_supplicant_notify_scanning(wpa_s, 0);
1417
1418 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1419 data ? &data->scan_info :
1420 NULL, 1);
1421 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001422 if (wpa_s->conf->ap_scan == 2 || ap ||
1423 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001425 if (!own_request)
1426 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001427 if (data && data->scan_info.external_scan)
1428 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1430 "scanning again");
1431 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001432 ret = -1;
1433 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 }
1435
1436#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 num = scan_res->num;
1438 if (num > 10)
1439 num = 10;
1440 for (i = 0; i < num; i++) {
1441 u8 buf[5];
1442 struct wpa_scan_res *res = scan_res->res[i];
1443 buf[0] = res->bssid[5];
1444 buf[1] = res->qual & 0xff;
1445 buf[2] = res->noise & 0xff;
1446 buf[3] = res->level & 0xff;
1447 buf[4] = res->tsf & 0xff;
1448 random_add_randomness(buf, sizeof(buf));
1449 }
1450#endif /* CONFIG_NO_RANDOM_POOL */
1451
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001452 if (own_request && wpa_s->scan_res_handler &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001453 !(data && data->scan_info.external_scan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1455 struct wpa_scan_results *scan_res);
1456
1457 scan_res_handler = wpa_s->scan_res_handler;
1458 wpa_s->scan_res_handler = NULL;
1459 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001460 ret = -2;
1461 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 }
1463
1464 if (ap) {
1465 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1466#ifdef CONFIG_AP
1467 if (wpa_s->ap_iface->scan_cb)
1468 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1469#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001470 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001472
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001473 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001474 wpa_s->own_scan_running,
1475 data ? data->scan_info.external_scan : 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001476 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001477 wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
1478 own_request && !(data && data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001479 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1480 wpa_s->manual_scan_id);
1481 wpa_s->manual_scan_use_id = 0;
1482 } else {
1483 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1484 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001485 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486
1487 wpas_notify_scan_done(wpa_s, 1);
1488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001489 if (data && data->scan_info.external_scan) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001490 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 -07001491 wpa_scan_results_free(scan_res);
1492 return 0;
1493 }
1494
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001495 if (wnm_scan_process(wpa_s, 1) > 0)
1496 goto scan_work_done;
1497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001498 if (sme_proc_obss_scan(wpa_s) > 0)
1499 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001501 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1502 goto scan_work_done;
1503
1504 if (autoscan_notify_scan(wpa_s, scan_res))
1505 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 if (wpa_s->disconnected) {
1508 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001509 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 }
1511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001512 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001513 bgscan_notify_scan(wpa_s, scan_res) == 1)
1514 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001516 wpas_wps_update_ap_info(wpa_s, scan_res);
1517
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001518 wpa_scan_results_free(scan_res);
1519
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001520 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001521 struct wpa_radio_work *work = wpa_s->scan_work;
1522 wpa_s->scan_work = NULL;
1523 radio_work_done(work);
1524 }
1525
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001526 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001527
1528scan_work_done:
1529 wpa_scan_results_free(scan_res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001530 if (own_request && wpa_s->scan_work) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001531 struct wpa_radio_work *work = wpa_s->scan_work;
1532 wpa_s->scan_work = NULL;
1533 radio_work_done(work);
1534 }
1535 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001536}
1537
1538
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001539static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001540 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001541{
1542 struct wpa_bss *selected;
1543 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001544 int time_to_reenable = wpas_reenabled_network_time(wpa_s);
1545
1546 if (time_to_reenable > 0) {
1547 wpa_dbg(wpa_s, MSG_DEBUG,
1548 "Postpone network selection by %d seconds since all networks are disabled",
1549 time_to_reenable);
1550 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
1551 eloop_register_timeout(time_to_reenable, 0,
1552 wpas_network_reenabled, wpa_s, NULL);
1553 return 0;
1554 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001555
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001556 if (wpa_s->p2p_mgmt)
1557 return 0; /* no normal connection on p2p_mgmt interface */
1558
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001559 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560
1561 if (selected) {
1562 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001563 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001564 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001565 if (new_scan)
1566 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001567 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001568 }
1569
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001570 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001571 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001572 return -1;
1573 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001574 if (new_scan)
1575 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001576 /*
1577 * Do not notify other virtual radios of scan results since we do not
1578 * want them to start other associations at the same time.
1579 */
1580 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001582#ifdef CONFIG_MESH
1583 if (wpa_s->ifmsh) {
1584 wpa_msg(wpa_s, MSG_INFO,
1585 "Avoiding join because we already joined a mesh group");
1586 return 0;
1587 }
1588#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001589 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1590 ssid = wpa_supplicant_pick_new_network(wpa_s);
1591 if (ssid) {
1592 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1593 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001594 if (new_scan)
1595 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001596 } else if (own_request) {
1597 /*
1598 * No SSID found. If SCAN results are as a result of
1599 * own scan request and not due to a scan request on
1600 * another shared interface, try another scan.
1601 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 int timeout_sec = wpa_s->scan_interval;
1603 int timeout_usec = 0;
1604#ifdef CONFIG_P2P
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001605 int res;
1606
1607 res = wpas_p2p_scan_no_go_seen(wpa_s);
1608 if (res == 2)
1609 return 2;
1610 if (res == 1)
Dmitry Shmidt04949592012-07-19 12:16:46 -07001611 return 0;
1612
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001613 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001614 wpa_s->show_group_started ||
1615 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616 /*
1617 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001618 * state and during P2P join-a-group operation
1619 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 */
1621 timeout_sec = 0;
1622 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001623 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1624 timeout_usec);
1625 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 }
1627#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001628#ifdef CONFIG_INTERWORKING
1629 if (wpa_s->conf->auto_interworking &&
1630 wpa_s->conf->interworking &&
1631 wpa_s->conf->cred) {
1632 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1633 "start ANQP fetch since no matching "
1634 "networks found");
1635 wpa_s->network_select = 1;
1636 wpa_s->auto_network_select = 1;
1637 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001638 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001639 }
1640#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001641#ifdef CONFIG_WPS
1642 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1643 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1644 timeout_sec = 0;
1645 timeout_usec = 500000;
1646 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1647 timeout_usec);
1648 return 0;
1649 }
1650#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001651 if (wpa_supplicant_req_sched_scan(wpa_s))
1652 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1653 timeout_usec);
Dmitry Shmidt41712582015-06-29 11:02:15 -07001654
1655 wpa_msg_ctrl(wpa_s, MSG_INFO,
1656 WPA_EVENT_NETWORK_NOT_FOUND);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 }
1658 }
1659 return 0;
1660}
1661
1662
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001663static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1664 union wpa_event_data *data)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 struct wpa_supplicant *ifs;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001667 int res;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001668
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001669 res = _wpa_supplicant_event_scan_results(wpa_s, data, 1);
1670 if (res == 2) {
1671 /*
1672 * Interface may have been removed, so must not dereference
1673 * wpa_s after this.
1674 */
1675 return 1;
1676 }
1677 if (res != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 /*
1679 * If no scan results could be fetched, then no need to
1680 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001681 * this scan. Similarly, if scan results started a new operation on this
1682 * interface, do not notify other interfaces to avoid concurrent
1683 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 */
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001685 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 }
1687
1688 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001689 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 * so, they get updated with this same scan info.
1691 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001692 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1693 radio_list) {
1694 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1696 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001697 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 }
1699 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001700
1701 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702}
1703
1704#endif /* CONFIG_NO_SCAN_PROCESSING */
1705
1706
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001707int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1708{
1709#ifdef CONFIG_NO_SCAN_PROCESSING
1710 return -1;
1711#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001712 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001713
Dmitry Shmidt41712582015-06-29 11:02:15 -07001714 if (wpa_s->last_scan_res_used == 0)
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001715 return -1;
1716
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001717 os_get_reltime(&now);
1718 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001719 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1720 return -1;
1721 }
1722
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001723 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001724#endif /* CONFIG_NO_SCAN_PROCESSING */
1725}
1726
Dmitry Shmidt04949592012-07-19 12:16:46 -07001727#ifdef CONFIG_WNM
1728
1729static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1730{
1731 struct wpa_supplicant *wpa_s = eloop_ctx;
1732
1733 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1734 return;
1735
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001736 if (!wpa_s->no_keep_alive) {
1737 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1738 MAC2STR(wpa_s->bssid));
1739 /* TODO: could skip this if normal data traffic has been sent */
1740 /* TODO: Consider using some more appropriate data frame for
1741 * this */
1742 if (wpa_s->l2)
1743 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1744 (u8 *) "", 0);
1745 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001746
1747#ifdef CONFIG_SME
1748 if (wpa_s->sme.bss_max_idle_period) {
1749 unsigned int msec;
1750 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1751 if (msec > 100)
1752 msec -= 100;
1753 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1754 wnm_bss_keep_alive, wpa_s, NULL);
1755 }
1756#endif /* CONFIG_SME */
1757}
1758
1759
1760static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1761 const u8 *ies, size_t ies_len)
1762{
1763 struct ieee802_11_elems elems;
1764
1765 if (ies == NULL)
1766 return;
1767
1768 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1769 return;
1770
1771#ifdef CONFIG_SME
1772 if (elems.bss_max_idle_period) {
1773 unsigned int msec;
1774 wpa_s->sme.bss_max_idle_period =
1775 WPA_GET_LE16(elems.bss_max_idle_period);
1776 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1777 "TU)%s", wpa_s->sme.bss_max_idle_period,
1778 (elems.bss_max_idle_period[2] & 0x01) ?
1779 " (protected keep-live required)" : "");
1780 if (wpa_s->sme.bss_max_idle_period == 0)
1781 wpa_s->sme.bss_max_idle_period = 1;
1782 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1783 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1784 /* msec times 1000 */
1785 msec = wpa_s->sme.bss_max_idle_period * 1024;
1786 if (msec > 100)
1787 msec -= 100;
1788 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1789 wnm_bss_keep_alive, wpa_s,
1790 NULL);
1791 }
1792 }
1793#endif /* CONFIG_SME */
1794}
1795
1796#endif /* CONFIG_WNM */
1797
1798
1799void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1800{
1801#ifdef CONFIG_WNM
1802 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1803#endif /* CONFIG_WNM */
1804}
1805
1806
Dmitry Shmidt051af732013-10-22 13:52:46 -07001807#ifdef CONFIG_INTERWORKING
1808
1809static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1810 size_t len)
1811{
1812 int res;
1813
1814 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1815 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1816 if (res) {
1817 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1818 }
1819
1820 return res;
1821}
1822
1823
1824static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1825 const u8 *ies, size_t ies_len)
1826{
1827 struct ieee802_11_elems elems;
1828
1829 if (ies == NULL)
1830 return;
1831
1832 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1833 return;
1834
1835 if (elems.qos_map_set) {
1836 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1837 elems.qos_map_set_len);
1838 }
1839}
1840
1841#endif /* CONFIG_INTERWORKING */
1842
1843
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1845 union wpa_event_data *data)
1846{
1847 int l, len, found = 0, wpa_found, rsn_found;
1848 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001849#ifdef CONFIG_IEEE80211R
1850 u8 bssid[ETH_ALEN];
1851#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852
1853 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1854 if (data->assoc_info.req_ies)
1855 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1856 data->assoc_info.req_ies_len);
1857 if (data->assoc_info.resp_ies) {
1858 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1859 data->assoc_info.resp_ies_len);
1860#ifdef CONFIG_TDLS
1861 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1862 data->assoc_info.resp_ies_len);
1863#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001864#ifdef CONFIG_WNM
1865 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1866 data->assoc_info.resp_ies_len);
1867#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001868#ifdef CONFIG_INTERWORKING
1869 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1870 data->assoc_info.resp_ies_len);
1871#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 }
1873 if (data->assoc_info.beacon_ies)
1874 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1875 data->assoc_info.beacon_ies,
1876 data->assoc_info.beacon_ies_len);
1877 if (data->assoc_info.freq)
1878 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1879 data->assoc_info.freq);
1880
1881 p = data->assoc_info.req_ies;
1882 l = data->assoc_info.req_ies_len;
1883
1884 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1885 while (p && l >= 2) {
1886 len = p[1] + 2;
1887 if (len > l) {
1888 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1889 p, l);
1890 break;
1891 }
1892 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1893 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1894 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1895 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1896 break;
1897 found = 1;
1898 wpa_find_assoc_pmkid(wpa_s);
1899 break;
1900 }
1901 l -= len;
1902 p += len;
1903 }
1904 if (!found && data->assoc_info.req_ies)
1905 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1906
1907#ifdef CONFIG_IEEE80211R
1908#ifdef CONFIG_SME
1909 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1911 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1912 data->assoc_info.resp_ies,
1913 data->assoc_info.resp_ies_len,
1914 bssid) < 0) {
1915 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1916 "Reassociation Response failed");
1917 wpa_supplicant_deauthenticate(
1918 wpa_s, WLAN_REASON_INVALID_IE);
1919 return -1;
1920 }
1921 }
1922
1923 p = data->assoc_info.resp_ies;
1924 l = data->assoc_info.resp_ies_len;
1925
1926#ifdef CONFIG_WPS_STRICT
1927 if (p && wpa_s->current_ssid &&
1928 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1929 struct wpabuf *wps;
1930 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1931 if (wps == NULL) {
1932 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1933 "include WPS IE in (Re)Association Response");
1934 return -1;
1935 }
1936
1937 if (wps_validate_assoc_resp(wps) < 0) {
1938 wpabuf_free(wps);
1939 wpa_supplicant_deauthenticate(
1940 wpa_s, WLAN_REASON_INVALID_IE);
1941 return -1;
1942 }
1943 wpabuf_free(wps);
1944 }
1945#endif /* CONFIG_WPS_STRICT */
1946
1947 /* Go through the IEs and make a copy of the MDIE, if present. */
1948 while (p && l >= 2) {
1949 len = p[1] + 2;
1950 if (len > l) {
1951 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1952 p, l);
1953 break;
1954 }
1955 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1956 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1957 wpa_s->sme.ft_used = 1;
1958 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1959 MOBILITY_DOMAIN_ID_LEN);
1960 break;
1961 }
1962 l -= len;
1963 p += len;
1964 }
1965#endif /* CONFIG_SME */
1966
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001967 /* Process FT when SME is in the driver */
1968 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1969 wpa_ft_is_completed(wpa_s->wpa)) {
1970 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1971 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1972 data->assoc_info.resp_ies,
1973 data->assoc_info.resp_ies_len,
1974 bssid) < 0) {
1975 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1976 "Reassociation Response failed");
1977 wpa_supplicant_deauthenticate(
1978 wpa_s, WLAN_REASON_INVALID_IE);
1979 return -1;
1980 }
1981 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1982 }
1983
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1985 data->assoc_info.resp_ies_len);
1986#endif /* CONFIG_IEEE80211R */
1987
1988 /* WPA/RSN IE from Beacon/ProbeResp */
1989 p = data->assoc_info.beacon_ies;
1990 l = data->assoc_info.beacon_ies_len;
1991
1992 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1993 */
1994 wpa_found = rsn_found = 0;
1995 while (p && l >= 2) {
1996 len = p[1] + 2;
1997 if (len > l) {
1998 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1999 p, l);
2000 break;
2001 }
2002 if (!wpa_found &&
2003 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
2004 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
2005 wpa_found = 1;
2006 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
2007 }
2008
2009 if (!rsn_found &&
2010 p[0] == WLAN_EID_RSN && p[1] >= 2) {
2011 rsn_found = 1;
2012 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
2013 }
2014
2015 l -= len;
2016 p += len;
2017 }
2018
2019 if (!wpa_found && data->assoc_info.beacon_ies)
2020 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
2021 if (!rsn_found && data->assoc_info.beacon_ies)
2022 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
2023 if (wpa_found || rsn_found)
2024 wpa_s->ap_ies_from_associnfo = 1;
2025
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002026#ifdef CONFIG_FST
2027 wpabuf_free(wpa_s->received_mb_ies);
2028 wpa_s->received_mb_ies = NULL;
2029 if (wpa_s->fst) {
2030 struct mb_ies_info mb_ies;
2031
2032 wpa_printf(MSG_DEBUG, "Looking for MB IE");
2033 if (!mb_ies_info_by_ies(&mb_ies, data->assoc_info.resp_ies,
2034 data->assoc_info.resp_ies_len))
2035 wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
2036 }
2037#endif /* CONFIG_FST */
2038
Jouni Malinen87fd2792011-05-16 18:35:42 +03002039 if (wpa_s->assoc_freq && data->assoc_info.freq &&
2040 wpa_s->assoc_freq != data->assoc_info.freq) {
2041 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
2042 "%u to %u MHz",
2043 wpa_s->assoc_freq, data->assoc_info.freq);
2044 wpa_supplicant_update_scan_results(wpa_s);
2045 }
2046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 wpa_s->assoc_freq = data->assoc_info.freq;
2048
2049 return 0;
2050}
2051
2052
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002053static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
2054{
2055 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
2056
2057 if (!wpa_s->current_bss || !wpa_s->current_ssid)
2058 return -1;
2059
2060 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
2061 return 0;
2062
2063 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
2064 WPA_IE_VENDOR_TYPE);
2065 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
2066
2067 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
2068 bss_wpa ? 2 + bss_wpa[1] : 0) ||
2069 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
2070 bss_rsn ? 2 + bss_rsn[1] : 0))
2071 return -1;
2072
2073 return 0;
2074}
2075
2076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
2078 union wpa_event_data *data)
2079{
2080 u8 bssid[ETH_ALEN];
2081 int ft_completed;
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002082 int new_bss = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083
2084#ifdef CONFIG_AP
2085 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002086 if (!data)
2087 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002088 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
2089 data->assoc_info.addr,
2090 data->assoc_info.req_ies,
2091 data->assoc_info.req_ies_len,
2092 data->assoc_info.reassoc);
2093 return;
2094 }
2095#endif /* CONFIG_AP */
2096
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002097 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
2098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
2100 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
2101 return;
2102
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002103 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
2104 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002105 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002106 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2107 return;
2108 }
2109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002111 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
2113 MACSTR, MAC2STR(bssid));
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002114 new_bss = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
2117 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002118 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119
2120 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
2121 wpa_clear_keys(wpa_s, bssid);
2122 }
2123 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002124 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2126 return;
2127 }
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002128 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002129
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002130#ifdef ANDROID
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002131 if (wpa_s->conf->ap_scan == 1) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002132#else
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002133 if (wpa_s->conf->ap_scan == 1 &&
2134 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08002135#endif
Amarnath Hullur Subramanyama82c83c2015-09-18 06:57:06 -07002136 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
2137 wpa_msg(wpa_s, MSG_WARNING,
2138 "WPA/RSN IEs not updated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 }
2140
2141#ifdef CONFIG_SME
2142 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
2143 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07002144 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145#endif /* CONFIG_SME */
2146
2147 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
2148 if (wpa_s->current_ssid) {
2149 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
2150 * initialized before association, but for other modes,
2151 * initialize PC/SC here, if the current configuration needs
2152 * smartcard or SIM/USIM. */
2153 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
2154 }
2155 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
2156 if (wpa_s->l2)
2157 l2_packet_notify_auth_start(wpa_s->l2);
2158
2159 /*
2160 * Set portEnabled first to FALSE in order to get EAP state machine out
2161 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
2162 * state machine may transit to AUTHENTICATING state based on obsolete
2163 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
2164 * AUTHENTICATED without ever giving chance to EAP state machine to
2165 * reset the state.
2166 */
2167 if (!ft_completed) {
2168 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2169 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2170 }
2171 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
2172 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
2173 /* 802.1X::portControl = Auto */
2174 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
2175 wpa_s->eapol_received = 0;
2176 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2177 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
2178 (wpa_s->current_ssid &&
2179 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07002180 if (wpa_s->current_ssid &&
2181 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002182 (wpa_s->drv_flags &
2183 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2184 /*
2185 * Set the key after having received joined-IBSS event
2186 * from the driver.
2187 */
2188 wpa_supplicant_set_wpa_none_key(wpa_s,
2189 wpa_s->current_ssid);
2190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 wpa_supplicant_cancel_auth_timeout(wpa_s);
2192 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2193 } else if (!ft_completed) {
2194 /* Timeout for receiving the first EAPOL packet */
2195 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
2196 }
2197 wpa_supplicant_cancel_scan(wpa_s);
2198
2199 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2200 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
2201 /*
2202 * We are done; the driver will take care of RSN 4-way
2203 * handshake.
2204 */
2205 wpa_supplicant_cancel_auth_timeout(wpa_s);
2206 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2207 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2208 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2209 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2210 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2211 /*
2212 * The driver will take care of RSN 4-way handshake, so we need
2213 * to allow EAPOL supplicant to complete its work without
2214 * waiting for WPA supplicant.
2215 */
2216 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2217 } else if (ft_completed) {
2218 /*
2219 * FT protocol completed - make sure EAPOL state machine ends
2220 * up in authenticated.
2221 */
2222 wpa_supplicant_cancel_auth_timeout(wpa_s);
2223 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2224 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2225 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2226 }
2227
Jouni Malinena05074c2012-12-21 21:35:35 +02002228 wpa_s->last_eapol_matches_bssid = 0;
2229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002231 struct os_reltime now, age;
2232 os_get_reltime(&now);
2233 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234 if (age.sec == 0 && age.usec < 100000 &&
2235 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2236 0) {
2237 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2238 "frame that was received just before "
2239 "association notification");
2240 wpa_supplicant_rx_eapol(
2241 wpa_s, wpa_s->pending_eapol_rx_src,
2242 wpabuf_head(wpa_s->pending_eapol_rx),
2243 wpabuf_len(wpa_s->pending_eapol_rx));
2244 }
2245 wpabuf_free(wpa_s->pending_eapol_rx);
2246 wpa_s->pending_eapol_rx = NULL;
2247 }
2248
2249 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2250 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002251 wpa_s->current_ssid &&
2252 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 /* Set static WEP keys again */
2254 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2255 }
2256
2257#ifdef CONFIG_IBSS_RSN
2258 if (wpa_s->current_ssid &&
2259 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2260 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2261 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2262 wpa_s->ibss_rsn == NULL) {
2263 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2264 if (!wpa_s->ibss_rsn) {
2265 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2266 wpa_supplicant_deauthenticate(
2267 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2268 return;
2269 }
2270
2271 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2272 }
2273#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002274
2275 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002276
2277 if (data) {
2278 wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
2279 data->assoc_info.resp_ies_len,
2280 &data->assoc_info.wmm_params);
2281
2282 if (wpa_s->reassoc_same_bss)
2283 wmm_ac_restore_tspecs(wpa_s);
2284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285}
2286
2287
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002288static int disconnect_reason_recoverable(u16 reason_code)
2289{
2290 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2291 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2292 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2293}
2294
2295
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002297 u16 reason_code,
2298 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299{
2300 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002301
2302 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2303 /*
2304 * At least Host AP driver and a Prism3 card seemed to be
2305 * generating streams of disconnected events when configuring
2306 * IBSS for WPA-None. Ignore them for now.
2307 */
2308 return;
2309 }
2310
2311 bssid = wpa_s->bssid;
2312 if (is_zero_ether_addr(bssid))
2313 bssid = wpa_s->pending_bssid;
2314
2315 if (!is_zero_ether_addr(bssid) ||
2316 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2317 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2318 " reason=%d%s",
2319 MAC2STR(bssid), reason_code,
2320 locally_generated ? " locally_generated=1" : "");
2321 }
2322}
2323
2324
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002325static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2326 int locally_generated)
2327{
2328 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2329 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2330 return 0; /* Not in 4-way handshake with PSK */
2331
2332 /*
2333 * It looks like connection was lost while trying to go through PSK
2334 * 4-way handshake. Filter out known disconnection cases that are caused
2335 * by something else than PSK mismatch to avoid confusing reports.
2336 */
2337
2338 if (locally_generated) {
2339 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2340 return 0;
2341 }
2342
2343 return 1;
2344}
2345
2346
Jouni Malinen2b89da82012-08-31 22:04:41 +03002347static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2348 u16 reason_code,
2349 int locally_generated)
2350{
2351 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 int authenticating;
2353 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002354 struct wpa_bss *fast_reconnect = NULL;
2355 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002356 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357
2358 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2359 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2360
2361 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2362 /*
2363 * At least Host AP driver and a Prism3 card seemed to be
2364 * generating streams of disconnected events when configuring
2365 * IBSS for WPA-None. Ignore them for now.
2366 */
2367 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2368 "IBSS/WPA-None mode");
2369 return;
2370 }
2371
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002372 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2374 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002375 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2376 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002377 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002379 if (!wpa_s->disconnected &&
2380 (!wpa_s->auto_reconnect_disabled ||
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002381 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2382 wpas_wps_searching(wpa_s))) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002383 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002384 "reconnect (wps=%d/%d wpa_state=%d)",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002385 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002386 wpas_wps_searching(wpa_s),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002387 wpa_s->wpa_state);
2388 if (wpa_s->wpa_state == WPA_COMPLETED &&
2389 wpa_s->current_ssid &&
2390 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002391 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002392 disconnect_reason_recoverable(reason_code)) {
2393 /*
2394 * It looks like the AP has dropped association with
2395 * us, but could allow us to get back in. Try to
2396 * reconnect to the same BSS without full scan to save
2397 * time for some common cases.
2398 */
2399 fast_reconnect = wpa_s->current_bss;
2400 fast_reconnect_ssid = wpa_s->current_ssid;
2401 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002402 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 else
2404 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2405 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002407 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408 "try to re-connect");
2409 wpa_s->reassociate = 0;
2410 wpa_s->disconnected = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002411 if (!wpa_s->pno)
2412 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413 }
2414 bssid = wpa_s->bssid;
2415 if (is_zero_ether_addr(bssid))
2416 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002417 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2418 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002420 if (locally_generated)
2421 wpa_s->disconnect_reason = -reason_code;
2422 else
2423 wpa_s->disconnect_reason = reason_code;
2424 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2426 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 wpa_clear_keys(wpa_s, wpa_s->bssid);
2428 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002429 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 wpa_supplicant_mark_disassoc(wpa_s);
2431
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002432 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002434 wpa_s->current_ssid = last_ssid;
2435 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002436
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002437 if (fast_reconnect &&
2438 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2439 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2440 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2441 fast_reconnect->ssid_len) &&
2442 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002443#ifndef CONFIG_NO_SCAN_PROCESSING
2444 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2445 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2446 fast_reconnect_ssid) < 0) {
2447 /* Recover through full scan */
2448 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2449 }
2450#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002451 } else if (fast_reconnect) {
2452 /*
2453 * Could not reconnect to the same BSS due to network being
2454 * disabled. Use a new scan to match the alternative behavior
2455 * above, i.e., to continue automatic reconnection attempt in a
2456 * way that enforces disabled network rules.
2457 */
2458 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002459 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460}
2461
2462
2463#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002464void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465{
2466 struct wpa_supplicant *wpa_s = eloop_ctx;
2467
2468 if (!wpa_s->pending_mic_error_report)
2469 return;
2470
2471 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2472 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2473 wpa_s->pending_mic_error_report = 0;
2474}
2475#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2476
2477
2478static void
2479wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2480 union wpa_event_data *data)
2481{
2482 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002483 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
2485 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2486 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002487 os_get_reltime(&t);
2488 if ((wpa_s->last_michael_mic_error.sec &&
2489 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 wpa_s->pending_mic_error_report) {
2491 if (wpa_s->pending_mic_error_report) {
2492 /*
2493 * Send the pending MIC error report immediately since
2494 * we are going to start countermeasures and AP better
2495 * do the same.
2496 */
2497 wpa_sm_key_request(wpa_s->wpa, 1,
2498 wpa_s->pending_mic_error_pairwise);
2499 }
2500
2501 /* Send the new MIC error report immediately since we are going
2502 * to start countermeasures and AP better do the same.
2503 */
2504 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2505
2506 /* initialize countermeasures */
2507 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508
2509 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2512
2513 /*
2514 * Need to wait for completion of request frame. We do not get
2515 * any callback for the message completion, so just wait a
2516 * short while and hope for the best. */
2517 os_sleep(0, 10000);
2518
2519 wpa_drv_set_countermeasures(wpa_s, 1);
2520 wpa_supplicant_deauthenticate(wpa_s,
2521 WLAN_REASON_MICHAEL_MIC_FAILURE);
2522 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2523 wpa_s, NULL);
2524 eloop_register_timeout(60, 0,
2525 wpa_supplicant_stop_countermeasures,
2526 wpa_s, NULL);
2527 /* TODO: mark the AP rejected for 60 second. STA is
2528 * allowed to associate with another AP.. */
2529 } else {
2530#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2531 if (wpa_s->mic_errors_seen) {
2532 /*
2533 * Reduce the effectiveness of Michael MIC error
2534 * reports as a means for attacking against TKIP if
2535 * more than one MIC failure is noticed with the same
2536 * PTK. We delay the transmission of the reports by a
2537 * random time between 0 and 60 seconds in order to
2538 * force the attacker wait 60 seconds before getting
2539 * the information on whether a frame resulted in a MIC
2540 * failure.
2541 */
2542 u8 rval[4];
2543 int sec;
2544
2545 if (os_get_random(rval, sizeof(rval)) < 0)
2546 sec = os_random() % 60;
2547 else
2548 sec = WPA_GET_BE32(rval) % 60;
2549 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2550 "report %d seconds", sec);
2551 wpa_s->pending_mic_error_report = 1;
2552 wpa_s->pending_mic_error_pairwise = pairwise;
2553 eloop_cancel_timeout(
2554 wpa_supplicant_delayed_mic_error_report,
2555 wpa_s, NULL);
2556 eloop_register_timeout(
2557 sec, os_random() % 1000000,
2558 wpa_supplicant_delayed_mic_error_report,
2559 wpa_s, NULL);
2560 } else {
2561 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2562 }
2563#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2564 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2565#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2566 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002567 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 wpa_s->mic_errors_seen++;
2569}
2570
2571
2572#ifdef CONFIG_TERMINATE_ONLASTIF
2573static int any_interfaces(struct wpa_supplicant *head)
2574{
2575 struct wpa_supplicant *wpa_s;
2576
2577 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2578 if (!wpa_s->interface_removed)
2579 return 1;
2580 return 0;
2581}
2582#endif /* CONFIG_TERMINATE_ONLASTIF */
2583
2584
2585static void
2586wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2587 union wpa_event_data *data)
2588{
2589 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2590 return;
2591
2592 switch (data->interface_status.ievent) {
2593 case EVENT_INTERFACE_ADDED:
2594 if (!wpa_s->interface_removed)
2595 break;
2596 wpa_s->interface_removed = 0;
2597 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2598 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2599 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2600 "driver after interface was added");
2601 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002602
2603#ifdef CONFIG_P2P
2604 if (!wpa_s->global->p2p &&
2605 !wpa_s->global->p2p_disabled &&
2606 !wpa_s->conf->p2p_disabled &&
2607 (wpa_s->drv_flags &
2608 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
2609 wpas_p2p_add_p2pdev_interface(
2610 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
2611 wpa_printf(MSG_INFO,
2612 "P2P: Failed to enable P2P Device interface");
2613 /* Try to continue without. P2P will be disabled. */
2614 }
2615#endif /* CONFIG_P2P */
2616
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617 break;
2618 case EVENT_INTERFACE_REMOVED:
2619 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2620 wpa_s->interface_removed = 1;
2621 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002622 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 l2_packet_deinit(wpa_s->l2);
2624 wpa_s->l2 = NULL;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002625
2626#ifdef CONFIG_P2P
2627 if (wpa_s->global->p2p &&
2628 wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
2629 (wpa_s->drv_flags &
2630 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
2631 wpa_dbg(wpa_s, MSG_DEBUG,
2632 "Removing P2P Device interface");
2633 wpa_supplicant_remove_iface(
2634 wpa_s->global, wpa_s->global->p2p_init_wpa_s,
2635 0);
2636 wpa_s->global->p2p_init_wpa_s = NULL;
2637 }
2638#endif /* CONFIG_P2P */
2639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640#ifdef CONFIG_TERMINATE_ONLASTIF
2641 /* check if last interface */
2642 if (!any_interfaces(wpa_s->global->ifaces))
2643 eloop_terminate();
2644#endif /* CONFIG_TERMINATE_ONLASTIF */
2645 break;
2646 }
2647}
2648
2649
2650#ifdef CONFIG_PEERKEY
2651static void
2652wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2653 union wpa_event_data *data)
2654{
2655 if (data == NULL)
2656 return;
2657 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2658}
2659#endif /* CONFIG_PEERKEY */
2660
2661
2662#ifdef CONFIG_TDLS
2663static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2664 union wpa_event_data *data)
2665{
2666 if (data == NULL)
2667 return;
2668 switch (data->tdls.oper) {
2669 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002670 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2671 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2672 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2673 else
2674 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 break;
2676 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002677 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2678 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2679 data->tdls.reason_code);
2680 else
2681 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2682 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 break;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002684 case TDLS_REQUEST_DISCOVER:
2685 wpa_tdls_send_discovery_request(wpa_s->wpa,
2686 data->tdls.peer);
2687 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688 }
2689}
2690#endif /* CONFIG_TDLS */
2691
2692
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002693#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002694static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2695 union wpa_event_data *data)
2696{
2697 if (data == NULL)
2698 return;
2699 switch (data->wnm.oper) {
2700 case WNM_OPER_SLEEP:
2701 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2702 "(action=%d, intval=%d)",
2703 data->wnm.sleep_action, data->wnm.sleep_intval);
2704 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002705 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002706 break;
2707 }
2708}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002709#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002710
2711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712#ifdef CONFIG_IEEE80211R
2713static void
2714wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2715 union wpa_event_data *data)
2716{
2717 if (data == NULL)
2718 return;
2719
2720 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2721 data->ft_ies.ies_len,
2722 data->ft_ies.ft_action,
2723 data->ft_ies.target_ap,
2724 data->ft_ies.ric_ies,
2725 data->ft_ies.ric_ies_len) < 0) {
2726 /* TODO: prevent MLME/driver from trying to associate? */
2727 }
2728}
2729#endif /* CONFIG_IEEE80211R */
2730
2731
2732#ifdef CONFIG_IBSS_RSN
2733static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2734 union wpa_event_data *data)
2735{
2736 struct wpa_ssid *ssid;
2737 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2738 return;
2739 if (data == NULL)
2740 return;
2741 ssid = wpa_s->current_ssid;
2742 if (ssid == NULL)
2743 return;
2744 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2745 return;
2746
2747 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2748}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002749
2750
2751static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2752 union wpa_event_data *data)
2753{
2754 struct wpa_ssid *ssid = wpa_s->current_ssid;
2755
2756 if (ssid == NULL)
2757 return;
2758
2759 /* check if the ssid is correctly configured as IBSS/RSN */
2760 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2761 return;
2762
2763 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2764 data->rx_mgmt.frame_len);
2765}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766#endif /* CONFIG_IBSS_RSN */
2767
2768
2769#ifdef CONFIG_IEEE80211R
2770static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2771 size_t len)
2772{
2773 const u8 *sta_addr, *target_ap_addr;
2774 u16 status;
2775
2776 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2777 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2778 return; /* only SME case supported for now */
2779 if (len < 1 + 2 * ETH_ALEN + 2)
2780 return;
2781 if (data[0] != 2)
2782 return; /* Only FT Action Response is supported for now */
2783 sta_addr = data + 1;
2784 target_ap_addr = data + 1 + ETH_ALEN;
2785 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2786 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2787 MACSTR " TargetAP " MACSTR " status %u",
2788 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2789
2790 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2791 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2792 " in FT Action Response", MAC2STR(sta_addr));
2793 return;
2794 }
2795
2796 if (status) {
2797 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2798 "failure (status code %d)", status);
2799 /* TODO: report error to FT code(?) */
2800 return;
2801 }
2802
2803 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2804 len - (1 + 2 * ETH_ALEN + 2), 1,
2805 target_ap_addr, NULL, 0) < 0)
2806 return;
2807
2808#ifdef CONFIG_SME
2809 {
2810 struct wpa_bss *bss;
2811 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2812 if (bss)
2813 wpa_s->sme.freq = bss->freq;
2814 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2815 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2816 WLAN_AUTH_FT);
2817 }
2818#endif /* CONFIG_SME */
2819}
2820#endif /* CONFIG_IEEE80211R */
2821
2822
2823static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2824 struct unprot_deauth *e)
2825{
2826#ifdef CONFIG_IEEE80211W
2827 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2828 "dropped: " MACSTR " -> " MACSTR
2829 " (reason code %u)",
2830 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2831 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2832#endif /* CONFIG_IEEE80211W */
2833}
2834
2835
2836static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2837 struct unprot_disassoc *e)
2838{
2839#ifdef CONFIG_IEEE80211W
2840 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2841 "dropped: " MACSTR " -> " MACSTR
2842 " (reason code %u)",
2843 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2844 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2845#endif /* CONFIG_IEEE80211W */
2846}
2847
2848
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002849static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2850 u16 reason_code, int locally_generated,
2851 const u8 *ie, size_t ie_len, int deauth)
2852{
2853#ifdef CONFIG_AP
2854 if (wpa_s->ap_iface && addr) {
2855 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2856 return;
2857 }
2858
2859 if (wpa_s->ap_iface) {
2860 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2861 return;
2862 }
2863#endif /* CONFIG_AP */
2864
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002865 if (!locally_generated)
2866 wpa_s->own_disconnect_req = 0;
2867
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002868 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2869
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002870 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2871 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2872 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2873 eapol_sm_failed(wpa_s->eapol))) &&
2874 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002875 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002876
2877#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002878 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002879 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2880 locally_generated) > 0) {
2881 /*
2882 * The interface was removed, so cannot continue
2883 * processing any additional operations after this.
2884 */
2885 return;
2886 }
2887 }
2888#endif /* CONFIG_P2P */
2889
2890 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2891 locally_generated);
2892}
2893
2894
2895static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2896 struct disassoc_info *info)
2897{
2898 u16 reason_code = 0;
2899 int locally_generated = 0;
2900 const u8 *addr = NULL;
2901 const u8 *ie = NULL;
2902 size_t ie_len = 0;
2903
2904 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2905
2906 if (info) {
2907 addr = info->addr;
2908 ie = info->ie;
2909 ie_len = info->ie_len;
2910 reason_code = info->reason_code;
2911 locally_generated = info->locally_generated;
2912 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2913 locally_generated ? " (locally generated)" : "");
2914 if (addr)
2915 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2916 MAC2STR(addr));
2917 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2918 ie, ie_len);
2919 }
2920
2921#ifdef CONFIG_AP
2922 if (wpa_s->ap_iface && info && info->addr) {
2923 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2924 return;
2925 }
2926
2927 if (wpa_s->ap_iface) {
2928 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2929 return;
2930 }
2931#endif /* CONFIG_AP */
2932
2933#ifdef CONFIG_P2P
2934 if (info) {
2935 wpas_p2p_disassoc_notif(
2936 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2937 locally_generated);
2938 }
2939#endif /* CONFIG_P2P */
2940
2941 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2942 sme_event_disassoc(wpa_s, info);
2943
2944 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2945 ie, ie_len, 0);
2946}
2947
2948
2949static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2950 struct deauth_info *info)
2951{
2952 u16 reason_code = 0;
2953 int locally_generated = 0;
2954 const u8 *addr = NULL;
2955 const u8 *ie = NULL;
2956 size_t ie_len = 0;
2957
2958 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2959
2960 if (info) {
2961 addr = info->addr;
2962 ie = info->ie;
2963 ie_len = info->ie_len;
2964 reason_code = info->reason_code;
2965 locally_generated = info->locally_generated;
2966 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2967 reason_code,
2968 locally_generated ? " (locally generated)" : "");
2969 if (addr) {
2970 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2971 MAC2STR(addr));
2972 }
2973 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2974 ie, ie_len);
2975 }
2976
2977 wpa_reset_ft_completed(wpa_s->wpa);
2978
2979 wpas_event_disconnect(wpa_s, addr, reason_code,
2980 locally_generated, ie, ie_len, 1);
2981}
2982
2983
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002984static const char * reg_init_str(enum reg_change_initiator init)
2985{
2986 switch (init) {
2987 case REGDOM_SET_BY_CORE:
2988 return "CORE";
2989 case REGDOM_SET_BY_USER:
2990 return "USER";
2991 case REGDOM_SET_BY_DRIVER:
2992 return "DRIVER";
2993 case REGDOM_SET_BY_COUNTRY_IE:
2994 return "COUNTRY_IE";
2995 case REGDOM_BEACON_HINT:
2996 return "BEACON_HINT";
2997 }
2998 return "?";
2999}
3000
3001
3002static const char * reg_type_str(enum reg_type type)
3003{
3004 switch (type) {
3005 case REGDOM_TYPE_UNKNOWN:
3006 return "UNKNOWN";
3007 case REGDOM_TYPE_COUNTRY:
3008 return "COUNTRY";
3009 case REGDOM_TYPE_WORLD:
3010 return "WORLD";
3011 case REGDOM_TYPE_CUSTOM_WORLD:
3012 return "CUSTOM_WORLD";
3013 case REGDOM_TYPE_INTERSECTION:
3014 return "INTERSECTION";
3015 }
3016 return "?";
3017}
3018
3019
3020static void wpa_supplicant_update_channel_list(
3021 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003022{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003023 struct wpa_supplicant *ifs;
3024
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003025 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003026 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003027 info->alpha2[0] ? " alpha2=" : "",
3028 info->alpha2[0] ? info->alpha2 : "");
3029
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003030 if (wpa_s->drv_priv == NULL)
3031 return; /* Ignore event during drv initialization */
3032
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003033 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
3034 radio_list) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003035 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
3036 ifs->ifname);
3037 free_hw_features(ifs);
3038 ifs->hw.modes = wpa_drv_get_hw_feature_data(
3039 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003040 }
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07003041
3042 /* Restart sched_scan with updated channel list */
3043 if (wpa_s->sched_scanning) {
3044 wpa_dbg(wpa_s, MSG_DEBUG,
3045 "Channel list changed restart sched scan.");
3046 wpa_supplicant_cancel_sched_scan(wpa_s);
3047 wpa_supplicant_req_scan(wpa_s, 0, 0);
3048 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003049
3050 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003051}
3052
3053
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003054static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003055 const u8 *frame, size_t len, int freq,
3056 int rssi)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003057{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003058 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003059 const u8 *payload;
3060 size_t plen;
3061 u8 category;
3062
3063 if (len < IEEE80211_HDRLEN + 2)
3064 return;
3065
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003066 mgmt = (const struct ieee80211_mgmt *) frame;
3067 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003068 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003069 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003070
3071 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
3072 " Category=%u DataLen=%d freq=%d MHz",
3073 MAC2STR(mgmt->sa), category, (int) plen, freq);
3074
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003075 if (category == WLAN_ACTION_WMM) {
3076 wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
3077 return;
3078 }
3079
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003080#ifdef CONFIG_IEEE80211R
3081 if (category == WLAN_ACTION_FT) {
3082 ft_rx_action(wpa_s, payload, plen);
3083 return;
3084 }
3085#endif /* CONFIG_IEEE80211R */
3086
3087#ifdef CONFIG_IEEE80211W
3088#ifdef CONFIG_SME
3089 if (category == WLAN_ACTION_SA_QUERY) {
3090 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
3091 return;
3092 }
3093#endif /* CONFIG_SME */
3094#endif /* CONFIG_IEEE80211W */
3095
3096#ifdef CONFIG_WNM
3097 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
3098 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
3099 return;
3100 }
3101#endif /* CONFIG_WNM */
3102
3103#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08003104 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
3105 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003106 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08003107 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003108 payload, plen, freq) == 0)
3109 return;
3110#endif /* CONFIG_GAS */
3111
3112#ifdef CONFIG_TDLS
3113 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
3114 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
3115 wpa_dbg(wpa_s, MSG_DEBUG,
3116 "TDLS: Received Discovery Response from " MACSTR,
3117 MAC2STR(mgmt->sa));
3118 return;
3119 }
3120#endif /* CONFIG_TDLS */
3121
3122#ifdef CONFIG_INTERWORKING
3123 if (category == WLAN_ACTION_QOS && plen >= 1 &&
3124 payload[0] == QOS_QOS_MAP_CONFIG) {
3125 const u8 *pos = payload + 1;
3126 size_t qlen = plen - 1;
3127 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
3128 MACSTR, MAC2STR(mgmt->sa));
3129 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
3130 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
3131 pos[1] <= qlen - 2 && pos[1] >= 16)
3132 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
3133 return;
3134 }
3135#endif /* CONFIG_INTERWORKING */
3136
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003137 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3138 payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
3139 wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
3140 return;
3141 }
3142
3143 if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
3144 payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
3145 wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
3146 payload + 1, plen - 1,
3147 rssi);
3148 return;
3149 }
3150
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003151#ifdef CONFIG_FST
3152 if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
3153 fst_rx_action(wpa_s->fst, mgmt, len);
3154 return;
3155 }
3156#endif /* CONFIG_FST */
3157
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003158 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
3159 category, payload, plen, freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003160 if (wpa_s->ifmsh)
3161 mesh_mpm_action_rx(wpa_s, mgmt, len);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003162}
3163
3164
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003165static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
3166 union wpa_event_data *event)
3167{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003168 struct wpa_freq_range_list *list;
3169 char *str = NULL;
3170
3171 list = &event->freq_range;
3172
3173 if (list->num)
3174 str = freq_range_list_str(list);
3175 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
3176 str ? str : "");
3177
3178#ifdef CONFIG_P2P
3179 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
3180 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
3181 __func__);
3182 } else {
3183 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003184
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003185 /*
3186 * The update channel flow will also take care of moving a GO
3187 * from the unsafe frequency if needed.
3188 */
3189 wpas_p2p_update_channel_list(wpa_s,
3190 WPAS_P2P_CHANNEL_UPDATE_AVOID);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003191 }
3192#endif /* CONFIG_P2P */
3193
3194 os_free(str);
3195}
3196
3197
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003198static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
3199 union wpa_event_data *data)
3200{
3201 wpa_dbg(wpa_s, MSG_DEBUG,
3202 "Connection authorized by device, previous state %d",
3203 wpa_s->wpa_state);
3204 if (wpa_s->wpa_state == WPA_ASSOCIATED) {
3205 wpa_supplicant_cancel_auth_timeout(wpa_s);
3206 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
3207 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3208 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
3209 }
3210 wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
3211 wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003212 data->assoc_info.ptk_kck_len,
3213 data->assoc_info.ptk_kek,
3214 data->assoc_info.ptk_kek_len);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003215}
3216
3217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
3219 union wpa_event_data *data)
3220{
3221 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003222 int resched;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223
3224 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
3225 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003226 event != EVENT_INTERFACE_STATUS &&
3227 event != EVENT_SCHED_SCAN_STOPPED) {
3228 wpa_dbg(wpa_s, MSG_DEBUG,
3229 "Ignore event %s (%d) while interface is disabled",
3230 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231 return;
3232 }
3233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234#ifndef CONFIG_NO_STDOUT_DEBUG
3235{
3236 int level = MSG_DEBUG;
3237
Dmitry Shmidt04949592012-07-19 12:16:46 -07003238 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003239 const struct ieee80211_hdr *hdr;
3240 u16 fc;
3241 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
3242 fc = le_to_host16(hdr->frame_control);
3243 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
3244 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
3245 level = MSG_EXCESSIVE;
3246 }
3247
3248 wpa_dbg(wpa_s, level, "Event %s (%d) received",
3249 event_to_string(event), event);
3250}
3251#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252
3253 switch (event) {
3254 case EVENT_AUTH:
3255 sme_event_auth(wpa_s, data);
3256 break;
3257 case EVENT_ASSOC:
3258 wpa_supplicant_event_assoc(wpa_s, data);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003259 if (data && data->assoc_info.authorized)
3260 wpa_supplicant_event_assoc_auth(wpa_s, data);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 break;
3262 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003263 wpas_event_disassoc(wpa_s,
3264 data ? &data->disassoc_info : NULL);
3265 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003267 wpas_event_deauth(wpa_s,
3268 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 break;
3270 case EVENT_MICHAEL_MIC_FAILURE:
3271 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
3272 break;
3273#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003274 case EVENT_SCAN_STARTED:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003275 if (wpa_s->own_scan_requested ||
3276 (data && !data->scan_info.external_scan)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003277 struct os_reltime diff;
3278
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003279 os_get_reltime(&wpa_s->scan_start_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003280 os_reltime_sub(&wpa_s->scan_start_time,
3281 &wpa_s->scan_trigger_time, &diff);
3282 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
3283 diff.sec, diff.usec);
3284 wpa_s->own_scan_requested = 0;
3285 wpa_s->own_scan_running = 1;
3286 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
3287 wpa_s->manual_scan_use_id) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003288 wpa_msg_ctrl(wpa_s, MSG_INFO,
3289 WPA_EVENT_SCAN_STARTED "id=%u",
3290 wpa_s->manual_scan_id);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003291 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003292 wpa_msg_ctrl(wpa_s, MSG_INFO,
3293 WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003294 }
3295 } else {
3296 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003297 wpa_s->radio->external_scan_running = 1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003298 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003299 }
3300 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 case EVENT_SCAN_RESULTS:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003302 if (!(data && data->scan_info.external_scan) &&
3303 os_reltime_initialized(&wpa_s->scan_start_time)) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003304 struct os_reltime now, diff;
3305 os_get_reltime(&now);
3306 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3307 wpa_s->scan_start_time.sec = 0;
3308 wpa_s->scan_start_time.usec = 0;
3309 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3310 diff.sec, diff.usec);
3311 }
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003312 if (wpa_supplicant_event_scan_results(wpa_s, data))
3313 break; /* interface may have been removed */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003314 if (!(data && data->scan_info.external_scan))
3315 wpa_s->own_scan_running = 0;
3316 if (data && data->scan_info.nl_scan_event)
3317 wpa_s->radio->external_scan_running = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003318 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 break;
3320#endif /* CONFIG_NO_SCAN_PROCESSING */
3321 case EVENT_ASSOCINFO:
3322 wpa_supplicant_event_associnfo(wpa_s, data);
3323 break;
3324 case EVENT_INTERFACE_STATUS:
3325 wpa_supplicant_event_interface_status(wpa_s, data);
3326 break;
3327 case EVENT_PMKID_CANDIDATE:
3328 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3329 break;
3330#ifdef CONFIG_PEERKEY
3331 case EVENT_STKSTART:
3332 wpa_supplicant_event_stkstart(wpa_s, data);
3333 break;
3334#endif /* CONFIG_PEERKEY */
3335#ifdef CONFIG_TDLS
3336 case EVENT_TDLS:
3337 wpa_supplicant_event_tdls(wpa_s, data);
3338 break;
3339#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003340#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003341 case EVENT_WNM:
3342 wpa_supplicant_event_wnm(wpa_s, data);
3343 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003344#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345#ifdef CONFIG_IEEE80211R
3346 case EVENT_FT_RESPONSE:
3347 wpa_supplicant_event_ft_response(wpa_s, data);
3348 break;
3349#endif /* CONFIG_IEEE80211R */
3350#ifdef CONFIG_IBSS_RSN
3351 case EVENT_IBSS_RSN_START:
3352 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3353 break;
3354#endif /* CONFIG_IBSS_RSN */
3355 case EVENT_ASSOC_REJECT:
3356 if (data->assoc_reject.bssid)
3357 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3358 "bssid=" MACSTR " status_code=%u",
3359 MAC2STR(data->assoc_reject.bssid),
3360 data->assoc_reject.status_code);
3361 else
3362 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3363 "status_code=%u",
3364 data->assoc_reject.status_code);
3365 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3366 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003367 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003368 const u8 *bssid = data->assoc_reject.bssid;
3369 if (bssid == NULL || is_zero_ether_addr(bssid))
3370 bssid = wpa_s->pending_bssid;
3371 wpas_connection_failed(wpa_s, bssid);
3372 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 break;
3375 case EVENT_AUTH_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003376 /* It is possible to get this event from earlier connection */
3377 if (wpa_s->current_ssid &&
3378 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3379 wpa_dbg(wpa_s, MSG_DEBUG,
3380 "Ignore AUTH_TIMED_OUT in mesh configuration");
3381 break;
3382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3384 sme_event_auth_timed_out(wpa_s, data);
3385 break;
3386 case EVENT_ASSOC_TIMED_OUT:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003387 /* It is possible to get this event from earlier connection */
3388 if (wpa_s->current_ssid &&
3389 wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
3390 wpa_dbg(wpa_s, MSG_DEBUG,
3391 "Ignore ASSOC_TIMED_OUT in mesh configuration");
3392 break;
3393 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3395 sme_event_assoc_timed_out(wpa_s, data);
3396 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 case EVENT_TX_STATUS:
3398 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3399 " type=%d stype=%d",
3400 MAC2STR(data->tx_status.dst),
3401 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003402#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003404#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003405 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3406 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003407 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003408 wpa_s, data->tx_status.dst,
3409 data->tx_status.data,
3410 data->tx_status.data_len,
3411 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003412 OFFCHANNEL_SEND_ACTION_SUCCESS :
3413 OFFCHANNEL_SEND_ACTION_NO_ACK);
3414#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 break;
3416 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003417#endif /* CONFIG_AP */
3418#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003419 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3420 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3421 /*
3422 * Catch TX status events for Action frames we sent via group
3423 * interface in GO mode.
3424 */
3425 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3426 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3427 os_memcmp(wpa_s->parent->pending_action_dst,
3428 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003429 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003430 wpa_s->parent, data->tx_status.dst,
3431 data->tx_status.data,
3432 data->tx_status.data_len,
3433 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003434 OFFCHANNEL_SEND_ACTION_SUCCESS :
3435 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436 break;
3437 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003438#endif /* CONFIG_OFFCHANNEL */
3439#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 switch (data->tx_status.type) {
3441 case WLAN_FC_TYPE_MGMT:
3442 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3443 data->tx_status.data_len,
3444 data->tx_status.stype,
3445 data->tx_status.ack);
3446 break;
3447 case WLAN_FC_TYPE_DATA:
3448 ap_tx_status(wpa_s, data->tx_status.dst,
3449 data->tx_status.data,
3450 data->tx_status.data_len,
3451 data->tx_status.ack);
3452 break;
3453 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003454#endif /* CONFIG_AP */
3455 break;
3456#ifdef CONFIG_AP
3457 case EVENT_EAPOL_TX_STATUS:
3458 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3459 data->eapol_tx_status.data,
3460 data->eapol_tx_status.data_len,
3461 data->eapol_tx_status.ack);
3462 break;
3463 case EVENT_DRIVER_CLIENT_POLL_OK:
3464 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 break;
3466 case EVENT_RX_FROM_UNKNOWN:
3467 if (wpa_s->ap_iface == NULL)
3468 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003469 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3470 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003472 case EVENT_CH_SWITCH:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003473 if (!data || !wpa_s->current_ssid)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003474 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003475
3476 wpa_s->assoc_freq = data->ch_switch.freq;
3477 wpa_s->current_ssid->frequency = data->ch_switch.freq;
3478
3479 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
3480 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
3481 wpa_s->current_ssid->mode ==
3482 WPAS_MODE_P2P_GROUP_FORMATION) {
3483 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3484 data->ch_switch.ht_enabled,
3485 data->ch_switch.ch_offset,
3486 data->ch_switch.ch_width,
3487 data->ch_switch.cf1,
3488 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003489 }
3490
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003491 wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003492 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003493#ifdef NEED_AP_MLME
3494 case EVENT_DFS_RADAR_DETECTED:
3495 if (data)
3496 wpas_event_dfs_radar_detected(wpa_s, &data->dfs_event);
3497 break;
3498 case EVENT_DFS_CAC_STARTED:
3499 if (data)
3500 wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
3501 break;
3502 case EVENT_DFS_CAC_FINISHED:
3503 if (data)
3504 wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
3505 break;
3506 case EVENT_DFS_CAC_ABORTED:
3507 if (data)
3508 wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
3509 break;
3510 case EVENT_DFS_NOP_FINISHED:
3511 if (data)
3512 wpas_event_dfs_cac_nop_finished(wpa_s,
3513 &data->dfs_event);
3514 break;
3515#endif /* NEED_AP_MLME */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003516#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003517 case EVENT_RX_MGMT: {
3518 u16 fc, stype;
3519 const struct ieee80211_mgmt *mgmt;
3520
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003521#ifdef CONFIG_TESTING_OPTIONS
3522 if (wpa_s->ext_mgmt_frame_handling) {
3523 struct rx_mgmt *rx = &data->rx_mgmt;
3524 size_t hex_len = 2 * rx->frame_len + 1;
3525 char *hex = os_malloc(hex_len);
3526 if (hex) {
3527 wpa_snprintf_hex(hex, hex_len,
3528 rx->frame, rx->frame_len);
3529 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3530 rx->freq, rx->datarate, rx->ssi_signal,
3531 hex);
3532 os_free(hex);
3533 }
3534 break;
3535 }
3536#endif /* CONFIG_TESTING_OPTIONS */
3537
Dmitry Shmidt04949592012-07-19 12:16:46 -07003538 mgmt = (const struct ieee80211_mgmt *)
3539 data->rx_mgmt.frame;
3540 fc = le_to_host16(mgmt->frame_control);
3541 stype = WLAN_FC_GET_STYPE(fc);
3542
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003543#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003545#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3548 data->rx_mgmt.frame_len > 24) {
3549 const u8 *src = mgmt->sa;
3550 const u8 *ie = mgmt->u.probe_req.variable;
3551 size_t ie_len = data->rx_mgmt.frame_len -
3552 (mgmt->u.probe_req.variable -
3553 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003554 wpas_p2p_probe_req_rx(
3555 wpa_s, src, mgmt->da,
3556 mgmt->bssid, ie, ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003557 data->rx_mgmt.freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003558 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 break;
3560 }
3561#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003562#ifdef CONFIG_IBSS_RSN
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003563 if (wpa_s->current_ssid &&
3564 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3565 stype == WLAN_FC_STYPE_AUTH &&
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003566 data->rx_mgmt.frame_len >= 30) {
3567 wpa_supplicant_event_ibss_auth(wpa_s, data);
3568 break;
3569 }
3570#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003571
3572 if (stype == WLAN_FC_STYPE_ACTION) {
3573 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003574 wpa_s, data->rx_mgmt.frame,
3575 data->rx_mgmt.frame_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003576 data->rx_mgmt.freq,
3577 data->rx_mgmt.ssi_signal);
3578 break;
3579 }
3580
3581 if (wpa_s->ifmsh) {
3582 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003583 break;
3584 }
3585
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3587 "management frame in non-AP mode");
3588 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003589#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003591
3592 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3593 data->rx_mgmt.frame_len > 24) {
3594 const u8 *ie = mgmt->u.probe_req.variable;
3595 size_t ie_len = data->rx_mgmt.frame_len -
3596 (mgmt->u.probe_req.variable -
3597 data->rx_mgmt.frame);
3598
3599 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3600 mgmt->bssid, ie, ie_len,
3601 data->rx_mgmt.ssi_signal);
3602 }
3603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003605#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 case EVENT_RX_PROBE_REQ:
3609 if (data->rx_probe_req.sa == NULL ||
3610 data->rx_probe_req.ie == NULL)
3611 break;
3612#ifdef CONFIG_AP
3613 if (wpa_s->ap_iface) {
3614 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3615 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003616 data->rx_probe_req.da,
3617 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003619 data->rx_probe_req.ie_len,
3620 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621 break;
3622 }
3623#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003625 data->rx_probe_req.da,
3626 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003628 data->rx_probe_req.ie_len,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003629 0,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003630 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003633#ifdef CONFIG_OFFCHANNEL
3634 offchannel_remain_on_channel_cb(
3635 wpa_s, data->remain_on_channel.freq,
3636 data->remain_on_channel.duration);
3637#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003638 wpas_p2p_remain_on_channel_cb(
3639 wpa_s, data->remain_on_channel.freq,
3640 data->remain_on_channel.duration);
3641 break;
3642 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003643#ifdef CONFIG_OFFCHANNEL
3644 offchannel_cancel_remain_on_channel_cb(
3645 wpa_s, data->remain_on_channel.freq);
3646#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 wpas_p2p_cancel_remain_on_channel_cb(
3648 wpa_s, data->remain_on_channel.freq);
3649 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 case EVENT_EAPOL_RX:
3651 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3652 data->eapol_rx.data,
3653 data->eapol_rx.data_len);
3654 break;
3655 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003656 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3657 "above=%d signal=%d noise=%d txrate=%d",
3658 data->signal_change.above_threshold,
3659 data->signal_change.current_signal,
3660 data->signal_change.current_noise,
3661 data->signal_change.current_txrate);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003662 wpa_bss_update_level(wpa_s->current_bss,
3663 data->signal_change.current_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 bgscan_notify_signal_change(
3665 wpa_s, data->signal_change.above_threshold,
3666 data->signal_change.current_signal,
3667 data->signal_change.current_noise,
3668 data->signal_change.current_txrate);
3669 break;
3670 case EVENT_INTERFACE_ENABLED:
3671 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3672 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003673 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003674 if (wpa_s->p2p_mgmt) {
3675 wpa_supplicant_set_state(wpa_s,
3676 WPA_DISCONNECTED);
3677 break;
3678 }
3679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680#ifdef CONFIG_AP
3681 if (!wpa_s->ap_iface) {
3682 wpa_supplicant_set_state(wpa_s,
3683 WPA_DISCONNECTED);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003684 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 wpa_supplicant_req_scan(wpa_s, 0, 0);
3686 } else
3687 wpa_supplicant_set_state(wpa_s,
3688 WPA_COMPLETED);
3689#else /* CONFIG_AP */
3690 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3691 wpa_supplicant_req_scan(wpa_s, 0, 0);
3692#endif /* CONFIG_AP */
3693 }
3694 break;
3695 case EVENT_INTERFACE_DISABLED:
3696 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003697#ifdef CONFIG_P2P
3698 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3699 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3700 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3701 /*
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003702 * Mark interface disabled if this happens to end up not
3703 * being removed as a separate P2P group interface.
3704 */
3705 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3706 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003707 * The interface was externally disabled. Remove
3708 * it assuming an external entity will start a
3709 * new session if needed.
3710 */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003711 if (wpa_s->current_ssid &&
3712 wpa_s->current_ssid->p2p_group)
3713 wpas_p2p_interface_unavailable(wpa_s);
3714 else
3715 wpas_p2p_disconnect(wpa_s);
3716 /*
3717 * wpa_s instance may have been freed, so must not use
3718 * it here anymore.
3719 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003720 break;
3721 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003722 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3723 p2p_in_progress(wpa_s->global->p2p) > 1) {
3724 /* This radio work will be cancelled, so clear P2P
3725 * state as well.
3726 */
3727 p2p_stop_find(wpa_s->global->p2p);
3728 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003729#endif /* CONFIG_P2P */
3730
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003731 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3732 /*
3733 * Indicate disconnection to keep ctrl_iface events
3734 * consistent.
3735 */
3736 wpa_supplicant_event_disassoc(
3737 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3738 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003740 radio_remove_works(wpa_s, NULL, 0);
3741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3743 break;
3744 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003745 wpa_supplicant_update_channel_list(
3746 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 break;
3748 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003749 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750 break;
3751 case EVENT_BEST_CHANNEL:
3752 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3753 "(%d %d %d)",
3754 data->best_chan.freq_24, data->best_chan.freq_5,
3755 data->best_chan.freq_overall);
3756 wpa_s->best_24_freq = data->best_chan.freq_24;
3757 wpa_s->best_5_freq = data->best_chan.freq_5;
3758 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3760 data->best_chan.freq_5,
3761 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 break;
3763 case EVENT_UNPROT_DEAUTH:
3764 wpa_supplicant_event_unprot_deauth(wpa_s,
3765 &data->unprot_deauth);
3766 break;
3767 case EVENT_UNPROT_DISASSOC:
3768 wpa_supplicant_event_unprot_disassoc(wpa_s,
3769 &data->unprot_disassoc);
3770 break;
3771 case EVENT_STATION_LOW_ACK:
3772#ifdef CONFIG_AP
3773 if (wpa_s->ap_iface && data)
3774 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3775 data->low_ack.addr);
3776#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003777#ifdef CONFIG_TDLS
3778 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003779 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3780 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003781#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 break;
3783 case EVENT_IBSS_PEER_LOST:
3784#ifdef CONFIG_IBSS_RSN
3785 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3786#endif /* CONFIG_IBSS_RSN */
3787 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003788 case EVENT_DRIVER_GTK_REKEY:
3789 if (os_memcmp(data->driver_gtk_rekey.bssid,
3790 wpa_s->bssid, ETH_ALEN))
3791 break;
3792 if (!wpa_s->wpa)
3793 break;
3794 wpa_sm_update_replay_ctr(wpa_s->wpa,
3795 data->driver_gtk_rekey.replay_ctr);
3796 break;
3797 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003798 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003799 wpa_s->sched_scanning = 0;
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003800 resched = wpa_s->scanning;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003801 wpa_supplicant_notify_scanning(wpa_s, 0);
3802
3803 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3804 break;
3805
3806 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003807 * Start a new sched scan to continue searching for more SSIDs
3808 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003809 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07003810 if (wpa_s->sched_scan_timed_out) {
3811 wpa_supplicant_req_sched_scan(wpa_s);
3812 } else if (wpa_s->pno_sched_pending) {
3813 wpa_s->pno_sched_pending = 0;
3814 wpas_start_pno(wpa_s);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -07003815 } else if (resched) {
3816 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003817 }
3818
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003819 break;
3820 case EVENT_WPS_BUTTON_PUSHED:
3821#ifdef CONFIG_WPS
3822 wpas_wps_start_pbc(wpa_s, NULL, 0);
3823#endif /* CONFIG_WPS */
3824 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003825 case EVENT_AVOID_FREQUENCIES:
3826 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3827 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003828 case EVENT_CONNECT_FAILED_REASON:
3829#ifdef CONFIG_AP
3830 if (!wpa_s->ap_iface || !data)
3831 break;
3832 hostapd_event_connect_failed_reason(
3833 wpa_s->ap_iface->bss[0],
3834 data->connect_failed_reason.addr,
3835 data->connect_failed_reason.code);
3836#endif /* CONFIG_AP */
3837 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003838 case EVENT_NEW_PEER_CANDIDATE:
3839#ifdef CONFIG_MESH
3840 if (!wpa_s->ifmsh || !data)
3841 break;
3842 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3843 data->mesh_peer.ies,
3844 data->mesh_peer.ie_len);
3845#endif /* CONFIG_MESH */
3846 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003847 default:
3848 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3849 break;
3850 }
3851}