Use sched_scan in driver init
[mech_eap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "pcsc_funcs.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "common/wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ap/hostapd.h"
31 #include "p2p/p2p.h"
32 #include "notify.h"
33 #include "common/ieee802_11_defs.h"
34 #include "common/ieee802_11_common.h"
35 #include "crypto/random.h"
36 #include "blacklist.h"
37 #include "wpas_glue.h"
38 #include "wps_supplicant.h"
39 #include "ibss_rsn.h"
40 #include "sme.h"
41 #include "gas_query.h"
42 #include "p2p_supplicant.h"
43 #include "bgscan.h"
44 #include "ap.h"
45 #include "bss.h"
46 #include "mlme.h"
47 #include "scan.h"
48 #include "offchannel.h"
49
50
51 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
52 {
53         struct wpa_ssid *ssid, *old_ssid;
54
55         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
56                 return 0;
57
58         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
59                 "information");
60         ssid = wpa_supplicant_get_ssid(wpa_s);
61         if (ssid == NULL) {
62                 wpa_msg(wpa_s, MSG_INFO,
63                         "No network configuration found for the current AP");
64                 return -1;
65         }
66
67         if (ssid->disabled) {
68                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
69                 return -1;
70         }
71
72         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
73                 "current AP");
74         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
75                               WPA_KEY_MGMT_WPA_NONE |
76                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
77                               WPA_KEY_MGMT_PSK_SHA256 |
78                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
79                 u8 wpa_ie[80];
80                 size_t wpa_ie_len = sizeof(wpa_ie);
81                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
82                                           wpa_ie, &wpa_ie_len);
83         } else {
84                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
85         }
86
87         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
88                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
89         old_ssid = wpa_s->current_ssid;
90         wpa_s->current_ssid = ssid;
91         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
92         wpa_supplicant_initiate_eapol(wpa_s);
93         if (old_ssid != wpa_s->current_ssid)
94                 wpas_notify_network_changed(wpa_s);
95
96         return 0;
97 }
98
99
100 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
101                                                 void *sock_ctx)
102 {
103         struct wpa_supplicant *wpa_s = eloop_ctx;
104
105         if (wpa_s->countermeasures) {
106                 wpa_s->countermeasures = 0;
107                 wpa_drv_set_countermeasures(wpa_s, 0);
108                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
109                 wpa_supplicant_req_scan(wpa_s, 0, 0);
110         }
111 }
112
113
114 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
115 {
116         int bssid_changed;
117
118 #ifdef CONFIG_IBSS_RSN
119         ibss_rsn_deinit(wpa_s->ibss_rsn);
120         wpa_s->ibss_rsn = NULL;
121 #endif /* CONFIG_IBSS_RSN */
122
123 #ifdef CONFIG_AP
124         wpa_supplicant_ap_deinit(wpa_s);
125 #endif /* CONFIG_AP */
126
127         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
128                 return;
129
130         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
131         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
132         os_memset(wpa_s->bssid, 0, ETH_ALEN);
133         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
134         wpa_s->current_bss = NULL;
135         wpa_s->assoc_freq = 0;
136 #ifdef CONFIG_IEEE80211R
137 #ifdef CONFIG_SME
138         if (wpa_s->sme.ft_ies)
139                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
140 #endif /* CONFIG_SME */
141 #endif /* CONFIG_IEEE80211R */
142
143         if (bssid_changed)
144                 wpas_notify_bssid_changed(wpa_s);
145
146         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
147         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
148         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
149                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
150         wpa_s->ap_ies_from_associnfo = 0;
151 }
152
153
154 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
155 {
156         struct wpa_ie_data ie;
157         int pmksa_set = -1;
158         size_t i;
159
160         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
161             ie.pmkid == NULL)
162                 return;
163
164         for (i = 0; i < ie.num_pmkid; i++) {
165                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
166                                                     ie.pmkid + i * PMKID_LEN,
167                                                     NULL, NULL, 0);
168                 if (pmksa_set == 0) {
169                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
170                         break;
171                 }
172         }
173
174         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
175                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
176 }
177
178
179 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
180                                                  union wpa_event_data *data)
181 {
182         if (data == NULL) {
183                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
184                         "event");
185                 return;
186         }
187         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
188                 " index=%d preauth=%d",
189                 MAC2STR(data->pmkid_candidate.bssid),
190                 data->pmkid_candidate.index,
191                 data->pmkid_candidate.preauth);
192
193         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
194                             data->pmkid_candidate.index,
195                             data->pmkid_candidate.preauth);
196 }
197
198
199 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
200 {
201         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
202             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
203                 return 0;
204
205 #ifdef IEEE8021X_EAPOL
206         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
207             wpa_s->current_ssid &&
208             !(wpa_s->current_ssid->eapol_flags &
209               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
210                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
211                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
212                  * plaintext or static WEP keys). */
213                 return 0;
214         }
215 #endif /* IEEE8021X_EAPOL */
216
217         return 1;
218 }
219
220
221 /**
222  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
223  * @wpa_s: pointer to wpa_supplicant data
224  * @ssid: Configuration data for the network
225  * Returns: 0 on success, -1 on failure
226  *
227  * This function is called when starting authentication with a network that is
228  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
229  */
230 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
231                               struct wpa_ssid *ssid)
232 {
233 #ifdef IEEE8021X_EAPOL
234         int aka = 0, sim = 0, type;
235
236         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
237                 return 0;
238
239         if (ssid->eap.eap_methods == NULL) {
240                 sim = 1;
241                 aka = 1;
242         } else {
243                 struct eap_method_type *eap = ssid->eap.eap_methods;
244                 while (eap->vendor != EAP_VENDOR_IETF ||
245                        eap->method != EAP_TYPE_NONE) {
246                         if (eap->vendor == EAP_VENDOR_IETF) {
247                                 if (eap->method == EAP_TYPE_SIM)
248                                         sim = 1;
249                                 else if (eap->method == EAP_TYPE_AKA)
250                                         aka = 1;
251                         }
252                         eap++;
253                 }
254         }
255
256         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
257                 sim = 0;
258         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
259                 aka = 0;
260
261         if (!sim && !aka) {
262                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
263                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
264                         "enabled");
265                 return 0;
266         }
267
268         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
269                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
270         if (sim && aka)
271                 type = SCARD_TRY_BOTH;
272         else if (aka)
273                 type = SCARD_USIM_ONLY;
274         else
275                 type = SCARD_GSM_SIM_ONLY;
276
277         wpa_s->scard = scard_init(type);
278         if (wpa_s->scard == NULL) {
279                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
280                         "(pcsc-lite)");
281                 return -1;
282         }
283         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
284         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
285 #endif /* IEEE8021X_EAPOL */
286
287         return 0;
288 }
289
290
291 #ifndef CONFIG_NO_SCAN_PROCESSING
292 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
293                                         struct wpa_ssid *ssid)
294 {
295         int i, privacy = 0;
296
297         if (ssid->mixed_cell)
298                 return 1;
299
300 #ifdef CONFIG_WPS
301         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
302                 return 1;
303 #endif /* CONFIG_WPS */
304
305         for (i = 0; i < NUM_WEP_KEYS; i++) {
306                 if (ssid->wep_key_len[i]) {
307                         privacy = 1;
308                         break;
309                 }
310         }
311 #ifdef IEEE8021X_EAPOL
312         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
313             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
314                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
315                 privacy = 1;
316 #endif /* IEEE8021X_EAPOL */
317
318         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
319                 privacy = 1;
320
321         if (bss->caps & IEEE80211_CAP_PRIVACY)
322                 return privacy;
323         return !privacy;
324 }
325
326
327 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
328                                          struct wpa_ssid *ssid,
329                                          struct wpa_scan_res *bss)
330 {
331         struct wpa_ie_data ie;
332         int proto_match = 0;
333         const u8 *rsn_ie, *wpa_ie;
334         int ret;
335         int wep_ok;
336
337         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
338         if (ret >= 0)
339                 return ret;
340
341         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
342         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
343                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
344                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
345                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
346
347         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
348         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
349                 proto_match++;
350
351                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
352                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
353                                 "failed");
354                         break;
355                 }
356
357                 if (wep_ok &&
358                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
359                 {
360                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
361                                 "in RSN IE");
362                         return 1;
363                 }
364
365                 if (!(ie.proto & ssid->proto)) {
366                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
367                                 "mismatch");
368                         break;
369                 }
370
371                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
372                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
373                                 "cipher mismatch");
374                         break;
375                 }
376
377                 if (!(ie.group_cipher & ssid->group_cipher)) {
378                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
379                                 "cipher mismatch");
380                         break;
381                 }
382
383                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
384                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
385                                 "mismatch");
386                         break;
387                 }
388
389 #ifdef CONFIG_IEEE80211W
390                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
391                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
392                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
393                                 "frame protection");
394                         break;
395                 }
396 #endif /* CONFIG_IEEE80211W */
397
398                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
399                 return 1;
400         }
401
402         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
403         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
404                 proto_match++;
405
406                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
407                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
408                                 "failed");
409                         break;
410                 }
411
412                 if (wep_ok &&
413                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
414                 {
415                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
416                                 "in WPA IE");
417                         return 1;
418                 }
419
420                 if (!(ie.proto & ssid->proto)) {
421                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
422                                 "mismatch");
423                         break;
424                 }
425
426                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
427                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
428                                 "cipher mismatch");
429                         break;
430                 }
431
432                 if (!(ie.group_cipher & ssid->group_cipher)) {
433                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
434                                 "cipher mismatch");
435                         break;
436                 }
437
438                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
439                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
440                                 "mismatch");
441                         break;
442                 }
443
444                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
445                 return 1;
446         }
447
448         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
449             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
450                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
451                 return 0;
452         }
453
454         if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
455                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
456                 return 1;
457         }
458
459         wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
460                 "WPA/WPA2");
461
462         return 0;
463 }
464
465
466 static int freq_allowed(int *freqs, int freq)
467 {
468         int i;
469
470         if (freqs == NULL)
471                 return 1;
472
473         for (i = 0; freqs[i]; i++)
474                 if (freqs[i] == freq)
475                         return 1;
476         return 0;
477 }
478
479
480 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
481                                             int i, struct wpa_scan_res *bss,
482                                             struct wpa_ssid *group)
483 {
484         const u8 *ssid_;
485         u8 wpa_ie_len, rsn_ie_len, ssid_len;
486         int wpa;
487         struct wpa_blacklist *e;
488         const u8 *ie;
489         struct wpa_ssid *ssid;
490
491         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
492         ssid_ = ie ? ie + 2 : (u8 *) "";
493         ssid_len = ie ? ie[1] : 0;
494
495         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
496         wpa_ie_len = ie ? ie[1] : 0;
497
498         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
499         rsn_ie_len = ie ? ie[1] : 0;
500
501         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
502                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
503                 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
504                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
505                 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
506
507         e = wpa_blacklist_get(wpa_s, bss->bssid);
508         if (e) {
509                 int limit = 1;
510                 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
511                         /*
512                          * When only a single network is enabled, we can
513                          * trigger blacklisting on the first failure. This
514                          * should not be done with multiple enabled networks to
515                          * avoid getting forced to move into a worse ESS on
516                          * single error if there are no other BSSes of the
517                          * current ESS.
518                          */
519                         limit = 0;
520                 }
521                 if (e->count > limit) {
522                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
523                                 "(count=%d limit=%d)", e->count, limit);
524                         return NULL;
525                 }
526         }
527
528         if (ssid_len == 0) {
529                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
530                 return NULL;
531         }
532
533         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
534
535         for (ssid = group; ssid; ssid = ssid->pnext) {
536                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
537
538                 if (ssid->disabled) {
539                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
540                         continue;
541                 }
542
543 #ifdef CONFIG_WPS
544                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
545                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
546                                 "(WPS)");
547                         continue;
548                 }
549
550                 if (wpa && ssid->ssid_len == 0 &&
551                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
552                         check_ssid = 0;
553
554                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
555                         /* Only allow wildcard SSID match if an AP
556                          * advertises active WPS operation that matches
557                          * with our mode. */
558                         check_ssid = 1;
559                         if (ssid->ssid_len == 0 &&
560                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
561                                 check_ssid = 0;
562                 }
563 #endif /* CONFIG_WPS */
564
565                 if (ssid->bssid_set && ssid->ssid_len == 0 &&
566                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
567                         check_ssid = 0;
568
569                 if (check_ssid &&
570                     (ssid_len != ssid->ssid_len ||
571                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
572                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
573                         continue;
574                 }
575
576                 if (ssid->bssid_set &&
577                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
578                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
579                         continue;
580                 }
581
582                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
583                         continue;
584
585                 if (!wpa &&
586                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
587                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
588                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
589                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
590                                 "not allowed");
591                         continue;
592                 }
593
594                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
595                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
596                                 "mismatch");
597                         continue;
598                 }
599
600                 if (bss->caps & IEEE80211_CAP_IBSS) {
601                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
602                                 "network");
603                         continue;
604                 }
605
606                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
607                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
608                                 "allowed");
609                         continue;
610                 }
611
612 #ifdef CONFIG_P2P
613                 /*
614                  * TODO: skip the AP if its P2P IE has Group Formation
615                  * bit set in the P2P Group Capability Bitmap and we
616                  * are not in Group Formation with that device.
617                  */
618 #endif /* CONFIG_P2P */
619
620                 /* Matching configuration found */
621                 return ssid;
622         }
623
624         /* No matching configuration found */
625         return NULL;
626 }
627
628
629 static struct wpa_bss *
630 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
631                           struct wpa_scan_results *scan_res,
632                           struct wpa_ssid *group,
633                           struct wpa_ssid **selected_ssid)
634 {
635         size_t i;
636
637         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
638                 group->priority);
639
640         for (i = 0; i < scan_res->num; i++) {
641                 struct wpa_scan_res *bss = scan_res->res[i];
642                 const u8 *ie, *ssid;
643                 u8 ssid_len;
644
645                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
646                 if (!*selected_ssid)
647                         continue;
648
649                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
650                 ssid = ie ? ie + 2 : (u8 *) "";
651                 ssid_len = ie ? ie[1] : 0;
652
653                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
654                         " ssid='%s'",
655                         MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
656                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
657         }
658
659         return NULL;
660 }
661
662
663 static struct wpa_bss *
664 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
665                             struct wpa_scan_results *scan_res,
666                             struct wpa_ssid **selected_ssid)
667 {
668         struct wpa_bss *selected = NULL;
669         int prio;
670
671         while (selected == NULL) {
672                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
673                         selected = wpa_supplicant_select_bss(
674                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
675                                 selected_ssid);
676                         if (selected)
677                                 break;
678                 }
679
680                 if (selected == NULL && wpa_s->blacklist) {
681                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
682                                 "blacklist and try again");
683                         wpa_blacklist_clear(wpa_s);
684                         wpa_s->blacklist_cleared++;
685                 } else if (selected == NULL)
686                         break;
687         }
688
689         return selected;
690 }
691
692
693 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
694                                         int timeout_sec, int timeout_usec)
695 {
696         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
697                 /*
698                  * No networks are enabled; short-circuit request so
699                  * we don't wait timeout seconds before transitioning
700                  * to INACTIVE state.
701                  */
702                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
703                 return;
704         }
705         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
706 }
707
708
709 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
710                             struct wpa_bss *selected,
711                             struct wpa_ssid *ssid)
712 {
713         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
714                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
715                         "PBC session overlap");
716 #ifdef CONFIG_P2P
717                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
718                         return;
719 #endif /* CONFIG_P2P */
720
721 #ifdef CONFIG_WPS
722                 wpas_wps_cancel(wpa_s);
723 #endif /* CONFIG_WPS */
724                 return;
725         }
726
727         /*
728          * Do not trigger new association unless the BSSID has changed or if
729          * reassociation is requested. If we are in process of associating with
730          * the selected BSSID, do not trigger new attempt.
731          */
732         if (wpa_s->reassociate ||
733             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
734              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
735                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
736               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
737               0))) {
738                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
739                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
740                         return;
741                 }
742                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
743                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
744                         "  pending: " MACSTR "  wpa_state: %s",
745                         wpa_s->reassociate, MAC2STR(selected->bssid),
746                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
747                         wpa_supplicant_state_txt(wpa_s->wpa_state));
748                 wpa_supplicant_associate(wpa_s, selected, ssid);
749         } else {
750                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
751                         "selected AP");
752         }
753 }
754
755
756 static struct wpa_ssid *
757 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
758 {
759         int prio;
760         struct wpa_ssid *ssid;
761
762         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
763                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
764                 {
765                         if (ssid->disabled)
766                                 continue;
767                         if (ssid->mode == IEEE80211_MODE_IBSS ||
768                             ssid->mode == IEEE80211_MODE_AP)
769                                 return ssid;
770                 }
771         }
772         return NULL;
773 }
774
775
776 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
777  * on BSS added and BSS changed events */
778 static void wpa_supplicant_rsn_preauth_scan_results(
779         struct wpa_supplicant *wpa_s)
780 {
781         struct wpa_bss *bss;
782
783         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
784                 return;
785
786         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
787                 const u8 *ssid, *rsn;
788
789                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
790                 if (ssid == NULL)
791                         continue;
792
793                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
794                 if (rsn == NULL)
795                         continue;
796
797                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
798         }
799
800 }
801
802
803 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
804                                        struct wpa_bss *selected,
805                                        struct wpa_ssid *ssid,
806                                        struct wpa_scan_results *scan_res)
807 {
808         size_t i;
809         struct wpa_scan_res *current_bss = NULL;
810         int min_diff;
811
812         if (wpa_s->reassociate)
813                 return 1; /* explicit request to reassociate */
814         if (wpa_s->wpa_state < WPA_ASSOCIATED)
815                 return 1; /* we are not associated; continue */
816         if (wpa_s->current_ssid == NULL)
817                 return 1; /* unknown current SSID */
818         if (wpa_s->current_ssid != ssid)
819                 return 1; /* different network block */
820
821         if (wpas_driver_bss_selection(wpa_s))
822                 return 0; /* Driver-based roaming */
823
824         for (i = 0; i < scan_res->num; i++) {
825                 struct wpa_scan_res *res = scan_res->res[i];
826                 const u8 *ie;
827                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
828                         continue;
829
830                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
831                 if (ie == NULL)
832                         continue;
833                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
834                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
835                         continue;
836                 current_bss = res;
837                 break;
838         }
839
840         if (!current_bss)
841                 return 1; /* current BSS not seen in scan results */
842
843         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
844         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
845                 MAC2STR(current_bss->bssid), current_bss->level);
846         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
847                 MAC2STR(selected->bssid), selected->level);
848
849         if (wpa_s->current_ssid->bssid_set &&
850             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
851             0) {
852                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
853                         "has preferred BSSID");
854                 return 1;
855         }
856
857         min_diff = 2;
858         if (current_bss->level < 0) {
859                 if (current_bss->level < -85)
860                         min_diff = 1;
861                 else if (current_bss->level < -80)
862                         min_diff = 2;
863                 else if (current_bss->level < -75)
864                         min_diff = 3;
865                 else if (current_bss->level < -70)
866                         min_diff = 4;
867                 else
868                         min_diff = 5;
869         }
870         if (abs(current_bss->level - selected->level) < min_diff) {
871                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
872                         "in signal level");
873                 return 0;
874         }
875
876         return 1;
877 }
878
879 /* Return < 0 if no scan results could be fetched. */
880 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
881                                               union wpa_event_data *data)
882 {
883         struct wpa_bss *selected;
884         struct wpa_ssid *ssid = NULL;
885         struct wpa_scan_results *scan_res;
886         int ap = 0;
887
888 #ifdef CONFIG_AP
889         if (wpa_s->ap_iface)
890                 ap = 1;
891 #endif /* CONFIG_AP */
892
893         wpa_supplicant_notify_scanning(wpa_s, 0);
894
895         scan_res = wpa_supplicant_get_scan_results(wpa_s,
896                                                    data ? &data->scan_info :
897                                                    NULL, 1);
898         if (scan_res == NULL) {
899                 if (wpa_s->conf->ap_scan == 2 || ap)
900                         return -1;
901                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
902                         "scanning again");
903                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
904                 return -1;
905         }
906
907 #ifndef CONFIG_NO_RANDOM_POOL
908         size_t i, num;
909         num = scan_res->num;
910         if (num > 10)
911                 num = 10;
912         for (i = 0; i < num; i++) {
913                 u8 buf[5];
914                 struct wpa_scan_res *res = scan_res->res[i];
915                 buf[0] = res->bssid[5];
916                 buf[1] = res->qual & 0xff;
917                 buf[2] = res->noise & 0xff;
918                 buf[3] = res->level & 0xff;
919                 buf[4] = res->tsf & 0xff;
920                 random_add_randomness(buf, sizeof(buf));
921         }
922 #endif /* CONFIG_NO_RANDOM_POOL */
923
924         if (wpa_s->scan_res_handler) {
925                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
926                                          struct wpa_scan_results *scan_res);
927
928                 scan_res_handler = wpa_s->scan_res_handler;
929                 wpa_s->scan_res_handler = NULL;
930                 scan_res_handler(wpa_s, scan_res);
931
932                 wpa_scan_results_free(scan_res);
933                 return 0;
934         }
935
936         if (ap) {
937                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
938 #ifdef CONFIG_AP
939                 if (wpa_s->ap_iface->scan_cb)
940                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
941 #endif /* CONFIG_AP */
942                 wpa_scan_results_free(scan_res);
943                 return 0;
944         }
945
946         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
947         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
948         wpas_notify_scan_results(wpa_s);
949
950         wpas_notify_scan_done(wpa_s, 1);
951
952         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
953                 wpa_scan_results_free(scan_res);
954                 return 0;
955         }
956
957         if (wpa_s->disconnected) {
958                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
959                 wpa_scan_results_free(scan_res);
960                 return 0;
961         }
962
963         if (!wpas_driver_bss_selection(wpa_s) &&
964             bgscan_notify_scan(wpa_s, scan_res) == 1) {
965                 wpa_scan_results_free(scan_res);
966                 return 0;
967         }
968
969         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
970
971         if (selected) {
972                 int skip;
973                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
974                                                     scan_res);
975                 wpa_scan_results_free(scan_res);
976                 if (skip)
977                         return 0;
978                 wpa_supplicant_connect(wpa_s, selected, ssid);
979                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
980         } else {
981                 wpa_scan_results_free(scan_res);
982                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
983                 ssid = wpa_supplicant_pick_new_network(wpa_s);
984                 if (ssid) {
985                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
986                         wpa_supplicant_associate(wpa_s, NULL, ssid);
987                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
988                 } else {
989                         int timeout_sec = wpa_s->scan_interval;
990                         int timeout_usec = 0;
991 #ifdef CONFIG_P2P
992                         if (wpa_s->p2p_in_provisioning) {
993                                 /*
994                                  * Use shorter wait during P2P Provisioning
995                                  * state to speed up group formation.
996                                  */
997                                 timeout_sec = 0;
998                                 timeout_usec = 250000;
999                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1000                                                             timeout_usec);
1001                                 return 0;
1002                         }
1003 #endif /* CONFIG_P2P */
1004                         if (wpa_supplicant_req_sched_scan(wpa_s))
1005                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1006                                                             timeout_usec);
1007                 }
1008         }
1009         return 0;
1010 }
1011
1012
1013 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1014                                               union wpa_event_data *data)
1015 {
1016         const char *rn, *rn2;
1017         struct wpa_supplicant *ifs;
1018
1019         if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1020                 /*
1021                  * If no scan results could be fetched, then no need to
1022                  * notify those interfaces that did not actually request
1023                  * this scan.
1024                  */
1025                 return;
1026         }
1027
1028         /*
1029          * Check other interfaces to see if they have the same radio-name. If
1030          * so, they get updated with this same scan info.
1031          */
1032         if (!wpa_s->driver->get_radio_name)
1033                 return;
1034
1035         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1036         if (rn == NULL || rn[0] == '\0')
1037                 return;
1038
1039         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1040                 "sharing same radio (%s) in event_scan_results", rn);
1041
1042         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1043                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1044                         continue;
1045
1046                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1047                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1048                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1049                                    "sibling", ifs->ifname);
1050                         _wpa_supplicant_event_scan_results(ifs, data);
1051                 }
1052         }
1053 }
1054
1055 #endif /* CONFIG_NO_SCAN_PROCESSING */
1056
1057
1058 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1059                                           union wpa_event_data *data)
1060 {
1061         int l, len, found = 0, wpa_found, rsn_found;
1062         const u8 *p;
1063
1064         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1065         if (data->assoc_info.req_ies)
1066                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1067                             data->assoc_info.req_ies_len);
1068         if (data->assoc_info.resp_ies) {
1069                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1070                             data->assoc_info.resp_ies_len);
1071 #ifdef CONFIG_TDLS
1072                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1073                                         data->assoc_info.resp_ies_len);
1074 #endif /* CONFIG_TDLS */
1075         }
1076         if (data->assoc_info.beacon_ies)
1077                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1078                             data->assoc_info.beacon_ies,
1079                             data->assoc_info.beacon_ies_len);
1080         if (data->assoc_info.freq)
1081                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1082                         data->assoc_info.freq);
1083
1084         p = data->assoc_info.req_ies;
1085         l = data->assoc_info.req_ies_len;
1086
1087         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1088         while (p && l >= 2) {
1089                 len = p[1] + 2;
1090                 if (len > l) {
1091                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1092                                     p, l);
1093                         break;
1094                 }
1095                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1096                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1097                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1098                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1099                                 break;
1100                         found = 1;
1101                         wpa_find_assoc_pmkid(wpa_s);
1102                         break;
1103                 }
1104                 l -= len;
1105                 p += len;
1106         }
1107         if (!found && data->assoc_info.req_ies)
1108                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1109
1110 #ifdef CONFIG_IEEE80211R
1111 #ifdef CONFIG_SME
1112         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1113                 u8 bssid[ETH_ALEN];
1114                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1115                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1116                                                  data->assoc_info.resp_ies,
1117                                                  data->assoc_info.resp_ies_len,
1118                                                  bssid) < 0) {
1119                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1120                                 "Reassociation Response failed");
1121                         wpa_supplicant_deauthenticate(
1122                                 wpa_s, WLAN_REASON_INVALID_IE);
1123                         return -1;
1124                 }
1125         }
1126
1127         p = data->assoc_info.resp_ies;
1128         l = data->assoc_info.resp_ies_len;
1129
1130 #ifdef CONFIG_WPS_STRICT
1131         if (p && wpa_s->current_ssid &&
1132             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1133                 struct wpabuf *wps;
1134                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1135                 if (wps == NULL) {
1136                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1137                                 "include WPS IE in (Re)Association Response");
1138                         return -1;
1139                 }
1140
1141                 if (wps_validate_assoc_resp(wps) < 0) {
1142                         wpabuf_free(wps);
1143                         wpa_supplicant_deauthenticate(
1144                                 wpa_s, WLAN_REASON_INVALID_IE);
1145                         return -1;
1146                 }
1147                 wpabuf_free(wps);
1148         }
1149 #endif /* CONFIG_WPS_STRICT */
1150
1151         /* Go through the IEs and make a copy of the MDIE, if present. */
1152         while (p && l >= 2) {
1153                 len = p[1] + 2;
1154                 if (len > l) {
1155                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1156                                     p, l);
1157                         break;
1158                 }
1159                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1160                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1161                         wpa_s->sme.ft_used = 1;
1162                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1163                                   MOBILITY_DOMAIN_ID_LEN);
1164                         break;
1165                 }
1166                 l -= len;
1167                 p += len;
1168         }
1169 #endif /* CONFIG_SME */
1170
1171         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1172                              data->assoc_info.resp_ies_len);
1173 #endif /* CONFIG_IEEE80211R */
1174
1175         /* WPA/RSN IE from Beacon/ProbeResp */
1176         p = data->assoc_info.beacon_ies;
1177         l = data->assoc_info.beacon_ies_len;
1178
1179         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1180          */
1181         wpa_found = rsn_found = 0;
1182         while (p && l >= 2) {
1183                 len = p[1] + 2;
1184                 if (len > l) {
1185                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1186                                     p, l);
1187                         break;
1188                 }
1189                 if (!wpa_found &&
1190                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1191                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1192                         wpa_found = 1;
1193                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1194                 }
1195
1196                 if (!rsn_found &&
1197                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1198                         rsn_found = 1;
1199                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1200                 }
1201
1202                 l -= len;
1203                 p += len;
1204         }
1205
1206         if (!wpa_found && data->assoc_info.beacon_ies)
1207                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1208         if (!rsn_found && data->assoc_info.beacon_ies)
1209                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1210         if (wpa_found || rsn_found)
1211                 wpa_s->ap_ies_from_associnfo = 1;
1212
1213         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1214             wpa_s->assoc_freq != data->assoc_info.freq) {
1215                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1216                            "%u to %u MHz",
1217                            wpa_s->assoc_freq, data->assoc_info.freq);
1218                 wpa_supplicant_update_scan_results(wpa_s);
1219         }
1220
1221         wpa_s->assoc_freq = data->assoc_info.freq;
1222
1223         return 0;
1224 }
1225
1226
1227 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1228                                        union wpa_event_data *data)
1229 {
1230         u8 bssid[ETH_ALEN];
1231         int ft_completed;
1232         int bssid_changed;
1233         struct wpa_driver_capa capa;
1234
1235 #ifdef CONFIG_AP
1236         if (wpa_s->ap_iface) {
1237                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1238                                     data->assoc_info.addr,
1239                                     data->assoc_info.req_ies,
1240                                     data->assoc_info.req_ies_len,
1241                                     data->assoc_info.reassoc);
1242                 return;
1243         }
1244 #endif /* CONFIG_AP */
1245
1246         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1247         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1248                 return;
1249
1250         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1251         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1252                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1253         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1254             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1255              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1256                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1257                         MACSTR, MAC2STR(bssid));
1258                 random_add_randomness(bssid, ETH_ALEN);
1259                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1260                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1261                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1262                 if (bssid_changed)
1263                         wpas_notify_bssid_changed(wpa_s);
1264
1265                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1266                         wpa_clear_keys(wpa_s, bssid);
1267                 }
1268                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1269                         wpa_supplicant_disassociate(
1270                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1271                         return;
1272                 }
1273                 if (wpa_s->current_ssid) {
1274                         struct wpa_bss *bss = NULL;
1275                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1276                         if (ssid->ssid_len > 0)
1277                                 bss = wpa_bss_get(wpa_s, bssid,
1278                                                   ssid->ssid, ssid->ssid_len);
1279                         if (!bss)
1280                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1281                         if (bss)
1282                                 wpa_s->current_bss = bss;
1283                 }
1284         }
1285
1286 #ifdef CONFIG_SME
1287         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1288         wpa_s->sme.prev_bssid_set = 1;
1289 #endif /* CONFIG_SME */
1290
1291         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1292         if (wpa_s->current_ssid) {
1293                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1294                  * initialized before association, but for other modes,
1295                  * initialize PC/SC here, if the current configuration needs
1296                  * smartcard or SIM/USIM. */
1297                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1298         }
1299         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1300         if (wpa_s->l2)
1301                 l2_packet_notify_auth_start(wpa_s->l2);
1302
1303         /*
1304          * Set portEnabled first to FALSE in order to get EAP state machine out
1305          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1306          * state machine may transit to AUTHENTICATING state based on obsolete
1307          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1308          * AUTHENTICATED without ever giving chance to EAP state machine to
1309          * reset the state.
1310          */
1311         if (!ft_completed) {
1312                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1313                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1314         }
1315         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1316                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1317         /* 802.1X::portControl = Auto */
1318         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1319         wpa_s->eapol_received = 0;
1320         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1321             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1322             (wpa_s->current_ssid &&
1323              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1324                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1325                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1326         } else if (!ft_completed) {
1327                 /* Timeout for receiving the first EAPOL packet */
1328                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1329         }
1330         wpa_supplicant_cancel_scan(wpa_s);
1331
1332         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1333             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1334                 /*
1335                  * We are done; the driver will take care of RSN 4-way
1336                  * handshake.
1337                  */
1338                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1339                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1340                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1341                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1342         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1343                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1344                 /*
1345                  * The driver will take care of RSN 4-way handshake, so we need
1346                  * to allow EAPOL supplicant to complete its work without
1347                  * waiting for WPA supplicant.
1348                  */
1349                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1350         } else if (ft_completed) {
1351                 /*
1352                  * FT protocol completed - make sure EAPOL state machine ends
1353                  * up in authenticated.
1354                  */
1355                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1356                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1357                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1358                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1359         }
1360
1361         if (wpa_s->pending_eapol_rx) {
1362                 struct os_time now, age;
1363                 os_get_time(&now);
1364                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1365                 if (age.sec == 0 && age.usec < 100000 &&
1366                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1367                     0) {
1368                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1369                                 "frame that was received just before "
1370                                 "association notification");
1371                         wpa_supplicant_rx_eapol(
1372                                 wpa_s, wpa_s->pending_eapol_rx_src,
1373                                 wpabuf_head(wpa_s->pending_eapol_rx),
1374                                 wpabuf_len(wpa_s->pending_eapol_rx));
1375                 }
1376                 wpabuf_free(wpa_s->pending_eapol_rx);
1377                 wpa_s->pending_eapol_rx = NULL;
1378         }
1379
1380         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1381              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1382             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1383             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1384                 /* Set static WEP keys again */
1385                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1386         }
1387
1388 #ifdef CONFIG_IBSS_RSN
1389         if (wpa_s->current_ssid &&
1390             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1391             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1392             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1393             wpa_s->ibss_rsn == NULL) {
1394                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1395                 if (!wpa_s->ibss_rsn) {
1396                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1397                         wpa_supplicant_deauthenticate(
1398                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1399                         return;
1400                 }
1401
1402                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1403         }
1404 #endif /* CONFIG_IBSS_RSN */
1405 }
1406
1407
1408 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1409                                           u16 reason_code)
1410 {
1411         const u8 *bssid;
1412         int authenticating;
1413         u8 prev_pending_bssid[ETH_ALEN];
1414
1415         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1416         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1417
1418         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1419                 /*
1420                  * At least Host AP driver and a Prism3 card seemed to be
1421                  * generating streams of disconnected events when configuring
1422                  * IBSS for WPA-None. Ignore them for now.
1423                  */
1424                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1425                         "IBSS/WPA-None mode");
1426                 return;
1427         }
1428
1429         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1430             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1431                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1432                         "pre-shared key may be incorrect");
1433         }
1434         if (!wpa_s->auto_reconnect_disabled ||
1435             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1436                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
1437                         "reconnect (wps=%d)",
1438                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1439                 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1440                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1441         } else {
1442                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
1443                         "try to re-connect");
1444                 wpa_s->reassociate = 0;
1445                 wpa_s->disconnected = 1;
1446         }
1447         bssid = wpa_s->bssid;
1448         if (is_zero_ether_addr(bssid))
1449                 bssid = wpa_s->pending_bssid;
1450         wpas_connection_failed(wpa_s, bssid);
1451         wpa_sm_notify_disassoc(wpa_s->wpa);
1452         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1453                 " reason=%d",
1454                 MAC2STR(bssid), reason_code);
1455         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1456                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1457                 wpa_s->keys_cleared = 0;
1458                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1459         }
1460         wpa_supplicant_mark_disassoc(wpa_s);
1461
1462         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1463                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1464 }
1465
1466
1467 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1468 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1469                                                     void *sock_ctx)
1470 {
1471         struct wpa_supplicant *wpa_s = eloop_ctx;
1472
1473         if (!wpa_s->pending_mic_error_report)
1474                 return;
1475
1476         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1477         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1478         wpa_s->pending_mic_error_report = 0;
1479 }
1480 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1481
1482
1483 static void
1484 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1485                                          union wpa_event_data *data)
1486 {
1487         int pairwise;
1488         struct os_time t;
1489
1490         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1491         pairwise = (data && data->michael_mic_failure.unicast);
1492         os_get_time(&t);
1493         if ((wpa_s->last_michael_mic_error &&
1494              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1495             wpa_s->pending_mic_error_report) {
1496                 if (wpa_s->pending_mic_error_report) {
1497                         /*
1498                          * Send the pending MIC error report immediately since
1499                          * we are going to start countermeasures and AP better
1500                          * do the same.
1501                          */
1502                         wpa_sm_key_request(wpa_s->wpa, 1,
1503                                            wpa_s->pending_mic_error_pairwise);
1504                 }
1505
1506                 /* Send the new MIC error report immediately since we are going
1507                  * to start countermeasures and AP better do the same.
1508                  */
1509                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1510
1511                 /* initialize countermeasures */
1512                 wpa_s->countermeasures = 1;
1513                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1514
1515                 /*
1516                  * Need to wait for completion of request frame. We do not get
1517                  * any callback for the message completion, so just wait a
1518                  * short while and hope for the best. */
1519                 os_sleep(0, 10000);
1520
1521                 wpa_drv_set_countermeasures(wpa_s, 1);
1522                 wpa_supplicant_deauthenticate(wpa_s,
1523                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1524                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1525                                      wpa_s, NULL);
1526                 eloop_register_timeout(60, 0,
1527                                        wpa_supplicant_stop_countermeasures,
1528                                        wpa_s, NULL);
1529                 /* TODO: mark the AP rejected for 60 second. STA is
1530                  * allowed to associate with another AP.. */
1531         } else {
1532 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1533                 if (wpa_s->mic_errors_seen) {
1534                         /*
1535                          * Reduce the effectiveness of Michael MIC error
1536                          * reports as a means for attacking against TKIP if
1537                          * more than one MIC failure is noticed with the same
1538                          * PTK. We delay the transmission of the reports by a
1539                          * random time between 0 and 60 seconds in order to
1540                          * force the attacker wait 60 seconds before getting
1541                          * the information on whether a frame resulted in a MIC
1542                          * failure.
1543                          */
1544                         u8 rval[4];
1545                         int sec;
1546
1547                         if (os_get_random(rval, sizeof(rval)) < 0)
1548                                 sec = os_random() % 60;
1549                         else
1550                                 sec = WPA_GET_BE32(rval) % 60;
1551                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1552                                 "report %d seconds", sec);
1553                         wpa_s->pending_mic_error_report = 1;
1554                         wpa_s->pending_mic_error_pairwise = pairwise;
1555                         eloop_cancel_timeout(
1556                                 wpa_supplicant_delayed_mic_error_report,
1557                                 wpa_s, NULL);
1558                         eloop_register_timeout(
1559                                 sec, os_random() % 1000000,
1560                                 wpa_supplicant_delayed_mic_error_report,
1561                                 wpa_s, NULL);
1562                 } else {
1563                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1564                 }
1565 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1566                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1567 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1568         }
1569         wpa_s->last_michael_mic_error = t.sec;
1570         wpa_s->mic_errors_seen++;
1571 }
1572
1573
1574 #ifdef CONFIG_TERMINATE_ONLASTIF
1575 static int any_interfaces(struct wpa_supplicant *head)
1576 {
1577         struct wpa_supplicant *wpa_s;
1578
1579         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1580                 if (!wpa_s->interface_removed)
1581                         return 1;
1582         return 0;
1583 }
1584 #endif /* CONFIG_TERMINATE_ONLASTIF */
1585
1586
1587 static void
1588 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1589                                       union wpa_event_data *data)
1590 {
1591         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1592                 return;
1593
1594         switch (data->interface_status.ievent) {
1595         case EVENT_INTERFACE_ADDED:
1596                 if (!wpa_s->interface_removed)
1597                         break;
1598                 wpa_s->interface_removed = 0;
1599                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1600                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1601                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1602                                 "driver after interface was added");
1603                 }
1604                 break;
1605         case EVENT_INTERFACE_REMOVED:
1606                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1607                 wpa_s->interface_removed = 1;
1608                 wpa_supplicant_mark_disassoc(wpa_s);
1609                 l2_packet_deinit(wpa_s->l2);
1610                 wpa_s->l2 = NULL;
1611 #ifdef CONFIG_IBSS_RSN
1612                 ibss_rsn_deinit(wpa_s->ibss_rsn);
1613                 wpa_s->ibss_rsn = NULL;
1614 #endif /* CONFIG_IBSS_RSN */
1615 #ifdef CONFIG_TERMINATE_ONLASTIF
1616                 /* check if last interface */
1617                 if (!any_interfaces(wpa_s->global->ifaces))
1618                         eloop_terminate();
1619 #endif /* CONFIG_TERMINATE_ONLASTIF */
1620                 break;
1621         }
1622 }
1623
1624
1625 #ifdef CONFIG_PEERKEY
1626 static void
1627 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1628                               union wpa_event_data *data)
1629 {
1630         if (data == NULL)
1631                 return;
1632         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1633 }
1634 #endif /* CONFIG_PEERKEY */
1635
1636
1637 #ifdef CONFIG_TDLS
1638 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1639                                       union wpa_event_data *data)
1640 {
1641         if (data == NULL)
1642                 return;
1643         switch (data->tdls.oper) {
1644         case TDLS_REQUEST_SETUP:
1645                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1646                 break;
1647         case TDLS_REQUEST_TEARDOWN:
1648                 /* request from driver to add FTIE */
1649                 wpa_tdls_recv_teardown_notify(wpa_s->wpa, data->tdls.peer,
1650                                               data->tdls.reason_code);
1651                 break;
1652         }
1653 }
1654 #endif /* CONFIG_TDLS */
1655
1656
1657 #ifdef CONFIG_IEEE80211R
1658 static void
1659 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1660                                  union wpa_event_data *data)
1661 {
1662         if (data == NULL)
1663                 return;
1664
1665         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1666                                     data->ft_ies.ies_len,
1667                                     data->ft_ies.ft_action,
1668                                     data->ft_ies.target_ap,
1669                                     data->ft_ies.ric_ies,
1670                                     data->ft_ies.ric_ies_len) < 0) {
1671                 /* TODO: prevent MLME/driver from trying to associate? */
1672         }
1673 }
1674 #endif /* CONFIG_IEEE80211R */
1675
1676
1677 #ifdef CONFIG_IBSS_RSN
1678 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1679                                                 union wpa_event_data *data)
1680 {
1681         struct wpa_ssid *ssid;
1682         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1683                 return;
1684         if (data == NULL)
1685                 return;
1686         ssid = wpa_s->current_ssid;
1687         if (ssid == NULL)
1688                 return;
1689         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1690                 return;
1691
1692         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1693 }
1694 #endif /* CONFIG_IBSS_RSN */
1695
1696
1697 #ifdef CONFIG_IEEE80211R
1698 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1699                          size_t len)
1700 {
1701         const u8 *sta_addr, *target_ap_addr;
1702         u16 status;
1703
1704         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1705         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1706                 return; /* only SME case supported for now */
1707         if (len < 1 + 2 * ETH_ALEN + 2)
1708                 return;
1709         if (data[0] != 2)
1710                 return; /* Only FT Action Response is supported for now */
1711         sta_addr = data + 1;
1712         target_ap_addr = data + 1 + ETH_ALEN;
1713         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1714         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1715                 MACSTR " TargetAP " MACSTR " status %u",
1716                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1717
1718         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1719                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1720                         " in FT Action Response", MAC2STR(sta_addr));
1721                 return;
1722         }
1723
1724         if (status) {
1725                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1726                         "failure (status code %d)", status);
1727                 /* TODO: report error to FT code(?) */
1728                 return;
1729         }
1730
1731         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1732                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1733                                     target_ap_addr, NULL, 0) < 0)
1734                 return;
1735
1736 #ifdef CONFIG_SME
1737         {
1738                 struct wpa_bss *bss;
1739                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1740                 if (bss)
1741                         wpa_s->sme.freq = bss->freq;
1742                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1743                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1744                               WLAN_AUTH_FT);
1745         }
1746 #endif /* CONFIG_SME */
1747 }
1748 #endif /* CONFIG_IEEE80211R */
1749
1750
1751 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1752                                                struct unprot_deauth *e)
1753 {
1754 #ifdef CONFIG_IEEE80211W
1755         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1756                    "dropped: " MACSTR " -> " MACSTR
1757                    " (reason code %u)",
1758                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1759         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1760 #endif /* CONFIG_IEEE80211W */
1761 }
1762
1763
1764 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1765                                                  struct unprot_disassoc *e)
1766 {
1767 #ifdef CONFIG_IEEE80211W
1768         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1769                    "dropped: " MACSTR " -> " MACSTR
1770                    " (reason code %u)",
1771                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1772         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1773 #endif /* CONFIG_IEEE80211W */
1774 }
1775
1776
1777 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1778                           union wpa_event_data *data)
1779 {
1780         struct wpa_supplicant *wpa_s = ctx;
1781         u16 reason_code = 0;
1782
1783         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1784             event != EVENT_INTERFACE_ENABLED &&
1785             event != EVENT_INTERFACE_STATUS) {
1786                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
1787                         "disabled", event);
1788                 return;
1789         }
1790
1791         wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
1792                 event, wpa_s->ifname);
1793
1794         switch (event) {
1795         case EVENT_AUTH:
1796                 sme_event_auth(wpa_s, data);
1797                 break;
1798         case EVENT_ASSOC:
1799                 wpa_supplicant_event_assoc(wpa_s, data);
1800                 break;
1801         case EVENT_DISASSOC:
1802                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
1803                 if (data) {
1804                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1805                                 data->disassoc_info.reason_code);
1806                         if (data->disassoc_info.addr)
1807                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
1808                                         MAC2STR(data->disassoc_info.addr));
1809                 }
1810 #ifdef CONFIG_AP
1811                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1812                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1813                                                data->disassoc_info.addr);
1814                         break;
1815                 }
1816 #endif /* CONFIG_AP */
1817                 if (data) {
1818                         reason_code = data->disassoc_info.reason_code;
1819                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1820                                     data->disassoc_info.ie,
1821                                     data->disassoc_info.ie_len);
1822 #ifdef CONFIG_P2P
1823                         wpas_p2p_disassoc_notif(
1824                                 wpa_s, data->disassoc_info.addr, reason_code,
1825                                 data->disassoc_info.ie,
1826                                 data->disassoc_info.ie_len);
1827 #endif /* CONFIG_P2P */
1828                 }
1829                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1830                         sme_event_disassoc(wpa_s, data);
1831                 /* fall through */
1832         case EVENT_DEAUTH:
1833                 if (event == EVENT_DEAUTH) {
1834                         wpa_dbg(wpa_s, MSG_DEBUG,
1835                                 "Deauthentication notification");
1836                         if (data) {
1837                                 reason_code = data->deauth_info.reason_code;
1838                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1839                                         data->deauth_info.reason_code);
1840                                 if (data->deauth_info.addr) {
1841                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
1842                                                 MACSTR,
1843                                                 MAC2STR(data->deauth_info.
1844                                                         addr));
1845                                 }
1846                                 wpa_hexdump(MSG_DEBUG,
1847                                             "Deauthentication frame IE(s)",
1848                                             data->deauth_info.ie,
1849                                             data->deauth_info.ie_len);
1850 #ifdef CONFIG_P2P
1851                                 wpas_p2p_deauth_notif(
1852                                         wpa_s, data->deauth_info.addr,
1853                                         reason_code,
1854                                         data->deauth_info.ie,
1855                                         data->deauth_info.ie_len);
1856 #endif /* CONFIG_P2P */
1857                         }
1858                 }
1859 #ifdef CONFIG_AP
1860                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1861                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1862                                                data->deauth_info.addr);
1863                         break;
1864                 }
1865 #endif /* CONFIG_AP */
1866                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1867                 break;
1868         case EVENT_MICHAEL_MIC_FAILURE:
1869                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1870                 break;
1871 #ifndef CONFIG_NO_SCAN_PROCESSING
1872         case EVENT_SCAN_RESULTS:
1873                 wpa_supplicant_event_scan_results(wpa_s, data);
1874                 break;
1875 #endif /* CONFIG_NO_SCAN_PROCESSING */
1876         case EVENT_ASSOCINFO:
1877                 wpa_supplicant_event_associnfo(wpa_s, data);
1878                 break;
1879         case EVENT_INTERFACE_STATUS:
1880                 wpa_supplicant_event_interface_status(wpa_s, data);
1881                 break;
1882         case EVENT_PMKID_CANDIDATE:
1883                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1884                 break;
1885 #ifdef CONFIG_PEERKEY
1886         case EVENT_STKSTART:
1887                 wpa_supplicant_event_stkstart(wpa_s, data);
1888                 break;
1889 #endif /* CONFIG_PEERKEY */
1890 #ifdef CONFIG_TDLS
1891         case EVENT_TDLS:
1892                 wpa_supplicant_event_tdls(wpa_s, data);
1893                 break;
1894 #endif /* CONFIG_TDLS */
1895 #ifdef CONFIG_IEEE80211R
1896         case EVENT_FT_RESPONSE:
1897                 wpa_supplicant_event_ft_response(wpa_s, data);
1898                 break;
1899 #endif /* CONFIG_IEEE80211R */
1900 #ifdef CONFIG_IBSS_RSN
1901         case EVENT_IBSS_RSN_START:
1902                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1903                 break;
1904 #endif /* CONFIG_IBSS_RSN */
1905         case EVENT_ASSOC_REJECT:
1906                 if (data->assoc_reject.bssid)
1907                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1908                                 "bssid=" MACSTR " status_code=%u",
1909                                 MAC2STR(data->assoc_reject.bssid),
1910                                 data->assoc_reject.status_code);
1911                 else
1912                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1913                                 "status_code=%u",
1914                                 data->assoc_reject.status_code);
1915                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1916                         sme_event_assoc_reject(wpa_s, data);
1917                 break;
1918         case EVENT_AUTH_TIMED_OUT:
1919                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1920                         sme_event_auth_timed_out(wpa_s, data);
1921                 break;
1922         case EVENT_ASSOC_TIMED_OUT:
1923                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1924                         sme_event_assoc_timed_out(wpa_s, data);
1925                 break;
1926         case EVENT_TX_STATUS:
1927                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
1928                         " type=%d stype=%d",
1929                         MAC2STR(data->tx_status.dst),
1930                         data->tx_status.type, data->tx_status.stype);
1931 #ifdef CONFIG_AP
1932                 if (wpa_s->ap_iface == NULL) {
1933 #ifdef CONFIG_OFFCHANNEL
1934                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1935                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1936                                 offchannel_send_action_tx_status(
1937                                         wpa_s, data->tx_status.dst,
1938                                         data->tx_status.data,
1939                                         data->tx_status.data_len,
1940                                         data->tx_status.ack ?
1941                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
1942                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
1943 #endif /* CONFIG_OFFCHANNEL */
1944                         break;
1945                 }
1946 #endif /* CONFIG_AP */
1947 #ifdef CONFIG_OFFCHANNEL
1948                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
1949                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
1950                 /*
1951                  * Catch TX status events for Action frames we sent via group
1952                  * interface in GO mode.
1953                  */
1954                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1955                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1956                     os_memcmp(wpa_s->parent->pending_action_dst,
1957                               data->tx_status.dst, ETH_ALEN) == 0) {
1958                         offchannel_send_action_tx_status(
1959                                 wpa_s->parent, data->tx_status.dst,
1960                                 data->tx_status.data,
1961                                 data->tx_status.data_len,
1962                                 data->tx_status.ack ?
1963                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
1964                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
1965                         break;
1966                 }
1967 #endif /* CONFIG_OFFCHANNEL */
1968 #ifdef CONFIG_AP
1969                 switch (data->tx_status.type) {
1970                 case WLAN_FC_TYPE_MGMT:
1971                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1972                                       data->tx_status.data_len,
1973                                       data->tx_status.stype,
1974                                       data->tx_status.ack);
1975                         break;
1976                 case WLAN_FC_TYPE_DATA:
1977                         ap_tx_status(wpa_s, data->tx_status.dst,
1978                                      data->tx_status.data,
1979                                      data->tx_status.data_len,
1980                                      data->tx_status.ack);
1981                         break;
1982                 }
1983 #endif /* CONFIG_AP */
1984                 break;
1985 #ifdef CONFIG_AP
1986         case EVENT_RX_FROM_UNKNOWN:
1987                 if (wpa_s->ap_iface == NULL)
1988                         break;
1989                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1990                                        data->rx_from_unknown.len);
1991                 break;
1992         case EVENT_RX_MGMT:
1993                 if (wpa_s->ap_iface == NULL) {
1994 #ifdef CONFIG_P2P
1995                         u16 fc, stype;
1996                         const struct ieee80211_mgmt *mgmt;
1997                         mgmt = (const struct ieee80211_mgmt *)
1998                                 data->rx_mgmt.frame;
1999                         fc = le_to_host16(mgmt->frame_control);
2000                         stype = WLAN_FC_GET_STYPE(fc);
2001                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2002                             data->rx_mgmt.frame_len > 24) {
2003                                 const u8 *src = mgmt->sa;
2004                                 const u8 *ie = mgmt->u.probe_req.variable;
2005                                 size_t ie_len = data->rx_mgmt.frame_len -
2006                                         (mgmt->u.probe_req.variable -
2007                                          data->rx_mgmt.frame);
2008                                 wpas_p2p_probe_req_rx(wpa_s, src, mgmt->da,
2009                                                       mgmt->bssid, ie, ie_len);
2010                                 break;
2011                         }
2012 #endif /* CONFIG_P2P */
2013                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2014                                 "management frame in non-AP mode");
2015                         break;
2016                 }
2017                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2018                 break;
2019 #endif /* CONFIG_AP */
2020         case EVENT_RX_ACTION:
2021                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2022                         " Category=%u DataLen=%d freq=%d MHz",
2023                         MAC2STR(data->rx_action.sa),
2024                         data->rx_action.category, (int) data->rx_action.len,
2025                         data->rx_action.freq);
2026 #ifdef CONFIG_IEEE80211R
2027                 if (data->rx_action.category == WLAN_ACTION_FT) {
2028                         ft_rx_action(wpa_s, data->rx_action.data,
2029                                      data->rx_action.len);
2030                         break;
2031                 }
2032 #endif /* CONFIG_IEEE80211R */
2033 #ifdef CONFIG_IEEE80211W
2034 #ifdef CONFIG_SME
2035                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2036                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2037                                         data->rx_action.data,
2038                                         data->rx_action.len);
2039                         break;
2040                 }
2041 #endif /* CONFIG_SME */
2042 #endif /* CONFIG_IEEE80211W */
2043 #ifdef CONFIG_GAS
2044                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2045                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2046                                  data->rx_action.sa, data->rx_action.bssid,
2047                                  data->rx_action.data, data->rx_action.len,
2048                                  data->rx_action.freq) == 0)
2049                         break;
2050 #endif /* CONFIG_GAS */
2051 #ifdef CONFIG_P2P
2052                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2053                                    data->rx_action.sa,
2054                                    data->rx_action.bssid,
2055                                    data->rx_action.category,
2056                                    data->rx_action.data,
2057                                    data->rx_action.len, data->rx_action.freq);
2058 #endif /* CONFIG_P2P */
2059                 break;
2060         case EVENT_RX_PROBE_REQ:
2061                 if (data->rx_probe_req.sa == NULL ||
2062                     data->rx_probe_req.ie == NULL)
2063                         break;
2064 #ifdef CONFIG_AP
2065                 if (wpa_s->ap_iface) {
2066                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2067                                              data->rx_probe_req.sa,
2068                                              data->rx_probe_req.da,
2069                                              data->rx_probe_req.bssid,
2070                                              data->rx_probe_req.ie,
2071                                              data->rx_probe_req.ie_len);
2072                         break;
2073                 }
2074 #endif /* CONFIG_AP */
2075 #ifdef CONFIG_P2P
2076                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2077                                       data->rx_probe_req.da,
2078                                       data->rx_probe_req.bssid,
2079                                       data->rx_probe_req.ie,
2080                                       data->rx_probe_req.ie_len);
2081 #endif /* CONFIG_P2P */
2082                 break;
2083         case EVENT_REMAIN_ON_CHANNEL:
2084 #ifdef CONFIG_OFFCHANNEL
2085                 offchannel_remain_on_channel_cb(
2086                         wpa_s, data->remain_on_channel.freq,
2087                         data->remain_on_channel.duration);
2088 #endif /* CONFIG_OFFCHANNEL */
2089 #ifdef CONFIG_P2P
2090                 wpas_p2p_remain_on_channel_cb(
2091                         wpa_s, data->remain_on_channel.freq,
2092                         data->remain_on_channel.duration);
2093 #endif /* CONFIG_P2P */
2094                 break;
2095         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2096 #ifdef CONFIG_OFFCHANNEL
2097                 offchannel_cancel_remain_on_channel_cb(
2098                         wpa_s, data->remain_on_channel.freq);
2099 #endif /* CONFIG_OFFCHANNEL */
2100 #ifdef CONFIG_P2P
2101                 wpas_p2p_cancel_remain_on_channel_cb(
2102                         wpa_s, data->remain_on_channel.freq);
2103 #endif /* CONFIG_P2P */
2104                 break;
2105 #ifdef CONFIG_P2P
2106         case EVENT_P2P_DEV_FOUND: {
2107                 struct p2p_peer_info peer_info;
2108
2109                 os_memset(&peer_info, 0, sizeof(peer_info));
2110                 if (data->p2p_dev_found.dev_addr)
2111                         os_memcpy(peer_info.p2p_device_addr,
2112                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2113                 if (data->p2p_dev_found.pri_dev_type)
2114                         os_memcpy(peer_info.pri_dev_type,
2115                                   data->p2p_dev_found.pri_dev_type,
2116                                   sizeof(peer_info.pri_dev_type));
2117                 if (data->p2p_dev_found.dev_name)
2118                         os_strlcpy(peer_info.device_name,
2119                                    data->p2p_dev_found.dev_name,
2120                                    sizeof(peer_info.device_name));
2121                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2122                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2123                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2124
2125                 /*
2126                  * FIX: new_device=1 is not necessarily correct. We should
2127                  * maintain a P2P peer database in wpa_supplicant and update
2128                  * this information based on whether the peer is truly new.
2129                  */
2130                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2131                 break;
2132                 }
2133         case EVENT_P2P_GO_NEG_REQ_RX:
2134                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2135                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2136                 break;
2137         case EVENT_P2P_GO_NEG_COMPLETED:
2138                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2139                 break;
2140         case EVENT_P2P_PROV_DISC_REQUEST:
2141                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2142                                    data->p2p_prov_disc_req.config_methods,
2143                                    data->p2p_prov_disc_req.dev_addr,
2144                                    data->p2p_prov_disc_req.pri_dev_type,
2145                                    data->p2p_prov_disc_req.dev_name,
2146                                    data->p2p_prov_disc_req.supp_config_methods,
2147                                    data->p2p_prov_disc_req.dev_capab,
2148                                    data->p2p_prov_disc_req.group_capab);
2149                 break;
2150         case EVENT_P2P_PROV_DISC_RESPONSE:
2151                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2152                                     data->p2p_prov_disc_resp.config_methods);
2153                 break;
2154         case EVENT_P2P_SD_REQUEST:
2155                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2156                                 data->p2p_sd_req.sa,
2157                                 data->p2p_sd_req.dialog_token,
2158                                 data->p2p_sd_req.update_indic,
2159                                 data->p2p_sd_req.tlvs,
2160                                 data->p2p_sd_req.tlvs_len);
2161                 break;
2162         case EVENT_P2P_SD_RESPONSE:
2163                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2164                                  data->p2p_sd_resp.update_indic,
2165                                  data->p2p_sd_resp.tlvs,
2166                                  data->p2p_sd_resp.tlvs_len);
2167                 break;
2168 #endif /* CONFIG_P2P */
2169 #ifdef CONFIG_CLIENT_MLME
2170         case EVENT_MLME_RX: {
2171                 struct ieee80211_rx_status rx_status;
2172                 os_memset(&rx_status, 0, sizeof(rx_status));
2173                 rx_status.freq = data->mlme_rx.freq;
2174                 rx_status.channel = data->mlme_rx.channel;
2175                 rx_status.ssi = data->mlme_rx.ssi;
2176                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
2177                                  &rx_status);
2178                 break;
2179         }
2180 #endif /* CONFIG_CLIENT_MLME */
2181         case EVENT_EAPOL_RX:
2182                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2183                                         data->eapol_rx.data,
2184                                         data->eapol_rx.data_len);
2185                 break;
2186         case EVENT_SIGNAL_CHANGE:
2187                 bgscan_notify_signal_change(
2188                         wpa_s, data->signal_change.above_threshold,
2189                         data->signal_change.current_signal,
2190                         data->signal_change.current_noise,
2191                         data->signal_change.current_txrate);
2192                 break;
2193         case EVENT_INTERFACE_ENABLED:
2194                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2195                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2196 #ifdef CONFIG_AP
2197                         if (!wpa_s->ap_iface) {
2198                                 wpa_supplicant_set_state(wpa_s,
2199                                                          WPA_DISCONNECTED);
2200                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2201                         } else
2202                                 wpa_supplicant_set_state(wpa_s,
2203                                                          WPA_COMPLETED);
2204 #else /* CONFIG_AP */
2205                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2206                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2207 #endif /* CONFIG_AP */
2208                 }
2209                 break;
2210         case EVENT_INTERFACE_DISABLED:
2211                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2212                 wpa_supplicant_mark_disassoc(wpa_s);
2213                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2214                 break;
2215         case EVENT_CHANNEL_LIST_CHANGED:
2216                 if (wpa_s->drv_priv == NULL)
2217                         break; /* Ignore event during drv initialization */
2218 #ifdef CONFIG_P2P
2219                 wpas_p2p_update_channel_list(wpa_s);
2220 #endif /* CONFIG_P2P */
2221                 break;
2222         case EVENT_INTERFACE_UNAVAILABLE:
2223 #ifdef CONFIG_P2P
2224                 wpas_p2p_interface_unavailable(wpa_s);
2225 #endif /* CONFIG_P2P */
2226                 break;
2227         case EVENT_BEST_CHANNEL:
2228                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2229                         "(%d %d %d)",
2230                         data->best_chan.freq_24, data->best_chan.freq_5,
2231                         data->best_chan.freq_overall);
2232                 wpa_s->best_24_freq = data->best_chan.freq_24;
2233                 wpa_s->best_5_freq = data->best_chan.freq_5;
2234                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2235 #ifdef CONFIG_P2P
2236                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2237                                               data->best_chan.freq_5,
2238                                               data->best_chan.freq_overall);
2239 #endif /* CONFIG_P2P */
2240                 break;
2241         case EVENT_UNPROT_DEAUTH:
2242                 wpa_supplicant_event_unprot_deauth(wpa_s,
2243                                                    &data->unprot_deauth);
2244                 break;
2245         case EVENT_UNPROT_DISASSOC:
2246                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2247                                                      &data->unprot_disassoc);
2248                 break;
2249         case EVENT_STATION_LOW_ACK:
2250 #ifdef CONFIG_AP
2251                 if (wpa_s->ap_iface && data)
2252                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2253                                                   data->low_ack.addr);
2254 #endif /* CONFIG_AP */
2255                 break;
2256         case EVENT_IBSS_PEER_LOST:
2257 #ifdef CONFIG_IBSS_RSN
2258                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2259 #endif /* CONFIG_IBSS_RSN */
2260                 break;
2261         case EVENT_DRIVER_GTK_REKEY:
2262                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2263                               wpa_s->bssid, ETH_ALEN))
2264                         break;
2265                 if (!wpa_s->wpa)
2266                         break;
2267                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2268                                          data->driver_gtk_rekey.replay_ctr);
2269                 break;
2270         case EVENT_SCHED_SCAN_STOPPED:
2271                 wpa_s->sched_scanning = 0;
2272                 wpa_supplicant_notify_scanning(wpa_s, 0);
2273
2274                 /*
2275                  * If we timed out, start a new sched scan to continue
2276                  * searching for more SSIDs.
2277                  */
2278                 if (wpa_s->sched_scan_timed_out)
2279                         wpa_supplicant_req_sched_scan(wpa_s);
2280                 break;
2281         default:
2282                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2283                 break;
2284         }
2285 }