blob: 2d0b8271f5ecb802e8b5348a0915b79b23a2c681 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, 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 Shmidt61d9df32012-08-29 16:22:06 -070026#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "notify.h"
28#include "common/ieee802_11_defs.h"
29#include "common/ieee802_11_common.h"
30#include "crypto/random.h"
31#include "blacklist.h"
32#include "wpas_glue.h"
33#include "wps_supplicant.h"
34#include "ibss_rsn.h"
35#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "p2p_supplicant.h"
38#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070039#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#include "ap.h"
41#include "bss.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043#include "offchannel.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070044#include "interworking.h"
45
46
Dmitry Shmidt34af3062013-07-11 10:46:32 -070047#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt8da800a2013-04-24 12:57:01 -070048static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -080049 int new_scan, int own_request);
Dmitry Shmidt34af3062013-07-11 10:46:32 -070050#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -080051
52
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070053static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
54 struct wpa_ssid *ssid)
55{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080056 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070057
58 if (ssid == NULL || ssid->disabled_until.sec == 0)
59 return 0;
60
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080061 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070062 if (ssid->disabled_until.sec > now.sec)
63 return ssid->disabled_until.sec - now.sec;
64
65 wpas_clear_temp_disabled(wpa_s, ssid, 0);
66
67 return 0;
68}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080071static struct wpa_bss * wpa_supplicant_get_new_bss(
72 struct wpa_supplicant *wpa_s, const u8 *bssid)
73{
74 struct wpa_bss *bss = NULL;
75 struct wpa_ssid *ssid = wpa_s->current_ssid;
76
77 if (ssid->ssid_len > 0)
78 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
79 if (!bss)
80 bss = wpa_bss_get_bssid(wpa_s, bssid);
81
82 return bss;
83}
84
85
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
87{
88 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080089 struct wpa_bss *bss;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070090 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091
92 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
93 return 0;
94
95 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
96 "information");
97 ssid = wpa_supplicant_get_ssid(wpa_s);
98 if (ssid == NULL) {
99 wpa_msg(wpa_s, MSG_INFO,
100 "No network configuration found for the current AP");
101 return -1;
102 }
103
Dmitry Shmidt04949592012-07-19 12:16:46 -0700104 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
106 return -1;
107 }
108
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800109 if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
110 disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
111 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
112 return -1;
113 }
114
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700115 res = wpas_temp_disabled(wpa_s, ssid);
116 if (res > 0) {
117 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
118 "disabled for %d second(s)", res);
119 return -1;
120 }
121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
123 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800124 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 u8 wpa_ie[80];
126 size_t wpa_ie_len = sizeof(wpa_ie);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800127 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
128 wpa_ie, &wpa_ie_len) < 0)
129 wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700130 } else {
131 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
132 }
133
134 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
135 eapol_sm_invalidate_cached_session(wpa_s->eapol);
136 old_ssid = wpa_s->current_ssid;
137 wpa_s->current_ssid = ssid;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800138
139 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
140 if (!bss) {
141 wpa_supplicant_update_scan_results(wpa_s);
142
143 /* Get the BSS from the new scan results */
144 bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
145 }
146
147 if (bss)
148 wpa_s->current_bss = bss;
149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
151 wpa_supplicant_initiate_eapol(wpa_s);
152 if (old_ssid != wpa_s->current_ssid)
153 wpas_notify_network_changed(wpa_s);
154
155 return 0;
156}
157
158
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800159void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160{
161 struct wpa_supplicant *wpa_s = eloop_ctx;
162
163 if (wpa_s->countermeasures) {
164 wpa_s->countermeasures = 0;
165 wpa_drv_set_countermeasures(wpa_s, 0);
166 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
167 wpa_supplicant_req_scan(wpa_s, 0, 0);
168 }
169}
170
171
172void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
173{
174 int bssid_changed;
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176 wnm_bss_keep_alive_deinit(wpa_s);
177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178#ifdef CONFIG_IBSS_RSN
179 ibss_rsn_deinit(wpa_s->ibss_rsn);
180 wpa_s->ibss_rsn = NULL;
181#endif /* CONFIG_IBSS_RSN */
182
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700183#ifdef CONFIG_AP
184 wpa_supplicant_ap_deinit(wpa_s);
185#endif /* CONFIG_AP */
186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
188 return;
189
190 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
191 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
192 os_memset(wpa_s->bssid, 0, ETH_ALEN);
193 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700194#ifdef CONFIG_SME
195 wpa_s->sme.prev_bssid_set = 0;
196#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800197#ifdef CONFIG_P2P
198 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
199#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 wpa_s->current_bss = NULL;
201 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700202#ifdef CONFIG_IEEE80211R
203#ifdef CONFIG_SME
204 if (wpa_s->sme.ft_ies)
205 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
206#endif /* CONFIG_SME */
207#endif /* CONFIG_IEEE80211R */
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 if (bssid_changed)
210 wpas_notify_bssid_changed(wpa_s);
211
212 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
213 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
214 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
215 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
216 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700217 wpa_s->current_ssid = NULL;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700218 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700219 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220}
221
222
223static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
224{
225 struct wpa_ie_data ie;
226 int pmksa_set = -1;
227 size_t i;
228
229 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
230 ie.pmkid == NULL)
231 return;
232
233 for (i = 0; i < ie.num_pmkid; i++) {
234 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
235 ie.pmkid + i * PMKID_LEN,
236 NULL, NULL, 0);
237 if (pmksa_set == 0) {
238 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
239 break;
240 }
241 }
242
243 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
244 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
245}
246
247
248static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
249 union wpa_event_data *data)
250{
251 if (data == NULL) {
252 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
253 "event");
254 return;
255 }
256 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
257 " index=%d preauth=%d",
258 MAC2STR(data->pmkid_candidate.bssid),
259 data->pmkid_candidate.index,
260 data->pmkid_candidate.preauth);
261
262 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
263 data->pmkid_candidate.index,
264 data->pmkid_candidate.preauth);
265}
266
267
268static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
269{
270 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
271 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
272 return 0;
273
274#ifdef IEEE8021X_EAPOL
275 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
276 wpa_s->current_ssid &&
277 !(wpa_s->current_ssid->eapol_flags &
278 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
279 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
280 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
281 * plaintext or static WEP keys). */
282 return 0;
283 }
284#endif /* IEEE8021X_EAPOL */
285
286 return 1;
287}
288
289
290/**
291 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
292 * @wpa_s: pointer to wpa_supplicant data
293 * @ssid: Configuration data for the network
294 * Returns: 0 on success, -1 on failure
295 *
296 * This function is called when starting authentication with a network that is
297 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
298 */
299int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
300 struct wpa_ssid *ssid)
301{
302#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303#ifdef PCSC_FUNCS
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700304 int aka = 0, sim = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700306 if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
307 wpa_s->scard != NULL || wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 return 0;
309
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700310 if (ssid == NULL || ssid->eap.eap_methods == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 sim = 1;
312 aka = 1;
313 } else {
314 struct eap_method_type *eap = ssid->eap.eap_methods;
315 while (eap->vendor != EAP_VENDOR_IETF ||
316 eap->method != EAP_TYPE_NONE) {
317 if (eap->vendor == EAP_VENDOR_IETF) {
318 if (eap->method == EAP_TYPE_SIM)
319 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700320 else if (eap->method == EAP_TYPE_AKA ||
321 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 aka = 1;
323 }
324 eap++;
325 }
326 }
327
328 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
329 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700330 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
331 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
332 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 aka = 0;
334
335 if (!sim && !aka) {
336 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
337 "use SIM, but neither EAP-SIM nor EAP-AKA are "
338 "enabled");
339 return 0;
340 }
341
342 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
343 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -0700345 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 if (wpa_s->scard == NULL) {
347 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
348 "(pcsc-lite)");
349 return -1;
350 }
351 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
352 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800353#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354#endif /* IEEE8021X_EAPOL */
355
356 return 0;
357}
358
359
360#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700361
362static int has_wep_key(struct wpa_ssid *ssid)
363{
364 int i;
365
366 for (i = 0; i < NUM_WEP_KEYS; i++) {
367 if (ssid->wep_key_len[i])
368 return 1;
369 }
370
371 return 0;
372}
373
374
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700375static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 struct wpa_ssid *ssid)
377{
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700378 int privacy = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379
380 if (ssid->mixed_cell)
381 return 1;
382
383#ifdef CONFIG_WPS
384 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
385 return 1;
386#endif /* CONFIG_WPS */
387
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700388 if (has_wep_key(ssid))
389 privacy = 1;
390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391#ifdef IEEE8021X_EAPOL
392 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
393 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
394 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
395 privacy = 1;
396#endif /* IEEE8021X_EAPOL */
397
Jouni Malinen75ecf522011-06-27 15:19:46 -0700398 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
399 privacy = 1;
400
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800401 if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
402 privacy = 1;
403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 if (bss->caps & IEEE80211_CAP_PRIVACY)
405 return privacy;
406 return !privacy;
407}
408
409
410static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
411 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700412 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413{
414 struct wpa_ie_data ie;
415 int proto_match = 0;
416 const u8 *rsn_ie, *wpa_ie;
417 int ret;
418 int wep_ok;
419
420 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
421 if (ret >= 0)
422 return ret;
423
424 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
425 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
426 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
427 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
428 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
429
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700430 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
432 proto_match++;
433
434 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
435 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
436 "failed");
437 break;
438 }
439
440 if (wep_ok &&
441 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
442 {
443 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
444 "in RSN IE");
445 return 1;
446 }
447
448 if (!(ie.proto & ssid->proto)) {
449 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
450 "mismatch");
451 break;
452 }
453
454 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
455 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
456 "cipher mismatch");
457 break;
458 }
459
460 if (!(ie.group_cipher & ssid->group_cipher)) {
461 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
462 "cipher mismatch");
463 break;
464 }
465
466 if (!(ie.key_mgmt & ssid->key_mgmt)) {
467 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
468 "mismatch");
469 break;
470 }
471
472#ifdef CONFIG_IEEE80211W
473 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800474 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
475 wpa_s->conf->pmf : ssid->ieee80211w) ==
476 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
478 "frame protection");
479 break;
480 }
481#endif /* CONFIG_IEEE80211W */
482
483 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
484 return 1;
485 }
486
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700487 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
489 proto_match++;
490
491 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
492 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
493 "failed");
494 break;
495 }
496
497 if (wep_ok &&
498 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
499 {
500 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
501 "in WPA IE");
502 return 1;
503 }
504
505 if (!(ie.proto & ssid->proto)) {
506 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
507 "mismatch");
508 break;
509 }
510
511 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
512 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
513 "cipher mismatch");
514 break;
515 }
516
517 if (!(ie.group_cipher & ssid->group_cipher)) {
518 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
519 "cipher mismatch");
520 break;
521 }
522
523 if (!(ie.key_mgmt & ssid->key_mgmt)) {
524 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
525 "mismatch");
526 break;
527 }
528
529 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
530 return 1;
531 }
532
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700533 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
534 !rsn_ie) {
535 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
536 return 1;
537 }
538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
540 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
541 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
542 return 0;
543 }
544
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800545 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
546 wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
547 wpa_dbg(wpa_s, MSG_DEBUG, " allow in OSEN");
548 return 1;
549 }
550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
552 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
553 return 1;
554 }
555
556 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
557 "WPA/WPA2");
558
559 return 0;
560}
561
562
563static int freq_allowed(int *freqs, int freq)
564{
565 int i;
566
567 if (freqs == NULL)
568 return 1;
569
570 for (i = 0; freqs[i]; i++)
571 if (freqs[i] == freq)
572 return 1;
573 return 0;
574}
575
576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800577static int ht_supported(const struct hostapd_hw_modes *mode)
578{
579 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
580 /*
581 * The driver did not indicate whether it supports HT. Assume
582 * it does to avoid connection issues.
583 */
584 return 1;
585 }
586
587 /*
588 * IEEE Std 802.11n-2009 20.1.1:
589 * An HT non-AP STA shall support all EQM rates for one spatial stream.
590 */
591 return mode->mcs_set[0] == 0xff;
592}
593
594
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700595static int vht_supported(const struct hostapd_hw_modes *mode)
596{
597 if (!(mode->flags & HOSTAPD_MODE_FLAG_VHT_INFO_KNOWN)) {
598 /*
599 * The driver did not indicate whether it supports VHT. Assume
600 * it does to avoid connection issues.
601 */
602 return 1;
603 }
604
605 /*
606 * A VHT non-AP STA shall support MCS 0-7 for one spatial stream.
607 * TODO: Verify if this complies with the standard
608 */
609 return (mode->vht_mcs_set[0] & 0x3) != 3;
610}
611
612
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700613static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614{
615 const struct hostapd_hw_modes *mode = NULL, *modes;
616 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
617 const u8 *rate_ie;
618 int i, j, k;
619
620 if (bss->freq == 0)
621 return 1; /* Cannot do matching without knowing band */
622
623 modes = wpa_s->hw.modes;
624 if (modes == NULL) {
625 /*
626 * The driver does not provide any additional information
627 * about the utilized hardware, so allow the connection attempt
628 * to continue.
629 */
630 return 1;
631 }
632
633 for (i = 0; i < wpa_s->hw.num_modes; i++) {
634 for (j = 0; j < modes[i].num_channels; j++) {
635 int freq = modes[i].channels[j].freq;
636 if (freq == bss->freq) {
637 if (mode &&
638 mode->mode == HOSTAPD_MODE_IEEE80211G)
639 break; /* do not allow 802.11b replace
640 * 802.11g */
641 mode = &modes[i];
642 break;
643 }
644 }
645 }
646
647 if (mode == NULL)
648 return 0;
649
650 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700651 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800652 if (rate_ie == NULL)
653 continue;
654
655 for (j = 2; j < rate_ie[1] + 2; j++) {
656 int flagged = !!(rate_ie[j] & 0x80);
657 int r = (rate_ie[j] & 0x7f) * 5;
658
659 /*
660 * IEEE Std 802.11n-2009 7.3.2.2:
661 * The new BSS Membership selector value is encoded
662 * like a legacy basic rate, but it is not a rate and
663 * only indicates if the BSS members are required to
664 * support the mandatory features of Clause 20 [HT PHY]
665 * in order to join the BSS.
666 */
667 if (flagged && ((rate_ie[j] & 0x7f) ==
668 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
669 if (!ht_supported(mode)) {
670 wpa_dbg(wpa_s, MSG_DEBUG,
671 " hardware does not support "
672 "HT PHY");
673 return 0;
674 }
675 continue;
676 }
677
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700678 /* There's also a VHT selector for 802.11ac */
679 if (flagged && ((rate_ie[j] & 0x7f) ==
680 BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
681 if (!vht_supported(mode)) {
682 wpa_dbg(wpa_s, MSG_DEBUG,
683 " hardware does not support "
684 "VHT PHY");
685 return 0;
686 }
687 continue;
688 }
689
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800690 if (!flagged)
691 continue;
692
693 /* check for legacy basic rates */
694 for (k = 0; k < mode->num_rates; k++) {
695 if (mode->rates[k] == r)
696 break;
697 }
698 if (k == mode->num_rates) {
699 /*
700 * IEEE Std 802.11-2007 7.3.2.2 demands that in
701 * order to join a BSS all required rates
702 * have to be supported by the hardware.
703 */
704 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
705 "not support required rate %d.%d Mbps",
706 r / 10, r % 10);
707 return 0;
708 }
709 }
710 }
711
712 return 1;
713}
714
715
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800716/*
717 * Test whether BSS is in an ESS.
718 * This is done differently in DMG (60 GHz) and non-DMG bands
719 */
720static int bss_is_ess(struct wpa_bss *bss)
721{
722 if (bss_is_dmg(bss)) {
723 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
724 IEEE80211_CAP_DMG_AP;
725 }
726
727 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
728 IEEE80211_CAP_ESS);
729}
730
731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700733 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800734 struct wpa_ssid *group,
735 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700737 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 int wpa;
739 struct wpa_blacklist *e;
740 const u8 *ie;
741 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800742 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700744 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745 wpa_ie_len = ie ? ie[1] : 0;
746
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700747 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 rsn_ie_len = ie ? ie[1] : 0;
749
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800750 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
751 osen = ie != NULL;
752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800754 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700755 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700757 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
758 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
759 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800760 " p2p" : "",
761 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762
763 e = wpa_blacklist_get(wpa_s, bss->bssid);
764 if (e) {
765 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767 /*
768 * When only a single network is enabled, we can
769 * trigger blacklisting on the first failure. This
770 * should not be done with multiple enabled networks to
771 * avoid getting forced to move into a worse ESS on
772 * single error if there are no other BSSes of the
773 * current ESS.
774 */
775 limit = 0;
776 }
777 if (e->count > limit) {
778 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
779 "(count=%d limit=%d)", e->count, limit);
780 return NULL;
781 }
782 }
783
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700784 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700785 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
786 return NULL;
787 }
788
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800789 if (disallowed_bssid(wpa_s, bss->bssid)) {
790 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
791 return NULL;
792 }
793
794 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
795 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
796 return NULL;
797 }
798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
800
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800801 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700803 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804
Dmitry Shmidt04949592012-07-19 12:16:46 -0700805 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
807 continue;
808 }
809
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700810 res = wpas_temp_disabled(wpa_s, ssid);
811 if (res > 0) {
812 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
813 "temporarily for %d second(s)", res);
814 continue;
815 }
816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817#ifdef CONFIG_WPS
818 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
819 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
820 "(WPS)");
821 continue;
822 }
823
824 if (wpa && ssid->ssid_len == 0 &&
825 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
826 check_ssid = 0;
827
828 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
829 /* Only allow wildcard SSID match if an AP
830 * advertises active WPS operation that matches
831 * with our mode. */
832 check_ssid = 1;
833 if (ssid->ssid_len == 0 &&
834 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
835 check_ssid = 0;
836 }
837#endif /* CONFIG_WPS */
838
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800839 if (ssid->bssid_set && ssid->ssid_len == 0 &&
840 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
841 check_ssid = 0;
842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700844 (bss->ssid_len != ssid->ssid_len ||
845 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
847 continue;
848 }
849
850 if (ssid->bssid_set &&
851 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
852 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
853 continue;
854 }
855
856 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
857 continue;
858
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800859 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
861 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
862 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
863 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
864 "not allowed");
865 continue;
866 }
867
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700868 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
869 has_wep_key(ssid)) {
870 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
871 continue;
872 }
873
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800874 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
875 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
876 "not allowed");
877 continue;
878 }
879
Jouni Malinen75ecf522011-06-27 15:19:46 -0700880 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
882 "mismatch");
883 continue;
884 }
885
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800886 if (!bss_is_ess(bss)) {
887 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 continue;
889 }
890
891 if (!freq_allowed(ssid->freq_list, bss->freq)) {
892 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
893 "allowed");
894 continue;
895 }
896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800897 if (!rate_match(wpa_s, bss)) {
898 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
899 "not match");
900 continue;
901 }
902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -0700904 if (ssid->p2p_group &&
905 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
906 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
907 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
908 continue;
909 }
910
Dmitry Shmidt54605472013-11-08 11:10:19 -0800911 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
912 struct wpabuf *p2p_ie;
913 u8 dev_addr[ETH_ALEN];
914
915 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
916 if (ie == NULL) {
917 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
918 continue;
919 }
920 p2p_ie = wpa_bss_get_vendor_ie_multi(
921 bss, P2P_IE_VENDOR_TYPE);
922 if (p2p_ie == NULL) {
923 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
924 continue;
925 }
926
927 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
928 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
929 ETH_ALEN) != 0) {
930 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
931 wpabuf_free(p2p_ie);
932 continue;
933 }
934 wpabuf_free(p2p_ie);
935 }
936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 /*
938 * TODO: skip the AP if its P2P IE has Group Formation
939 * bit set in the P2P Group Capability Bitmap and we
940 * are not in Group Formation with that device.
941 */
942#endif /* CONFIG_P2P */
943
944 /* Matching configuration found */
945 return ssid;
946 }
947
948 /* No matching configuration found */
949 return NULL;
950}
951
952
953static struct wpa_bss *
954wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800956 struct wpa_ssid **selected_ssid,
957 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700959 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800961 if (only_first_ssid)
962 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
963 group->id);
964 else
965 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
966 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700968 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
969 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800970 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
971 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 if (!*selected_ssid)
973 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
975 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700976 MAC2STR(bss->bssid),
977 wpa_ssid_txt(bss->ssid, bss->ssid_len));
978 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 }
980
981 return NULL;
982}
983
984
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700985struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
986 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987{
988 struct wpa_bss *selected = NULL;
989 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800990 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700992 if (wpa_s->last_scan_res == NULL ||
993 wpa_s->last_scan_res_used == 0)
994 return NULL; /* no scan results from last update */
995
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800996 if (wpa_s->next_ssid) {
997 struct wpa_ssid *ssid;
998
999 /* check that next_ssid is still valid */
1000 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1001 if (ssid == wpa_s->next_ssid)
1002 break;
1003 }
1004 next_ssid = ssid;
1005 wpa_s->next_ssid = NULL;
1006 }
1007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001009 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1010 if (next_ssid && next_ssid->priority ==
1011 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001012 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001013 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001014 if (selected)
1015 break;
1016 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001018 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001019 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 if (selected)
1021 break;
1022 }
1023
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001024 if (selected == NULL && wpa_s->blacklist &&
1025 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1027 "blacklist and try again");
1028 wpa_blacklist_clear(wpa_s);
1029 wpa_s->blacklist_cleared++;
1030 } else if (selected == NULL)
1031 break;
1032 }
1033
1034 return selected;
1035}
1036
1037
1038static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1039 int timeout_sec, int timeout_usec)
1040{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 /*
1043 * No networks are enabled; short-circuit request so
1044 * we don't wait timeout seconds before transitioning
1045 * to INACTIVE state.
1046 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001047 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1048 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1050 return;
1051 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001052
1053 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1055}
1056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001057
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001058int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001059 struct wpa_bss *selected,
1060 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061{
1062 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1063 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1064 "PBC session overlap");
1065#ifdef CONFIG_P2P
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001066 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
1067 wpa_s->p2p_in_provisioning) {
1068 eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
1069 wpa_s, NULL);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001070 return -1;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072#endif /* CONFIG_P2P */
1073
1074#ifdef CONFIG_WPS
1075 wpas_wps_cancel(wpa_s);
1076#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001077 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 }
1079
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001080 wpa_msg(wpa_s, MSG_DEBUG,
1081 "Considering connect request: reassociate: %d selected: "
1082 MACSTR " bssid: " MACSTR " pending: " MACSTR
1083 " wpa_state: %s ssid=%p current_ssid=%p",
1084 wpa_s->reassociate, MAC2STR(selected->bssid),
1085 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1086 wpa_supplicant_state_txt(wpa_s->wpa_state),
1087 ssid, wpa_s->current_ssid);
1088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 /*
1090 * Do not trigger new association unless the BSSID has changed or if
1091 * reassociation is requested. If we are in process of associating with
1092 * the selected BSSID, do not trigger new attempt.
1093 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001094 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1096 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1097 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001098 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1099 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1100 0) ||
1101 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1102 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1104 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001105 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001107 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1108 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 wpa_supplicant_associate(wpa_s, selected, ssid);
1110 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001111 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1112 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001114
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001115 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116}
1117
1118
1119static struct wpa_ssid *
1120wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1121{
1122 int prio;
1123 struct wpa_ssid *ssid;
1124
1125 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1126 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1127 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001128 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001129 continue;
1130 if (ssid->mode == IEEE80211_MODE_IBSS ||
1131 ssid->mode == IEEE80211_MODE_AP)
1132 return ssid;
1133 }
1134 }
1135 return NULL;
1136}
1137
1138
1139/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1140 * on BSS added and BSS changed events */
1141static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001142 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001144 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145
1146 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1147 return;
1148
Jouni Malinen87fd2792011-05-16 18:35:42 +03001149 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151
Jouni Malinen87fd2792011-05-16 18:35:42 +03001152 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 if (ssid == NULL)
1154 continue;
1155
Jouni Malinen87fd2792011-05-16 18:35:42 +03001156 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 if (rsn == NULL)
1158 continue;
1159
Jouni Malinen87fd2792011-05-16 18:35:42 +03001160 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161 }
1162
1163}
1164
1165
1166static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1167 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001168 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001170 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 int min_diff;
1172
1173 if (wpa_s->reassociate)
1174 return 1; /* explicit request to reassociate */
1175 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1176 return 1; /* we are not associated; continue */
1177 if (wpa_s->current_ssid == NULL)
1178 return 1; /* unknown current SSID */
1179 if (wpa_s->current_ssid != ssid)
1180 return 1; /* different network block */
1181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182 if (wpas_driver_bss_selection(wpa_s))
1183 return 0; /* Driver-based roaming */
1184
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001185 if (wpa_s->current_ssid->ssid)
1186 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1187 wpa_s->current_ssid->ssid,
1188 wpa_s->current_ssid->ssid_len);
1189 if (!current_bss)
1190 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191
1192 if (!current_bss)
1193 return 1; /* current BSS not seen in scan results */
1194
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001195 if (current_bss == selected)
1196 return 0;
1197
1198 if (selected->last_update_idx > current_bss->last_update_idx)
1199 return 1; /* current BSS not seen in the last scan */
1200
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001201#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1203 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1204 MAC2STR(current_bss->bssid), current_bss->level);
1205 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1206 MAC2STR(selected->bssid), selected->level);
1207
1208 if (wpa_s->current_ssid->bssid_set &&
1209 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1210 0) {
1211 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1212 "has preferred BSSID");
1213 return 1;
1214 }
1215
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001216 if (current_bss->level < 0 && current_bss->level > selected->level) {
1217 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1218 "signal level");
1219 return 0;
1220 }
1221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 min_diff = 2;
1223 if (current_bss->level < 0) {
1224 if (current_bss->level < -85)
1225 min_diff = 1;
1226 else if (current_bss->level < -80)
1227 min_diff = 2;
1228 else if (current_bss->level < -75)
1229 min_diff = 3;
1230 else if (current_bss->level < -70)
1231 min_diff = 4;
1232 else
1233 min_diff = 5;
1234 }
1235 if (abs(current_bss->level - selected->level) < min_diff) {
1236 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1237 "in signal level");
1238 return 0;
1239 }
1240
1241 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001242#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001243 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001244#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245}
1246
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001247
Jouni Malinen89ca7022012-09-14 13:03:12 -07001248/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001249 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001250static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001251 union wpa_event_data *data,
1252 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001254 struct wpa_scan_results *scan_res = NULL;
1255 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001257#ifndef CONFIG_NO_RANDOM_POOL
1258 size_t i, num;
1259#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260
1261#ifdef CONFIG_AP
1262 if (wpa_s->ap_iface)
1263 ap = 1;
1264#endif /* CONFIG_AP */
1265
1266 wpa_supplicant_notify_scanning(wpa_s, 0);
1267
1268 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1269 data ? &data->scan_info :
1270 NULL, 1);
1271 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001272 if (wpa_s->conf->ap_scan == 2 || ap ||
1273 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001275 if (!own_request)
1276 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1278 "scanning again");
1279 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001280 ret = -1;
1281 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 }
1283
1284#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 num = scan_res->num;
1286 if (num > 10)
1287 num = 10;
1288 for (i = 0; i < num; i++) {
1289 u8 buf[5];
1290 struct wpa_scan_res *res = scan_res->res[i];
1291 buf[0] = res->bssid[5];
1292 buf[1] = res->qual & 0xff;
1293 buf[2] = res->noise & 0xff;
1294 buf[3] = res->level & 0xff;
1295 buf[4] = res->tsf & 0xff;
1296 random_add_randomness(buf, sizeof(buf));
1297 }
1298#endif /* CONFIG_NO_RANDOM_POOL */
1299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001300 if (own_request && wpa_s->scan_res_handler &&
1301 (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1303 struct wpa_scan_results *scan_res);
1304
1305 scan_res_handler = wpa_s->scan_res_handler;
1306 wpa_s->scan_res_handler = NULL;
1307 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001308 ret = -2;
1309 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 }
1311
1312 if (ap) {
1313 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1314#ifdef CONFIG_AP
1315 if (wpa_s->ap_iface->scan_cb)
1316 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1317#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001318 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001320
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001321 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1322 wpa_s->own_scan_running, wpa_s->external_scan_running);
1323 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1324 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1325 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1326 wpa_s->manual_scan_id);
1327 wpa_s->manual_scan_use_id = 0;
1328 } else {
1329 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1330 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001331 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332
1333 wpas_notify_scan_done(wpa_s, 1);
1334
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001335 if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1336 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 -07001337 wpa_scan_results_free(scan_res);
1338 return 0;
1339 }
1340
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001341 if (sme_proc_obss_scan(wpa_s) > 0)
1342 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001344 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1345 goto scan_work_done;
1346
1347 if (autoscan_notify_scan(wpa_s, scan_res))
1348 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 if (wpa_s->disconnected) {
1351 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001352 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001355 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001356 bgscan_notify_scan(wpa_s, scan_res) == 1)
1357 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001359 wpas_wps_update_ap_info(wpa_s, scan_res);
1360
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001361 wpa_scan_results_free(scan_res);
1362
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001363 if (wpa_s->scan_work) {
1364 struct wpa_radio_work *work = wpa_s->scan_work;
1365 wpa_s->scan_work = NULL;
1366 radio_work_done(work);
1367 }
1368
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001369 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001370
1371scan_work_done:
1372 wpa_scan_results_free(scan_res);
1373 if (wpa_s->scan_work) {
1374 struct wpa_radio_work *work = wpa_s->scan_work;
1375 wpa_s->scan_work = NULL;
1376 radio_work_done(work);
1377 }
1378 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001379}
1380
1381
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001382static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001383 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001384{
1385 struct wpa_bss *selected;
1386 struct wpa_ssid *ssid = NULL;
1387
1388 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389
1390 if (selected) {
1391 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001392 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001393 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001394 if (new_scan)
1395 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001397 }
1398
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001399 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001401 return -1;
1402 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001403 if (new_scan)
1404 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001405 /*
1406 * Do not notify other virtual radios of scan results since we do not
1407 * want them to start other associations at the same time.
1408 */
1409 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1412 ssid = wpa_supplicant_pick_new_network(wpa_s);
1413 if (ssid) {
1414 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1415 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001416 if (new_scan)
1417 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001418 } else if (own_request) {
1419 /*
1420 * No SSID found. If SCAN results are as a result of
1421 * own scan request and not due to a scan request on
1422 * another shared interface, try another scan.
1423 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424 int timeout_sec = wpa_s->scan_interval;
1425 int timeout_usec = 0;
1426#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001427 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1428 return 0;
1429
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001430 if (wpa_s->p2p_in_provisioning ||
Dmitry Shmidt15907092014-03-25 10:42:57 -07001431 wpa_s->show_group_started ||
1432 wpa_s->p2p_in_invitation) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 /*
1434 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001435 * state and during P2P join-a-group operation
1436 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 */
1438 timeout_sec = 0;
1439 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001440 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1441 timeout_usec);
1442 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 }
1444#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001445#ifdef CONFIG_INTERWORKING
1446 if (wpa_s->conf->auto_interworking &&
1447 wpa_s->conf->interworking &&
1448 wpa_s->conf->cred) {
1449 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1450 "start ANQP fetch since no matching "
1451 "networks found");
1452 wpa_s->network_select = 1;
1453 wpa_s->auto_network_select = 1;
1454 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001455 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001456 }
1457#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001458#ifdef CONFIG_WPS
1459 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1460 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1461 timeout_sec = 0;
1462 timeout_usec = 500000;
1463 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1464 timeout_usec);
1465 return 0;
1466 }
1467#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001468 if (wpa_supplicant_req_sched_scan(wpa_s))
1469 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1470 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001471 }
1472 }
1473 return 0;
1474}
1475
1476
1477static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1478 union wpa_event_data *data)
1479{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001481
1482 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001483 /*
1484 * If no scan results could be fetched, then no need to
1485 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001486 * this scan. Similarly, if scan results started a new operation on this
1487 * interface, do not notify other interfaces to avoid concurrent
1488 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 */
1490 return;
1491 }
1492
1493 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001494 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 * so, they get updated with this same scan info.
1496 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001497 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1498 radio_list) {
1499 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1501 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001502 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 }
1504 }
1505}
1506
1507#endif /* CONFIG_NO_SCAN_PROCESSING */
1508
1509
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001510int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1511{
1512#ifdef CONFIG_NO_SCAN_PROCESSING
1513 return -1;
1514#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001515 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001516
1517 if (wpa_s->last_scan_res_used <= 0)
1518 return -1;
1519
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001520 os_get_reltime(&now);
1521 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001522 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1523 return -1;
1524 }
1525
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001526 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001527#endif /* CONFIG_NO_SCAN_PROCESSING */
1528}
1529
Dmitry Shmidt04949592012-07-19 12:16:46 -07001530#ifdef CONFIG_WNM
1531
1532static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1533{
1534 struct wpa_supplicant *wpa_s = eloop_ctx;
1535
1536 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1537 return;
1538
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001539 if (!wpa_s->no_keep_alive) {
1540 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1541 MAC2STR(wpa_s->bssid));
1542 /* TODO: could skip this if normal data traffic has been sent */
1543 /* TODO: Consider using some more appropriate data frame for
1544 * this */
1545 if (wpa_s->l2)
1546 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1547 (u8 *) "", 0);
1548 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001549
1550#ifdef CONFIG_SME
1551 if (wpa_s->sme.bss_max_idle_period) {
1552 unsigned int msec;
1553 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1554 if (msec > 100)
1555 msec -= 100;
1556 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1557 wnm_bss_keep_alive, wpa_s, NULL);
1558 }
1559#endif /* CONFIG_SME */
1560}
1561
1562
1563static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1564 const u8 *ies, size_t ies_len)
1565{
1566 struct ieee802_11_elems elems;
1567
1568 if (ies == NULL)
1569 return;
1570
1571 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1572 return;
1573
1574#ifdef CONFIG_SME
1575 if (elems.bss_max_idle_period) {
1576 unsigned int msec;
1577 wpa_s->sme.bss_max_idle_period =
1578 WPA_GET_LE16(elems.bss_max_idle_period);
1579 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1580 "TU)%s", wpa_s->sme.bss_max_idle_period,
1581 (elems.bss_max_idle_period[2] & 0x01) ?
1582 " (protected keep-live required)" : "");
1583 if (wpa_s->sme.bss_max_idle_period == 0)
1584 wpa_s->sme.bss_max_idle_period = 1;
1585 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1586 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1587 /* msec times 1000 */
1588 msec = wpa_s->sme.bss_max_idle_period * 1024;
1589 if (msec > 100)
1590 msec -= 100;
1591 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1592 wnm_bss_keep_alive, wpa_s,
1593 NULL);
1594 }
1595 }
1596#endif /* CONFIG_SME */
1597}
1598
1599#endif /* CONFIG_WNM */
1600
1601
1602void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1603{
1604#ifdef CONFIG_WNM
1605 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1606#endif /* CONFIG_WNM */
1607}
1608
1609
Dmitry Shmidt051af732013-10-22 13:52:46 -07001610#ifdef CONFIG_INTERWORKING
1611
1612static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1613 size_t len)
1614{
1615 int res;
1616
1617 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1618 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1619 if (res) {
1620 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1621 }
1622
1623 return res;
1624}
1625
1626
1627static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1628 const u8 *ies, size_t ies_len)
1629{
1630 struct ieee802_11_elems elems;
1631
1632 if (ies == NULL)
1633 return;
1634
1635 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1636 return;
1637
1638 if (elems.qos_map_set) {
1639 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1640 elems.qos_map_set_len);
1641 }
1642}
1643
1644#endif /* CONFIG_INTERWORKING */
1645
1646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1648 union wpa_event_data *data)
1649{
1650 int l, len, found = 0, wpa_found, rsn_found;
1651 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001652#ifdef CONFIG_IEEE80211R
1653 u8 bssid[ETH_ALEN];
1654#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655
1656 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1657 if (data->assoc_info.req_ies)
1658 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1659 data->assoc_info.req_ies_len);
1660 if (data->assoc_info.resp_ies) {
1661 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1662 data->assoc_info.resp_ies_len);
1663#ifdef CONFIG_TDLS
1664 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1665 data->assoc_info.resp_ies_len);
1666#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001667#ifdef CONFIG_WNM
1668 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1669 data->assoc_info.resp_ies_len);
1670#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001671#ifdef CONFIG_INTERWORKING
1672 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1673 data->assoc_info.resp_ies_len);
1674#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 }
1676 if (data->assoc_info.beacon_ies)
1677 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1678 data->assoc_info.beacon_ies,
1679 data->assoc_info.beacon_ies_len);
1680 if (data->assoc_info.freq)
1681 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1682 data->assoc_info.freq);
1683
1684 p = data->assoc_info.req_ies;
1685 l = data->assoc_info.req_ies_len;
1686
1687 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1688 while (p && l >= 2) {
1689 len = p[1] + 2;
1690 if (len > l) {
1691 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1692 p, l);
1693 break;
1694 }
1695 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1696 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1697 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1698 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1699 break;
1700 found = 1;
1701 wpa_find_assoc_pmkid(wpa_s);
1702 break;
1703 }
1704 l -= len;
1705 p += len;
1706 }
1707 if (!found && data->assoc_info.req_ies)
1708 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1709
1710#ifdef CONFIG_IEEE80211R
1711#ifdef CONFIG_SME
1712 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1714 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1715 data->assoc_info.resp_ies,
1716 data->assoc_info.resp_ies_len,
1717 bssid) < 0) {
1718 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1719 "Reassociation Response failed");
1720 wpa_supplicant_deauthenticate(
1721 wpa_s, WLAN_REASON_INVALID_IE);
1722 return -1;
1723 }
1724 }
1725
1726 p = data->assoc_info.resp_ies;
1727 l = data->assoc_info.resp_ies_len;
1728
1729#ifdef CONFIG_WPS_STRICT
1730 if (p && wpa_s->current_ssid &&
1731 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1732 struct wpabuf *wps;
1733 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1734 if (wps == NULL) {
1735 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1736 "include WPS IE in (Re)Association Response");
1737 return -1;
1738 }
1739
1740 if (wps_validate_assoc_resp(wps) < 0) {
1741 wpabuf_free(wps);
1742 wpa_supplicant_deauthenticate(
1743 wpa_s, WLAN_REASON_INVALID_IE);
1744 return -1;
1745 }
1746 wpabuf_free(wps);
1747 }
1748#endif /* CONFIG_WPS_STRICT */
1749
1750 /* Go through the IEs and make a copy of the MDIE, if present. */
1751 while (p && l >= 2) {
1752 len = p[1] + 2;
1753 if (len > l) {
1754 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1755 p, l);
1756 break;
1757 }
1758 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1759 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1760 wpa_s->sme.ft_used = 1;
1761 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1762 MOBILITY_DOMAIN_ID_LEN);
1763 break;
1764 }
1765 l -= len;
1766 p += len;
1767 }
1768#endif /* CONFIG_SME */
1769
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001770 /* Process FT when SME is in the driver */
1771 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1772 wpa_ft_is_completed(wpa_s->wpa)) {
1773 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1774 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1775 data->assoc_info.resp_ies,
1776 data->assoc_info.resp_ies_len,
1777 bssid) < 0) {
1778 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1779 "Reassociation Response failed");
1780 wpa_supplicant_deauthenticate(
1781 wpa_s, WLAN_REASON_INVALID_IE);
1782 return -1;
1783 }
1784 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1785 }
1786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1788 data->assoc_info.resp_ies_len);
1789#endif /* CONFIG_IEEE80211R */
1790
1791 /* WPA/RSN IE from Beacon/ProbeResp */
1792 p = data->assoc_info.beacon_ies;
1793 l = data->assoc_info.beacon_ies_len;
1794
1795 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1796 */
1797 wpa_found = rsn_found = 0;
1798 while (p && l >= 2) {
1799 len = p[1] + 2;
1800 if (len > l) {
1801 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1802 p, l);
1803 break;
1804 }
1805 if (!wpa_found &&
1806 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1807 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1808 wpa_found = 1;
1809 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1810 }
1811
1812 if (!rsn_found &&
1813 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1814 rsn_found = 1;
1815 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1816 }
1817
1818 l -= len;
1819 p += len;
1820 }
1821
1822 if (!wpa_found && data->assoc_info.beacon_ies)
1823 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1824 if (!rsn_found && data->assoc_info.beacon_ies)
1825 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1826 if (wpa_found || rsn_found)
1827 wpa_s->ap_ies_from_associnfo = 1;
1828
Jouni Malinen87fd2792011-05-16 18:35:42 +03001829 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1830 wpa_s->assoc_freq != data->assoc_info.freq) {
1831 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1832 "%u to %u MHz",
1833 wpa_s->assoc_freq, data->assoc_info.freq);
1834 wpa_supplicant_update_scan_results(wpa_s);
1835 }
1836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 wpa_s->assoc_freq = data->assoc_info.freq;
1838
1839 return 0;
1840}
1841
1842
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001843static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1844{
1845 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1846
1847 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1848 return -1;
1849
1850 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1851 return 0;
1852
1853 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1854 WPA_IE_VENDOR_TYPE);
1855 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1856
1857 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1858 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1859 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1860 bss_rsn ? 2 + bss_rsn[1] : 0))
1861 return -1;
1862
1863 return 0;
1864}
1865
1866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1868 union wpa_event_data *data)
1869{
1870 u8 bssid[ETH_ALEN];
1871 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873#ifdef CONFIG_AP
1874 if (wpa_s->ap_iface) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001875 if (!data)
1876 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1878 data->assoc_info.addr,
1879 data->assoc_info.req_ies,
1880 data->assoc_info.req_ies_len,
1881 data->assoc_info.reassoc);
1882 return;
1883 }
1884#endif /* CONFIG_AP */
1885
1886 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1887 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1888 return;
1889
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001890 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1891 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001892 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001893 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1894 return;
1895 }
1896
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001897 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001898 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1900 MACSTR, MAC2STR(bssid));
1901 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001902 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1903 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001904 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905
1906 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1907 wpa_clear_keys(wpa_s, bssid);
1908 }
1909 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001910 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1912 return;
1913 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001914
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001915#ifdef ANDROID
1916 if (wpa_s->conf->ap_scan == 1) {
1917#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001918 if (wpa_s->conf->ap_scan == 1 &&
1919 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001920#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001921 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1922 wpa_msg(wpa_s, MSG_WARNING,
1923 "WPA/RSN IEs not updated");
1924 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 }
1926
1927#ifdef CONFIG_SME
1928 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1929 wpa_s->sme.prev_bssid_set = 1;
Dmitry Shmidt0c08fdc2014-06-20 10:16:40 -07001930 wpa_s->sme.last_unprot_disconnect.sec = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931#endif /* CONFIG_SME */
1932
1933 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1934 if (wpa_s->current_ssid) {
1935 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1936 * initialized before association, but for other modes,
1937 * initialize PC/SC here, if the current configuration needs
1938 * smartcard or SIM/USIM. */
1939 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1940 }
1941 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1942 if (wpa_s->l2)
1943 l2_packet_notify_auth_start(wpa_s->l2);
1944
1945 /*
1946 * Set portEnabled first to FALSE in order to get EAP state machine out
1947 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1948 * state machine may transit to AUTHENTICATING state based on obsolete
1949 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1950 * AUTHENTICATED without ever giving chance to EAP state machine to
1951 * reset the state.
1952 */
1953 if (!ft_completed) {
1954 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1955 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1956 }
1957 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1958 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1959 /* 802.1X::portControl = Auto */
1960 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1961 wpa_s->eapol_received = 0;
1962 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1963 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1964 (wpa_s->current_ssid &&
1965 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001966 if (wpa_s->current_ssid &&
1967 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001968 (wpa_s->drv_flags &
1969 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1970 /*
1971 * Set the key after having received joined-IBSS event
1972 * from the driver.
1973 */
1974 wpa_supplicant_set_wpa_none_key(wpa_s,
1975 wpa_s->current_ssid);
1976 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001977 wpa_supplicant_cancel_auth_timeout(wpa_s);
1978 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1979 } else if (!ft_completed) {
1980 /* Timeout for receiving the first EAPOL packet */
1981 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1982 }
1983 wpa_supplicant_cancel_scan(wpa_s);
1984
1985 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1986 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1987 /*
1988 * We are done; the driver will take care of RSN 4-way
1989 * handshake.
1990 */
1991 wpa_supplicant_cancel_auth_timeout(wpa_s);
1992 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1993 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1994 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1995 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1996 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1997 /*
1998 * The driver will take care of RSN 4-way handshake, so we need
1999 * to allow EAPOL supplicant to complete its work without
2000 * waiting for WPA supplicant.
2001 */
2002 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2003 } else if (ft_completed) {
2004 /*
2005 * FT protocol completed - make sure EAPOL state machine ends
2006 * up in authenticated.
2007 */
2008 wpa_supplicant_cancel_auth_timeout(wpa_s);
2009 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2010 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2011 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2012 }
2013
Jouni Malinena05074c2012-12-21 21:35:35 +02002014 wpa_s->last_eapol_matches_bssid = 0;
2015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002017 struct os_reltime now, age;
2018 os_get_reltime(&now);
2019 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 if (age.sec == 0 && age.usec < 100000 &&
2021 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2022 0) {
2023 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2024 "frame that was received just before "
2025 "association notification");
2026 wpa_supplicant_rx_eapol(
2027 wpa_s, wpa_s->pending_eapol_rx_src,
2028 wpabuf_head(wpa_s->pending_eapol_rx),
2029 wpabuf_len(wpa_s->pending_eapol_rx));
2030 }
2031 wpabuf_free(wpa_s->pending_eapol_rx);
2032 wpa_s->pending_eapol_rx = NULL;
2033 }
2034
2035 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2036 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002037 wpa_s->current_ssid &&
2038 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 /* Set static WEP keys again */
2040 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2041 }
2042
2043#ifdef CONFIG_IBSS_RSN
2044 if (wpa_s->current_ssid &&
2045 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2046 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2047 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2048 wpa_s->ibss_rsn == NULL) {
2049 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2050 if (!wpa_s->ibss_rsn) {
2051 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2052 wpa_supplicant_deauthenticate(
2053 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2054 return;
2055 }
2056
2057 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2058 }
2059#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002060
2061 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062}
2063
2064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002065static int disconnect_reason_recoverable(u16 reason_code)
2066{
2067 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2068 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2069 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2070}
2071
2072
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002074 u16 reason_code,
2075 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076{
2077 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002078
2079 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2080 /*
2081 * At least Host AP driver and a Prism3 card seemed to be
2082 * generating streams of disconnected events when configuring
2083 * IBSS for WPA-None. Ignore them for now.
2084 */
2085 return;
2086 }
2087
2088 bssid = wpa_s->bssid;
2089 if (is_zero_ether_addr(bssid))
2090 bssid = wpa_s->pending_bssid;
2091
2092 if (!is_zero_ether_addr(bssid) ||
2093 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2094 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2095 " reason=%d%s",
2096 MAC2STR(bssid), reason_code,
2097 locally_generated ? " locally_generated=1" : "");
2098 }
2099}
2100
2101
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002102static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2103 int locally_generated)
2104{
2105 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2106 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2107 return 0; /* Not in 4-way handshake with PSK */
2108
2109 /*
2110 * It looks like connection was lost while trying to go through PSK
2111 * 4-way handshake. Filter out known disconnection cases that are caused
2112 * by something else than PSK mismatch to avoid confusing reports.
2113 */
2114
2115 if (locally_generated) {
2116 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2117 return 0;
2118 }
2119
2120 return 1;
2121}
2122
2123
Jouni Malinen2b89da82012-08-31 22:04:41 +03002124static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2125 u16 reason_code,
2126 int locally_generated)
2127{
2128 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 int authenticating;
2130 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131 struct wpa_bss *fast_reconnect = NULL;
2132 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002133 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134
2135 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2136 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2137
2138 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2139 /*
2140 * At least Host AP driver and a Prism3 card seemed to be
2141 * generating streams of disconnected events when configuring
2142 * IBSS for WPA-None. Ignore them for now.
2143 */
2144 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2145 "IBSS/WPA-None mode");
2146 return;
2147 }
2148
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002149 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2151 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002152 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2153 return; /* P2P group removed */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002154 wpas_auth_failed(wpa_s, "WRONG_KEY");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002156 if (!wpa_s->disconnected &&
2157 (!wpa_s->auto_reconnect_disabled ||
2158 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002159 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2160 "reconnect (wps=%d wpa_state=%d)",
2161 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2162 wpa_s->wpa_state);
2163 if (wpa_s->wpa_state == WPA_COMPLETED &&
2164 wpa_s->current_ssid &&
2165 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002166 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002167 disconnect_reason_recoverable(reason_code)) {
2168 /*
2169 * It looks like the AP has dropped association with
2170 * us, but could allow us to get back in. Try to
2171 * reconnect to the same BSS without full scan to save
2172 * time for some common cases.
2173 */
2174 fast_reconnect = wpa_s->current_bss;
2175 fast_reconnect_ssid = wpa_s->current_ssid;
2176 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002177 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002178 else
2179 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2180 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002182 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 "try to re-connect");
2184 wpa_s->reassociate = 0;
2185 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002186 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 }
2188 bssid = wpa_s->bssid;
2189 if (is_zero_ether_addr(bssid))
2190 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002191 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2192 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002194 if (locally_generated)
2195 wpa_s->disconnect_reason = -reason_code;
2196 else
2197 wpa_s->disconnect_reason = reason_code;
2198 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2200 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 wpa_clear_keys(wpa_s, wpa_s->bssid);
2202 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002203 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 wpa_supplicant_mark_disassoc(wpa_s);
2205
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002206 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002208 wpa_s->current_ssid = last_ssid;
2209 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002210
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002211 if (fast_reconnect &&
2212 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2213 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2214 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2215 fast_reconnect->ssid_len) &&
2216 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217#ifndef CONFIG_NO_SCAN_PROCESSING
2218 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2219 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2220 fast_reconnect_ssid) < 0) {
2221 /* Recover through full scan */
2222 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2223 }
2224#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002225 } else if (fast_reconnect) {
2226 /*
2227 * Could not reconnect to the same BSS due to network being
2228 * disabled. Use a new scan to match the alternative behavior
2229 * above, i.e., to continue automatic reconnection attempt in a
2230 * way that enforces disabled network rules.
2231 */
2232 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234}
2235
2236
2237#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002238void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239{
2240 struct wpa_supplicant *wpa_s = eloop_ctx;
2241
2242 if (!wpa_s->pending_mic_error_report)
2243 return;
2244
2245 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2246 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2247 wpa_s->pending_mic_error_report = 0;
2248}
2249#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2250
2251
2252static void
2253wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2254 union wpa_event_data *data)
2255{
2256 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002257 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258
2259 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2260 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002261 os_get_reltime(&t);
2262 if ((wpa_s->last_michael_mic_error.sec &&
2263 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 wpa_s->pending_mic_error_report) {
2265 if (wpa_s->pending_mic_error_report) {
2266 /*
2267 * Send the pending MIC error report immediately since
2268 * we are going to start countermeasures and AP better
2269 * do the same.
2270 */
2271 wpa_sm_key_request(wpa_s->wpa, 1,
2272 wpa_s->pending_mic_error_pairwise);
2273 }
2274
2275 /* Send the new MIC error report immediately since we are going
2276 * to start countermeasures and AP better do the same.
2277 */
2278 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2279
2280 /* initialize countermeasures */
2281 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002282
2283 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2286
2287 /*
2288 * Need to wait for completion of request frame. We do not get
2289 * any callback for the message completion, so just wait a
2290 * short while and hope for the best. */
2291 os_sleep(0, 10000);
2292
2293 wpa_drv_set_countermeasures(wpa_s, 1);
2294 wpa_supplicant_deauthenticate(wpa_s,
2295 WLAN_REASON_MICHAEL_MIC_FAILURE);
2296 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2297 wpa_s, NULL);
2298 eloop_register_timeout(60, 0,
2299 wpa_supplicant_stop_countermeasures,
2300 wpa_s, NULL);
2301 /* TODO: mark the AP rejected for 60 second. STA is
2302 * allowed to associate with another AP.. */
2303 } else {
2304#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2305 if (wpa_s->mic_errors_seen) {
2306 /*
2307 * Reduce the effectiveness of Michael MIC error
2308 * reports as a means for attacking against TKIP if
2309 * more than one MIC failure is noticed with the same
2310 * PTK. We delay the transmission of the reports by a
2311 * random time between 0 and 60 seconds in order to
2312 * force the attacker wait 60 seconds before getting
2313 * the information on whether a frame resulted in a MIC
2314 * failure.
2315 */
2316 u8 rval[4];
2317 int sec;
2318
2319 if (os_get_random(rval, sizeof(rval)) < 0)
2320 sec = os_random() % 60;
2321 else
2322 sec = WPA_GET_BE32(rval) % 60;
2323 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2324 "report %d seconds", sec);
2325 wpa_s->pending_mic_error_report = 1;
2326 wpa_s->pending_mic_error_pairwise = pairwise;
2327 eloop_cancel_timeout(
2328 wpa_supplicant_delayed_mic_error_report,
2329 wpa_s, NULL);
2330 eloop_register_timeout(
2331 sec, os_random() % 1000000,
2332 wpa_supplicant_delayed_mic_error_report,
2333 wpa_s, NULL);
2334 } else {
2335 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2336 }
2337#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2338 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2339#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2340 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002341 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 wpa_s->mic_errors_seen++;
2343}
2344
2345
2346#ifdef CONFIG_TERMINATE_ONLASTIF
2347static int any_interfaces(struct wpa_supplicant *head)
2348{
2349 struct wpa_supplicant *wpa_s;
2350
2351 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2352 if (!wpa_s->interface_removed)
2353 return 1;
2354 return 0;
2355}
2356#endif /* CONFIG_TERMINATE_ONLASTIF */
2357
2358
2359static void
2360wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2361 union wpa_event_data *data)
2362{
2363 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2364 return;
2365
2366 switch (data->interface_status.ievent) {
2367 case EVENT_INTERFACE_ADDED:
2368 if (!wpa_s->interface_removed)
2369 break;
2370 wpa_s->interface_removed = 0;
2371 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2372 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2373 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2374 "driver after interface was added");
2375 }
2376 break;
2377 case EVENT_INTERFACE_REMOVED:
2378 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2379 wpa_s->interface_removed = 1;
2380 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 l2_packet_deinit(wpa_s->l2);
2383 wpa_s->l2 = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384#ifdef CONFIG_TERMINATE_ONLASTIF
2385 /* check if last interface */
2386 if (!any_interfaces(wpa_s->global->ifaces))
2387 eloop_terminate();
2388#endif /* CONFIG_TERMINATE_ONLASTIF */
2389 break;
2390 }
2391}
2392
2393
2394#ifdef CONFIG_PEERKEY
2395static void
2396wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2397 union wpa_event_data *data)
2398{
2399 if (data == NULL)
2400 return;
2401 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2402}
2403#endif /* CONFIG_PEERKEY */
2404
2405
2406#ifdef CONFIG_TDLS
2407static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2408 union wpa_event_data *data)
2409{
2410 if (data == NULL)
2411 return;
2412 switch (data->tdls.oper) {
2413 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002414 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2415 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2416 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2417 else
2418 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 break;
2420 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002421 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2422 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2423 data->tdls.reason_code);
2424 else
2425 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2426 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002427 break;
2428 }
2429}
2430#endif /* CONFIG_TDLS */
2431
2432
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002433#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002434static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2435 union wpa_event_data *data)
2436{
2437 if (data == NULL)
2438 return;
2439 switch (data->wnm.oper) {
2440 case WNM_OPER_SLEEP:
2441 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2442 "(action=%d, intval=%d)",
2443 data->wnm.sleep_action, data->wnm.sleep_intval);
2444 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002445 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002446 break;
2447 }
2448}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002449#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002450
2451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452#ifdef CONFIG_IEEE80211R
2453static void
2454wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2455 union wpa_event_data *data)
2456{
2457 if (data == NULL)
2458 return;
2459
2460 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2461 data->ft_ies.ies_len,
2462 data->ft_ies.ft_action,
2463 data->ft_ies.target_ap,
2464 data->ft_ies.ric_ies,
2465 data->ft_ies.ric_ies_len) < 0) {
2466 /* TODO: prevent MLME/driver from trying to associate? */
2467 }
2468}
2469#endif /* CONFIG_IEEE80211R */
2470
2471
2472#ifdef CONFIG_IBSS_RSN
2473static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2474 union wpa_event_data *data)
2475{
2476 struct wpa_ssid *ssid;
2477 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2478 return;
2479 if (data == NULL)
2480 return;
2481 ssid = wpa_s->current_ssid;
2482 if (ssid == NULL)
2483 return;
2484 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2485 return;
2486
2487 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2488}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002489
2490
2491static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2492 union wpa_event_data *data)
2493{
2494 struct wpa_ssid *ssid = wpa_s->current_ssid;
2495
2496 if (ssid == NULL)
2497 return;
2498
2499 /* check if the ssid is correctly configured as IBSS/RSN */
2500 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2501 return;
2502
2503 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2504 data->rx_mgmt.frame_len);
2505}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506#endif /* CONFIG_IBSS_RSN */
2507
2508
2509#ifdef CONFIG_IEEE80211R
2510static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2511 size_t len)
2512{
2513 const u8 *sta_addr, *target_ap_addr;
2514 u16 status;
2515
2516 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2517 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2518 return; /* only SME case supported for now */
2519 if (len < 1 + 2 * ETH_ALEN + 2)
2520 return;
2521 if (data[0] != 2)
2522 return; /* Only FT Action Response is supported for now */
2523 sta_addr = data + 1;
2524 target_ap_addr = data + 1 + ETH_ALEN;
2525 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2526 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2527 MACSTR " TargetAP " MACSTR " status %u",
2528 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2529
2530 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2531 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2532 " in FT Action Response", MAC2STR(sta_addr));
2533 return;
2534 }
2535
2536 if (status) {
2537 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2538 "failure (status code %d)", status);
2539 /* TODO: report error to FT code(?) */
2540 return;
2541 }
2542
2543 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2544 len - (1 + 2 * ETH_ALEN + 2), 1,
2545 target_ap_addr, NULL, 0) < 0)
2546 return;
2547
2548#ifdef CONFIG_SME
2549 {
2550 struct wpa_bss *bss;
2551 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2552 if (bss)
2553 wpa_s->sme.freq = bss->freq;
2554 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2555 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2556 WLAN_AUTH_FT);
2557 }
2558#endif /* CONFIG_SME */
2559}
2560#endif /* CONFIG_IEEE80211R */
2561
2562
2563static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2564 struct unprot_deauth *e)
2565{
2566#ifdef CONFIG_IEEE80211W
2567 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2568 "dropped: " MACSTR " -> " MACSTR
2569 " (reason code %u)",
2570 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2571 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2572#endif /* CONFIG_IEEE80211W */
2573}
2574
2575
2576static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2577 struct unprot_disassoc *e)
2578{
2579#ifdef CONFIG_IEEE80211W
2580 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2581 "dropped: " MACSTR " -> " MACSTR
2582 " (reason code %u)",
2583 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2584 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2585#endif /* CONFIG_IEEE80211W */
2586}
2587
2588
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002589static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2590 u16 reason_code, int locally_generated,
2591 const u8 *ie, size_t ie_len, int deauth)
2592{
2593#ifdef CONFIG_AP
2594 if (wpa_s->ap_iface && addr) {
2595 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2596 return;
2597 }
2598
2599 if (wpa_s->ap_iface) {
2600 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2601 return;
2602 }
2603#endif /* CONFIG_AP */
2604
2605 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2606
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002607 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2608 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2609 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2610 eapol_sm_failed(wpa_s->eapol))) &&
2611 !wpa_s->eap_expected_failure))
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07002612 wpas_auth_failed(wpa_s, "AUTH_FAILED");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002613
2614#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002615 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002616 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2617 locally_generated) > 0) {
2618 /*
2619 * The interface was removed, so cannot continue
2620 * processing any additional operations after this.
2621 */
2622 return;
2623 }
2624 }
2625#endif /* CONFIG_P2P */
2626
2627 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2628 locally_generated);
2629}
2630
2631
2632static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2633 struct disassoc_info *info)
2634{
2635 u16 reason_code = 0;
2636 int locally_generated = 0;
2637 const u8 *addr = NULL;
2638 const u8 *ie = NULL;
2639 size_t ie_len = 0;
2640
2641 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2642
2643 if (info) {
2644 addr = info->addr;
2645 ie = info->ie;
2646 ie_len = info->ie_len;
2647 reason_code = info->reason_code;
2648 locally_generated = info->locally_generated;
2649 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2650 locally_generated ? " (locally generated)" : "");
2651 if (addr)
2652 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2653 MAC2STR(addr));
2654 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2655 ie, ie_len);
2656 }
2657
2658#ifdef CONFIG_AP
2659 if (wpa_s->ap_iface && info && info->addr) {
2660 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2661 return;
2662 }
2663
2664 if (wpa_s->ap_iface) {
2665 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2666 return;
2667 }
2668#endif /* CONFIG_AP */
2669
2670#ifdef CONFIG_P2P
2671 if (info) {
2672 wpas_p2p_disassoc_notif(
2673 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2674 locally_generated);
2675 }
2676#endif /* CONFIG_P2P */
2677
2678 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2679 sme_event_disassoc(wpa_s, info);
2680
2681 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2682 ie, ie_len, 0);
2683}
2684
2685
2686static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2687 struct deauth_info *info)
2688{
2689 u16 reason_code = 0;
2690 int locally_generated = 0;
2691 const u8 *addr = NULL;
2692 const u8 *ie = NULL;
2693 size_t ie_len = 0;
2694
2695 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2696
2697 if (info) {
2698 addr = info->addr;
2699 ie = info->ie;
2700 ie_len = info->ie_len;
2701 reason_code = info->reason_code;
2702 locally_generated = info->locally_generated;
2703 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2704 reason_code,
2705 locally_generated ? " (locally generated)" : "");
2706 if (addr) {
2707 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2708 MAC2STR(addr));
2709 }
2710 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2711 ie, ie_len);
2712 }
2713
2714 wpa_reset_ft_completed(wpa_s->wpa);
2715
2716 wpas_event_disconnect(wpa_s, addr, reason_code,
2717 locally_generated, ie, ie_len, 1);
2718}
2719
2720
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002721static const char * reg_init_str(enum reg_change_initiator init)
2722{
2723 switch (init) {
2724 case REGDOM_SET_BY_CORE:
2725 return "CORE";
2726 case REGDOM_SET_BY_USER:
2727 return "USER";
2728 case REGDOM_SET_BY_DRIVER:
2729 return "DRIVER";
2730 case REGDOM_SET_BY_COUNTRY_IE:
2731 return "COUNTRY_IE";
2732 case REGDOM_BEACON_HINT:
2733 return "BEACON_HINT";
2734 }
2735 return "?";
2736}
2737
2738
2739static const char * reg_type_str(enum reg_type type)
2740{
2741 switch (type) {
2742 case REGDOM_TYPE_UNKNOWN:
2743 return "UNKNOWN";
2744 case REGDOM_TYPE_COUNTRY:
2745 return "COUNTRY";
2746 case REGDOM_TYPE_WORLD:
2747 return "WORLD";
2748 case REGDOM_TYPE_CUSTOM_WORLD:
2749 return "CUSTOM_WORLD";
2750 case REGDOM_TYPE_INTERSECTION:
2751 return "INTERSECTION";
2752 }
2753 return "?";
2754}
2755
2756
2757static void wpa_supplicant_update_channel_list(
2758 struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002759{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002760 struct wpa_supplicant *ifs;
2761
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002762 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002763 reg_init_str(info->initiator), reg_type_str(info->type),
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07002764 info->alpha2[0] ? " alpha2=" : "",
2765 info->alpha2[0] ? info->alpha2 : "");
2766
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002767 if (wpa_s->drv_priv == NULL)
2768 return; /* Ignore event during drv initialization */
2769
2770 free_hw_features(wpa_s);
2771 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2772 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2773
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002774 wpas_p2p_update_channel_list(wpa_s);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002775
2776 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002777 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002778 * so, they get updated with this same hw mode info.
2779 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002780 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2781 radio_list) {
2782 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002783 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2784 ifs->ifname);
2785 free_hw_features(ifs);
2786 ifs->hw.modes = wpa_drv_get_hw_feature_data(
2787 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2788 }
2789 }
2790}
2791
2792
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002793static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002794 const u8 *frame, size_t len, int freq)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002795{
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002796 const struct ieee80211_mgmt *mgmt;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002797 const u8 *payload;
2798 size_t plen;
2799 u8 category;
2800
2801 if (len < IEEE80211_HDRLEN + 2)
2802 return;
2803
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002804 mgmt = (const struct ieee80211_mgmt *) frame;
2805 payload = frame + IEEE80211_HDRLEN;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002806 category = *payload++;
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07002807 plen = len - IEEE80211_HDRLEN - 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002808
2809 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2810 " Category=%u DataLen=%d freq=%d MHz",
2811 MAC2STR(mgmt->sa), category, (int) plen, freq);
2812
2813#ifdef CONFIG_IEEE80211R
2814 if (category == WLAN_ACTION_FT) {
2815 ft_rx_action(wpa_s, payload, plen);
2816 return;
2817 }
2818#endif /* CONFIG_IEEE80211R */
2819
2820#ifdef CONFIG_IEEE80211W
2821#ifdef CONFIG_SME
2822 if (category == WLAN_ACTION_SA_QUERY) {
2823 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2824 return;
2825 }
2826#endif /* CONFIG_SME */
2827#endif /* CONFIG_IEEE80211W */
2828
2829#ifdef CONFIG_WNM
2830 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2831 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2832 return;
2833 }
2834#endif /* CONFIG_WNM */
2835
2836#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08002837 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2838 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002839 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08002840 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002841 payload, plen, freq) == 0)
2842 return;
2843#endif /* CONFIG_GAS */
2844
2845#ifdef CONFIG_TDLS
2846 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2847 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2848 wpa_dbg(wpa_s, MSG_DEBUG,
2849 "TDLS: Received Discovery Response from " MACSTR,
2850 MAC2STR(mgmt->sa));
2851 return;
2852 }
2853#endif /* CONFIG_TDLS */
2854
2855#ifdef CONFIG_INTERWORKING
2856 if (category == WLAN_ACTION_QOS && plen >= 1 &&
2857 payload[0] == QOS_QOS_MAP_CONFIG) {
2858 const u8 *pos = payload + 1;
2859 size_t qlen = plen - 1;
2860 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2861 MACSTR, MAC2STR(mgmt->sa));
2862 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2863 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2864 pos[1] <= qlen - 2 && pos[1] >= 16)
2865 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2866 return;
2867 }
2868#endif /* CONFIG_INTERWORKING */
2869
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002870 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2871 category, payload, plen, freq);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002872}
2873
2874
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002875static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2876 union wpa_event_data *event)
2877{
2878#ifdef CONFIG_P2P
2879 struct wpa_supplicant *ifs;
2880#endif /* CONFIG_P2P */
2881 struct wpa_freq_range_list *list;
2882 char *str = NULL;
2883
2884 list = &event->freq_range;
2885
2886 if (list->num)
2887 str = freq_range_list_str(list);
2888 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2889 str ? str : "");
2890
2891#ifdef CONFIG_P2P
2892 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2893 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2894 __func__);
2895 } else {
2896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2897 wpas_p2p_update_channel_list(wpa_s);
2898 }
2899
2900 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2901 int freq;
2902 if (!ifs->current_ssid ||
2903 !ifs->current_ssid->p2p_group ||
2904 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2905 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2906 continue;
2907
2908 freq = ifs->current_ssid->frequency;
2909 if (!freq_range_list_includes(list, freq)) {
2910 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2911 freq);
2912 continue;
2913 }
2914
2915 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2916 freq);
2917 /* TODO: Consider using CSA or removing the group within
2918 * wpa_supplicant */
2919 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2920 }
2921#endif /* CONFIG_P2P */
2922
2923 os_free(str);
2924}
2925
2926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2928 union wpa_event_data *data)
2929{
2930 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
2932 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2933 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002934 event != EVENT_INTERFACE_STATUS &&
2935 event != EVENT_SCHED_SCAN_STOPPED) {
2936 wpa_dbg(wpa_s, MSG_DEBUG,
2937 "Ignore event %s (%d) while interface is disabled",
2938 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939 return;
2940 }
2941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002942#ifndef CONFIG_NO_STDOUT_DEBUG
2943{
2944 int level = MSG_DEBUG;
2945
Dmitry Shmidt04949592012-07-19 12:16:46 -07002946 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002947 const struct ieee80211_hdr *hdr;
2948 u16 fc;
2949 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2950 fc = le_to_host16(hdr->frame_control);
2951 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2952 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2953 level = MSG_EXCESSIVE;
2954 }
2955
2956 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2957 event_to_string(event), event);
2958}
2959#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960
2961 switch (event) {
2962 case EVENT_AUTH:
2963 sme_event_auth(wpa_s, data);
2964 break;
2965 case EVENT_ASSOC:
2966 wpa_supplicant_event_assoc(wpa_s, data);
2967 break;
2968 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002969 wpas_event_disassoc(wpa_s,
2970 data ? &data->disassoc_info : NULL);
2971 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002973 wpas_event_deauth(wpa_s,
2974 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 break;
2976 case EVENT_MICHAEL_MIC_FAILURE:
2977 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2978 break;
2979#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002980 case EVENT_SCAN_STARTED:
2981 os_get_reltime(&wpa_s->scan_start_time);
2982 if (wpa_s->own_scan_requested) {
2983 struct os_reltime diff;
2984
2985 os_reltime_sub(&wpa_s->scan_start_time,
2986 &wpa_s->scan_trigger_time, &diff);
2987 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2988 diff.sec, diff.usec);
2989 wpa_s->own_scan_requested = 0;
2990 wpa_s->own_scan_running = 1;
2991 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2992 wpa_s->manual_scan_use_id) {
2993 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
2994 wpa_s->manual_scan_id);
2995 } else {
2996 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2997 }
2998 } else {
2999 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
3000 wpa_s->external_scan_running = 1;
3001 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
3002 }
3003 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003005 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3006 struct os_reltime now, diff;
3007 os_get_reltime(&now);
3008 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3009 wpa_s->scan_start_time.sec = 0;
3010 wpa_s->scan_start_time.usec = 0;
3011 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3012 diff.sec, diff.usec);
3013 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003015 wpa_s->own_scan_running = 0;
3016 wpa_s->external_scan_running = 0;
3017 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 break;
3019#endif /* CONFIG_NO_SCAN_PROCESSING */
3020 case EVENT_ASSOCINFO:
3021 wpa_supplicant_event_associnfo(wpa_s, data);
3022 break;
3023 case EVENT_INTERFACE_STATUS:
3024 wpa_supplicant_event_interface_status(wpa_s, data);
3025 break;
3026 case EVENT_PMKID_CANDIDATE:
3027 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3028 break;
3029#ifdef CONFIG_PEERKEY
3030 case EVENT_STKSTART:
3031 wpa_supplicant_event_stkstart(wpa_s, data);
3032 break;
3033#endif /* CONFIG_PEERKEY */
3034#ifdef CONFIG_TDLS
3035 case EVENT_TDLS:
3036 wpa_supplicant_event_tdls(wpa_s, data);
3037 break;
3038#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003039#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003040 case EVENT_WNM:
3041 wpa_supplicant_event_wnm(wpa_s, data);
3042 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003043#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044#ifdef CONFIG_IEEE80211R
3045 case EVENT_FT_RESPONSE:
3046 wpa_supplicant_event_ft_response(wpa_s, data);
3047 break;
3048#endif /* CONFIG_IEEE80211R */
3049#ifdef CONFIG_IBSS_RSN
3050 case EVENT_IBSS_RSN_START:
3051 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3052 break;
3053#endif /* CONFIG_IBSS_RSN */
3054 case EVENT_ASSOC_REJECT:
3055 if (data->assoc_reject.bssid)
3056 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3057 "bssid=" MACSTR " status_code=%u",
3058 MAC2STR(data->assoc_reject.bssid),
3059 data->assoc_reject.status_code);
3060 else
3061 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3062 "status_code=%u",
3063 data->assoc_reject.status_code);
3064 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3065 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003066 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003067 const u8 *bssid = data->assoc_reject.bssid;
3068 if (bssid == NULL || is_zero_ether_addr(bssid))
3069 bssid = wpa_s->pending_bssid;
3070 wpas_connection_failed(wpa_s, bssid);
3071 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 break;
3074 case EVENT_AUTH_TIMED_OUT:
3075 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3076 sme_event_auth_timed_out(wpa_s, data);
3077 break;
3078 case EVENT_ASSOC_TIMED_OUT:
3079 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3080 sme_event_assoc_timed_out(wpa_s, data);
3081 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082 case EVENT_TX_STATUS:
3083 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3084 " type=%d stype=%d",
3085 MAC2STR(data->tx_status.dst),
3086 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003087#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003088 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003089#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3091 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003092 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 wpa_s, data->tx_status.dst,
3094 data->tx_status.data,
3095 data->tx_status.data_len,
3096 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003097 OFFCHANNEL_SEND_ACTION_SUCCESS :
3098 OFFCHANNEL_SEND_ACTION_NO_ACK);
3099#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 break;
3101 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003102#endif /* CONFIG_AP */
3103#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3105 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3106 /*
3107 * Catch TX status events for Action frames we sent via group
3108 * interface in GO mode.
3109 */
3110 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3111 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3112 os_memcmp(wpa_s->parent->pending_action_dst,
3113 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003114 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 wpa_s->parent, data->tx_status.dst,
3116 data->tx_status.data,
3117 data->tx_status.data_len,
3118 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003119 OFFCHANNEL_SEND_ACTION_SUCCESS :
3120 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 break;
3122 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003123#endif /* CONFIG_OFFCHANNEL */
3124#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 switch (data->tx_status.type) {
3126 case WLAN_FC_TYPE_MGMT:
3127 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3128 data->tx_status.data_len,
3129 data->tx_status.stype,
3130 data->tx_status.ack);
3131 break;
3132 case WLAN_FC_TYPE_DATA:
3133 ap_tx_status(wpa_s, data->tx_status.dst,
3134 data->tx_status.data,
3135 data->tx_status.data_len,
3136 data->tx_status.ack);
3137 break;
3138 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003139#endif /* CONFIG_AP */
3140 break;
3141#ifdef CONFIG_AP
3142 case EVENT_EAPOL_TX_STATUS:
3143 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3144 data->eapol_tx_status.data,
3145 data->eapol_tx_status.data_len,
3146 data->eapol_tx_status.ack);
3147 break;
3148 case EVENT_DRIVER_CLIENT_POLL_OK:
3149 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 break;
3151 case EVENT_RX_FROM_UNKNOWN:
3152 if (wpa_s->ap_iface == NULL)
3153 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003154 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3155 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003157 case EVENT_CH_SWITCH:
3158 if (!data)
3159 break;
3160 if (!wpa_s->ap_iface) {
3161 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3162 "event in non-AP mode");
3163 break;
3164 }
3165
Dmitry Shmidt04949592012-07-19 12:16:46 -07003166 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3167 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003168 data->ch_switch.ch_offset,
3169 data->ch_switch.ch_width,
3170 data->ch_switch.cf1,
3171 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003172 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003173#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003174 case EVENT_RX_MGMT: {
3175 u16 fc, stype;
3176 const struct ieee80211_mgmt *mgmt;
3177
Dmitry Shmidt818ea482014-03-10 13:15:21 -07003178#ifdef CONFIG_TESTING_OPTIONS
3179 if (wpa_s->ext_mgmt_frame_handling) {
3180 struct rx_mgmt *rx = &data->rx_mgmt;
3181 size_t hex_len = 2 * rx->frame_len + 1;
3182 char *hex = os_malloc(hex_len);
3183 if (hex) {
3184 wpa_snprintf_hex(hex, hex_len,
3185 rx->frame, rx->frame_len);
3186 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3187 rx->freq, rx->datarate, rx->ssi_signal,
3188 hex);
3189 os_free(hex);
3190 }
3191 break;
3192 }
3193#endif /* CONFIG_TESTING_OPTIONS */
3194
Dmitry Shmidt04949592012-07-19 12:16:46 -07003195 mgmt = (const struct ieee80211_mgmt *)
3196 data->rx_mgmt.frame;
3197 fc = le_to_host16(mgmt->frame_control);
3198 stype = WLAN_FC_GET_STYPE(fc);
3199
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003200#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003201 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003202#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3205 data->rx_mgmt.frame_len > 24) {
3206 const u8 *src = mgmt->sa;
3207 const u8 *ie = mgmt->u.probe_req.variable;
3208 size_t ie_len = data->rx_mgmt.frame_len -
3209 (mgmt->u.probe_req.variable -
3210 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003211 wpas_p2p_probe_req_rx(
3212 wpa_s, src, mgmt->da,
3213 mgmt->bssid, ie, ie_len,
3214 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215 break;
3216 }
3217#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003218#ifdef CONFIG_IBSS_RSN
3219 if (stype == WLAN_FC_STYPE_AUTH &&
3220 data->rx_mgmt.frame_len >= 30) {
3221 wpa_supplicant_event_ibss_auth(wpa_s, data);
3222 break;
3223 }
3224#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003225
3226 if (stype == WLAN_FC_STYPE_ACTION) {
3227 wpas_event_rx_mgmt_action(
Dmitry Shmidt623d63a2014-06-13 11:05:14 -07003228 wpa_s, data->rx_mgmt.frame,
3229 data->rx_mgmt.frame_len,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003230 data->rx_mgmt.freq);
3231 break;
3232 }
3233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3235 "management frame in non-AP mode");
3236 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003237#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003239
3240 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3241 data->rx_mgmt.frame_len > 24) {
3242 const u8 *ie = mgmt->u.probe_req.variable;
3243 size_t ie_len = data->rx_mgmt.frame_len -
3244 (mgmt->u.probe_req.variable -
3245 data->rx_mgmt.frame);
3246
3247 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3248 mgmt->bssid, ie, ie_len,
3249 data->rx_mgmt.ssi_signal);
3250 }
3251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003253#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 case EVENT_RX_PROBE_REQ:
3257 if (data->rx_probe_req.sa == NULL ||
3258 data->rx_probe_req.ie == NULL)
3259 break;
3260#ifdef CONFIG_AP
3261 if (wpa_s->ap_iface) {
3262 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3263 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003264 data->rx_probe_req.da,
3265 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003267 data->rx_probe_req.ie_len,
3268 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 break;
3270 }
3271#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003273 data->rx_probe_req.da,
3274 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003276 data->rx_probe_req.ie_len,
3277 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003280#ifdef CONFIG_OFFCHANNEL
3281 offchannel_remain_on_channel_cb(
3282 wpa_s, data->remain_on_channel.freq,
3283 data->remain_on_channel.duration);
3284#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 wpas_p2p_remain_on_channel_cb(
3286 wpa_s, data->remain_on_channel.freq,
3287 data->remain_on_channel.duration);
3288 break;
3289 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003290#ifdef CONFIG_OFFCHANNEL
3291 offchannel_cancel_remain_on_channel_cb(
3292 wpa_s, data->remain_on_channel.freq);
3293#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 wpas_p2p_cancel_remain_on_channel_cb(
3295 wpa_s, data->remain_on_channel.freq);
3296 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 case EVENT_EAPOL_RX:
3298 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3299 data->eapol_rx.data,
3300 data->eapol_rx.data_len);
3301 break;
3302 case EVENT_SIGNAL_CHANGE:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003303 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3304 "above=%d signal=%d noise=%d txrate=%d",
3305 data->signal_change.above_threshold,
3306 data->signal_change.current_signal,
3307 data->signal_change.current_noise,
3308 data->signal_change.current_txrate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 bgscan_notify_signal_change(
3310 wpa_s, data->signal_change.above_threshold,
3311 data->signal_change.current_signal,
3312 data->signal_change.current_noise,
3313 data->signal_change.current_txrate);
3314 break;
3315 case EVENT_INTERFACE_ENABLED:
3316 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3317 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003318 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003319 if (wpa_s->p2p_mgmt) {
3320 wpa_supplicant_set_state(wpa_s,
3321 WPA_DISCONNECTED);
3322 break;
3323 }
3324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325#ifdef CONFIG_AP
3326 if (!wpa_s->ap_iface) {
3327 wpa_supplicant_set_state(wpa_s,
3328 WPA_DISCONNECTED);
3329 wpa_supplicant_req_scan(wpa_s, 0, 0);
3330 } else
3331 wpa_supplicant_set_state(wpa_s,
3332 WPA_COMPLETED);
3333#else /* CONFIG_AP */
3334 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3335 wpa_supplicant_req_scan(wpa_s, 0, 0);
3336#endif /* CONFIG_AP */
3337 }
3338 break;
3339 case EVENT_INTERFACE_DISABLED:
3340 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003341#ifdef CONFIG_P2P
3342 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3343 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3344 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3345 /*
3346 * The interface was externally disabled. Remove
3347 * it assuming an external entity will start a
3348 * new session if needed.
3349 */
3350 wpas_p2p_disconnect(wpa_s);
3351 break;
3352 }
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07003353 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3354 p2p_in_progress(wpa_s->global->p2p) > 1) {
3355 /* This radio work will be cancelled, so clear P2P
3356 * state as well.
3357 */
3358 p2p_stop_find(wpa_s->global->p2p);
3359 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003360#endif /* CONFIG_P2P */
3361
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07003362 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3363 /*
3364 * Indicate disconnection to keep ctrl_iface events
3365 * consistent.
3366 */
3367 wpa_supplicant_event_disassoc(
3368 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003370 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003371 radio_remove_works(wpa_s, NULL, 0);
3372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3374 break;
3375 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07003376 wpa_supplicant_update_channel_list(
3377 wpa_s, &data->channel_list_changed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 break;
3379 case EVENT_INTERFACE_UNAVAILABLE:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003380 wpas_p2p_interface_unavailable(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003381 break;
3382 case EVENT_BEST_CHANNEL:
3383 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3384 "(%d %d %d)",
3385 data->best_chan.freq_24, data->best_chan.freq_5,
3386 data->best_chan.freq_overall);
3387 wpa_s->best_24_freq = data->best_chan.freq_24;
3388 wpa_s->best_5_freq = data->best_chan.freq_5;
3389 wpa_s->best_overall_freq = data->best_chan.freq_overall;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3391 data->best_chan.freq_5,
3392 data->best_chan.freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003393 break;
3394 case EVENT_UNPROT_DEAUTH:
3395 wpa_supplicant_event_unprot_deauth(wpa_s,
3396 &data->unprot_deauth);
3397 break;
3398 case EVENT_UNPROT_DISASSOC:
3399 wpa_supplicant_event_unprot_disassoc(wpa_s,
3400 &data->unprot_disassoc);
3401 break;
3402 case EVENT_STATION_LOW_ACK:
3403#ifdef CONFIG_AP
3404 if (wpa_s->ap_iface && data)
3405 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3406 data->low_ack.addr);
3407#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003408#ifdef CONFIG_TDLS
3409 if (data)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003410 wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3411 data->low_ack.addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003412#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 break;
3414 case EVENT_IBSS_PEER_LOST:
3415#ifdef CONFIG_IBSS_RSN
3416 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3417#endif /* CONFIG_IBSS_RSN */
3418 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003419 case EVENT_DRIVER_GTK_REKEY:
3420 if (os_memcmp(data->driver_gtk_rekey.bssid,
3421 wpa_s->bssid, ETH_ALEN))
3422 break;
3423 if (!wpa_s->wpa)
3424 break;
3425 wpa_sm_update_replay_ctr(wpa_s->wpa,
3426 data->driver_gtk_rekey.replay_ctr);
3427 break;
3428 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003429 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003430 wpa_s->sched_scanning = 0;
3431 wpa_supplicant_notify_scanning(wpa_s, 0);
3432
3433 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3434 break;
3435
3436 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003437 * Start a new sched scan to continue searching for more SSIDs
3438 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003439 */
Dmitry Shmidt98660862014-03-11 17:26:21 -07003440 if (wpa_s->sched_scan_timed_out) {
3441 wpa_supplicant_req_sched_scan(wpa_s);
3442 } else if (wpa_s->pno_sched_pending) {
3443 wpa_s->pno_sched_pending = 0;
3444 wpas_start_pno(wpa_s);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003445 }
3446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003447 break;
3448 case EVENT_WPS_BUTTON_PUSHED:
3449#ifdef CONFIG_WPS
3450 wpas_wps_start_pbc(wpa_s, NULL, 0);
3451#endif /* CONFIG_WPS */
3452 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003453 case EVENT_AVOID_FREQUENCIES:
3454 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3455 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003456 case EVENT_CONNECT_FAILED_REASON:
3457#ifdef CONFIG_AP
3458 if (!wpa_s->ap_iface || !data)
3459 break;
3460 hostapd_event_connect_failed_reason(
3461 wpa_s->ap_iface->bss[0],
3462 data->connect_failed_reason.addr,
3463 data->connect_failed_reason.code);
3464#endif /* CONFIG_AP */
3465 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003466 default:
3467 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3468 break;
3469 }
3470}