blob: bea7f8ad9da546ef809849240bf24d3514bd2355 [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 Shmidt051af732013-10-22 13:52:46 -0700306 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL ||
307 wpa_s->conf->external_sim)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 return 0;
309
310 if (ssid->eap.eap_methods == NULL) {
311 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 Shmidte0e48dc2013-11-18 12:00:06 -0800345 wpa_s->scard = scard_init(NULL);
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 -0800716static int bss_is_dmg(struct wpa_bss *bss)
717{
718 return bss->freq > 45000;
719}
720
721
722/*
723 * Test whether BSS is in an ESS.
724 * This is done differently in DMG (60 GHz) and non-DMG bands
725 */
726static int bss_is_ess(struct wpa_bss *bss)
727{
728 if (bss_is_dmg(bss)) {
729 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
730 IEEE80211_CAP_DMG_AP;
731 }
732
733 return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
734 IEEE80211_CAP_ESS);
735}
736
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700739 int i, struct wpa_bss *bss,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800740 struct wpa_ssid *group,
741 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700743 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 int wpa;
745 struct wpa_blacklist *e;
746 const u8 *ie;
747 struct wpa_ssid *ssid;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800748 int osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700750 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 wpa_ie_len = ie ? ie[1] : 0;
752
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700753 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 rsn_ie_len = ie ? ie[1] : 0;
755
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800756 ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
757 osen = ie != NULL;
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800760 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s%s%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700761 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700763 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "",
764 (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
765 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) ?
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800766 " p2p" : "",
767 osen ? " osen=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768
769 e = wpa_blacklist_get(wpa_s, bss->bssid);
770 if (e) {
771 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700772 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 /*
774 * When only a single network is enabled, we can
775 * trigger blacklisting on the first failure. This
776 * should not be done with multiple enabled networks to
777 * avoid getting forced to move into a worse ESS on
778 * single error if there are no other BSSes of the
779 * current ESS.
780 */
781 limit = 0;
782 }
783 if (e->count > limit) {
784 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
785 "(count=%d limit=%d)", e->count, limit);
786 return NULL;
787 }
788 }
789
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700790 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
792 return NULL;
793 }
794
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800795 if (disallowed_bssid(wpa_s, bss->bssid)) {
796 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID disallowed");
797 return NULL;
798 }
799
800 if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
801 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID disallowed");
802 return NULL;
803 }
804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
806
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800807 for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700809 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810
Dmitry Shmidt04949592012-07-19 12:16:46 -0700811 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
813 continue;
814 }
815
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700816 res = wpas_temp_disabled(wpa_s, ssid);
817 if (res > 0) {
818 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
819 "temporarily for %d second(s)", res);
820 continue;
821 }
822
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823#ifdef CONFIG_WPS
824 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
825 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
826 "(WPS)");
827 continue;
828 }
829
830 if (wpa && ssid->ssid_len == 0 &&
831 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
832 check_ssid = 0;
833
834 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
835 /* Only allow wildcard SSID match if an AP
836 * advertises active WPS operation that matches
837 * with our mode. */
838 check_ssid = 1;
839 if (ssid->ssid_len == 0 &&
840 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
841 check_ssid = 0;
842 }
843#endif /* CONFIG_WPS */
844
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800845 if (ssid->bssid_set && ssid->ssid_len == 0 &&
846 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
847 check_ssid = 0;
848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700850 (bss->ssid_len != ssid->ssid_len ||
851 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
853 continue;
854 }
855
856 if (ssid->bssid_set &&
857 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
858 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
859 continue;
860 }
861
862 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
863 continue;
864
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800865 if (!osen && !wpa &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
867 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
868 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
869 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
870 "not allowed");
871 continue;
872 }
873
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700874 if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
875 has_wep_key(ssid)) {
876 wpa_dbg(wpa_s, MSG_DEBUG, " skip - ignore WPA/WPA2 AP for WEP network block");
877 continue;
878 }
879
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800880 if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen) {
881 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-OSEN network "
882 "not allowed");
883 continue;
884 }
885
Jouni Malinen75ecf522011-06-27 15:19:46 -0700886 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
888 "mismatch");
889 continue;
890 }
891
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800892 if (!bss_is_ess(bss)) {
893 wpa_dbg(wpa_s, MSG_DEBUG, " skip - not ESS network");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 continue;
895 }
896
897 if (!freq_allowed(ssid->freq_list, bss->freq)) {
898 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
899 "allowed");
900 continue;
901 }
902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 if (!rate_match(wpa_s, bss)) {
904 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
905 "not match");
906 continue;
907 }
908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909#ifdef CONFIG_P2P
Dmitry Shmidt96571392013-10-14 12:54:46 -0700910 if (ssid->p2p_group &&
911 !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
912 !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
913 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P IE seen");
914 continue;
915 }
916
Dmitry Shmidt54605472013-11-08 11:10:19 -0800917 if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
918 struct wpabuf *p2p_ie;
919 u8 dev_addr[ETH_ALEN];
920
921 ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
922 if (ie == NULL) {
923 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no P2P element");
924 continue;
925 }
926 p2p_ie = wpa_bss_get_vendor_ie_multi(
927 bss, P2P_IE_VENDOR_TYPE);
928 if (p2p_ie == NULL) {
929 wpa_dbg(wpa_s, MSG_DEBUG, " skip - could not fetch P2P element");
930 continue;
931 }
932
933 if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
934 || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
935 ETH_ALEN) != 0) {
936 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no matching GO P2P Device Address in P2P element");
937 wpabuf_free(p2p_ie);
938 continue;
939 }
940 wpabuf_free(p2p_ie);
941 }
942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 /*
944 * TODO: skip the AP if its P2P IE has Group Formation
945 * bit set in the P2P Group Capability Bitmap and we
946 * are not in Group Formation with that device.
947 */
948#endif /* CONFIG_P2P */
949
950 /* Matching configuration found */
951 return ssid;
952 }
953
954 /* No matching configuration found */
955 return NULL;
956}
957
958
959static struct wpa_bss *
960wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 struct wpa_ssid *group,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800962 struct wpa_ssid **selected_ssid,
963 int only_first_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700965 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 if (only_first_ssid)
968 wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
969 group->id);
970 else
971 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
972 group->priority);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700974 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
975 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800976 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
977 only_first_ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 if (!*selected_ssid)
979 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
981 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700982 MAC2STR(bss->bssid),
983 wpa_ssid_txt(bss->ssid, bss->ssid_len));
984 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 }
986
987 return NULL;
988}
989
990
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700991struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
992 struct wpa_ssid **selected_ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993{
994 struct wpa_bss *selected = NULL;
995 int prio;
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -0800996 struct wpa_ssid *next_ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700998 if (wpa_s->last_scan_res == NULL ||
999 wpa_s->last_scan_res_used == 0)
1000 return NULL; /* no scan results from last update */
1001
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001002 if (wpa_s->next_ssid) {
1003 struct wpa_ssid *ssid;
1004
1005 /* check that next_ssid is still valid */
1006 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1007 if (ssid == wpa_s->next_ssid)
1008 break;
1009 }
1010 next_ssid = ssid;
1011 wpa_s->next_ssid = NULL;
1012 }
1013
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 while (selected == NULL) {
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001015 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1016 if (next_ssid && next_ssid->priority ==
1017 wpa_s->conf->pssid[prio]->priority) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001018 selected = wpa_supplicant_select_bss(
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08001019 wpa_s, next_ssid, selected_ssid, 1);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001020 if (selected)
1021 break;
1022 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001024 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001025 selected_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 if (selected)
1027 break;
1028 }
1029
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030 if (selected == NULL && wpa_s->blacklist &&
1031 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
1033 "blacklist and try again");
1034 wpa_blacklist_clear(wpa_s);
1035 wpa_s->blacklist_cleared++;
1036 } else if (selected == NULL)
1037 break;
1038 }
1039
1040 return selected;
1041}
1042
1043
1044static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
1045 int timeout_sec, int timeout_usec)
1046{
Dmitry Shmidt04949592012-07-19 12:16:46 -07001047 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 /*
1049 * No networks are enabled; short-circuit request so
1050 * we don't wait timeout seconds before transitioning
1051 * to INACTIVE state.
1052 */
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001053 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
1054 "since there are no enabled networks");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1056 return;
1057 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001058
1059 wpa_s->scan_for_connection = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
1061}
1062
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001063
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001064int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065 struct wpa_bss *selected,
1066 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067{
1068 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
1069 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
1070 "PBC session overlap");
1071#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001072 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001073 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074#endif /* CONFIG_P2P */
1075
1076#ifdef CONFIG_WPS
1077 wpas_wps_cancel(wpa_s);
1078#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001079 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 }
1081
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001082 wpa_msg(wpa_s, MSG_DEBUG,
1083 "Considering connect request: reassociate: %d selected: "
1084 MACSTR " bssid: " MACSTR " pending: " MACSTR
1085 " wpa_state: %s ssid=%p current_ssid=%p",
1086 wpa_s->reassociate, MAC2STR(selected->bssid),
1087 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1088 wpa_supplicant_state_txt(wpa_s->wpa_state),
1089 ssid, wpa_s->current_ssid);
1090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 /*
1092 * Do not trigger new association unless the BSSID has changed or if
1093 * reassociation is requested. If we are in process of associating with
1094 * the selected BSSID, do not trigger new attempt.
1095 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
1098 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
1099 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001100 (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1101 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
1102 0) ||
1103 (is_zero_ether_addr(wpa_s->pending_bssid) &&
1104 ssid != wpa_s->current_ssid)))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
1106 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001107 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 }
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001109 wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
1110 MAC2STR(selected->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 wpa_supplicant_associate(wpa_s, selected, ssid);
1112 } else {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001113 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
1114 "connect with the selected AP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001116
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001117 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118}
1119
1120
1121static struct wpa_ssid *
1122wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
1123{
1124 int prio;
1125 struct wpa_ssid *ssid;
1126
1127 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
1128 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
1129 {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001130 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 continue;
1132 if (ssid->mode == IEEE80211_MODE_IBSS ||
1133 ssid->mode == IEEE80211_MODE_AP)
1134 return ssid;
1135 }
1136 }
1137 return NULL;
1138}
1139
1140
1141/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1142 * on BSS added and BSS changed events */
1143static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +03001144 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145{
Jouni Malinen87fd2792011-05-16 18:35:42 +03001146 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147
1148 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1149 return;
1150
Jouni Malinen87fd2792011-05-16 18:35:42 +03001151 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
Jouni Malinen87fd2792011-05-16 18:35:42 +03001154 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 if (ssid == NULL)
1156 continue;
1157
Jouni Malinen87fd2792011-05-16 18:35:42 +03001158 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 if (rsn == NULL)
1160 continue;
1161
Jouni Malinen87fd2792011-05-16 18:35:42 +03001162 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 }
1164
1165}
1166
1167
1168static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1169 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001170 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001172 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 int min_diff;
1174
1175 if (wpa_s->reassociate)
1176 return 1; /* explicit request to reassociate */
1177 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1178 return 1; /* we are not associated; continue */
1179 if (wpa_s->current_ssid == NULL)
1180 return 1; /* unknown current SSID */
1181 if (wpa_s->current_ssid != ssid)
1182 return 1; /* different network block */
1183
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 if (wpas_driver_bss_selection(wpa_s))
1185 return 0; /* Driver-based roaming */
1186
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001187 if (wpa_s->current_ssid->ssid)
1188 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1189 wpa_s->current_ssid->ssid,
1190 wpa_s->current_ssid->ssid_len);
1191 if (!current_bss)
1192 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193
1194 if (!current_bss)
1195 return 1; /* current BSS not seen in scan results */
1196
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001197 if (current_bss == selected)
1198 return 0;
1199
1200 if (selected->last_update_idx > current_bss->last_update_idx)
1201 return 1; /* current BSS not seen in the last scan */
1202
Dmitry Shmidt9e077672012-04-13 10:07:27 -07001203#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1205 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1206 MAC2STR(current_bss->bssid), current_bss->level);
1207 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1208 MAC2STR(selected->bssid), selected->level);
1209
1210 if (wpa_s->current_ssid->bssid_set &&
1211 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1212 0) {
1213 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1214 "has preferred BSSID");
1215 return 1;
1216 }
1217
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001218 if (current_bss->level < 0 && current_bss->level > selected->level) {
1219 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1220 "signal level");
1221 return 0;
1222 }
1223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 min_diff = 2;
1225 if (current_bss->level < 0) {
1226 if (current_bss->level < -85)
1227 min_diff = 1;
1228 else if (current_bss->level < -80)
1229 min_diff = 2;
1230 else if (current_bss->level < -75)
1231 min_diff = 3;
1232 else if (current_bss->level < -70)
1233 min_diff = 4;
1234 else
1235 min_diff = 5;
1236 }
1237 if (abs(current_bss->level - selected->level) < min_diff) {
1238 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1239 "in signal level");
1240 return 0;
1241 }
1242
1243 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001244#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001245 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001246#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001247}
1248
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001249
Jouni Malinen89ca7022012-09-14 13:03:12 -07001250/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001251 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001252static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001253 union wpa_event_data *data,
1254 int own_request)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001256 struct wpa_scan_results *scan_res = NULL;
1257 int ret = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001259#ifndef CONFIG_NO_RANDOM_POOL
1260 size_t i, num;
1261#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262
1263#ifdef CONFIG_AP
1264 if (wpa_s->ap_iface)
1265 ap = 1;
1266#endif /* CONFIG_AP */
1267
1268 wpa_supplicant_notify_scanning(wpa_s, 0);
1269
1270 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1271 data ? &data->scan_info :
1272 NULL, 1);
1273 if (scan_res == NULL) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001274 if (wpa_s->conf->ap_scan == 2 || ap ||
1275 wpa_s->scan_res_handler == scan_only_handler)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 return -1;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001277 if (!own_request)
1278 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1280 "scanning again");
1281 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001282 ret = -1;
1283 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284 }
1285
1286#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 num = scan_res->num;
1288 if (num > 10)
1289 num = 10;
1290 for (i = 0; i < num; i++) {
1291 u8 buf[5];
1292 struct wpa_scan_res *res = scan_res->res[i];
1293 buf[0] = res->bssid[5];
1294 buf[1] = res->qual & 0xff;
1295 buf[2] = res->noise & 0xff;
1296 buf[3] = res->level & 0xff;
1297 buf[4] = res->tsf & 0xff;
1298 random_add_randomness(buf, sizeof(buf));
1299 }
1300#endif /* CONFIG_NO_RANDOM_POOL */
1301
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001302 if (own_request && wpa_s->scan_res_handler &&
1303 (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1305 struct wpa_scan_results *scan_res);
1306
1307 scan_res_handler = wpa_s->scan_res_handler;
1308 wpa_s->scan_res_handler = NULL;
1309 scan_res_handler(wpa_s, scan_res);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001310 ret = -2;
1311 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 }
1313
1314 if (ap) {
1315 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1316#ifdef CONFIG_AP
1317 if (wpa_s->ap_iface->scan_cb)
1318 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1319#endif /* CONFIG_AP */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001320 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 }
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001322
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001323 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1324 wpa_s->own_scan_running, wpa_s->external_scan_running);
1325 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1326 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1327 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1328 wpa_s->manual_scan_id);
1329 wpa_s->manual_scan_use_id = 0;
1330 } else {
1331 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1332 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001333 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334
1335 wpas_notify_scan_done(wpa_s, 1);
1336
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001337 if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1338 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 -07001339 wpa_scan_results_free(scan_res);
1340 return 0;
1341 }
1342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001343 if (sme_proc_obss_scan(wpa_s) > 0)
1344 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001346 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1347 goto scan_work_done;
1348
1349 if (autoscan_notify_scan(wpa_s, scan_res))
1350 goto scan_work_done;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001352 if (wpa_s->disconnected) {
1353 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001354 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 }
1356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001357 if (!wpas_driver_bss_selection(wpa_s) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001358 bgscan_notify_scan(wpa_s, scan_res) == 1)
1359 goto scan_work_done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001361 wpas_wps_update_ap_info(wpa_s, scan_res);
1362
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001363 wpa_scan_results_free(scan_res);
1364
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001365 if (wpa_s->scan_work) {
1366 struct wpa_radio_work *work = wpa_s->scan_work;
1367 wpa_s->scan_work = NULL;
1368 radio_work_done(work);
1369 }
1370
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001371 return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001372
1373scan_work_done:
1374 wpa_scan_results_free(scan_res);
1375 if (wpa_s->scan_work) {
1376 struct wpa_radio_work *work = wpa_s->scan_work;
1377 wpa_s->scan_work = NULL;
1378 radio_work_done(work);
1379 }
1380 return ret;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001381}
1382
1383
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001384static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001385 int new_scan, int own_request)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001386{
1387 struct wpa_bss *selected;
1388 struct wpa_ssid *ssid = NULL;
1389
1390 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391
1392 if (selected) {
1393 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001394 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001395 if (skip) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001396 if (new_scan)
1397 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399 }
1400
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001401 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001402 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001403 return -1;
1404 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001405 if (new_scan)
1406 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001407 /*
1408 * Do not notify other virtual radios of scan results since we do not
1409 * want them to start other associations at the same time.
1410 */
1411 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001413 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1414 ssid = wpa_supplicant_pick_new_network(wpa_s);
1415 if (ssid) {
1416 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1417 wpa_supplicant_associate(wpa_s, NULL, ssid);
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001418 if (new_scan)
1419 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001420 } else if (own_request) {
1421 /*
1422 * No SSID found. If SCAN results are as a result of
1423 * own scan request and not due to a scan request on
1424 * another shared interface, try another scan.
1425 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 int timeout_sec = wpa_s->scan_interval;
1427 int timeout_usec = 0;
1428#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001429 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1430 return 0;
1431
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001432 if (wpa_s->p2p_in_provisioning ||
1433 wpa_s->show_group_started) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434 /*
1435 * Use shorter wait during P2P Provisioning
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001436 * state and during P2P join-a-group operation
1437 * to speed up group formation.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 */
1439 timeout_sec = 0;
1440 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001441 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1442 timeout_usec);
1443 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 }
1445#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001446#ifdef CONFIG_INTERWORKING
1447 if (wpa_s->conf->auto_interworking &&
1448 wpa_s->conf->interworking &&
1449 wpa_s->conf->cred) {
1450 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1451 "start ANQP fetch since no matching "
1452 "networks found");
1453 wpa_s->network_select = 1;
1454 wpa_s->auto_network_select = 1;
1455 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001456 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001457 }
1458#endif /* CONFIG_INTERWORKING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001459#ifdef CONFIG_WPS
1460 if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1461 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1462 timeout_sec = 0;
1463 timeout_usec = 500000;
1464 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1465 timeout_usec);
1466 return 0;
1467 }
1468#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001469 if (wpa_supplicant_req_sched_scan(wpa_s))
1470 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1471 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 }
1473 }
1474 return 0;
1475}
1476
1477
1478static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1479 union wpa_event_data *data)
1480{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481 struct wpa_supplicant *ifs;
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001482
1483 if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 /*
1485 * If no scan results could be fetched, then no need to
1486 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001487 * this scan. Similarly, if scan results started a new operation on this
1488 * interface, do not notify other interfaces to avoid concurrent
1489 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490 */
1491 return;
1492 }
1493
1494 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001495 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 * so, they get updated with this same scan info.
1497 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001498 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1499 radio_list) {
1500 if (ifs != wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1502 "sibling", ifs->ifname);
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001503 _wpa_supplicant_event_scan_results(ifs, data, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 }
1505 }
1506}
1507
1508#endif /* CONFIG_NO_SCAN_PROCESSING */
1509
1510
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001511int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1512{
1513#ifdef CONFIG_NO_SCAN_PROCESSING
1514 return -1;
1515#else /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001516 struct os_reltime now;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001517
1518 if (wpa_s->last_scan_res_used <= 0)
1519 return -1;
1520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001521 os_get_reltime(&now);
1522 if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001523 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1524 return -1;
1525 }
1526
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001527 return wpas_select_network_from_last_scan(wpa_s, 0, 1);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001528#endif /* CONFIG_NO_SCAN_PROCESSING */
1529}
1530
Dmitry Shmidt04949592012-07-19 12:16:46 -07001531#ifdef CONFIG_WNM
1532
1533static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1534{
1535 struct wpa_supplicant *wpa_s = eloop_ctx;
1536
1537 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1538 return;
1539
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001540 if (!wpa_s->no_keep_alive) {
1541 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1542 MAC2STR(wpa_s->bssid));
1543 /* TODO: could skip this if normal data traffic has been sent */
1544 /* TODO: Consider using some more appropriate data frame for
1545 * this */
1546 if (wpa_s->l2)
1547 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1548 (u8 *) "", 0);
1549 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001550
1551#ifdef CONFIG_SME
1552 if (wpa_s->sme.bss_max_idle_period) {
1553 unsigned int msec;
1554 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1555 if (msec > 100)
1556 msec -= 100;
1557 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1558 wnm_bss_keep_alive, wpa_s, NULL);
1559 }
1560#endif /* CONFIG_SME */
1561}
1562
1563
1564static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1565 const u8 *ies, size_t ies_len)
1566{
1567 struct ieee802_11_elems elems;
1568
1569 if (ies == NULL)
1570 return;
1571
1572 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1573 return;
1574
1575#ifdef CONFIG_SME
1576 if (elems.bss_max_idle_period) {
1577 unsigned int msec;
1578 wpa_s->sme.bss_max_idle_period =
1579 WPA_GET_LE16(elems.bss_max_idle_period);
1580 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1581 "TU)%s", wpa_s->sme.bss_max_idle_period,
1582 (elems.bss_max_idle_period[2] & 0x01) ?
1583 " (protected keep-live required)" : "");
1584 if (wpa_s->sme.bss_max_idle_period == 0)
1585 wpa_s->sme.bss_max_idle_period = 1;
1586 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1587 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1588 /* msec times 1000 */
1589 msec = wpa_s->sme.bss_max_idle_period * 1024;
1590 if (msec > 100)
1591 msec -= 100;
1592 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1593 wnm_bss_keep_alive, wpa_s,
1594 NULL);
1595 }
1596 }
1597#endif /* CONFIG_SME */
1598}
1599
1600#endif /* CONFIG_WNM */
1601
1602
1603void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1604{
1605#ifdef CONFIG_WNM
1606 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1607#endif /* CONFIG_WNM */
1608}
1609
1610
Dmitry Shmidt051af732013-10-22 13:52:46 -07001611#ifdef CONFIG_INTERWORKING
1612
1613static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1614 size_t len)
1615{
1616 int res;
1617
1618 wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1619 res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1620 if (res) {
1621 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1622 }
1623
1624 return res;
1625}
1626
1627
1628static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1629 const u8 *ies, size_t ies_len)
1630{
1631 struct ieee802_11_elems elems;
1632
1633 if (ies == NULL)
1634 return;
1635
1636 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1637 return;
1638
1639 if (elems.qos_map_set) {
1640 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1641 elems.qos_map_set_len);
1642 }
1643}
1644
1645#endif /* CONFIG_INTERWORKING */
1646
1647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1649 union wpa_event_data *data)
1650{
1651 int l, len, found = 0, wpa_found, rsn_found;
1652 const u8 *p;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001653#ifdef CONFIG_IEEE80211R
1654 u8 bssid[ETH_ALEN];
1655#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656
1657 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1658 if (data->assoc_info.req_ies)
1659 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1660 data->assoc_info.req_ies_len);
1661 if (data->assoc_info.resp_ies) {
1662 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1663 data->assoc_info.resp_ies_len);
1664#ifdef CONFIG_TDLS
1665 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1666 data->assoc_info.resp_ies_len);
1667#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001668#ifdef CONFIG_WNM
1669 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1670 data->assoc_info.resp_ies_len);
1671#endif /* CONFIG_WNM */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001672#ifdef CONFIG_INTERWORKING
1673 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1674 data->assoc_info.resp_ies_len);
1675#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 }
1677 if (data->assoc_info.beacon_ies)
1678 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1679 data->assoc_info.beacon_ies,
1680 data->assoc_info.beacon_ies_len);
1681 if (data->assoc_info.freq)
1682 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1683 data->assoc_info.freq);
1684
1685 p = data->assoc_info.req_ies;
1686 l = data->assoc_info.req_ies_len;
1687
1688 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1689 while (p && l >= 2) {
1690 len = p[1] + 2;
1691 if (len > l) {
1692 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1693 p, l);
1694 break;
1695 }
1696 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1697 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1698 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1699 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1700 break;
1701 found = 1;
1702 wpa_find_assoc_pmkid(wpa_s);
1703 break;
1704 }
1705 l -= len;
1706 p += len;
1707 }
1708 if (!found && data->assoc_info.req_ies)
1709 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1710
1711#ifdef CONFIG_IEEE80211R
1712#ifdef CONFIG_SME
1713 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1715 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1716 data->assoc_info.resp_ies,
1717 data->assoc_info.resp_ies_len,
1718 bssid) < 0) {
1719 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1720 "Reassociation Response failed");
1721 wpa_supplicant_deauthenticate(
1722 wpa_s, WLAN_REASON_INVALID_IE);
1723 return -1;
1724 }
1725 }
1726
1727 p = data->assoc_info.resp_ies;
1728 l = data->assoc_info.resp_ies_len;
1729
1730#ifdef CONFIG_WPS_STRICT
1731 if (p && wpa_s->current_ssid &&
1732 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1733 struct wpabuf *wps;
1734 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1735 if (wps == NULL) {
1736 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1737 "include WPS IE in (Re)Association Response");
1738 return -1;
1739 }
1740
1741 if (wps_validate_assoc_resp(wps) < 0) {
1742 wpabuf_free(wps);
1743 wpa_supplicant_deauthenticate(
1744 wpa_s, WLAN_REASON_INVALID_IE);
1745 return -1;
1746 }
1747 wpabuf_free(wps);
1748 }
1749#endif /* CONFIG_WPS_STRICT */
1750
1751 /* Go through the IEs and make a copy of the MDIE, if present. */
1752 while (p && l >= 2) {
1753 len = p[1] + 2;
1754 if (len > l) {
1755 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1756 p, l);
1757 break;
1758 }
1759 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1760 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1761 wpa_s->sme.ft_used = 1;
1762 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1763 MOBILITY_DOMAIN_ID_LEN);
1764 break;
1765 }
1766 l -= len;
1767 p += len;
1768 }
1769#endif /* CONFIG_SME */
1770
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001771 /* Process FT when SME is in the driver */
1772 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1773 wpa_ft_is_completed(wpa_s->wpa)) {
1774 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1775 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1776 data->assoc_info.resp_ies,
1777 data->assoc_info.resp_ies_len,
1778 bssid) < 0) {
1779 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1780 "Reassociation Response failed");
1781 wpa_supplicant_deauthenticate(
1782 wpa_s, WLAN_REASON_INVALID_IE);
1783 return -1;
1784 }
1785 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1786 }
1787
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1789 data->assoc_info.resp_ies_len);
1790#endif /* CONFIG_IEEE80211R */
1791
1792 /* WPA/RSN IE from Beacon/ProbeResp */
1793 p = data->assoc_info.beacon_ies;
1794 l = data->assoc_info.beacon_ies_len;
1795
1796 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1797 */
1798 wpa_found = rsn_found = 0;
1799 while (p && l >= 2) {
1800 len = p[1] + 2;
1801 if (len > l) {
1802 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1803 p, l);
1804 break;
1805 }
1806 if (!wpa_found &&
1807 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1808 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1809 wpa_found = 1;
1810 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1811 }
1812
1813 if (!rsn_found &&
1814 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1815 rsn_found = 1;
1816 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1817 }
1818
1819 l -= len;
1820 p += len;
1821 }
1822
1823 if (!wpa_found && data->assoc_info.beacon_ies)
1824 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1825 if (!rsn_found && data->assoc_info.beacon_ies)
1826 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1827 if (wpa_found || rsn_found)
1828 wpa_s->ap_ies_from_associnfo = 1;
1829
Jouni Malinen87fd2792011-05-16 18:35:42 +03001830 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1831 wpa_s->assoc_freq != data->assoc_info.freq) {
1832 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1833 "%u to %u MHz",
1834 wpa_s->assoc_freq, data->assoc_info.freq);
1835 wpa_supplicant_update_scan_results(wpa_s);
1836 }
1837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 wpa_s->assoc_freq = data->assoc_info.freq;
1839
1840 return 0;
1841}
1842
1843
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001844static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1845{
1846 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1847
1848 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1849 return -1;
1850
1851 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1852 return 0;
1853
1854 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1855 WPA_IE_VENDOR_TYPE);
1856 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1857
1858 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1859 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1860 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1861 bss_rsn ? 2 + bss_rsn[1] : 0))
1862 return -1;
1863
1864 return 0;
1865}
1866
1867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1869 union wpa_event_data *data)
1870{
1871 u8 bssid[ETH_ALEN];
1872 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873
1874#ifdef CONFIG_AP
1875 if (wpa_s->ap_iface) {
1876 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1877 data->assoc_info.addr,
1878 data->assoc_info.req_ies,
1879 data->assoc_info.req_ies_len,
1880 data->assoc_info.reassoc);
1881 return;
1882 }
1883#endif /* CONFIG_AP */
1884
1885 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1886 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1887 return;
1888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001889 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1890 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001891 wpa_supplicant_deauthenticate(
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001892 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1893 return;
1894 }
1895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001897 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1899 MACSTR, MAC2STR(bssid));
1900 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001901 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1902 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001903 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904
1905 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1906 wpa_clear_keys(wpa_s, bssid);
1907 }
1908 if (wpa_supplicant_select_config(wpa_s) < 0) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001909 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001910 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1911 return;
1912 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001913
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001914#ifdef ANDROID
1915 if (wpa_s->conf->ap_scan == 1) {
1916#else
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001917 if (wpa_s->conf->ap_scan == 1 &&
1918 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
Dmitry Shmidt1e1c48d2013-02-14 16:44:44 -08001919#endif
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001920 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1921 wpa_msg(wpa_s, MSG_WARNING,
1922 "WPA/RSN IEs not updated");
1923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 }
1925
1926#ifdef CONFIG_SME
1927 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1928 wpa_s->sme.prev_bssid_set = 1;
1929#endif /* CONFIG_SME */
1930
1931 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1932 if (wpa_s->current_ssid) {
1933 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1934 * initialized before association, but for other modes,
1935 * initialize PC/SC here, if the current configuration needs
1936 * smartcard or SIM/USIM. */
1937 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1938 }
1939 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1940 if (wpa_s->l2)
1941 l2_packet_notify_auth_start(wpa_s->l2);
1942
1943 /*
1944 * Set portEnabled first to FALSE in order to get EAP state machine out
1945 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1946 * state machine may transit to AUTHENTICATING state based on obsolete
1947 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1948 * AUTHENTICATED without ever giving chance to EAP state machine to
1949 * reset the state.
1950 */
1951 if (!ft_completed) {
1952 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1953 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1954 }
1955 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1956 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1957 /* 802.1X::portControl = Auto */
1958 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1959 wpa_s->eapol_received = 0;
1960 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1961 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1962 (wpa_s->current_ssid &&
1963 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001964 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1965 (wpa_s->drv_flags &
1966 WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1967 /*
1968 * Set the key after having received joined-IBSS event
1969 * from the driver.
1970 */
1971 wpa_supplicant_set_wpa_none_key(wpa_s,
1972 wpa_s->current_ssid);
1973 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 wpa_supplicant_cancel_auth_timeout(wpa_s);
1975 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1976 } else if (!ft_completed) {
1977 /* Timeout for receiving the first EAPOL packet */
1978 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1979 }
1980 wpa_supplicant_cancel_scan(wpa_s);
1981
1982 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1983 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1984 /*
1985 * We are done; the driver will take care of RSN 4-way
1986 * handshake.
1987 */
1988 wpa_supplicant_cancel_auth_timeout(wpa_s);
1989 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1990 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1991 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1992 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1993 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1994 /*
1995 * The driver will take care of RSN 4-way handshake, so we need
1996 * to allow EAPOL supplicant to complete its work without
1997 * waiting for WPA supplicant.
1998 */
1999 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2000 } else if (ft_completed) {
2001 /*
2002 * FT protocol completed - make sure EAPOL state machine ends
2003 * up in authenticated.
2004 */
2005 wpa_supplicant_cancel_auth_timeout(wpa_s);
2006 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2007 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2008 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2009 }
2010
Jouni Malinena05074c2012-12-21 21:35:35 +02002011 wpa_s->last_eapol_matches_bssid = 0;
2012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002014 struct os_reltime now, age;
2015 os_get_reltime(&now);
2016 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 if (age.sec == 0 && age.usec < 100000 &&
2018 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
2019 0) {
2020 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
2021 "frame that was received just before "
2022 "association notification");
2023 wpa_supplicant_rx_eapol(
2024 wpa_s, wpa_s->pending_eapol_rx_src,
2025 wpabuf_head(wpa_s->pending_eapol_rx),
2026 wpabuf_len(wpa_s->pending_eapol_rx));
2027 }
2028 wpabuf_free(wpa_s->pending_eapol_rx);
2029 wpa_s->pending_eapol_rx = NULL;
2030 }
2031
2032 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2033 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002034 wpa_s->current_ssid &&
2035 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 /* Set static WEP keys again */
2037 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2038 }
2039
2040#ifdef CONFIG_IBSS_RSN
2041 if (wpa_s->current_ssid &&
2042 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2043 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2044 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2045 wpa_s->ibss_rsn == NULL) {
2046 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2047 if (!wpa_s->ibss_rsn) {
2048 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2049 wpa_supplicant_deauthenticate(
2050 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2051 return;
2052 }
2053
2054 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2055 }
2056#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002057
2058 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059}
2060
2061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002062static int disconnect_reason_recoverable(u16 reason_code)
2063{
2064 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2065 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2066 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2067}
2068
2069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002071 u16 reason_code,
2072 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073{
2074 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03002075
2076 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2077 /*
2078 * At least Host AP driver and a Prism3 card seemed to be
2079 * generating streams of disconnected events when configuring
2080 * IBSS for WPA-None. Ignore them for now.
2081 */
2082 return;
2083 }
2084
2085 bssid = wpa_s->bssid;
2086 if (is_zero_ether_addr(bssid))
2087 bssid = wpa_s->pending_bssid;
2088
2089 if (!is_zero_ether_addr(bssid) ||
2090 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2091 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2092 " reason=%d%s",
2093 MAC2STR(bssid), reason_code,
2094 locally_generated ? " locally_generated=1" : "");
2095 }
2096}
2097
2098
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002099static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2100 int locally_generated)
2101{
2102 if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2103 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2104 return 0; /* Not in 4-way handshake with PSK */
2105
2106 /*
2107 * It looks like connection was lost while trying to go through PSK
2108 * 4-way handshake. Filter out known disconnection cases that are caused
2109 * by something else than PSK mismatch to avoid confusing reports.
2110 */
2111
2112 if (locally_generated) {
2113 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2114 return 0;
2115 }
2116
2117 return 1;
2118}
2119
2120
Jouni Malinen2b89da82012-08-31 22:04:41 +03002121static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2122 u16 reason_code,
2123 int locally_generated)
2124{
2125 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002126 int authenticating;
2127 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002128 struct wpa_bss *fast_reconnect = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002129#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002130 struct wpa_ssid *fast_reconnect_ssid = NULL;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002131#endif /* CONFIG_NO_SCAN_PROCESSING */
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002132 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133
2134 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2135 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2136
2137 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2138 /*
2139 * At least Host AP driver and a Prism3 card seemed to be
2140 * generating streams of disconnected events when configuring
2141 * IBSS for WPA-None. Ignore them for now.
2142 */
2143 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2144 "IBSS/WPA-None mode");
2145 return;
2146 }
2147
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002148 if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2150 "pre-shared key may be incorrect");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002151 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2152 return; /* P2P group removed */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002153 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -07002155 if (!wpa_s->disconnected &&
2156 (!wpa_s->auto_reconnect_disabled ||
2157 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002158 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2159 "reconnect (wps=%d wpa_state=%d)",
2160 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2161 wpa_s->wpa_state);
2162 if (wpa_s->wpa_state == WPA_COMPLETED &&
2163 wpa_s->current_ssid &&
2164 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002165 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002166 disconnect_reason_recoverable(reason_code)) {
2167 /*
2168 * It looks like the AP has dropped association with
2169 * us, but could allow us to get back in. Try to
2170 * reconnect to the same BSS without full scan to save
2171 * time for some common cases.
2172 */
2173 fast_reconnect = wpa_s->current_bss;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002174#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002175 fast_reconnect_ssid = wpa_s->current_ssid;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002176#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002177 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002178 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002179 else
2180 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2181 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002183 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 "try to re-connect");
2185 wpa_s->reassociate = 0;
2186 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002187 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 }
2189 bssid = wpa_s->bssid;
2190 if (is_zero_ether_addr(bssid))
2191 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002192 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2193 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002195 if (locally_generated)
2196 wpa_s->disconnect_reason = -reason_code;
2197 else
2198 wpa_s->disconnect_reason = reason_code;
2199 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 if (wpa_supplicant_dynamic_keys(wpa_s)) {
2201 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202 wpa_clear_keys(wpa_s, wpa_s->bssid);
2203 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002204 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002205 wpa_supplicant_mark_disassoc(wpa_s);
2206
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002207 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002208 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03002209 wpa_s->current_ssid = last_ssid;
2210 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002211
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002212 if (fast_reconnect &&
2213 !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2214 !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2215 !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2216 fast_reconnect->ssid_len) &&
2217 !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218#ifndef CONFIG_NO_SCAN_PROCESSING
2219 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2220 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2221 fast_reconnect_ssid) < 0) {
2222 /* Recover through full scan */
2223 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2224 }
2225#endif /* CONFIG_NO_SCAN_PROCESSING */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002226 } else if (fast_reconnect) {
2227 /*
2228 * Could not reconnect to the same BSS due to network being
2229 * disabled. Use a new scan to match the alternative behavior
2230 * above, i.e., to continue automatic reconnection attempt in a
2231 * way that enforces disabled network rules.
2232 */
2233 wpa_supplicant_req_scan(wpa_s, 0, 100000);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235}
2236
2237
2238#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002239void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240{
2241 struct wpa_supplicant *wpa_s = eloop_ctx;
2242
2243 if (!wpa_s->pending_mic_error_report)
2244 return;
2245
2246 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2247 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2248 wpa_s->pending_mic_error_report = 0;
2249}
2250#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2251
2252
2253static void
2254wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2255 union wpa_event_data *data)
2256{
2257 int pairwise;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002258 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002259
2260 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2261 pairwise = (data && data->michael_mic_failure.unicast);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002262 os_get_reltime(&t);
2263 if ((wpa_s->last_michael_mic_error.sec &&
2264 !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 wpa_s->pending_mic_error_report) {
2266 if (wpa_s->pending_mic_error_report) {
2267 /*
2268 * Send the pending MIC error report immediately since
2269 * we are going to start countermeasures and AP better
2270 * do the same.
2271 */
2272 wpa_sm_key_request(wpa_s->wpa, 1,
2273 wpa_s->pending_mic_error_pairwise);
2274 }
2275
2276 /* Send the new MIC error report immediately since we are going
2277 * to start countermeasures and AP better do the same.
2278 */
2279 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2280
2281 /* initialize countermeasures */
2282 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002283
2284 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002286 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2287
2288 /*
2289 * Need to wait for completion of request frame. We do not get
2290 * any callback for the message completion, so just wait a
2291 * short while and hope for the best. */
2292 os_sleep(0, 10000);
2293
2294 wpa_drv_set_countermeasures(wpa_s, 1);
2295 wpa_supplicant_deauthenticate(wpa_s,
2296 WLAN_REASON_MICHAEL_MIC_FAILURE);
2297 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2298 wpa_s, NULL);
2299 eloop_register_timeout(60, 0,
2300 wpa_supplicant_stop_countermeasures,
2301 wpa_s, NULL);
2302 /* TODO: mark the AP rejected for 60 second. STA is
2303 * allowed to associate with another AP.. */
2304 } else {
2305#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2306 if (wpa_s->mic_errors_seen) {
2307 /*
2308 * Reduce the effectiveness of Michael MIC error
2309 * reports as a means for attacking against TKIP if
2310 * more than one MIC failure is noticed with the same
2311 * PTK. We delay the transmission of the reports by a
2312 * random time between 0 and 60 seconds in order to
2313 * force the attacker wait 60 seconds before getting
2314 * the information on whether a frame resulted in a MIC
2315 * failure.
2316 */
2317 u8 rval[4];
2318 int sec;
2319
2320 if (os_get_random(rval, sizeof(rval)) < 0)
2321 sec = os_random() % 60;
2322 else
2323 sec = WPA_GET_BE32(rval) % 60;
2324 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2325 "report %d seconds", sec);
2326 wpa_s->pending_mic_error_report = 1;
2327 wpa_s->pending_mic_error_pairwise = pairwise;
2328 eloop_cancel_timeout(
2329 wpa_supplicant_delayed_mic_error_report,
2330 wpa_s, NULL);
2331 eloop_register_timeout(
2332 sec, os_random() % 1000000,
2333 wpa_supplicant_delayed_mic_error_report,
2334 wpa_s, NULL);
2335 } else {
2336 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2337 }
2338#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2339 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2340#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2341 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002342 wpa_s->last_michael_mic_error = t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 wpa_s->mic_errors_seen++;
2344}
2345
2346
2347#ifdef CONFIG_TERMINATE_ONLASTIF
2348static int any_interfaces(struct wpa_supplicant *head)
2349{
2350 struct wpa_supplicant *wpa_s;
2351
2352 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2353 if (!wpa_s->interface_removed)
2354 return 1;
2355 return 0;
2356}
2357#endif /* CONFIG_TERMINATE_ONLASTIF */
2358
2359
2360static void
2361wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2362 union wpa_event_data *data)
2363{
2364 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2365 return;
2366
2367 switch (data->interface_status.ievent) {
2368 case EVENT_INTERFACE_ADDED:
2369 if (!wpa_s->interface_removed)
2370 break;
2371 wpa_s->interface_removed = 0;
2372 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2373 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2374 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2375 "driver after interface was added");
2376 }
2377 break;
2378 case EVENT_INTERFACE_REMOVED:
2379 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2380 wpa_s->interface_removed = 1;
2381 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002382 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 l2_packet_deinit(wpa_s->l2);
2384 wpa_s->l2 = NULL;
2385#ifdef CONFIG_IBSS_RSN
2386 ibss_rsn_deinit(wpa_s->ibss_rsn);
2387 wpa_s->ibss_rsn = NULL;
2388#endif /* CONFIG_IBSS_RSN */
2389#ifdef CONFIG_TERMINATE_ONLASTIF
2390 /* check if last interface */
2391 if (!any_interfaces(wpa_s->global->ifaces))
2392 eloop_terminate();
2393#endif /* CONFIG_TERMINATE_ONLASTIF */
2394 break;
2395 }
2396}
2397
2398
2399#ifdef CONFIG_PEERKEY
2400static void
2401wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2402 union wpa_event_data *data)
2403{
2404 if (data == NULL)
2405 return;
2406 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2407}
2408#endif /* CONFIG_PEERKEY */
2409
2410
2411#ifdef CONFIG_TDLS
2412static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2413 union wpa_event_data *data)
2414{
2415 if (data == NULL)
2416 return;
2417 switch (data->tdls.oper) {
2418 case TDLS_REQUEST_SETUP:
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002419 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2420 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2421 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2422 else
2423 wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424 break;
2425 case TDLS_REQUEST_TEARDOWN:
Sunil Dutt6a9f5222013-09-30 17:10:18 +03002426 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2427 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2428 data->tdls.reason_code);
2429 else
2430 wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2431 data->tdls.peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432 break;
2433 }
2434}
2435#endif /* CONFIG_TDLS */
2436
2437
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002438#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002439static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2440 union wpa_event_data *data)
2441{
2442 if (data == NULL)
2443 return;
2444 switch (data->wnm.oper) {
2445 case WNM_OPER_SLEEP:
2446 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2447 "(action=%d, intval=%d)",
2448 data->wnm.sleep_action, data->wnm.sleep_intval);
2449 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002450 data->wnm.sleep_intval, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002451 break;
2452 }
2453}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002454#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002455
2456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457#ifdef CONFIG_IEEE80211R
2458static void
2459wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2460 union wpa_event_data *data)
2461{
2462 if (data == NULL)
2463 return;
2464
2465 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2466 data->ft_ies.ies_len,
2467 data->ft_ies.ft_action,
2468 data->ft_ies.target_ap,
2469 data->ft_ies.ric_ies,
2470 data->ft_ies.ric_ies_len) < 0) {
2471 /* TODO: prevent MLME/driver from trying to associate? */
2472 }
2473}
2474#endif /* CONFIG_IEEE80211R */
2475
2476
2477#ifdef CONFIG_IBSS_RSN
2478static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2479 union wpa_event_data *data)
2480{
2481 struct wpa_ssid *ssid;
2482 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2483 return;
2484 if (data == NULL)
2485 return;
2486 ssid = wpa_s->current_ssid;
2487 if (ssid == NULL)
2488 return;
2489 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2490 return;
2491
2492 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2493}
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002494
2495
2496static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2497 union wpa_event_data *data)
2498{
2499 struct wpa_ssid *ssid = wpa_s->current_ssid;
2500
2501 if (ssid == NULL)
2502 return;
2503
2504 /* check if the ssid is correctly configured as IBSS/RSN */
2505 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2506 return;
2507
2508 ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2509 data->rx_mgmt.frame_len);
2510}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511#endif /* CONFIG_IBSS_RSN */
2512
2513
2514#ifdef CONFIG_IEEE80211R
2515static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2516 size_t len)
2517{
2518 const u8 *sta_addr, *target_ap_addr;
2519 u16 status;
2520
2521 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2522 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2523 return; /* only SME case supported for now */
2524 if (len < 1 + 2 * ETH_ALEN + 2)
2525 return;
2526 if (data[0] != 2)
2527 return; /* Only FT Action Response is supported for now */
2528 sta_addr = data + 1;
2529 target_ap_addr = data + 1 + ETH_ALEN;
2530 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2531 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2532 MACSTR " TargetAP " MACSTR " status %u",
2533 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2534
2535 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2536 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2537 " in FT Action Response", MAC2STR(sta_addr));
2538 return;
2539 }
2540
2541 if (status) {
2542 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2543 "failure (status code %d)", status);
2544 /* TODO: report error to FT code(?) */
2545 return;
2546 }
2547
2548 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2549 len - (1 + 2 * ETH_ALEN + 2), 1,
2550 target_ap_addr, NULL, 0) < 0)
2551 return;
2552
2553#ifdef CONFIG_SME
2554 {
2555 struct wpa_bss *bss;
2556 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2557 if (bss)
2558 wpa_s->sme.freq = bss->freq;
2559 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2560 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2561 WLAN_AUTH_FT);
2562 }
2563#endif /* CONFIG_SME */
2564}
2565#endif /* CONFIG_IEEE80211R */
2566
2567
2568static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2569 struct unprot_deauth *e)
2570{
2571#ifdef CONFIG_IEEE80211W
2572 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2573 "dropped: " MACSTR " -> " MACSTR
2574 " (reason code %u)",
2575 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2576 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2577#endif /* CONFIG_IEEE80211W */
2578}
2579
2580
2581static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2582 struct unprot_disassoc *e)
2583{
2584#ifdef CONFIG_IEEE80211W
2585 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2586 "dropped: " MACSTR " -> " MACSTR
2587 " (reason code %u)",
2588 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2589 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2590#endif /* CONFIG_IEEE80211W */
2591}
2592
2593
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002594static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2595 u16 reason_code, int locally_generated,
2596 const u8 *ie, size_t ie_len, int deauth)
2597{
2598#ifdef CONFIG_AP
2599 if (wpa_s->ap_iface && addr) {
2600 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2601 return;
2602 }
2603
2604 if (wpa_s->ap_iface) {
2605 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2606 return;
2607 }
2608#endif /* CONFIG_AP */
2609
2610 wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2611
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002612 if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2613 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2614 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2615 eapol_sm_failed(wpa_s->eapol))) &&
2616 !wpa_s->eap_expected_failure))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002617 wpas_auth_failed(wpa_s);
2618
2619#ifdef CONFIG_P2P
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002620 if (deauth && reason_code > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002621 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2622 locally_generated) > 0) {
2623 /*
2624 * The interface was removed, so cannot continue
2625 * processing any additional operations after this.
2626 */
2627 return;
2628 }
2629 }
2630#endif /* CONFIG_P2P */
2631
2632 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2633 locally_generated);
2634}
2635
2636
2637static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2638 struct disassoc_info *info)
2639{
2640 u16 reason_code = 0;
2641 int locally_generated = 0;
2642 const u8 *addr = NULL;
2643 const u8 *ie = NULL;
2644 size_t ie_len = 0;
2645
2646 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2647
2648 if (info) {
2649 addr = info->addr;
2650 ie = info->ie;
2651 ie_len = info->ie_len;
2652 reason_code = info->reason_code;
2653 locally_generated = info->locally_generated;
2654 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2655 locally_generated ? " (locally generated)" : "");
2656 if (addr)
2657 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2658 MAC2STR(addr));
2659 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2660 ie, ie_len);
2661 }
2662
2663#ifdef CONFIG_AP
2664 if (wpa_s->ap_iface && info && info->addr) {
2665 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2666 return;
2667 }
2668
2669 if (wpa_s->ap_iface) {
2670 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2671 return;
2672 }
2673#endif /* CONFIG_AP */
2674
2675#ifdef CONFIG_P2P
2676 if (info) {
2677 wpas_p2p_disassoc_notif(
2678 wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2679 locally_generated);
2680 }
2681#endif /* CONFIG_P2P */
2682
2683 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2684 sme_event_disassoc(wpa_s, info);
2685
2686 wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2687 ie, ie_len, 0);
2688}
2689
2690
2691static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2692 struct deauth_info *info)
2693{
2694 u16 reason_code = 0;
2695 int locally_generated = 0;
2696 const u8 *addr = NULL;
2697 const u8 *ie = NULL;
2698 size_t ie_len = 0;
2699
2700 wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2701
2702 if (info) {
2703 addr = info->addr;
2704 ie = info->ie;
2705 ie_len = info->ie_len;
2706 reason_code = info->reason_code;
2707 locally_generated = info->locally_generated;
2708 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2709 reason_code,
2710 locally_generated ? " (locally generated)" : "");
2711 if (addr) {
2712 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2713 MAC2STR(addr));
2714 }
2715 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2716 ie, ie_len);
2717 }
2718
2719 wpa_reset_ft_completed(wpa_s->wpa);
2720
2721 wpas_event_disconnect(wpa_s, addr, reason_code,
2722 locally_generated, ie, ie_len, 1);
2723}
2724
2725
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002726static void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s)
2727{
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002728 struct wpa_supplicant *ifs;
2729
2730 if (wpa_s->drv_priv == NULL)
2731 return; /* Ignore event during drv initialization */
2732
2733 free_hw_features(wpa_s);
2734 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2735 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2736
2737#ifdef CONFIG_P2P
2738 wpas_p2p_update_channel_list(wpa_s);
2739#endif /* CONFIG_P2P */
2740
2741 /*
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002742 * Check other interfaces to see if they share the same radio. If
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002743 * so, they get updated with this same hw mode info.
2744 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002745 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2746 radio_list) {
2747 if (ifs != wpa_s) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002748 wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2749 ifs->ifname);
2750 free_hw_features(ifs);
2751 ifs->hw.modes = wpa_drv_get_hw_feature_data(
2752 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2753 }
2754 }
2755}
2756
2757
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002758static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2759 const struct ieee80211_mgmt *mgmt,
2760 size_t len, int freq)
2761{
2762 const u8 *payload;
2763 size_t plen;
2764 u8 category;
2765
2766 if (len < IEEE80211_HDRLEN + 2)
2767 return;
2768
2769 payload = &mgmt->u.action.category;
2770 category = *payload++;
2771 plen = (((const u8 *) mgmt) + len) - payload;
2772
2773 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2774 " Category=%u DataLen=%d freq=%d MHz",
2775 MAC2STR(mgmt->sa), category, (int) plen, freq);
2776
2777#ifdef CONFIG_IEEE80211R
2778 if (category == WLAN_ACTION_FT) {
2779 ft_rx_action(wpa_s, payload, plen);
2780 return;
2781 }
2782#endif /* CONFIG_IEEE80211R */
2783
2784#ifdef CONFIG_IEEE80211W
2785#ifdef CONFIG_SME
2786 if (category == WLAN_ACTION_SA_QUERY) {
2787 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2788 return;
2789 }
2790#endif /* CONFIG_SME */
2791#endif /* CONFIG_IEEE80211W */
2792
2793#ifdef CONFIG_WNM
2794 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2795 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2796 return;
2797 }
2798#endif /* CONFIG_WNM */
2799
2800#ifdef CONFIG_GAS
Dmitry Shmidt18463232014-01-24 12:29:41 -08002801 if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2802 mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002803 gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
Dmitry Shmidt18463232014-01-24 12:29:41 -08002804 mgmt->u.action.category,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002805 payload, plen, freq) == 0)
2806 return;
2807#endif /* CONFIG_GAS */
2808
2809#ifdef CONFIG_TDLS
2810 if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2811 payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2812 wpa_dbg(wpa_s, MSG_DEBUG,
2813 "TDLS: Received Discovery Response from " MACSTR,
2814 MAC2STR(mgmt->sa));
2815 return;
2816 }
2817#endif /* CONFIG_TDLS */
2818
2819#ifdef CONFIG_INTERWORKING
2820 if (category == WLAN_ACTION_QOS && plen >= 1 &&
2821 payload[0] == QOS_QOS_MAP_CONFIG) {
2822 const u8 *pos = payload + 1;
2823 size_t qlen = plen - 1;
2824 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2825 MACSTR, MAC2STR(mgmt->sa));
2826 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2827 qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2828 pos[1] <= qlen - 2 && pos[1] >= 16)
2829 wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2830 return;
2831 }
2832#endif /* CONFIG_INTERWORKING */
2833
2834#ifdef CONFIG_P2P
2835 wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2836 category, payload, plen, freq);
2837#endif /* CONFIG_P2P */
2838}
2839
2840
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002841static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2842 union wpa_event_data *event)
2843{
2844#ifdef CONFIG_P2P
2845 struct wpa_supplicant *ifs;
2846#endif /* CONFIG_P2P */
2847 struct wpa_freq_range_list *list;
2848 char *str = NULL;
2849
2850 list = &event->freq_range;
2851
2852 if (list->num)
2853 str = freq_range_list_str(list);
2854 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2855 str ? str : "");
2856
2857#ifdef CONFIG_P2P
2858 if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2859 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2860 __func__);
2861 } else {
2862 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2863 wpas_p2p_update_channel_list(wpa_s);
2864 }
2865
2866 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2867 int freq;
2868 if (!ifs->current_ssid ||
2869 !ifs->current_ssid->p2p_group ||
2870 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2871 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2872 continue;
2873
2874 freq = ifs->current_ssid->frequency;
2875 if (!freq_range_list_includes(list, freq)) {
2876 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2877 freq);
2878 continue;
2879 }
2880
2881 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2882 freq);
2883 /* TODO: Consider using CSA or removing the group within
2884 * wpa_supplicant */
2885 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2886 }
2887#endif /* CONFIG_P2P */
2888
2889 os_free(str);
2890}
2891
2892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2894 union wpa_event_data *data)
2895{
2896 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897
2898 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2899 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002900 event != EVENT_INTERFACE_STATUS &&
2901 event != EVENT_SCHED_SCAN_STOPPED) {
2902 wpa_dbg(wpa_s, MSG_DEBUG,
2903 "Ignore event %s (%d) while interface is disabled",
2904 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 return;
2906 }
2907
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002908#ifndef CONFIG_NO_STDOUT_DEBUG
2909{
2910 int level = MSG_DEBUG;
2911
Dmitry Shmidt04949592012-07-19 12:16:46 -07002912 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002913 const struct ieee80211_hdr *hdr;
2914 u16 fc;
2915 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2916 fc = le_to_host16(hdr->frame_control);
2917 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2918 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2919 level = MSG_EXCESSIVE;
2920 }
2921
2922 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2923 event_to_string(event), event);
2924}
2925#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926
2927 switch (event) {
2928 case EVENT_AUTH:
2929 sme_event_auth(wpa_s, data);
2930 break;
2931 case EVENT_ASSOC:
2932 wpa_supplicant_event_assoc(wpa_s, data);
2933 break;
2934 case EVENT_DISASSOC:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002935 wpas_event_disassoc(wpa_s,
2936 data ? &data->disassoc_info : NULL);
2937 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 case EVENT_DEAUTH:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002939 wpas_event_deauth(wpa_s,
2940 data ? &data->deauth_info : NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 break;
2942 case EVENT_MICHAEL_MIC_FAILURE:
2943 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2944 break;
2945#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002946 case EVENT_SCAN_STARTED:
2947 os_get_reltime(&wpa_s->scan_start_time);
2948 if (wpa_s->own_scan_requested) {
2949 struct os_reltime diff;
2950
2951 os_reltime_sub(&wpa_s->scan_start_time,
2952 &wpa_s->scan_trigger_time, &diff);
2953 wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2954 diff.sec, diff.usec);
2955 wpa_s->own_scan_requested = 0;
2956 wpa_s->own_scan_running = 1;
2957 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2958 wpa_s->manual_scan_use_id) {
2959 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED "id=%u",
2960 wpa_s->manual_scan_id);
2961 } else {
2962 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2963 }
2964 } else {
2965 wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
2966 wpa_s->external_scan_running = 1;
2967 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2968 }
2969 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970 case EVENT_SCAN_RESULTS:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002971 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
2972 struct os_reltime now, diff;
2973 os_get_reltime(&now);
2974 os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
2975 wpa_s->scan_start_time.sec = 0;
2976 wpa_s->scan_start_time.usec = 0;
2977 wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
2978 diff.sec, diff.usec);
2979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002981 wpa_s->own_scan_running = 0;
2982 wpa_s->external_scan_running = 0;
2983 radio_work_check_next(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 break;
2985#endif /* CONFIG_NO_SCAN_PROCESSING */
2986 case EVENT_ASSOCINFO:
2987 wpa_supplicant_event_associnfo(wpa_s, data);
2988 break;
2989 case EVENT_INTERFACE_STATUS:
2990 wpa_supplicant_event_interface_status(wpa_s, data);
2991 break;
2992 case EVENT_PMKID_CANDIDATE:
2993 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2994 break;
2995#ifdef CONFIG_PEERKEY
2996 case EVENT_STKSTART:
2997 wpa_supplicant_event_stkstart(wpa_s, data);
2998 break;
2999#endif /* CONFIG_PEERKEY */
3000#ifdef CONFIG_TDLS
3001 case EVENT_TDLS:
3002 wpa_supplicant_event_tdls(wpa_s, data);
3003 break;
3004#endif /* CONFIG_TDLS */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003005#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006 case EVENT_WNM:
3007 wpa_supplicant_event_wnm(wpa_s, data);
3008 break;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003009#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010#ifdef CONFIG_IEEE80211R
3011 case EVENT_FT_RESPONSE:
3012 wpa_supplicant_event_ft_response(wpa_s, data);
3013 break;
3014#endif /* CONFIG_IEEE80211R */
3015#ifdef CONFIG_IBSS_RSN
3016 case EVENT_IBSS_RSN_START:
3017 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3018 break;
3019#endif /* CONFIG_IBSS_RSN */
3020 case EVENT_ASSOC_REJECT:
3021 if (data->assoc_reject.bssid)
3022 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3023 "bssid=" MACSTR " status_code=%u",
3024 MAC2STR(data->assoc_reject.bssid),
3025 data->assoc_reject.status_code);
3026 else
3027 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3028 "status_code=%u",
3029 data->assoc_reject.status_code);
3030 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3031 sme_event_assoc_reject(wpa_s, data);
Jeff Johnsonb485b182012-10-21 18:19:27 -07003032 else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003033 const u8 *bssid = data->assoc_reject.bssid;
3034 if (bssid == NULL || is_zero_ether_addr(bssid))
3035 bssid = wpa_s->pending_bssid;
3036 wpas_connection_failed(wpa_s, bssid);
3037 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003038 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 break;
3040 case EVENT_AUTH_TIMED_OUT:
3041 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3042 sme_event_auth_timed_out(wpa_s, data);
3043 break;
3044 case EVENT_ASSOC_TIMED_OUT:
3045 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3046 sme_event_assoc_timed_out(wpa_s, data);
3047 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 case EVENT_TX_STATUS:
3049 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3050 " type=%d stype=%d",
3051 MAC2STR(data->tx_status.dst),
3052 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003053#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003055#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3057 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003058 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 wpa_s, data->tx_status.dst,
3060 data->tx_status.data,
3061 data->tx_status.data_len,
3062 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003063 OFFCHANNEL_SEND_ACTION_SUCCESS :
3064 OFFCHANNEL_SEND_ACTION_NO_ACK);
3065#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003066 break;
3067 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003068#endif /* CONFIG_AP */
3069#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3071 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3072 /*
3073 * Catch TX status events for Action frames we sent via group
3074 * interface in GO mode.
3075 */
3076 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3077 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3078 os_memcmp(wpa_s->parent->pending_action_dst,
3079 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003080 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 wpa_s->parent, data->tx_status.dst,
3082 data->tx_status.data,
3083 data->tx_status.data_len,
3084 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003085 OFFCHANNEL_SEND_ACTION_SUCCESS :
3086 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003087 break;
3088 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003089#endif /* CONFIG_OFFCHANNEL */
3090#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091 switch (data->tx_status.type) {
3092 case WLAN_FC_TYPE_MGMT:
3093 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3094 data->tx_status.data_len,
3095 data->tx_status.stype,
3096 data->tx_status.ack);
3097 break;
3098 case WLAN_FC_TYPE_DATA:
3099 ap_tx_status(wpa_s, data->tx_status.dst,
3100 data->tx_status.data,
3101 data->tx_status.data_len,
3102 data->tx_status.ack);
3103 break;
3104 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105#endif /* CONFIG_AP */
3106 break;
3107#ifdef CONFIG_AP
3108 case EVENT_EAPOL_TX_STATUS:
3109 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3110 data->eapol_tx_status.data,
3111 data->eapol_tx_status.data_len,
3112 data->eapol_tx_status.ack);
3113 break;
3114 case EVENT_DRIVER_CLIENT_POLL_OK:
3115 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 break;
3117 case EVENT_RX_FROM_UNKNOWN:
3118 if (wpa_s->ap_iface == NULL)
3119 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003120 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3121 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003123 case EVENT_CH_SWITCH:
3124 if (!data)
3125 break;
3126 if (!wpa_s->ap_iface) {
3127 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3128 "event in non-AP mode");
3129 break;
3130 }
3131
Dmitry Shmidt04949592012-07-19 12:16:46 -07003132 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3133 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003134 data->ch_switch.ch_offset,
3135 data->ch_switch.ch_width,
3136 data->ch_switch.cf1,
3137 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003138 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003139#endif /* CONFIG_AP */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003140 case EVENT_RX_MGMT: {
3141 u16 fc, stype;
3142 const struct ieee80211_mgmt *mgmt;
3143
3144 mgmt = (const struct ieee80211_mgmt *)
3145 data->rx_mgmt.frame;
3146 fc = le_to_host16(mgmt->frame_control);
3147 stype = WLAN_FC_GET_STYPE(fc);
3148
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003149#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003151#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003152#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003153 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3154 data->rx_mgmt.frame_len > 24) {
3155 const u8 *src = mgmt->sa;
3156 const u8 *ie = mgmt->u.probe_req.variable;
3157 size_t ie_len = data->rx_mgmt.frame_len -
3158 (mgmt->u.probe_req.variable -
3159 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003160 wpas_p2p_probe_req_rx(
3161 wpa_s, src, mgmt->da,
3162 mgmt->bssid, ie, ie_len,
3163 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 break;
3165 }
3166#endif /* CONFIG_P2P */
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003167#ifdef CONFIG_IBSS_RSN
3168 if (stype == WLAN_FC_STYPE_AUTH &&
3169 data->rx_mgmt.frame_len >= 30) {
3170 wpa_supplicant_event_ibss_auth(wpa_s, data);
3171 break;
3172 }
3173#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003174
3175 if (stype == WLAN_FC_STYPE_ACTION) {
3176 wpas_event_rx_mgmt_action(
3177 wpa_s, mgmt, data->rx_mgmt.frame_len,
3178 data->rx_mgmt.freq);
3179 break;
3180 }
3181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3183 "management frame in non-AP mode");
3184 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003185#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003187
3188 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3189 data->rx_mgmt.frame_len > 24) {
3190 const u8 *ie = mgmt->u.probe_req.variable;
3191 size_t ie_len = data->rx_mgmt.frame_len -
3192 (mgmt->u.probe_req.variable -
3193 data->rx_mgmt.frame);
3194
3195 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3196 mgmt->bssid, ie, ie_len,
3197 data->rx_mgmt.ssi_signal);
3198 }
3199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003201#endif /* CONFIG_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003202 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003203 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 case EVENT_RX_PROBE_REQ:
3205 if (data->rx_probe_req.sa == NULL ||
3206 data->rx_probe_req.ie == NULL)
3207 break;
3208#ifdef CONFIG_AP
3209 if (wpa_s->ap_iface) {
3210 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3211 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003212 data->rx_probe_req.da,
3213 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003215 data->rx_probe_req.ie_len,
3216 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217 break;
3218 }
3219#endif /* CONFIG_AP */
3220#ifdef CONFIG_P2P
3221 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003222 data->rx_probe_req.da,
3223 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003225 data->rx_probe_req.ie_len,
3226 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227#endif /* CONFIG_P2P */
3228 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003230#ifdef CONFIG_OFFCHANNEL
3231 offchannel_remain_on_channel_cb(
3232 wpa_s, data->remain_on_channel.freq,
3233 data->remain_on_channel.duration);
3234#endif /* CONFIG_OFFCHANNEL */
3235#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 wpas_p2p_remain_on_channel_cb(
3237 wpa_s, data->remain_on_channel.freq,
3238 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003239#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240 break;
3241 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003242#ifdef CONFIG_OFFCHANNEL
3243 offchannel_cancel_remain_on_channel_cb(
3244 wpa_s, data->remain_on_channel.freq);
3245#endif /* CONFIG_OFFCHANNEL */
3246#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 wpas_p2p_cancel_remain_on_channel_cb(
3248 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003249#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003250 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 case EVENT_EAPOL_RX:
3252 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3253 data->eapol_rx.data,
3254 data->eapol_rx.data_len);
3255 break;
3256 case EVENT_SIGNAL_CHANGE:
3257 bgscan_notify_signal_change(
3258 wpa_s, data->signal_change.above_threshold,
3259 data->signal_change.current_signal,
3260 data->signal_change.current_noise,
3261 data->signal_change.current_txrate);
3262 break;
3263 case EVENT_INTERFACE_ENABLED:
3264 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3265 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003266 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267#ifdef CONFIG_AP
3268 if (!wpa_s->ap_iface) {
3269 wpa_supplicant_set_state(wpa_s,
3270 WPA_DISCONNECTED);
3271 wpa_supplicant_req_scan(wpa_s, 0, 0);
3272 } else
3273 wpa_supplicant_set_state(wpa_s,
3274 WPA_COMPLETED);
3275#else /* CONFIG_AP */
3276 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3277 wpa_supplicant_req_scan(wpa_s, 0, 0);
3278#endif /* CONFIG_AP */
3279 }
3280 break;
3281 case EVENT_INTERFACE_DISABLED:
3282 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003283#ifdef CONFIG_P2P
3284 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3285 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3286 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3287 /*
3288 * The interface was externally disabled. Remove
3289 * it assuming an external entity will start a
3290 * new session if needed.
3291 */
3292 wpas_p2p_disconnect(wpa_s);
3293 break;
3294 }
3295#endif /* CONFIG_P2P */
3296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003298 radio_remove_works(wpa_s, NULL, 0);
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3301 break;
3302 case EVENT_CHANNEL_LIST_CHANGED:
Dmitry Shmidtcce06662013-11-04 18:44:24 -08003303 wpa_supplicant_update_channel_list(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 break;
3305 case EVENT_INTERFACE_UNAVAILABLE:
3306#ifdef CONFIG_P2P
3307 wpas_p2p_interface_unavailable(wpa_s);
3308#endif /* CONFIG_P2P */
3309 break;
3310 case EVENT_BEST_CHANNEL:
3311 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3312 "(%d %d %d)",
3313 data->best_chan.freq_24, data->best_chan.freq_5,
3314 data->best_chan.freq_overall);
3315 wpa_s->best_24_freq = data->best_chan.freq_24;
3316 wpa_s->best_5_freq = data->best_chan.freq_5;
3317 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3318#ifdef CONFIG_P2P
3319 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3320 data->best_chan.freq_5,
3321 data->best_chan.freq_overall);
3322#endif /* CONFIG_P2P */
3323 break;
3324 case EVENT_UNPROT_DEAUTH:
3325 wpa_supplicant_event_unprot_deauth(wpa_s,
3326 &data->unprot_deauth);
3327 break;
3328 case EVENT_UNPROT_DISASSOC:
3329 wpa_supplicant_event_unprot_disassoc(wpa_s,
3330 &data->unprot_disassoc);
3331 break;
3332 case EVENT_STATION_LOW_ACK:
3333#ifdef CONFIG_AP
3334 if (wpa_s->ap_iface && data)
3335 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3336 data->low_ack.addr);
3337#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003338#ifdef CONFIG_TDLS
3339 if (data)
3340 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
3341#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003342 break;
3343 case EVENT_IBSS_PEER_LOST:
3344#ifdef CONFIG_IBSS_RSN
3345 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3346#endif /* CONFIG_IBSS_RSN */
3347 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003348 case EVENT_DRIVER_GTK_REKEY:
3349 if (os_memcmp(data->driver_gtk_rekey.bssid,
3350 wpa_s->bssid, ETH_ALEN))
3351 break;
3352 if (!wpa_s->wpa)
3353 break;
3354 wpa_sm_update_replay_ctr(wpa_s->wpa,
3355 data->driver_gtk_rekey.replay_ctr);
3356 break;
3357 case EVENT_SCHED_SCAN_STOPPED:
Dmitry Shmidt18463232014-01-24 12:29:41 -08003358 wpa_s->pno = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003359 wpa_s->sched_scanning = 0;
3360 wpa_supplicant_notify_scanning(wpa_s, 0);
3361
3362 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3363 break;
3364
3365 /*
Dmitry Shmidt18463232014-01-24 12:29:41 -08003366 * Start a new sched scan to continue searching for more SSIDs
3367 * either if timed out or PNO schedule scan is pending.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 */
Dmitry Shmidt18463232014-01-24 12:29:41 -08003369 if (wpa_s->sched_scan_timed_out || wpa_s->pno_sched_pending) {
3370
3371 if (wpa_supplicant_req_sched_scan(wpa_s) < 0 &&
3372 wpa_s->pno_sched_pending) {
3373 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
3374 } else if (wpa_s->pno_sched_pending) {
3375 wpa_s->pno_sched_pending = 0;
3376 wpa_s->pno = 1;
3377 }
3378 }
3379
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003380 break;
3381 case EVENT_WPS_BUTTON_PUSHED:
3382#ifdef CONFIG_WPS
3383 wpas_wps_start_pbc(wpa_s, NULL, 0);
3384#endif /* CONFIG_WPS */
3385 break;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003386 case EVENT_AVOID_FREQUENCIES:
3387 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3388 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08003389 case EVENT_CONNECT_FAILED_REASON:
3390#ifdef CONFIG_AP
3391 if (!wpa_s->ap_iface || !data)
3392 break;
3393 hostapd_event_connect_failed_reason(
3394 wpa_s->ap_iface->bss[0],
3395 data->connect_failed_reason.addr,
3396 data->connect_failed_reason.code);
3397#endif /* CONFIG_AP */
3398 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 default:
3400 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3401 break;
3402 }
3403}