blob: fdcbcc210ae29b40d3703e5218294cf55b6fb046 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Driver event processing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2003-2012, 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
47static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
48 struct wpa_ssid *ssid)
49{
50 struct os_time now;
51
52 if (ssid == NULL || ssid->disabled_until.sec == 0)
53 return 0;
54
55 os_get_time(&now);
56 if (ssid->disabled_until.sec > now.sec)
57 return ssid->disabled_until.sec - now.sec;
58
59 wpas_clear_temp_disabled(wpa_s, ssid, 0);
60
61 return 0;
62}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063
64
65static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
66{
67 struct wpa_ssid *ssid, *old_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070068 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069
70 if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
71 return 0;
72
73 wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
74 "information");
75 ssid = wpa_supplicant_get_ssid(wpa_s);
76 if (ssid == NULL) {
77 wpa_msg(wpa_s, MSG_INFO,
78 "No network configuration found for the current AP");
79 return -1;
80 }
81
Dmitry Shmidt04949592012-07-19 12:16:46 -070082 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
84 return -1;
85 }
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087 res = wpas_temp_disabled(wpa_s, ssid);
88 if (res > 0) {
89 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
90 "disabled for %d second(s)", res);
91 return -1;
92 }
93
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
95 "current AP");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080096 if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 u8 wpa_ie[80];
98 size_t wpa_ie_len = sizeof(wpa_ie);
99 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
100 wpa_ie, &wpa_ie_len);
101 } else {
102 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
103 }
104
105 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
106 eapol_sm_invalidate_cached_session(wpa_s->eapol);
107 old_ssid = wpa_s->current_ssid;
108 wpa_s->current_ssid = ssid;
109 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
110 wpa_supplicant_initiate_eapol(wpa_s);
111 if (old_ssid != wpa_s->current_ssid)
112 wpas_notify_network_changed(wpa_s);
113
114 return 0;
115}
116
117
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800118void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
120 struct wpa_supplicant *wpa_s = eloop_ctx;
121
122 if (wpa_s->countermeasures) {
123 wpa_s->countermeasures = 0;
124 wpa_drv_set_countermeasures(wpa_s, 0);
125 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
126 wpa_supplicant_req_scan(wpa_s, 0, 0);
127 }
128}
129
130
131void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
132{
133 int bssid_changed;
134
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 wnm_bss_keep_alive_deinit(wpa_s);
136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137#ifdef CONFIG_IBSS_RSN
138 ibss_rsn_deinit(wpa_s->ibss_rsn);
139 wpa_s->ibss_rsn = NULL;
140#endif /* CONFIG_IBSS_RSN */
141
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700142#ifdef CONFIG_AP
143 wpa_supplicant_ap_deinit(wpa_s);
144#endif /* CONFIG_AP */
145
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
147 return;
148
149 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800150#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -0700151 wpa_s->conf->ap_scan = DEFAULT_AP_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800152#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
154 os_memset(wpa_s->bssid, 0, ETH_ALEN);
155 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700156#ifdef CONFIG_SME
157 wpa_s->sme.prev_bssid_set = 0;
158#endif /* CONFIG_SME */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800159#ifdef CONFIG_P2P
160 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
161#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 wpa_s->current_bss = NULL;
163 wpa_s->assoc_freq = 0;
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700164#ifdef CONFIG_IEEE80211R
165#ifdef CONFIG_SME
166 if (wpa_s->sme.ft_ies)
167 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
168#endif /* CONFIG_SME */
169#endif /* CONFIG_IEEE80211R */
170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171 if (bssid_changed)
172 wpas_notify_bssid_changed(wpa_s);
173
174 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
175 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
176 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
177 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
178 wpa_s->ap_ies_from_associnfo = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700179 wpa_s->current_ssid = NULL;
180 wpa_s->key_mgmt = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181}
182
183
184static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
185{
186 struct wpa_ie_data ie;
187 int pmksa_set = -1;
188 size_t i;
189
190 if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
191 ie.pmkid == NULL)
192 return;
193
194 for (i = 0; i < ie.num_pmkid; i++) {
195 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
196 ie.pmkid + i * PMKID_LEN,
197 NULL, NULL, 0);
198 if (pmksa_set == 0) {
199 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
200 break;
201 }
202 }
203
204 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
205 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
206}
207
208
209static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
210 union wpa_event_data *data)
211{
212 if (data == NULL) {
213 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
214 "event");
215 return;
216 }
217 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
218 " index=%d preauth=%d",
219 MAC2STR(data->pmkid_candidate.bssid),
220 data->pmkid_candidate.index,
221 data->pmkid_candidate.preauth);
222
223 pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
224 data->pmkid_candidate.index,
225 data->pmkid_candidate.preauth);
226}
227
228
229static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
230{
231 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
232 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
233 return 0;
234
235#ifdef IEEE8021X_EAPOL
236 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
237 wpa_s->current_ssid &&
238 !(wpa_s->current_ssid->eapol_flags &
239 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
240 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
241 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
242 * plaintext or static WEP keys). */
243 return 0;
244 }
245#endif /* IEEE8021X_EAPOL */
246
247 return 1;
248}
249
250
251/**
252 * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
253 * @wpa_s: pointer to wpa_supplicant data
254 * @ssid: Configuration data for the network
255 * Returns: 0 on success, -1 on failure
256 *
257 * This function is called when starting authentication with a network that is
258 * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
259 */
260int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
261 struct wpa_ssid *ssid)
262{
263#ifdef IEEE8021X_EAPOL
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264#ifdef PCSC_FUNCS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 int aka = 0, sim = 0, type;
266
267 if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
268 return 0;
269
270 if (ssid->eap.eap_methods == NULL) {
271 sim = 1;
272 aka = 1;
273 } else {
274 struct eap_method_type *eap = ssid->eap.eap_methods;
275 while (eap->vendor != EAP_VENDOR_IETF ||
276 eap->method != EAP_TYPE_NONE) {
277 if (eap->vendor == EAP_VENDOR_IETF) {
278 if (eap->method == EAP_TYPE_SIM)
279 sim = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700280 else if (eap->method == EAP_TYPE_AKA ||
281 eap->method == EAP_TYPE_AKA_PRIME)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282 aka = 1;
283 }
284 eap++;
285 }
286 }
287
288 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
289 sim = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700290 if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
291 eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
292 NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 aka = 0;
294
295 if (!sim && !aka) {
296 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
297 "use SIM, but neither EAP-SIM nor EAP-AKA are "
298 "enabled");
299 return 0;
300 }
301
302 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
303 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
304 if (sim && aka)
305 type = SCARD_TRY_BOTH;
306 else if (aka)
307 type = SCARD_USIM_ONLY;
308 else
309 type = SCARD_GSM_SIM_ONLY;
310
Dmitry Shmidt04949592012-07-19 12:16:46 -0700311 wpa_s->scard = scard_init(type, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 if (wpa_s->scard == NULL) {
313 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
314 "(pcsc-lite)");
315 return -1;
316 }
317 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
318 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319#endif /* PCSC_FUNCS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320#endif /* IEEE8021X_EAPOL */
321
322 return 0;
323}
324
325
326#ifndef CONFIG_NO_SCAN_PROCESSING
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700327static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 struct wpa_ssid *ssid)
329{
330 int i, privacy = 0;
331
332 if (ssid->mixed_cell)
333 return 1;
334
335#ifdef CONFIG_WPS
336 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
337 return 1;
338#endif /* CONFIG_WPS */
339
340 for (i = 0; i < NUM_WEP_KEYS; i++) {
341 if (ssid->wep_key_len[i]) {
342 privacy = 1;
343 break;
344 }
345 }
346#ifdef IEEE8021X_EAPOL
347 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
348 ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
349 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
350 privacy = 1;
351#endif /* IEEE8021X_EAPOL */
352
Jouni Malinen75ecf522011-06-27 15:19:46 -0700353 if (wpa_key_mgmt_wpa(ssid->key_mgmt))
354 privacy = 1;
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 if (bss->caps & IEEE80211_CAP_PRIVACY)
357 return privacy;
358 return !privacy;
359}
360
361
362static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
363 struct wpa_ssid *ssid,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700364 struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365{
366 struct wpa_ie_data ie;
367 int proto_match = 0;
368 const u8 *rsn_ie, *wpa_ie;
369 int ret;
370 int wep_ok;
371
372 ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
373 if (ret >= 0)
374 return ret;
375
376 /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
377 wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
378 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
379 ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
380 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
381
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700382 rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
384 proto_match++;
385
386 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
387 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - parse "
388 "failed");
389 break;
390 }
391
392 if (wep_ok &&
393 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
394 {
395 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
396 "in RSN IE");
397 return 1;
398 }
399
400 if (!(ie.proto & ssid->proto)) {
401 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - proto "
402 "mismatch");
403 break;
404 }
405
406 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
407 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - PTK "
408 "cipher mismatch");
409 break;
410 }
411
412 if (!(ie.group_cipher & ssid->group_cipher)) {
413 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - GTK "
414 "cipher mismatch");
415 break;
416 }
417
418 if (!(ie.key_mgmt & ssid->key_mgmt)) {
419 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - key mgmt "
420 "mismatch");
421 break;
422 }
423
424#ifdef CONFIG_IEEE80211W
425 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
426 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
427 wpa_dbg(wpa_s, MSG_DEBUG, " skip RSN IE - no mgmt "
428 "frame protection");
429 break;
430 }
431#endif /* CONFIG_IEEE80211W */
432
433 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on RSN IE");
434 return 1;
435 }
436
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700437 wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438 while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
439 proto_match++;
440
441 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
442 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - parse "
443 "failed");
444 break;
445 }
446
447 if (wep_ok &&
448 (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
449 {
450 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on TSN "
451 "in WPA IE");
452 return 1;
453 }
454
455 if (!(ie.proto & ssid->proto)) {
456 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - proto "
457 "mismatch");
458 break;
459 }
460
461 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
462 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - PTK "
463 "cipher mismatch");
464 break;
465 }
466
467 if (!(ie.group_cipher & ssid->group_cipher)) {
468 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - GTK "
469 "cipher mismatch");
470 break;
471 }
472
473 if (!(ie.key_mgmt & ssid->key_mgmt)) {
474 wpa_dbg(wpa_s, MSG_DEBUG, " skip WPA IE - key mgmt "
475 "mismatch");
476 break;
477 }
478
479 wpa_dbg(wpa_s, MSG_DEBUG, " selected based on WPA IE");
480 return 1;
481 }
482
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483 if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
484 !rsn_ie) {
485 wpa_dbg(wpa_s, MSG_DEBUG, " allow for non-WPA IEEE 802.1X");
486 return 1;
487 }
488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
490 wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
491 wpa_dbg(wpa_s, MSG_DEBUG, " skip - no WPA/RSN proto match");
492 return 0;
493 }
494
495 if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
496 wpa_dbg(wpa_s, MSG_DEBUG, " allow in non-WPA/WPA2");
497 return 1;
498 }
499
500 wpa_dbg(wpa_s, MSG_DEBUG, " reject due to mismatch with "
501 "WPA/WPA2");
502
503 return 0;
504}
505
506
507static int freq_allowed(int *freqs, int freq)
508{
509 int i;
510
511 if (freqs == NULL)
512 return 1;
513
514 for (i = 0; freqs[i]; i++)
515 if (freqs[i] == freq)
516 return 1;
517 return 0;
518}
519
520
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800521static int ht_supported(const struct hostapd_hw_modes *mode)
522{
523 if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
524 /*
525 * The driver did not indicate whether it supports HT. Assume
526 * it does to avoid connection issues.
527 */
528 return 1;
529 }
530
531 /*
532 * IEEE Std 802.11n-2009 20.1.1:
533 * An HT non-AP STA shall support all EQM rates for one spatial stream.
534 */
535 return mode->mcs_set[0] == 0xff;
536}
537
538
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700539static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800540{
541 const struct hostapd_hw_modes *mode = NULL, *modes;
542 const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
543 const u8 *rate_ie;
544 int i, j, k;
545
546 if (bss->freq == 0)
547 return 1; /* Cannot do matching without knowing band */
548
549 modes = wpa_s->hw.modes;
550 if (modes == NULL) {
551 /*
552 * The driver does not provide any additional information
553 * about the utilized hardware, so allow the connection attempt
554 * to continue.
555 */
556 return 1;
557 }
558
559 for (i = 0; i < wpa_s->hw.num_modes; i++) {
560 for (j = 0; j < modes[i].num_channels; j++) {
561 int freq = modes[i].channels[j].freq;
562 if (freq == bss->freq) {
563 if (mode &&
564 mode->mode == HOSTAPD_MODE_IEEE80211G)
565 break; /* do not allow 802.11b replace
566 * 802.11g */
567 mode = &modes[i];
568 break;
569 }
570 }
571 }
572
573 if (mode == NULL)
574 return 0;
575
576 for (i = 0; i < (int) sizeof(scan_ie); i++) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700577 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800578 if (rate_ie == NULL)
579 continue;
580
581 for (j = 2; j < rate_ie[1] + 2; j++) {
582 int flagged = !!(rate_ie[j] & 0x80);
583 int r = (rate_ie[j] & 0x7f) * 5;
584
585 /*
586 * IEEE Std 802.11n-2009 7.3.2.2:
587 * The new BSS Membership selector value is encoded
588 * like a legacy basic rate, but it is not a rate and
589 * only indicates if the BSS members are required to
590 * support the mandatory features of Clause 20 [HT PHY]
591 * in order to join the BSS.
592 */
593 if (flagged && ((rate_ie[j] & 0x7f) ==
594 BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
595 if (!ht_supported(mode)) {
596 wpa_dbg(wpa_s, MSG_DEBUG,
597 " hardware does not support "
598 "HT PHY");
599 return 0;
600 }
601 continue;
602 }
603
604 if (!flagged)
605 continue;
606
607 /* check for legacy basic rates */
608 for (k = 0; k < mode->num_rates; k++) {
609 if (mode->rates[k] == r)
610 break;
611 }
612 if (k == mode->num_rates) {
613 /*
614 * IEEE Std 802.11-2007 7.3.2.2 demands that in
615 * order to join a BSS all required rates
616 * have to be supported by the hardware.
617 */
618 wpa_dbg(wpa_s, MSG_DEBUG, " hardware does "
619 "not support required rate %d.%d Mbps",
620 r / 10, r % 10);
621 return 0;
622 }
623 }
624 }
625
626 return 1;
627}
628
629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700631 int i, struct wpa_bss *bss,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 struct wpa_ssid *group)
633{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700634 u8 wpa_ie_len, rsn_ie_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 int wpa;
636 struct wpa_blacklist *e;
637 const u8 *ie;
638 struct wpa_ssid *ssid;
639
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700640 ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 wpa_ie_len = ie ? ie[1] : 0;
642
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700643 ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 rsn_ie_len = ie ? ie[1] : 0;
645
646 wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
647 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700648 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700650 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651
652 e = wpa_blacklist_get(wpa_s, bss->bssid);
653 if (e) {
654 int limit = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700655 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 /*
657 * When only a single network is enabled, we can
658 * trigger blacklisting on the first failure. This
659 * should not be done with multiple enabled networks to
660 * avoid getting forced to move into a worse ESS on
661 * single error if there are no other BSSes of the
662 * current ESS.
663 */
664 limit = 0;
665 }
666 if (e->count > limit) {
667 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
668 "(count=%d limit=%d)", e->count, limit);
669 return NULL;
670 }
671 }
672
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700673 if (bss->ssid_len == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID not known");
675 return NULL;
676 }
677
678 wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
679
680 for (ssid = group; ssid; ssid = ssid->pnext) {
681 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700682 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683
Dmitry Shmidt04949592012-07-19 12:16:46 -0700684 if (wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled");
686 continue;
687 }
688
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700689 res = wpas_temp_disabled(wpa_s, ssid);
690 if (res > 0) {
691 wpa_dbg(wpa_s, MSG_DEBUG, " skip - disabled "
692 "temporarily for %d second(s)", res);
693 continue;
694 }
695
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696#ifdef CONFIG_WPS
697 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
698 wpa_dbg(wpa_s, MSG_DEBUG, " skip - blacklisted "
699 "(WPS)");
700 continue;
701 }
702
703 if (wpa && ssid->ssid_len == 0 &&
704 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
705 check_ssid = 0;
706
707 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
708 /* Only allow wildcard SSID match if an AP
709 * advertises active WPS operation that matches
710 * with our mode. */
711 check_ssid = 1;
712 if (ssid->ssid_len == 0 &&
713 wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
714 check_ssid = 0;
715 }
716#endif /* CONFIG_WPS */
717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800718 if (ssid->bssid_set && ssid->ssid_len == 0 &&
719 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
720 check_ssid = 0;
721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 if (check_ssid &&
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700723 (bss->ssid_len != ssid->ssid_len ||
724 os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 wpa_dbg(wpa_s, MSG_DEBUG, " skip - SSID mismatch");
726 continue;
727 }
728
729 if (ssid->bssid_set &&
730 os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
731 wpa_dbg(wpa_s, MSG_DEBUG, " skip - BSSID mismatch");
732 continue;
733 }
734
735 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
736 continue;
737
738 if (!wpa &&
739 !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
740 !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
741 !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
742 wpa_dbg(wpa_s, MSG_DEBUG, " skip - non-WPA network "
743 "not allowed");
744 continue;
745 }
746
Jouni Malinen75ecf522011-06-27 15:19:46 -0700747 if (!wpa_supplicant_match_privacy(bss, ssid)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 wpa_dbg(wpa_s, MSG_DEBUG, " skip - privacy "
749 "mismatch");
750 continue;
751 }
752
Jouni Malinen75ecf522011-06-27 15:19:46 -0700753 if (bss->caps & IEEE80211_CAP_IBSS) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 wpa_dbg(wpa_s, MSG_DEBUG, " skip - IBSS (adhoc) "
755 "network");
756 continue;
757 }
758
759 if (!freq_allowed(ssid->freq_list, bss->freq)) {
760 wpa_dbg(wpa_s, MSG_DEBUG, " skip - frequency not "
761 "allowed");
762 continue;
763 }
764
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800765 if (!rate_match(wpa_s, bss)) {
766 wpa_dbg(wpa_s, MSG_DEBUG, " skip - rate sets do "
767 "not match");
768 continue;
769 }
770
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771#ifdef CONFIG_P2P
772 /*
773 * TODO: skip the AP if its P2P IE has Group Formation
774 * bit set in the P2P Group Capability Bitmap and we
775 * are not in Group Formation with that device.
776 */
777#endif /* CONFIG_P2P */
778
779 /* Matching configuration found */
780 return ssid;
781 }
782
783 /* No matching configuration found */
784 return NULL;
785}
786
787
788static struct wpa_bss *
789wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 struct wpa_ssid *group,
791 struct wpa_ssid **selected_ssid)
792{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700793 unsigned int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794
795 wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
796 group->priority);
797
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700798 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
799 struct wpa_bss *bss = wpa_s->last_scan_res[i];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
801 if (!*selected_ssid)
802 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 wpa_dbg(wpa_s, MSG_DEBUG, " selected BSS " MACSTR
804 " ssid='%s'",
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700805 MAC2STR(bss->bssid),
806 wpa_ssid_txt(bss->ssid, bss->ssid_len));
807 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 }
809
810 return NULL;
811}
812
813
814static struct wpa_bss *
815wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 struct wpa_ssid **selected_ssid)
817{
818 struct wpa_bss *selected = NULL;
819 int prio;
820
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700821 if (wpa_s->last_scan_res == NULL ||
822 wpa_s->last_scan_res_used == 0)
823 return NULL; /* no scan results from last update */
824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 while (selected == NULL) {
826 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
827 selected = wpa_supplicant_select_bss(
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700828 wpa_s, wpa_s->conf->pssid[prio],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 selected_ssid);
830 if (selected)
831 break;
832 }
833
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800834 if (selected == NULL && wpa_s->blacklist &&
835 !wpa_s->countermeasures) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
837 "blacklist and try again");
838 wpa_blacklist_clear(wpa_s);
839 wpa_s->blacklist_cleared++;
840 } else if (selected == NULL)
841 break;
842 }
843
844 return selected;
845}
846
847
848static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
849 int timeout_sec, int timeout_usec)
850{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700851 if (!wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 /*
853 * No networks are enabled; short-circuit request so
854 * we don't wait timeout seconds before transitioning
855 * to INACTIVE state.
856 */
857 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
858 return;
859 }
860 wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
861}
862
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800863
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700864int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800865 struct wpa_bss *selected,
866 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867{
868 if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
869 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
870 "PBC session overlap");
871#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700873 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874#endif /* CONFIG_P2P */
875
876#ifdef CONFIG_WPS
877 wpas_wps_cancel(wpa_s);
878#endif /* CONFIG_WPS */
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700879 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 }
881
882 /*
883 * Do not trigger new association unless the BSSID has changed or if
884 * reassociation is requested. If we are in process of associating with
885 * the selected BSSID, do not trigger new attempt.
886 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887 if (wpa_s->reassociate ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
889 ((wpa_s->wpa_state != WPA_ASSOCIATING &&
890 wpa_s->wpa_state != WPA_AUTHENTICATING) ||
891 os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
892 0))) {
893 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
894 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700895 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
898 "reassociate: %d selected: "MACSTR " bssid: " MACSTR
899 " pending: " MACSTR " wpa_state: %s",
900 wpa_s->reassociate, MAC2STR(selected->bssid),
901 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
902 wpa_supplicant_state_txt(wpa_s->wpa_state));
903 wpa_supplicant_associate(wpa_s, selected, ssid);
904 } else {
905 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
906 "selected AP");
907 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800908
Dmitry Shmidt44da0252011-08-23 12:30:30 -0700909 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910}
911
912
913static struct wpa_ssid *
914wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
915{
916 int prio;
917 struct wpa_ssid *ssid;
918
919 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
920 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
921 {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700922 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 continue;
924 if (ssid->mode == IEEE80211_MODE_IBSS ||
925 ssid->mode == IEEE80211_MODE_AP)
926 return ssid;
927 }
928 }
929 return NULL;
930}
931
932
933/* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
934 * on BSS added and BSS changed events */
935static void wpa_supplicant_rsn_preauth_scan_results(
Jouni Malinen87fd2792011-05-16 18:35:42 +0300936 struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937{
Jouni Malinen87fd2792011-05-16 18:35:42 +0300938 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939
940 if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
941 return;
942
Jouni Malinen87fd2792011-05-16 18:35:42 +0300943 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 const u8 *ssid, *rsn;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945
Jouni Malinen87fd2792011-05-16 18:35:42 +0300946 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (ssid == NULL)
948 continue;
949
Jouni Malinen87fd2792011-05-16 18:35:42 +0300950 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 if (rsn == NULL)
952 continue;
953
Jouni Malinen87fd2792011-05-16 18:35:42 +0300954 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 }
956
957}
958
959
960static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
961 struct wpa_bss *selected,
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700962 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700964 struct wpa_bss *current_bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 int min_diff;
966
967 if (wpa_s->reassociate)
968 return 1; /* explicit request to reassociate */
969 if (wpa_s->wpa_state < WPA_ASSOCIATED)
970 return 1; /* we are not associated; continue */
971 if (wpa_s->current_ssid == NULL)
972 return 1; /* unknown current SSID */
973 if (wpa_s->current_ssid != ssid)
974 return 1; /* different network block */
975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800976 if (wpas_driver_bss_selection(wpa_s))
977 return 0; /* Driver-based roaming */
978
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700979 if (wpa_s->current_ssid->ssid)
980 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
981 wpa_s->current_ssid->ssid,
982 wpa_s->current_ssid->ssid_len);
983 if (!current_bss)
984 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985
986 if (!current_bss)
987 return 1; /* current BSS not seen in scan results */
988
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700989 if (current_bss == selected)
990 return 0;
991
992 if (selected->last_update_idx > current_bss->last_update_idx)
993 return 1; /* current BSS not seen in the last scan */
994
Dmitry Shmidt9e077672012-04-13 10:07:27 -0700995#ifndef CONFIG_NO_ROAMING
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
997 wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
998 MAC2STR(current_bss->bssid), current_bss->level);
999 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1000 MAC2STR(selected->bssid), selected->level);
1001
1002 if (wpa_s->current_ssid->bssid_set &&
1003 os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1004 0) {
1005 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1006 "has preferred BSSID");
1007 return 1;
1008 }
1009
1010 min_diff = 2;
1011 if (current_bss->level < 0) {
1012 if (current_bss->level < -85)
1013 min_diff = 1;
1014 else if (current_bss->level < -80)
1015 min_diff = 2;
1016 else if (current_bss->level < -75)
1017 min_diff = 3;
1018 else if (current_bss->level < -70)
1019 min_diff = 4;
1020 else
1021 min_diff = 5;
1022 }
1023 if (abs(current_bss->level - selected->level) < min_diff) {
1024 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1025 "in signal level");
1026 return 0;
1027 }
1028
1029 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001030#else /* CONFIG_NO_ROAMING */
Dmitry Shmidtefdec2e2011-08-16 11:55:46 -07001031 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001032#endif /* CONFIG_NO_ROAMING */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033}
1034
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001035
Jouni Malinen89ca7022012-09-14 13:03:12 -07001036/* Return != 0 if no scan results could be fetched or if scan results should not
Dmitry Shmidt04949592012-07-19 12:16:46 -07001037 * be shared with other virtual interfaces. */
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001038#ifdef ANDROID_P2P
1039static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1040 union wpa_event_data *data, int suppress_event)
1041#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1043 union wpa_event_data *data)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001044#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 struct wpa_scan_results *scan_res;
1047 int ap = 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001048#ifndef CONFIG_NO_RANDOM_POOL
1049 size_t i, num;
1050#endif /* CONFIG_NO_RANDOM_POOL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051
1052#ifdef CONFIG_AP
1053 if (wpa_s->ap_iface)
1054 ap = 1;
1055#endif /* CONFIG_AP */
1056
1057 wpa_supplicant_notify_scanning(wpa_s, 0);
1058
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001059#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07001060 if (wpa_s->global->p2p_cb_on_scan_complete &&
1061 !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001062 wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07001063 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001064 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1065 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1066 "stopped scan processing");
Jouni Malinenfa08f9e2012-09-13 18:05:55 -07001067 wpa_s->sta_scan_pending = 1;
1068 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001069 return -1;
1070 }
1071 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001072 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001073#endif /* CONFIG_P2P */
1074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 scan_res = wpa_supplicant_get_scan_results(wpa_s,
1076 data ? &data->scan_info :
1077 NULL, 1);
1078 if (scan_res == NULL) {
1079 if (wpa_s->conf->ap_scan == 2 || ap)
1080 return -1;
1081 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1082 "scanning again");
1083 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1084 return -1;
1085 }
1086
1087#ifndef CONFIG_NO_RANDOM_POOL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 num = scan_res->num;
1089 if (num > 10)
1090 num = 10;
1091 for (i = 0; i < num; i++) {
1092 u8 buf[5];
1093 struct wpa_scan_res *res = scan_res->res[i];
1094 buf[0] = res->bssid[5];
1095 buf[1] = res->qual & 0xff;
1096 buf[2] = res->noise & 0xff;
1097 buf[3] = res->level & 0xff;
1098 buf[4] = res->tsf & 0xff;
1099 random_add_randomness(buf, sizeof(buf));
1100 }
1101#endif /* CONFIG_NO_RANDOM_POOL */
1102
1103 if (wpa_s->scan_res_handler) {
1104 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1105 struct wpa_scan_results *scan_res);
1106
1107 scan_res_handler = wpa_s->scan_res_handler;
1108 wpa_s->scan_res_handler = NULL;
1109 scan_res_handler(wpa_s, scan_res);
1110
1111 wpa_scan_results_free(scan_res);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001112 return -2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001113 }
1114
1115 if (ap) {
1116 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1117#ifdef CONFIG_AP
1118 if (wpa_s->ap_iface->scan_cb)
1119 wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1120#endif /* CONFIG_AP */
1121 wpa_scan_results_free(scan_res);
1122 return 0;
1123 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001124#ifdef ANDROID_P2P
1125 if(!suppress_event)
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001126 {
1127 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1128 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1129 wpas_notify_scan_results(wpa_s);
1130 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001131#else
1132 wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1133 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1134 wpas_notify_scan_results(wpa_s);
1135#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136
1137 wpas_notify_scan_done(wpa_s, 1);
1138
Dmitry Shmidt04949592012-07-19 12:16:46 -07001139 if (sme_proc_obss_scan(wpa_s) > 0) {
1140 wpa_scan_results_free(scan_res);
1141 return 0;
1142 }
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1145 wpa_scan_results_free(scan_res);
1146 return 0;
1147 }
1148
Dmitry Shmidt04949592012-07-19 12:16:46 -07001149 if (autoscan_notify_scan(wpa_s, scan_res)) {
1150 wpa_scan_results_free(scan_res);
1151 return 0;
1152 }
1153
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154 if (wpa_s->disconnected) {
1155 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1156 wpa_scan_results_free(scan_res);
1157 return 0;
1158 }
1159
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001160 if (!wpas_driver_bss_selection(wpa_s) &&
1161 bgscan_notify_scan(wpa_s, scan_res) == 1) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 wpa_scan_results_free(scan_res);
1163 return 0;
1164 }
1165
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001166 wpas_wps_update_ap_info(wpa_s, scan_res);
1167
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001168 wpa_scan_results_free(scan_res);
1169
1170 return wpas_select_network_from_last_scan(wpa_s);
1171}
1172
1173
1174int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
1175{
1176 struct wpa_bss *selected;
1177 struct wpa_ssid *ssid = NULL;
1178
1179 selected = wpa_supplicant_pick_network(wpa_s, &ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180
1181 if (selected) {
1182 int skip;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001183 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 if (skip) {
1185 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001187 }
1188
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001189 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
Dmitry Shmidt44da0252011-08-23 12:30:30 -07001191 return -1;
1192 }
Jouni Malinen87fd2792011-05-16 18:35:42 +03001193 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001194 /*
1195 * Do not notify other virtual radios of scan results since we do not
1196 * want them to start other associations at the same time.
1197 */
1198 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1201 ssid = wpa_supplicant_pick_new_network(wpa_s);
1202 if (ssid) {
1203 wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1204 wpa_supplicant_associate(wpa_s, NULL, ssid);
Jouni Malinen87fd2792011-05-16 18:35:42 +03001205 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001206 } else {
1207 int timeout_sec = wpa_s->scan_interval;
1208 int timeout_usec = 0;
1209#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001210 if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1211 return 0;
1212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213 if (wpa_s->p2p_in_provisioning) {
1214 /*
1215 * Use shorter wait during P2P Provisioning
1216 * state to speed up group formation.
1217 */
1218 timeout_sec = 0;
1219 timeout_usec = 250000;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001220 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1221 timeout_usec);
1222 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 }
1224#endif /* CONFIG_P2P */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001225#ifdef CONFIG_INTERWORKING
1226 if (wpa_s->conf->auto_interworking &&
1227 wpa_s->conf->interworking &&
1228 wpa_s->conf->cred) {
1229 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1230 "start ANQP fetch since no matching "
1231 "networks found");
1232 wpa_s->network_select = 1;
1233 wpa_s->auto_network_select = 1;
1234 interworking_start_fetch_anqp(wpa_s);
Jouni Malinen89ca7022012-09-14 13:03:12 -07001235 return 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001236 }
1237#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238 if (wpa_supplicant_req_sched_scan(wpa_s))
1239 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1240 timeout_usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 }
1242 }
1243 return 0;
1244}
1245
1246
1247static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1248 union wpa_event_data *data)
1249{
1250 const char *rn, *rn2;
1251 struct wpa_supplicant *ifs;
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001252#ifdef ANDROID_P2P
Jouni Malinen89ca7022012-09-14 13:03:12 -07001253 if (_wpa_supplicant_event_scan_results(wpa_s, data, 0) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001254#else
Jouni Malinen89ca7022012-09-14 13:03:12 -07001255 if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001256#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 /*
1258 * If no scan results could be fetched, then no need to
1259 * notify those interfaces that did not actually request
Jouni Malinen89ca7022012-09-14 13:03:12 -07001260 * this scan. Similarly, if scan results started a new operation on this
1261 * interface, do not notify other interfaces to avoid concurrent
1262 * operations during a connection attempt.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 */
1264 return;
1265 }
1266
1267 /*
1268 * Check other interfaces to see if they have the same radio-name. If
1269 * so, they get updated with this same scan info.
1270 */
1271 if (!wpa_s->driver->get_radio_name)
1272 return;
1273
1274 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1275 if (rn == NULL || rn[0] == '\0')
1276 return;
1277
1278 wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1279 "sharing same radio (%s) in event_scan_results", rn);
1280
1281 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1282 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1283 continue;
1284
1285 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1286 if (rn2 && os_strcmp(rn, rn2) == 0) {
1287 wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1288 "sibling", ifs->ifname);
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001289#ifdef ANDROID_P2P
1290 if ( (ifs->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) || (ifs->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
1291 /* Do not update the scan results from STA interface to p2p interfaces */
1292 wpa_printf(MSG_DEBUG, "Not Updating scan results on interface %s from "
1293 "sibling %s", ifs->ifname, wpa_s->ifname);
1294 continue;
1295 }
1296 else {
1297 /* P2P_FIND will result in too many SCAN_RESULT_EVENTS within
1298 * no time. Avoid announcing it to application as it may not
1299 * be that useful (since results will be that of only 1,6,11).
1300 * over to any other interface as it
1301 */
1302 if(p2p_search_in_progress(wpa_s->global->p2p))
1303 _wpa_supplicant_event_scan_results(ifs, data, 1);
1304 else
1305 _wpa_supplicant_event_scan_results(ifs, data, 0);
1306 }
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001307#else
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001308 _wpa_supplicant_event_scan_results(ifs, data);
Dmitry Shmidtf6c92c42012-01-26 12:57:43 -08001309#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 }
1311 }
1312}
1313
1314#endif /* CONFIG_NO_SCAN_PROCESSING */
1315
1316
Dmitry Shmidt04949592012-07-19 12:16:46 -07001317#ifdef CONFIG_WNM
1318
1319static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1320{
1321 struct wpa_supplicant *wpa_s = eloop_ctx;
1322
1323 if (wpa_s->wpa_state < WPA_ASSOCIATED)
1324 return;
1325
1326 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1327 MAC2STR(wpa_s->bssid));
1328 /* TODO: could skip this if normal data traffic has been sent */
1329 /* TODO: Consider using some more appropriate data frame for this */
1330 if (wpa_s->l2)
1331 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800, (u8 *) "", 0);
1332
1333#ifdef CONFIG_SME
1334 if (wpa_s->sme.bss_max_idle_period) {
1335 unsigned int msec;
1336 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1337 if (msec > 100)
1338 msec -= 100;
1339 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1340 wnm_bss_keep_alive, wpa_s, NULL);
1341 }
1342#endif /* CONFIG_SME */
1343}
1344
1345
1346static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1347 const u8 *ies, size_t ies_len)
1348{
1349 struct ieee802_11_elems elems;
1350
1351 if (ies == NULL)
1352 return;
1353
1354 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1355 return;
1356
1357#ifdef CONFIG_SME
1358 if (elems.bss_max_idle_period) {
1359 unsigned int msec;
1360 wpa_s->sme.bss_max_idle_period =
1361 WPA_GET_LE16(elems.bss_max_idle_period);
1362 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1363 "TU)%s", wpa_s->sme.bss_max_idle_period,
1364 (elems.bss_max_idle_period[2] & 0x01) ?
1365 " (protected keep-live required)" : "");
1366 if (wpa_s->sme.bss_max_idle_period == 0)
1367 wpa_s->sme.bss_max_idle_period = 1;
1368 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1369 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1370 /* msec times 1000 */
1371 msec = wpa_s->sme.bss_max_idle_period * 1024;
1372 if (msec > 100)
1373 msec -= 100;
1374 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1375 wnm_bss_keep_alive, wpa_s,
1376 NULL);
1377 }
1378 }
1379#endif /* CONFIG_SME */
1380}
1381
1382#endif /* CONFIG_WNM */
1383
1384
1385void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1386{
1387#ifdef CONFIG_WNM
1388 eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1389#endif /* CONFIG_WNM */
1390}
1391
1392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1394 union wpa_event_data *data)
1395{
1396 int l, len, found = 0, wpa_found, rsn_found;
1397 const u8 *p;
1398
1399 wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1400 if (data->assoc_info.req_ies)
1401 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1402 data->assoc_info.req_ies_len);
1403 if (data->assoc_info.resp_ies) {
1404 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1405 data->assoc_info.resp_ies_len);
1406#ifdef CONFIG_TDLS
1407 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1408 data->assoc_info.resp_ies_len);
1409#endif /* CONFIG_TDLS */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001410#ifdef CONFIG_WNM
1411 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1412 data->assoc_info.resp_ies_len);
1413#endif /* CONFIG_WNM */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 }
1415 if (data->assoc_info.beacon_ies)
1416 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1417 data->assoc_info.beacon_ies,
1418 data->assoc_info.beacon_ies_len);
1419 if (data->assoc_info.freq)
1420 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1421 data->assoc_info.freq);
1422
1423 p = data->assoc_info.req_ies;
1424 l = data->assoc_info.req_ies_len;
1425
1426 /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1427 while (p && l >= 2) {
1428 len = p[1] + 2;
1429 if (len > l) {
1430 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1431 p, l);
1432 break;
1433 }
1434 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1435 (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1436 (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1437 if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1438 break;
1439 found = 1;
1440 wpa_find_assoc_pmkid(wpa_s);
1441 break;
1442 }
1443 l -= len;
1444 p += len;
1445 }
1446 if (!found && data->assoc_info.req_ies)
1447 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1448
1449#ifdef CONFIG_IEEE80211R
1450#ifdef CONFIG_SME
1451 if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1452 u8 bssid[ETH_ALEN];
1453 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1454 wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1455 data->assoc_info.resp_ies,
1456 data->assoc_info.resp_ies_len,
1457 bssid) < 0) {
1458 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1459 "Reassociation Response failed");
1460 wpa_supplicant_deauthenticate(
1461 wpa_s, WLAN_REASON_INVALID_IE);
1462 return -1;
1463 }
1464 }
1465
1466 p = data->assoc_info.resp_ies;
1467 l = data->assoc_info.resp_ies_len;
1468
1469#ifdef CONFIG_WPS_STRICT
1470 if (p && wpa_s->current_ssid &&
1471 wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1472 struct wpabuf *wps;
1473 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1474 if (wps == NULL) {
1475 wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1476 "include WPS IE in (Re)Association Response");
1477 return -1;
1478 }
1479
1480 if (wps_validate_assoc_resp(wps) < 0) {
1481 wpabuf_free(wps);
1482 wpa_supplicant_deauthenticate(
1483 wpa_s, WLAN_REASON_INVALID_IE);
1484 return -1;
1485 }
1486 wpabuf_free(wps);
1487 }
1488#endif /* CONFIG_WPS_STRICT */
1489
1490 /* Go through the IEs and make a copy of the MDIE, if present. */
1491 while (p && l >= 2) {
1492 len = p[1] + 2;
1493 if (len > l) {
1494 wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1495 p, l);
1496 break;
1497 }
1498 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1499 p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1500 wpa_s->sme.ft_used = 1;
1501 os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1502 MOBILITY_DOMAIN_ID_LEN);
1503 break;
1504 }
1505 l -= len;
1506 p += len;
1507 }
1508#endif /* CONFIG_SME */
1509
1510 wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1511 data->assoc_info.resp_ies_len);
1512#endif /* CONFIG_IEEE80211R */
1513
1514 /* WPA/RSN IE from Beacon/ProbeResp */
1515 p = data->assoc_info.beacon_ies;
1516 l = data->assoc_info.beacon_ies_len;
1517
1518 /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1519 */
1520 wpa_found = rsn_found = 0;
1521 while (p && l >= 2) {
1522 len = p[1] + 2;
1523 if (len > l) {
1524 wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1525 p, l);
1526 break;
1527 }
1528 if (!wpa_found &&
1529 p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1530 os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1531 wpa_found = 1;
1532 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1533 }
1534
1535 if (!rsn_found &&
1536 p[0] == WLAN_EID_RSN && p[1] >= 2) {
1537 rsn_found = 1;
1538 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1539 }
1540
1541 l -= len;
1542 p += len;
1543 }
1544
1545 if (!wpa_found && data->assoc_info.beacon_ies)
1546 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1547 if (!rsn_found && data->assoc_info.beacon_ies)
1548 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1549 if (wpa_found || rsn_found)
1550 wpa_s->ap_ies_from_associnfo = 1;
1551
Jouni Malinen87fd2792011-05-16 18:35:42 +03001552 if (wpa_s->assoc_freq && data->assoc_info.freq &&
1553 wpa_s->assoc_freq != data->assoc_info.freq) {
1554 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1555 "%u to %u MHz",
1556 wpa_s->assoc_freq, data->assoc_info.freq);
1557 wpa_supplicant_update_scan_results(wpa_s);
1558 }
1559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 wpa_s->assoc_freq = data->assoc_info.freq;
1561
1562 return 0;
1563}
1564
1565
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001566static struct wpa_bss * wpa_supplicant_get_new_bss(
1567 struct wpa_supplicant *wpa_s, const u8 *bssid)
1568{
1569 struct wpa_bss *bss = NULL;
1570 struct wpa_ssid *ssid = wpa_s->current_ssid;
1571
1572 if (ssid->ssid_len > 0)
1573 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1574 if (!bss)
1575 bss = wpa_bss_get_bssid(wpa_s, bssid);
1576
1577 return bss;
1578}
1579
1580
1581static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1582{
1583 const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1584
1585 if (!wpa_s->current_bss || !wpa_s->current_ssid)
1586 return -1;
1587
1588 if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1589 return 0;
1590
1591 bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1592 WPA_IE_VENDOR_TYPE);
1593 bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1594
1595 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1596 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1597 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1598 bss_rsn ? 2 + bss_rsn[1] : 0))
1599 return -1;
1600
1601 return 0;
1602}
1603
1604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1606 union wpa_event_data *data)
1607{
1608 u8 bssid[ETH_ALEN];
1609 int ft_completed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 struct wpa_driver_capa capa;
1611
1612#ifdef CONFIG_AP
1613 if (wpa_s->ap_iface) {
1614 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1615 data->assoc_info.addr,
1616 data->assoc_info.req_ies,
1617 data->assoc_info.req_ies_len,
1618 data->assoc_info.reassoc);
1619 return;
1620 }
1621#endif /* CONFIG_AP */
1622
1623 ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1624 if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1625 return;
1626
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001627 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1628 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1629 wpa_supplicant_disassociate(
1630 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1631 return;
1632 }
1633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001635 if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1637 MACSTR, MAC2STR(bssid));
1638 random_add_randomness(bssid, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1640 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001641 wpas_notify_bssid_changed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642
1643 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1644 wpa_clear_keys(wpa_s, bssid);
1645 }
1646 if (wpa_supplicant_select_config(wpa_s) < 0) {
1647 wpa_supplicant_disassociate(
1648 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1649 return;
1650 }
1651 if (wpa_s->current_ssid) {
1652 struct wpa_bss *bss = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001653
1654 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1655 if (!bss) {
1656 wpa_supplicant_update_scan_results(wpa_s);
1657
1658 /* Get the BSS from the new scan results */
1659 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1660 }
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 if (bss)
1663 wpa_s->current_bss = bss;
1664 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001665
1666 if (wpa_s->conf->ap_scan == 1 &&
1667 wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1668 if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1669 wpa_msg(wpa_s, MSG_WARNING,
1670 "WPA/RSN IEs not updated");
1671 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 }
1673
1674#ifdef CONFIG_SME
1675 os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1676 wpa_s->sme.prev_bssid_set = 1;
1677#endif /* CONFIG_SME */
1678
1679 wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1680 if (wpa_s->current_ssid) {
1681 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1682 * initialized before association, but for other modes,
1683 * initialize PC/SC here, if the current configuration needs
1684 * smartcard or SIM/USIM. */
1685 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1686 }
1687 wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1688 if (wpa_s->l2)
1689 l2_packet_notify_auth_start(wpa_s->l2);
1690
1691 /*
1692 * Set portEnabled first to FALSE in order to get EAP state machine out
1693 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1694 * state machine may transit to AUTHENTICATING state based on obsolete
1695 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1696 * AUTHENTICATED without ever giving chance to EAP state machine to
1697 * reset the state.
1698 */
1699 if (!ft_completed) {
1700 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1701 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1702 }
1703 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1704 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1705 /* 802.1X::portControl = Auto */
1706 eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1707 wpa_s->eapol_received = 0;
1708 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1709 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1710 (wpa_s->current_ssid &&
1711 wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1712 wpa_supplicant_cancel_auth_timeout(wpa_s);
1713 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1714 } else if (!ft_completed) {
1715 /* Timeout for receiving the first EAPOL packet */
1716 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1717 }
1718 wpa_supplicant_cancel_scan(wpa_s);
1719
1720 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1721 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1722 /*
1723 * We are done; the driver will take care of RSN 4-way
1724 * handshake.
1725 */
1726 wpa_supplicant_cancel_auth_timeout(wpa_s);
1727 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1728 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1729 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1730 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1731 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1732 /*
1733 * The driver will take care of RSN 4-way handshake, so we need
1734 * to allow EAPOL supplicant to complete its work without
1735 * waiting for WPA supplicant.
1736 */
1737 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1738 } else if (ft_completed) {
1739 /*
1740 * FT protocol completed - make sure EAPOL state machine ends
1741 * up in authenticated.
1742 */
1743 wpa_supplicant_cancel_auth_timeout(wpa_s);
1744 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1745 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1746 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1747 }
1748
1749 if (wpa_s->pending_eapol_rx) {
1750 struct os_time now, age;
1751 os_get_time(&now);
1752 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1753 if (age.sec == 0 && age.usec < 100000 &&
1754 os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1755 0) {
1756 wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1757 "frame that was received just before "
1758 "association notification");
1759 wpa_supplicant_rx_eapol(
1760 wpa_s, wpa_s->pending_eapol_rx_src,
1761 wpabuf_head(wpa_s->pending_eapol_rx),
1762 wpabuf_len(wpa_s->pending_eapol_rx));
1763 }
1764 wpabuf_free(wpa_s->pending_eapol_rx);
1765 wpa_s->pending_eapol_rx = NULL;
1766 }
1767
1768 if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1769 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1770 wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1771 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1772 /* Set static WEP keys again */
1773 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1774 }
1775
1776#ifdef CONFIG_IBSS_RSN
1777 if (wpa_s->current_ssid &&
1778 wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1779 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1780 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1781 wpa_s->ibss_rsn == NULL) {
1782 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1783 if (!wpa_s->ibss_rsn) {
1784 wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1785 wpa_supplicant_deauthenticate(
1786 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1787 return;
1788 }
1789
1790 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1791 }
1792#endif /* CONFIG_IBSS_RSN */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001793
1794 wpas_wps_notify_assoc(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001795}
1796
1797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798static int disconnect_reason_recoverable(u16 reason_code)
1799{
1800 return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1801 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1802 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1803}
1804
1805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001807 u16 reason_code,
1808 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809{
1810 const u8 *bssid;
Jouni Malinen2b89da82012-08-31 22:04:41 +03001811
1812 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1813 /*
1814 * At least Host AP driver and a Prism3 card seemed to be
1815 * generating streams of disconnected events when configuring
1816 * IBSS for WPA-None. Ignore them for now.
1817 */
1818 return;
1819 }
1820
1821 bssid = wpa_s->bssid;
1822 if (is_zero_ether_addr(bssid))
1823 bssid = wpa_s->pending_bssid;
1824
1825 if (!is_zero_ether_addr(bssid) ||
1826 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1827 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1828 " reason=%d%s",
1829 MAC2STR(bssid), reason_code,
1830 locally_generated ? " locally_generated=1" : "");
1831 }
1832}
1833
1834
1835static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1836 u16 reason_code,
1837 int locally_generated)
1838{
1839 const u8 *bssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 int authenticating;
1841 u8 prev_pending_bssid[ETH_ALEN];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 struct wpa_bss *fast_reconnect = NULL;
1843 struct wpa_ssid *fast_reconnect_ssid = NULL;
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001844 struct wpa_ssid *last_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845
1846 authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1847 os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1848
1849 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1850 /*
1851 * At least Host AP driver and a Prism3 card seemed to be
1852 * generating streams of disconnected events when configuring
1853 * IBSS for WPA-None. Ignore them for now.
1854 */
1855 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1856 "IBSS/WPA-None mode");
1857 return;
1858 }
1859
1860 if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1861 wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1862 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1863 "pre-shared key may be incorrect");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001864 wpas_auth_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 }
1866 if (!wpa_s->auto_reconnect_disabled ||
1867 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001868 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1869 "reconnect (wps=%d wpa_state=%d)",
1870 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1871 wpa_s->wpa_state);
1872 if (wpa_s->wpa_state == WPA_COMPLETED &&
1873 wpa_s->current_ssid &&
1874 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001875 !locally_generated &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001876 disconnect_reason_recoverable(reason_code)) {
1877 /*
1878 * It looks like the AP has dropped association with
1879 * us, but could allow us to get back in. Try to
1880 * reconnect to the same BSS without full scan to save
1881 * time for some common cases.
1882 */
1883 fast_reconnect = wpa_s->current_bss;
1884 fast_reconnect_ssid = wpa_s->current_ssid;
1885 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001886#ifdef ANDROID
Dmitry Shmidt43007fd2011-04-11 15:58:40 -07001887 wpa_supplicant_req_scan(wpa_s, 0, 500000);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001888#else
1889 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1890#endif
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001891 else
1892 wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1893 "immediate scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001895 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 "try to re-connect");
1897 wpa_s->reassociate = 0;
1898 wpa_s->disconnected = 1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001899 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 }
1901 bssid = wpa_s->bssid;
1902 if (is_zero_ether_addr(bssid))
1903 bssid = wpa_s->pending_bssid;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001904 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1905 wpas_connection_failed(wpa_s, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001907 if (locally_generated)
1908 wpa_s->disconnect_reason = -reason_code;
1909 else
1910 wpa_s->disconnect_reason = reason_code;
1911 wpas_notify_disconnect_reason(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 if (wpa_supplicant_dynamic_keys(wpa_s)) {
1913 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1914 wpa_s->keys_cleared = 0;
1915 wpa_clear_keys(wpa_s, wpa_s->bssid);
1916 }
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001917 last_ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 wpa_supplicant_mark_disassoc(wpa_s);
1919
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001920 if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
Jouni Malinenf8a26a82012-09-01 17:20:27 +03001922 wpa_s->current_ssid = last_ssid;
1923 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001924
1925 if (fast_reconnect) {
1926#ifndef CONFIG_NO_SCAN_PROCESSING
1927 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1928 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1929 fast_reconnect_ssid) < 0) {
1930 /* Recover through full scan */
1931 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1932 }
1933#endif /* CONFIG_NO_SCAN_PROCESSING */
1934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935}
1936
1937
1938#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001939void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001940{
1941 struct wpa_supplicant *wpa_s = eloop_ctx;
1942
1943 if (!wpa_s->pending_mic_error_report)
1944 return;
1945
1946 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1947 wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1948 wpa_s->pending_mic_error_report = 0;
1949}
1950#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1951
1952
1953static void
1954wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1955 union wpa_event_data *data)
1956{
1957 int pairwise;
1958 struct os_time t;
1959
1960 wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1961 pairwise = (data && data->michael_mic_failure.unicast);
1962 os_get_time(&t);
1963 if ((wpa_s->last_michael_mic_error &&
1964 t.sec - wpa_s->last_michael_mic_error <= 60) ||
1965 wpa_s->pending_mic_error_report) {
1966 if (wpa_s->pending_mic_error_report) {
1967 /*
1968 * Send the pending MIC error report immediately since
1969 * we are going to start countermeasures and AP better
1970 * do the same.
1971 */
1972 wpa_sm_key_request(wpa_s->wpa, 1,
1973 wpa_s->pending_mic_error_pairwise);
1974 }
1975
1976 /* Send the new MIC error report immediately since we are going
1977 * to start countermeasures and AP better do the same.
1978 */
1979 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1980
1981 /* initialize countermeasures */
1982 wpa_s->countermeasures = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001983
1984 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1987
1988 /*
1989 * Need to wait for completion of request frame. We do not get
1990 * any callback for the message completion, so just wait a
1991 * short while and hope for the best. */
1992 os_sleep(0, 10000);
1993
1994 wpa_drv_set_countermeasures(wpa_s, 1);
1995 wpa_supplicant_deauthenticate(wpa_s,
1996 WLAN_REASON_MICHAEL_MIC_FAILURE);
1997 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1998 wpa_s, NULL);
1999 eloop_register_timeout(60, 0,
2000 wpa_supplicant_stop_countermeasures,
2001 wpa_s, NULL);
2002 /* TODO: mark the AP rejected for 60 second. STA is
2003 * allowed to associate with another AP.. */
2004 } else {
2005#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2006 if (wpa_s->mic_errors_seen) {
2007 /*
2008 * Reduce the effectiveness of Michael MIC error
2009 * reports as a means for attacking against TKIP if
2010 * more than one MIC failure is noticed with the same
2011 * PTK. We delay the transmission of the reports by a
2012 * random time between 0 and 60 seconds in order to
2013 * force the attacker wait 60 seconds before getting
2014 * the information on whether a frame resulted in a MIC
2015 * failure.
2016 */
2017 u8 rval[4];
2018 int sec;
2019
2020 if (os_get_random(rval, sizeof(rval)) < 0)
2021 sec = os_random() % 60;
2022 else
2023 sec = WPA_GET_BE32(rval) % 60;
2024 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2025 "report %d seconds", sec);
2026 wpa_s->pending_mic_error_report = 1;
2027 wpa_s->pending_mic_error_pairwise = pairwise;
2028 eloop_cancel_timeout(
2029 wpa_supplicant_delayed_mic_error_report,
2030 wpa_s, NULL);
2031 eloop_register_timeout(
2032 sec, os_random() % 1000000,
2033 wpa_supplicant_delayed_mic_error_report,
2034 wpa_s, NULL);
2035 } else {
2036 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2037 }
2038#else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2039 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2040#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2041 }
2042 wpa_s->last_michael_mic_error = t.sec;
2043 wpa_s->mic_errors_seen++;
2044}
2045
2046
2047#ifdef CONFIG_TERMINATE_ONLASTIF
2048static int any_interfaces(struct wpa_supplicant *head)
2049{
2050 struct wpa_supplicant *wpa_s;
2051
2052 for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2053 if (!wpa_s->interface_removed)
2054 return 1;
2055 return 0;
2056}
2057#endif /* CONFIG_TERMINATE_ONLASTIF */
2058
2059
2060static void
2061wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2062 union wpa_event_data *data)
2063{
2064 if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2065 return;
2066
2067 switch (data->interface_status.ievent) {
2068 case EVENT_INTERFACE_ADDED:
2069 if (!wpa_s->interface_removed)
2070 break;
2071 wpa_s->interface_removed = 0;
2072 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2073 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2074 wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2075 "driver after interface was added");
2076 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002077 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 break;
2079 case EVENT_INTERFACE_REMOVED:
2080 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2081 wpa_s->interface_removed = 1;
2082 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002083 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 l2_packet_deinit(wpa_s->l2);
2085 wpa_s->l2 = NULL;
2086#ifdef CONFIG_IBSS_RSN
2087 ibss_rsn_deinit(wpa_s->ibss_rsn);
2088 wpa_s->ibss_rsn = NULL;
2089#endif /* CONFIG_IBSS_RSN */
2090#ifdef CONFIG_TERMINATE_ONLASTIF
2091 /* check if last interface */
2092 if (!any_interfaces(wpa_s->global->ifaces))
2093 eloop_terminate();
2094#endif /* CONFIG_TERMINATE_ONLASTIF */
2095 break;
2096 }
2097}
2098
2099
2100#ifdef CONFIG_PEERKEY
2101static void
2102wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2103 union wpa_event_data *data)
2104{
2105 if (data == NULL)
2106 return;
2107 wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2108}
2109#endif /* CONFIG_PEERKEY */
2110
2111
2112#ifdef CONFIG_TDLS
2113static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2114 union wpa_event_data *data)
2115{
2116 if (data == NULL)
2117 return;
2118 switch (data->tdls.oper) {
2119 case TDLS_REQUEST_SETUP:
2120 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2121 break;
2122 case TDLS_REQUEST_TEARDOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002123 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2124 data->tdls.reason_code);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 break;
2126 }
2127}
2128#endif /* CONFIG_TDLS */
2129
2130
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002131#ifdef CONFIG_IEEE80211V
2132static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2133 union wpa_event_data *data)
2134{
2135 if (data == NULL)
2136 return;
2137 switch (data->wnm.oper) {
2138 case WNM_OPER_SLEEP:
2139 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2140 "(action=%d, intval=%d)",
2141 data->wnm.sleep_action, data->wnm.sleep_intval);
2142 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2143 data->wnm.sleep_intval);
2144 break;
2145 }
2146}
2147#endif /* CONFIG_IEEE80211V */
2148
2149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150#ifdef CONFIG_IEEE80211R
2151static void
2152wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2153 union wpa_event_data *data)
2154{
2155 if (data == NULL)
2156 return;
2157
2158 if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2159 data->ft_ies.ies_len,
2160 data->ft_ies.ft_action,
2161 data->ft_ies.target_ap,
2162 data->ft_ies.ric_ies,
2163 data->ft_ies.ric_ies_len) < 0) {
2164 /* TODO: prevent MLME/driver from trying to associate? */
2165 }
2166}
2167#endif /* CONFIG_IEEE80211R */
2168
2169
2170#ifdef CONFIG_IBSS_RSN
2171static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2172 union wpa_event_data *data)
2173{
2174 struct wpa_ssid *ssid;
2175 if (wpa_s->wpa_state < WPA_ASSOCIATED)
2176 return;
2177 if (data == NULL)
2178 return;
2179 ssid = wpa_s->current_ssid;
2180 if (ssid == NULL)
2181 return;
2182 if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2183 return;
2184
2185 ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2186}
2187#endif /* CONFIG_IBSS_RSN */
2188
2189
2190#ifdef CONFIG_IEEE80211R
2191static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2192 size_t len)
2193{
2194 const u8 *sta_addr, *target_ap_addr;
2195 u16 status;
2196
2197 wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2198 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2199 return; /* only SME case supported for now */
2200 if (len < 1 + 2 * ETH_ALEN + 2)
2201 return;
2202 if (data[0] != 2)
2203 return; /* Only FT Action Response is supported for now */
2204 sta_addr = data + 1;
2205 target_ap_addr = data + 1 + ETH_ALEN;
2206 status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2207 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2208 MACSTR " TargetAP " MACSTR " status %u",
2209 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2210
2211 if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2212 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2213 " in FT Action Response", MAC2STR(sta_addr));
2214 return;
2215 }
2216
2217 if (status) {
2218 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2219 "failure (status code %d)", status);
2220 /* TODO: report error to FT code(?) */
2221 return;
2222 }
2223
2224 if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2225 len - (1 + 2 * ETH_ALEN + 2), 1,
2226 target_ap_addr, NULL, 0) < 0)
2227 return;
2228
2229#ifdef CONFIG_SME
2230 {
2231 struct wpa_bss *bss;
2232 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2233 if (bss)
2234 wpa_s->sme.freq = bss->freq;
2235 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2236 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2237 WLAN_AUTH_FT);
2238 }
2239#endif /* CONFIG_SME */
2240}
2241#endif /* CONFIG_IEEE80211R */
2242
2243
2244static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2245 struct unprot_deauth *e)
2246{
2247#ifdef CONFIG_IEEE80211W
2248 wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2249 "dropped: " MACSTR " -> " MACSTR
2250 " (reason code %u)",
2251 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2252 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2253#endif /* CONFIG_IEEE80211W */
2254}
2255
2256
2257static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2258 struct unprot_disassoc *e)
2259{
2260#ifdef CONFIG_IEEE80211W
2261 wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2262 "dropped: " MACSTR " -> " MACSTR
2263 " (reason code %u)",
2264 MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2265 sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2266#endif /* CONFIG_IEEE80211W */
2267}
2268
2269
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002270static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2271{
2272 u8 action, mode;
2273 const u8 *pos, *end;
2274
2275 if (rx->data == NULL || rx->len == 0)
2276 return;
2277
2278 pos = rx->data;
2279 end = pos + rx->len;
2280 action = *pos++;
2281
2282 wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2283 action, MAC2STR(rx->sa));
2284 switch (action) {
2285 case WNM_BSS_TRANS_MGMT_REQ:
2286 if (pos + 5 > end)
2287 break;
2288 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2289 "Request: dialog_token=%u request_mode=0x%x "
2290 "disassoc_timer=%u validity_interval=%u",
2291 pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2292 mode = pos[1];
2293 pos += 5;
2294 if (mode & 0x08)
2295 pos += 12; /* BSS Termination Duration */
2296 if (mode & 0x10) {
2297 char url[256];
2298 if (pos + 1 > end || pos + 1 + pos[0] > end) {
2299 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2300 "Transition Management Request "
2301 "(URL)");
2302 break;
2303 }
2304 os_memcpy(url, pos + 1, pos[0]);
2305 url[pos[0]] = '\0';
2306 wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2307 "Imminent - session_info_url=%s", url);
2308 }
2309 break;
2310 }
2311}
2312
2313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2315 union wpa_event_data *data)
2316{
2317 struct wpa_supplicant *wpa_s = ctx;
2318 u16 reason_code = 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002319 int locally_generated = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320
2321 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2322 event != EVENT_INTERFACE_ENABLED &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002323 event != EVENT_INTERFACE_STATUS &&
2324 event != EVENT_SCHED_SCAN_STOPPED) {
2325 wpa_dbg(wpa_s, MSG_DEBUG,
2326 "Ignore event %s (%d) while interface is disabled",
2327 event_to_string(event), event);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328 return;
2329 }
2330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002331#ifndef CONFIG_NO_STDOUT_DEBUG
2332{
2333 int level = MSG_DEBUG;
2334
Dmitry Shmidt04949592012-07-19 12:16:46 -07002335 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002336 const struct ieee80211_hdr *hdr;
2337 u16 fc;
2338 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2339 fc = le_to_host16(hdr->frame_control);
2340 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2341 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2342 level = MSG_EXCESSIVE;
2343 }
2344
2345 wpa_dbg(wpa_s, level, "Event %s (%d) received",
2346 event_to_string(event), event);
2347}
2348#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349
2350 switch (event) {
2351 case EVENT_AUTH:
2352 sme_event_auth(wpa_s, data);
2353 break;
2354 case EVENT_ASSOC:
2355 wpa_supplicant_event_assoc(wpa_s, data);
2356 break;
2357 case EVENT_DISASSOC:
2358 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2359 if (data) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002360 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2361 data->disassoc_info.reason_code,
2362 data->disassoc_info.locally_generated ?
2363 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364 if (data->disassoc_info.addr)
2365 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2366 MAC2STR(data->disassoc_info.addr));
2367 }
2368#ifdef CONFIG_AP
2369 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2370 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2371 data->disassoc_info.addr);
2372 break;
2373 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 if (wpa_s->ap_iface) {
2375 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2376 "AP mode");
2377 break;
2378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379#endif /* CONFIG_AP */
2380 if (data) {
2381 reason_code = data->disassoc_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002382 locally_generated =
2383 data->disassoc_info.locally_generated;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2385 data->disassoc_info.ie,
2386 data->disassoc_info.ie_len);
2387#ifdef CONFIG_P2P
2388 wpas_p2p_disassoc_notif(
2389 wpa_s, data->disassoc_info.addr, reason_code,
2390 data->disassoc_info.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002391 data->disassoc_info.ie_len,
2392 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393#endif /* CONFIG_P2P */
2394 }
2395 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2396 sme_event_disassoc(wpa_s, data);
2397 /* fall through */
2398 case EVENT_DEAUTH:
2399 if (event == EVENT_DEAUTH) {
2400 wpa_dbg(wpa_s, MSG_DEBUG,
2401 "Deauthentication notification");
2402 if (data) {
2403 reason_code = data->deauth_info.reason_code;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002404 locally_generated =
2405 data->deauth_info.locally_generated;
2406 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2407 data->deauth_info.reason_code,
2408 data->deauth_info.locally_generated ?
2409 " (locally generated)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002410 if (data->deauth_info.addr) {
2411 wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2412 MACSTR,
2413 MAC2STR(data->deauth_info.
2414 addr));
2415 }
2416 wpa_hexdump(MSG_DEBUG,
2417 "Deauthentication frame IE(s)",
2418 data->deauth_info.ie,
2419 data->deauth_info.ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420 }
2421 }
2422#ifdef CONFIG_AP
2423 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2424 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2425 data->deauth_info.addr);
2426 break;
2427 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002428 if (wpa_s->ap_iface) {
2429 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2430 "AP mode");
2431 break;
2432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433#endif /* CONFIG_AP */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002434 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2435 locally_generated);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002436 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2437 ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2438 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2439 eapol_sm_failed(wpa_s->eapol)))
2440 wpas_auth_failed(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002441#ifdef CONFIG_P2P
2442 if (event == EVENT_DEAUTH && data) {
Jouni Malinen2b89da82012-08-31 22:04:41 +03002443 if (wpas_p2p_deauth_notif(wpa_s,
2444 data->deauth_info.addr,
2445 reason_code,
2446 data->deauth_info.ie,
2447 data->deauth_info.ie_len,
2448 locally_generated) > 0) {
2449 /*
2450 * The interface was removed, so cannot
2451 * continue processing any additional
2452 * operations after this.
2453 */
2454 break;
2455 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002456 }
2457#endif /* CONFIG_P2P */
Jouni Malinen2b89da82012-08-31 22:04:41 +03002458 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2459 locally_generated);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 break;
2461 case EVENT_MICHAEL_MIC_FAILURE:
2462 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2463 break;
2464#ifndef CONFIG_NO_SCAN_PROCESSING
2465 case EVENT_SCAN_RESULTS:
2466 wpa_supplicant_event_scan_results(wpa_s, data);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002467#ifdef CONFIG_P2P
Jouni Malinendc7b7132012-09-14 12:53:47 -07002468 if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002469 wpa_s->global->p2p != NULL &&
2470 wpa_s->wpa_state != WPA_AUTHENTICATING &&
2471 wpa_s->wpa_state != WPA_ASSOCIATING) {
Jouni Malinendc7b7132012-09-14 12:53:47 -07002472 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002473 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2474 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2475 "continued after scan result processing");
2476 }
2477 }
2478#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479 break;
2480#endif /* CONFIG_NO_SCAN_PROCESSING */
2481 case EVENT_ASSOCINFO:
2482 wpa_supplicant_event_associnfo(wpa_s, data);
2483 break;
2484 case EVENT_INTERFACE_STATUS:
2485 wpa_supplicant_event_interface_status(wpa_s, data);
2486 break;
2487 case EVENT_PMKID_CANDIDATE:
2488 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2489 break;
2490#ifdef CONFIG_PEERKEY
2491 case EVENT_STKSTART:
2492 wpa_supplicant_event_stkstart(wpa_s, data);
2493 break;
2494#endif /* CONFIG_PEERKEY */
2495#ifdef CONFIG_TDLS
2496 case EVENT_TDLS:
2497 wpa_supplicant_event_tdls(wpa_s, data);
2498 break;
2499#endif /* CONFIG_TDLS */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002500#ifdef CONFIG_IEEE80211V
2501 case EVENT_WNM:
2502 wpa_supplicant_event_wnm(wpa_s, data);
2503 break;
2504#endif /* CONFIG_IEEE80211V */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505#ifdef CONFIG_IEEE80211R
2506 case EVENT_FT_RESPONSE:
2507 wpa_supplicant_event_ft_response(wpa_s, data);
2508 break;
2509#endif /* CONFIG_IEEE80211R */
2510#ifdef CONFIG_IBSS_RSN
2511 case EVENT_IBSS_RSN_START:
2512 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2513 break;
2514#endif /* CONFIG_IBSS_RSN */
2515 case EVENT_ASSOC_REJECT:
2516 if (data->assoc_reject.bssid)
2517 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2518 "bssid=" MACSTR " status_code=%u",
2519 MAC2STR(data->assoc_reject.bssid),
2520 data->assoc_reject.status_code);
2521 else
2522 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2523 "status_code=%u",
2524 data->assoc_reject.status_code);
2525 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2526 sme_event_assoc_reject(wpa_s, data);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002527#ifdef ANDROID_P2P
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002528#ifdef CONFIG_P2P
2529 else if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002530 if(!wpa_s->current_ssid) {
2531 wpa_printf(MSG_ERROR, "current_ssid == NULL");
2532 break;
2533 }
2534 /* If assoc reject is reported by the driver, then avoid
2535 * waiting for the authentication timeout. Cancel the
2536 * authentication timeout and retry the assoc.
2537 */
2538 if(wpa_s->current_ssid->assoc_retry++ < 5) {
2539 wpa_printf(MSG_ERROR, "Retrying assoc: %d ",
2540 wpa_s->current_ssid->assoc_retry);
2541 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002542
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002543 /* Clear the states */
2544 wpa_sm_notify_disassoc(wpa_s->wpa);
2545 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2546
2547 wpa_s->reassociate = 1;
2548 wpa_supplicant_req_scan(wpa_s, 1, 0);
2549 } else {
2550 /* If we ASSOC_REJECT's hits threshold, disable the
2551 * network
2552 */
2553 wpa_printf(MSG_ERROR, "Assoc retry threshold reached. "
2554 "Disabling the network");
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002555 wpa_s->current_ssid->assoc_retry = 0;
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002556 wpa_supplicant_disable_network(wpa_s, wpa_s->current_ssid);
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002557 wpas_p2p_group_remove(wpa_s, wpa_s->ifname);
Irfan Sheriff7db4ef72012-06-18 09:39:07 -07002558 }
2559 }
Irfan Sheriff2fb835a2012-06-18 09:39:07 -07002560#endif
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07002561#endif /* ANDROID_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 break;
2563 case EVENT_AUTH_TIMED_OUT:
2564 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2565 sme_event_auth_timed_out(wpa_s, data);
2566 break;
2567 case EVENT_ASSOC_TIMED_OUT:
2568 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2569 sme_event_assoc_timed_out(wpa_s, data);
2570 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 case EVENT_TX_STATUS:
2572 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2573 " type=%d stype=%d",
2574 MAC2STR(data->tx_status.dst),
2575 data->tx_status.type, data->tx_status.stype);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002576#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 if (wpa_s->ap_iface == NULL) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2580 data->tx_status.stype == WLAN_FC_STYPE_ACTION)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002581 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582 wpa_s, data->tx_status.dst,
2583 data->tx_status.data,
2584 data->tx_status.data_len,
2585 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002586 OFFCHANNEL_SEND_ACTION_SUCCESS :
2587 OFFCHANNEL_SEND_ACTION_NO_ACK);
2588#endif /* CONFIG_OFFCHANNEL */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589 break;
2590 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002591#endif /* CONFIG_AP */
2592#ifdef CONFIG_OFFCHANNEL
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2594 MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2595 /*
2596 * Catch TX status events for Action frames we sent via group
2597 * interface in GO mode.
2598 */
2599 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2600 data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2601 os_memcmp(wpa_s->parent->pending_action_dst,
2602 data->tx_status.dst, ETH_ALEN) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002603 offchannel_send_action_tx_status(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 wpa_s->parent, data->tx_status.dst,
2605 data->tx_status.data,
2606 data->tx_status.data_len,
2607 data->tx_status.ack ?
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002608 OFFCHANNEL_SEND_ACTION_SUCCESS :
2609 OFFCHANNEL_SEND_ACTION_NO_ACK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 break;
2611 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002612#endif /* CONFIG_OFFCHANNEL */
2613#ifdef CONFIG_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614 switch (data->tx_status.type) {
2615 case WLAN_FC_TYPE_MGMT:
2616 ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2617 data->tx_status.data_len,
2618 data->tx_status.stype,
2619 data->tx_status.ack);
2620 break;
2621 case WLAN_FC_TYPE_DATA:
2622 ap_tx_status(wpa_s, data->tx_status.dst,
2623 data->tx_status.data,
2624 data->tx_status.data_len,
2625 data->tx_status.ack);
2626 break;
2627 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002628#endif /* CONFIG_AP */
2629 break;
2630#ifdef CONFIG_AP
2631 case EVENT_EAPOL_TX_STATUS:
2632 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2633 data->eapol_tx_status.data,
2634 data->eapol_tx_status.data_len,
2635 data->eapol_tx_status.ack);
2636 break;
2637 case EVENT_DRIVER_CLIENT_POLL_OK:
2638 ap_client_poll_ok(wpa_s, data->client_poll.addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 break;
2640 case EVENT_RX_FROM_UNKNOWN:
2641 if (wpa_s->ap_iface == NULL)
2642 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002643 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2644 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002646 case EVENT_CH_SWITCH:
2647 if (!data)
2648 break;
2649 if (!wpa_s->ap_iface) {
2650 wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2651 "event in non-AP mode");
2652 break;
2653 }
2654
2655#ifdef CONFIG_AP
2656 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2657 data->ch_switch.ht_enabled,
2658 data->ch_switch.ch_offset);
2659#endif /* CONFIG_AP */
2660 break;
2661 case EVENT_RX_MGMT: {
2662 u16 fc, stype;
2663 const struct ieee80211_mgmt *mgmt;
2664
2665 mgmt = (const struct ieee80211_mgmt *)
2666 data->rx_mgmt.frame;
2667 fc = le_to_host16(mgmt->frame_control);
2668 stype = WLAN_FC_GET_STYPE(fc);
2669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 if (wpa_s->ap_iface == NULL) {
2671#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002672 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2673 data->rx_mgmt.frame_len > 24) {
2674 const u8 *src = mgmt->sa;
2675 const u8 *ie = mgmt->u.probe_req.variable;
2676 size_t ie_len = data->rx_mgmt.frame_len -
2677 (mgmt->u.probe_req.variable -
2678 data->rx_mgmt.frame);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002679 wpas_p2p_probe_req_rx(
2680 wpa_s, src, mgmt->da,
2681 mgmt->bssid, ie, ie_len,
2682 data->rx_mgmt.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002683 break;
2684 }
2685#endif /* CONFIG_P2P */
2686 wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2687 "management frame in non-AP mode");
2688 break;
2689 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002690
2691 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2692 data->rx_mgmt.frame_len > 24) {
2693 const u8 *ie = mgmt->u.probe_req.variable;
2694 size_t ie_len = data->rx_mgmt.frame_len -
2695 (mgmt->u.probe_req.variable -
2696 data->rx_mgmt.frame);
2697
2698 wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2699 mgmt->bssid, ie, ie_len,
2700 data->rx_mgmt.ssi_signal);
2701 }
2702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2704 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002705 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706#endif /* CONFIG_AP */
2707 case EVENT_RX_ACTION:
2708 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2709 " Category=%u DataLen=%d freq=%d MHz",
2710 MAC2STR(data->rx_action.sa),
2711 data->rx_action.category, (int) data->rx_action.len,
2712 data->rx_action.freq);
2713#ifdef CONFIG_IEEE80211R
2714 if (data->rx_action.category == WLAN_ACTION_FT) {
2715 ft_rx_action(wpa_s, data->rx_action.data,
2716 data->rx_action.len);
2717 break;
2718 }
2719#endif /* CONFIG_IEEE80211R */
2720#ifdef CONFIG_IEEE80211W
2721#ifdef CONFIG_SME
2722 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2723 sme_sa_query_rx(wpa_s, data->rx_action.sa,
2724 data->rx_action.data,
2725 data->rx_action.len);
2726 break;
2727 }
2728#endif /* CONFIG_SME */
2729#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002730#ifdef CONFIG_IEEE80211V
2731 if (data->rx_action.category == WLAN_ACTION_WNM) {
2732 ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2733 break;
2734 }
2735#endif /* CONFIG_IEEE80211V */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736#ifdef CONFIG_GAS
2737 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2738 gas_query_rx(wpa_s->gas, data->rx_action.da,
2739 data->rx_action.sa, data->rx_action.bssid,
2740 data->rx_action.data, data->rx_action.len,
2741 data->rx_action.freq) == 0)
2742 break;
2743#endif /* CONFIG_GAS */
2744 if (data->rx_action.category == WLAN_ACTION_WNM) {
2745 wnm_action_rx(wpa_s, &data->rx_action);
2746 break;
2747 }
2748#ifdef CONFIG_TDLS
2749 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2750 data->rx_action.len >= 4 &&
2751 data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2752 wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2753 "Response from " MACSTR,
2754 MAC2STR(data->rx_action.sa));
2755 break;
2756 }
2757#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758#ifdef CONFIG_P2P
2759 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2760 data->rx_action.sa,
2761 data->rx_action.bssid,
2762 data->rx_action.category,
2763 data->rx_action.data,
2764 data->rx_action.len, data->rx_action.freq);
2765#endif /* CONFIG_P2P */
2766 break;
2767 case EVENT_RX_PROBE_REQ:
2768 if (data->rx_probe_req.sa == NULL ||
2769 data->rx_probe_req.ie == NULL)
2770 break;
2771#ifdef CONFIG_AP
2772 if (wpa_s->ap_iface) {
2773 hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2774 data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002775 data->rx_probe_req.da,
2776 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002778 data->rx_probe_req.ie_len,
2779 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780 break;
2781 }
2782#endif /* CONFIG_AP */
2783#ifdef CONFIG_P2P
2784 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002785 data->rx_probe_req.da,
2786 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002788 data->rx_probe_req.ie_len,
2789 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790#endif /* CONFIG_P2P */
2791 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792 case EVENT_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002793#ifdef CONFIG_OFFCHANNEL
2794 offchannel_remain_on_channel_cb(
2795 wpa_s, data->remain_on_channel.freq,
2796 data->remain_on_channel.duration);
2797#endif /* CONFIG_OFFCHANNEL */
2798#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 wpas_p2p_remain_on_channel_cb(
2800 wpa_s, data->remain_on_channel.freq,
2801 data->remain_on_channel.duration);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002802#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 break;
2804 case EVENT_CANCEL_REMAIN_ON_CHANNEL:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002805#ifdef CONFIG_OFFCHANNEL
2806 offchannel_cancel_remain_on_channel_cb(
2807 wpa_s, data->remain_on_channel.freq);
2808#endif /* CONFIG_OFFCHANNEL */
2809#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 wpas_p2p_cancel_remain_on_channel_cb(
2811 wpa_s, data->remain_on_channel.freq);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002812#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002814#ifdef CONFIG_P2P
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815 case EVENT_P2P_DEV_FOUND: {
2816 struct p2p_peer_info peer_info;
2817
2818 os_memset(&peer_info, 0, sizeof(peer_info));
2819 if (data->p2p_dev_found.dev_addr)
2820 os_memcpy(peer_info.p2p_device_addr,
2821 data->p2p_dev_found.dev_addr, ETH_ALEN);
2822 if (data->p2p_dev_found.pri_dev_type)
2823 os_memcpy(peer_info.pri_dev_type,
2824 data->p2p_dev_found.pri_dev_type,
2825 sizeof(peer_info.pri_dev_type));
2826 if (data->p2p_dev_found.dev_name)
2827 os_strlcpy(peer_info.device_name,
2828 data->p2p_dev_found.dev_name,
2829 sizeof(peer_info.device_name));
2830 peer_info.config_methods = data->p2p_dev_found.config_methods;
2831 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2832 peer_info.group_capab = data->p2p_dev_found.group_capab;
2833
2834 /*
2835 * FIX: new_device=1 is not necessarily correct. We should
2836 * maintain a P2P peer database in wpa_supplicant and update
2837 * this information based on whether the peer is truly new.
2838 */
2839 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2840 break;
2841 }
2842 case EVENT_P2P_GO_NEG_REQ_RX:
2843 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2844 data->p2p_go_neg_req_rx.dev_passwd_id);
2845 break;
2846 case EVENT_P2P_GO_NEG_COMPLETED:
2847 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2848 break;
2849 case EVENT_P2P_PROV_DISC_REQUEST:
2850 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2851 data->p2p_prov_disc_req.config_methods,
2852 data->p2p_prov_disc_req.dev_addr,
2853 data->p2p_prov_disc_req.pri_dev_type,
2854 data->p2p_prov_disc_req.dev_name,
2855 data->p2p_prov_disc_req.supp_config_methods,
2856 data->p2p_prov_disc_req.dev_capab,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002857 data->p2p_prov_disc_req.group_capab,
2858 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 break;
2860 case EVENT_P2P_PROV_DISC_RESPONSE:
2861 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2862 data->p2p_prov_disc_resp.config_methods);
2863 break;
2864 case EVENT_P2P_SD_REQUEST:
2865 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2866 data->p2p_sd_req.sa,
2867 data->p2p_sd_req.dialog_token,
2868 data->p2p_sd_req.update_indic,
2869 data->p2p_sd_req.tlvs,
2870 data->p2p_sd_req.tlvs_len);
2871 break;
2872 case EVENT_P2P_SD_RESPONSE:
2873 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2874 data->p2p_sd_resp.update_indic,
2875 data->p2p_sd_resp.tlvs,
2876 data->p2p_sd_resp.tlvs_len);
2877 break;
2878#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 case EVENT_EAPOL_RX:
2880 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2881 data->eapol_rx.data,
2882 data->eapol_rx.data_len);
2883 break;
2884 case EVENT_SIGNAL_CHANGE:
2885 bgscan_notify_signal_change(
2886 wpa_s, data->signal_change.above_threshold,
2887 data->signal_change.current_signal,
2888 data->signal_change.current_noise,
2889 data->signal_change.current_txrate);
2890 break;
2891 case EVENT_INTERFACE_ENABLED:
2892 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2893 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002894 wpa_supplicant_update_mac_addr(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895#ifdef CONFIG_AP
2896 if (!wpa_s->ap_iface) {
2897 wpa_supplicant_set_state(wpa_s,
2898 WPA_DISCONNECTED);
2899 wpa_supplicant_req_scan(wpa_s, 0, 0);
2900 } else
2901 wpa_supplicant_set_state(wpa_s,
2902 WPA_COMPLETED);
2903#else /* CONFIG_AP */
2904 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2905 wpa_supplicant_req_scan(wpa_s, 0, 0);
2906#endif /* CONFIG_AP */
2907 }
2908 break;
2909 case EVENT_INTERFACE_DISABLED:
2910 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2911 wpa_supplicant_mark_disassoc(wpa_s);
2912 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2913 break;
2914 case EVENT_CHANNEL_LIST_CHANGED:
2915 if (wpa_s->drv_priv == NULL)
2916 break; /* Ignore event during drv initialization */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002917
2918 free_hw_features(wpa_s);
2919 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2920 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922#ifdef CONFIG_P2P
2923 wpas_p2p_update_channel_list(wpa_s);
2924#endif /* CONFIG_P2P */
2925 break;
2926 case EVENT_INTERFACE_UNAVAILABLE:
2927#ifdef CONFIG_P2P
2928 wpas_p2p_interface_unavailable(wpa_s);
2929#endif /* CONFIG_P2P */
2930 break;
2931 case EVENT_BEST_CHANNEL:
2932 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2933 "(%d %d %d)",
2934 data->best_chan.freq_24, data->best_chan.freq_5,
2935 data->best_chan.freq_overall);
2936 wpa_s->best_24_freq = data->best_chan.freq_24;
2937 wpa_s->best_5_freq = data->best_chan.freq_5;
2938 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2939#ifdef CONFIG_P2P
2940 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2941 data->best_chan.freq_5,
2942 data->best_chan.freq_overall);
2943#endif /* CONFIG_P2P */
2944 break;
2945 case EVENT_UNPROT_DEAUTH:
2946 wpa_supplicant_event_unprot_deauth(wpa_s,
2947 &data->unprot_deauth);
2948 break;
2949 case EVENT_UNPROT_DISASSOC:
2950 wpa_supplicant_event_unprot_disassoc(wpa_s,
2951 &data->unprot_disassoc);
2952 break;
2953 case EVENT_STATION_LOW_ACK:
2954#ifdef CONFIG_AP
2955 if (wpa_s->ap_iface && data)
2956 hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2957 data->low_ack.addr);
2958#endif /* CONFIG_AP */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002959#ifdef CONFIG_TDLS
2960 if (data)
2961 wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2962#endif /* CONFIG_TDLS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 break;
2964 case EVENT_IBSS_PEER_LOST:
2965#ifdef CONFIG_IBSS_RSN
2966 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2967#endif /* CONFIG_IBSS_RSN */
2968 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002969 case EVENT_DRIVER_GTK_REKEY:
2970 if (os_memcmp(data->driver_gtk_rekey.bssid,
2971 wpa_s->bssid, ETH_ALEN))
2972 break;
2973 if (!wpa_s->wpa)
2974 break;
2975 wpa_sm_update_replay_ctr(wpa_s->wpa,
2976 data->driver_gtk_rekey.replay_ctr);
2977 break;
2978 case EVENT_SCHED_SCAN_STOPPED:
2979 wpa_s->sched_scanning = 0;
2980 wpa_supplicant_notify_scanning(wpa_s, 0);
2981
2982 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2983 break;
2984
2985 /*
2986 * If we timed out, start a new sched scan to continue
2987 * searching for more SSIDs.
2988 */
2989 if (wpa_s->sched_scan_timed_out)
2990 wpa_supplicant_req_sched_scan(wpa_s);
2991 break;
2992 case EVENT_WPS_BUTTON_PUSHED:
2993#ifdef CONFIG_WPS
2994 wpas_wps_start_pbc(wpa_s, NULL, 0);
2995#endif /* CONFIG_WPS */
2996 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 default:
2998 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2999 break;
3000 }
3001}