FT: Reset FT flag upon STA deauthentication
[mech_eap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eapol_supp/eapol_supp_sm.h"
13 #include "rsn_supp/wpa.h"
14 #include "eloop.h"
15 #include "config.h"
16 #include "l2_packet/l2_packet.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "pcsc_funcs.h"
20 #include "rsn_supp/preauth.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "common/wpa_ctrl.h"
23 #include "eap_peer/eap.h"
24 #include "ap/hostapd.h"
25 #include "p2p/p2p.h"
26 #include "wnm_sta.h"
27 #include "notify.h"
28 #include "common/ieee802_11_defs.h"
29 #include "common/ieee802_11_common.h"
30 #include "crypto/random.h"
31 #include "blacklist.h"
32 #include "wpas_glue.h"
33 #include "wps_supplicant.h"
34 #include "ibss_rsn.h"
35 #include "sme.h"
36 #include "gas_query.h"
37 #include "p2p_supplicant.h"
38 #include "bgscan.h"
39 #include "autoscan.h"
40 #include "ap.h"
41 #include "bss.h"
42 #include "scan.h"
43 #include "offchannel.h"
44 #include "interworking.h"
45
46
47 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
48                                               int new_scan);
49
50
51 static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
52                               struct wpa_ssid *ssid)
53 {
54         struct os_time now;
55
56         if (ssid == NULL || ssid->disabled_until.sec == 0)
57                 return 0;
58
59         os_get_time(&now);
60         if (ssid->disabled_until.sec > now.sec)
61                 return ssid->disabled_until.sec - now.sec;
62
63         wpas_clear_temp_disabled(wpa_s, ssid, 0);
64
65         return 0;
66 }
67
68
69 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
70 {
71         struct wpa_ssid *ssid, *old_ssid;
72         int res;
73
74         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
75                 return 0;
76
77         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
78                 "information");
79         ssid = wpa_supplicant_get_ssid(wpa_s);
80         if (ssid == NULL) {
81                 wpa_msg(wpa_s, MSG_INFO,
82                         "No network configuration found for the current AP");
83                 return -1;
84         }
85
86         if (wpas_network_disabled(wpa_s, ssid)) {
87                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
88                 return -1;
89         }
90
91         if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
92             disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
93                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
94                 return -1;
95         }
96
97         res = wpas_temp_disabled(wpa_s, ssid);
98         if (res > 0) {
99                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
100                         "disabled for %d second(s)", res);
101                 return -1;
102         }
103
104         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
105                 "current AP");
106         if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
107                 u8 wpa_ie[80];
108                 size_t wpa_ie_len = sizeof(wpa_ie);
109                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
110                                               wpa_ie, &wpa_ie_len) < 0)
111                         wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
112         } else {
113                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
114         }
115
116         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
117                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
118         old_ssid = wpa_s->current_ssid;
119         wpa_s->current_ssid = ssid;
120         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
121         wpa_supplicant_initiate_eapol(wpa_s);
122         if (old_ssid != wpa_s->current_ssid)
123                 wpas_notify_network_changed(wpa_s);
124
125         return 0;
126 }
127
128
129 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
130 {
131         struct wpa_supplicant *wpa_s = eloop_ctx;
132
133         if (wpa_s->countermeasures) {
134                 wpa_s->countermeasures = 0;
135                 wpa_drv_set_countermeasures(wpa_s, 0);
136                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
137                 wpa_supplicant_req_scan(wpa_s, 0, 0);
138         }
139 }
140
141
142 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
143 {
144         int bssid_changed;
145
146         wnm_bss_keep_alive_deinit(wpa_s);
147
148 #ifdef CONFIG_IBSS_RSN
149         ibss_rsn_deinit(wpa_s->ibss_rsn);
150         wpa_s->ibss_rsn = NULL;
151 #endif /* CONFIG_IBSS_RSN */
152
153 #ifdef CONFIG_AP
154         wpa_supplicant_ap_deinit(wpa_s);
155 #endif /* CONFIG_AP */
156
157         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
158                 return;
159
160         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
161         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
162         os_memset(wpa_s->bssid, 0, ETH_ALEN);
163         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
164 #ifdef CONFIG_SME
165         wpa_s->sme.prev_bssid_set = 0;
166 #endif /* CONFIG_SME */
167 #ifdef CONFIG_P2P
168         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
169 #endif /* CONFIG_P2P */
170         wpa_s->current_bss = NULL;
171         wpa_s->assoc_freq = 0;
172 #ifdef CONFIG_IEEE80211R
173 #ifdef CONFIG_SME
174         if (wpa_s->sme.ft_ies)
175                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
176 #endif /* CONFIG_SME */
177 #endif /* CONFIG_IEEE80211R */
178
179         if (bssid_changed)
180                 wpas_notify_bssid_changed(wpa_s);
181
182         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
183         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
184         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
185                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
186         wpa_s->ap_ies_from_associnfo = 0;
187         wpa_s->current_ssid = NULL;
188         wpa_s->key_mgmt = 0;
189 }
190
191
192 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
193 {
194         struct wpa_ie_data ie;
195         int pmksa_set = -1;
196         size_t i;
197
198         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
199             ie.pmkid == NULL)
200                 return;
201
202         for (i = 0; i < ie.num_pmkid; i++) {
203                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
204                                                     ie.pmkid + i * PMKID_LEN,
205                                                     NULL, NULL, 0);
206                 if (pmksa_set == 0) {
207                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
208                         break;
209                 }
210         }
211
212         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
213                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
214 }
215
216
217 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
218                                                  union wpa_event_data *data)
219 {
220         if (data == NULL) {
221                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
222                         "event");
223                 return;
224         }
225         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
226                 " index=%d preauth=%d",
227                 MAC2STR(data->pmkid_candidate.bssid),
228                 data->pmkid_candidate.index,
229                 data->pmkid_candidate.preauth);
230
231         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
232                             data->pmkid_candidate.index,
233                             data->pmkid_candidate.preauth);
234 }
235
236
237 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
238 {
239         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
240             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
241                 return 0;
242
243 #ifdef IEEE8021X_EAPOL
244         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
245             wpa_s->current_ssid &&
246             !(wpa_s->current_ssid->eapol_flags &
247               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
248                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
249                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
250                  * plaintext or static WEP keys). */
251                 return 0;
252         }
253 #endif /* IEEE8021X_EAPOL */
254
255         return 1;
256 }
257
258
259 /**
260  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
261  * @wpa_s: pointer to wpa_supplicant data
262  * @ssid: Configuration data for the network
263  * Returns: 0 on success, -1 on failure
264  *
265  * This function is called when starting authentication with a network that is
266  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
267  */
268 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
269                               struct wpa_ssid *ssid)
270 {
271 #ifdef IEEE8021X_EAPOL
272 #ifdef PCSC_FUNCS
273         int aka = 0, sim = 0, type;
274
275         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
276                 return 0;
277
278         if (ssid->eap.eap_methods == NULL) {
279                 sim = 1;
280                 aka = 1;
281         } else {
282                 struct eap_method_type *eap = ssid->eap.eap_methods;
283                 while (eap->vendor != EAP_VENDOR_IETF ||
284                        eap->method != EAP_TYPE_NONE) {
285                         if (eap->vendor == EAP_VENDOR_IETF) {
286                                 if (eap->method == EAP_TYPE_SIM)
287                                         sim = 1;
288                                 else if (eap->method == EAP_TYPE_AKA ||
289                                          eap->method == EAP_TYPE_AKA_PRIME)
290                                         aka = 1;
291                         }
292                         eap++;
293                 }
294         }
295
296         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
297                 sim = 0;
298         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
299             eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
300             NULL)
301                 aka = 0;
302
303         if (!sim && !aka) {
304                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
305                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
306                         "enabled");
307                 return 0;
308         }
309
310         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
311                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
312         if (sim && aka)
313                 type = SCARD_TRY_BOTH;
314         else if (aka)
315                 type = SCARD_USIM_ONLY;
316         else
317                 type = SCARD_GSM_SIM_ONLY;
318
319         wpa_s->scard = scard_init(type, NULL);
320         if (wpa_s->scard == NULL) {
321                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
322                         "(pcsc-lite)");
323                 return -1;
324         }
325         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
326         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
327 #endif /* PCSC_FUNCS */
328 #endif /* IEEE8021X_EAPOL */
329
330         return 0;
331 }
332
333
334 #ifndef CONFIG_NO_SCAN_PROCESSING
335 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
336                                         struct wpa_ssid *ssid)
337 {
338         int i, privacy = 0;
339
340         if (ssid->mixed_cell)
341                 return 1;
342
343 #ifdef CONFIG_WPS
344         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
345                 return 1;
346 #endif /* CONFIG_WPS */
347
348         for (i = 0; i < NUM_WEP_KEYS; i++) {
349                 if (ssid->wep_key_len[i]) {
350                         privacy = 1;
351                         break;
352                 }
353         }
354 #ifdef IEEE8021X_EAPOL
355         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
356             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
357                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
358                 privacy = 1;
359 #endif /* IEEE8021X_EAPOL */
360
361         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
362                 privacy = 1;
363
364         if (bss->caps & IEEE80211_CAP_PRIVACY)
365                 return privacy;
366         return !privacy;
367 }
368
369
370 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
371                                          struct wpa_ssid *ssid,
372                                          struct wpa_bss *bss)
373 {
374         struct wpa_ie_data ie;
375         int proto_match = 0;
376         const u8 *rsn_ie, *wpa_ie;
377         int ret;
378         int wep_ok;
379
380         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
381         if (ret >= 0)
382                 return ret;
383
384         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
385         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
386                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
387                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
388                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
389
390         rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
391         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
392                 proto_match++;
393
394                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
395                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
396                                 "failed");
397                         break;
398                 }
399
400                 if (wep_ok &&
401                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
402                 {
403                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
404                                 "in RSN IE");
405                         return 1;
406                 }
407
408                 if (!(ie.proto & ssid->proto)) {
409                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
410                                 "mismatch");
411                         break;
412                 }
413
414                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
415                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
416                                 "cipher mismatch");
417                         break;
418                 }
419
420                 if (!(ie.group_cipher & ssid->group_cipher)) {
421                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
422                                 "cipher mismatch");
423                         break;
424                 }
425
426                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
427                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
428                                 "mismatch");
429                         break;
430                 }
431
432 #ifdef CONFIG_IEEE80211W
433                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
434                     (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
435                      wpa_s->conf->pmf : ssid->ieee80211w) ==
436                     MGMT_FRAME_PROTECTION_REQUIRED) {
437                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
438                                 "frame protection");
439                         break;
440                 }
441 #endif /* CONFIG_IEEE80211W */
442
443                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
444                 return 1;
445         }
446
447         wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
448         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
449                 proto_match++;
450
451                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
452                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
453                                 "failed");
454                         break;
455                 }
456
457                 if (wep_ok &&
458                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
459                 {
460                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
461                                 "in WPA IE");
462                         return 1;
463                 }
464
465                 if (!(ie.proto & ssid->proto)) {
466                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
467                                 "mismatch");
468                         break;
469                 }
470
471                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
472                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
473                                 "cipher mismatch");
474                         break;
475                 }
476
477                 if (!(ie.group_cipher & ssid->group_cipher)) {
478                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
479                                 "cipher mismatch");
480                         break;
481                 }
482
483                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
484                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
485                                 "mismatch");
486                         break;
487                 }
488
489                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
490                 return 1;
491         }
492
493         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
494             !rsn_ie) {
495                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow for non-WPA IEEE 802.1X");
496                 return 1;
497         }
498
499         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
500             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
501                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
502                 return 0;
503         }
504
505         if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
506                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
507                 return 1;
508         }
509
510         wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
511                 "WPA/WPA2");
512
513         return 0;
514 }
515
516
517 static int freq_allowed(int *freqs, int freq)
518 {
519         int i;
520
521         if (freqs == NULL)
522                 return 1;
523
524         for (i = 0; freqs[i]; i++)
525                 if (freqs[i] == freq)
526                         return 1;
527         return 0;
528 }
529
530
531 static int ht_supported(const struct hostapd_hw_modes *mode)
532 {
533         if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
534                 /*
535                  * The driver did not indicate whether it supports HT. Assume
536                  * it does to avoid connection issues.
537                  */
538                 return 1;
539         }
540
541         /*
542          * IEEE Std 802.11n-2009 20.1.1:
543          * An HT non-AP STA shall support all EQM rates for one spatial stream.
544          */
545         return mode->mcs_set[0] == 0xff;
546 }
547
548
549 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
550 {
551         const struct hostapd_hw_modes *mode = NULL, *modes;
552         const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
553         const u8 *rate_ie;
554         int i, j, k;
555
556         if (bss->freq == 0)
557                 return 1; /* Cannot do matching without knowing band */
558
559         modes = wpa_s->hw.modes;
560         if (modes == NULL) {
561                 /*
562                  * The driver does not provide any additional information
563                  * about the utilized hardware, so allow the connection attempt
564                  * to continue.
565                  */
566                 return 1;
567         }
568
569         for (i = 0; i < wpa_s->hw.num_modes; i++) {
570                 for (j = 0; j < modes[i].num_channels; j++) {
571                         int freq = modes[i].channels[j].freq;
572                         if (freq == bss->freq) {
573                                 if (mode &&
574                                     mode->mode == HOSTAPD_MODE_IEEE80211G)
575                                         break; /* do not allow 802.11b replace
576                                                 * 802.11g */
577                                 mode = &modes[i];
578                                 break;
579                         }
580                 }
581         }
582
583         if (mode == NULL)
584                 return 0;
585
586         for (i = 0; i < (int) sizeof(scan_ie); i++) {
587                 rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
588                 if (rate_ie == NULL)
589                         continue;
590
591                 for (j = 2; j < rate_ie[1] + 2; j++) {
592                         int flagged = !!(rate_ie[j] & 0x80);
593                         int r = (rate_ie[j] & 0x7f) * 5;
594
595                         /*
596                          * IEEE Std 802.11n-2009 7.3.2.2:
597                          * The new BSS Membership selector value is encoded
598                          * like a legacy basic rate, but it is not a rate and
599                          * only indicates if the BSS members are required to
600                          * support the mandatory features of Clause 20 [HT PHY]
601                          * in order to join the BSS.
602                          */
603                         if (flagged && ((rate_ie[j] & 0x7f) ==
604                                         BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
605                                 if (!ht_supported(mode)) {
606                                         wpa_dbg(wpa_s, MSG_DEBUG,
607                                                 "   hardware does not support "
608                                                 "HT PHY");
609                                         return 0;
610                                 }
611                                 continue;
612                         }
613
614                         if (!flagged)
615                                 continue;
616
617                         /* check for legacy basic rates */
618                         for (k = 0; k < mode->num_rates; k++) {
619                                 if (mode->rates[k] == r)
620                                         break;
621                         }
622                         if (k == mode->num_rates) {
623                                 /*
624                                  * IEEE Std 802.11-2007 7.3.2.2 demands that in
625                                  * order to join a BSS all required rates
626                                  * have to be supported by the hardware.
627                                  */
628                                 wpa_dbg(wpa_s, MSG_DEBUG, "   hardware does "
629                                         "not support required rate %d.%d Mbps",
630                                         r / 10, r % 10);
631                                 return 0;
632                         }
633                 }
634         }
635
636         return 1;
637 }
638
639
640 static int bss_is_dmg(struct wpa_bss *bss)
641 {
642         return bss->freq > 45000;
643 }
644
645
646 /*
647  * Test whether BSS is in an ESS.
648  * This is done differently in DMG (60 GHz) and non-DMG bands
649  */
650 static int bss_is_ess(struct wpa_bss *bss)
651 {
652         if (bss_is_dmg(bss)) {
653                 return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
654                         IEEE80211_CAP_DMG_AP;
655         }
656
657         return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
658                 IEEE80211_CAP_ESS);
659 }
660
661
662 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
663                                             int i, struct wpa_bss *bss,
664                                             struct wpa_ssid *group)
665 {
666         u8 wpa_ie_len, rsn_ie_len;
667         int wpa;
668         struct wpa_blacklist *e;
669         const u8 *ie;
670         struct wpa_ssid *ssid;
671
672         ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
673         wpa_ie_len = ie ? ie[1] : 0;
674
675         ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
676         rsn_ie_len = ie ? ie[1] : 0;
677
678         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
679                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
680                 i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
681                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
682                 wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
683
684         e = wpa_blacklist_get(wpa_s, bss->bssid);
685         if (e) {
686                 int limit = 1;
687                 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
688                         /*
689                          * When only a single network is enabled, we can
690                          * trigger blacklisting on the first failure. This
691                          * should not be done with multiple enabled networks to
692                          * avoid getting forced to move into a worse ESS on
693                          * single error if there are no other BSSes of the
694                          * current ESS.
695                          */
696                         limit = 0;
697                 }
698                 if (e->count > limit) {
699                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
700                                 "(count=%d limit=%d)", e->count, limit);
701                         return NULL;
702                 }
703         }
704
705         if (bss->ssid_len == 0) {
706                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
707                 return NULL;
708         }
709
710         if (disallowed_bssid(wpa_s, bss->bssid)) {
711                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
712                 return NULL;
713         }
714
715         if (disallowed_ssid(wpa_s, bss->ssid, bss->ssid_len)) {
716                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
717                 return NULL;
718         }
719
720         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
721
722         for (ssid = group; ssid; ssid = ssid->pnext) {
723                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
724                 int res;
725
726                 if (wpas_network_disabled(wpa_s, ssid)) {
727                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
728                         continue;
729                 }
730
731                 res = wpas_temp_disabled(wpa_s, ssid);
732                 if (res > 0) {
733                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled "
734                                 "temporarily for %d second(s)", res);
735                         continue;
736                 }
737
738 #ifdef CONFIG_WPS
739                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
740                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
741                                 "(WPS)");
742                         continue;
743                 }
744
745                 if (wpa && ssid->ssid_len == 0 &&
746                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
747                         check_ssid = 0;
748
749                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
750                         /* Only allow wildcard SSID match if an AP
751                          * advertises active WPS operation that matches
752                          * with our mode. */
753                         check_ssid = 1;
754                         if (ssid->ssid_len == 0 &&
755                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
756                                 check_ssid = 0;
757                 }
758 #endif /* CONFIG_WPS */
759
760                 if (ssid->bssid_set && ssid->ssid_len == 0 &&
761                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
762                         check_ssid = 0;
763
764                 if (check_ssid &&
765                     (bss->ssid_len != ssid->ssid_len ||
766                      os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
767                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
768                         continue;
769                 }
770
771                 if (ssid->bssid_set &&
772                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
773                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
774                         continue;
775                 }
776
777                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
778                         continue;
779
780                 if (!wpa &&
781                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
782                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
783                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
784                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
785                                 "not allowed");
786                         continue;
787                 }
788
789                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
790                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
791                                 "mismatch");
792                         continue;
793                 }
794
795                 if (!bss_is_ess(bss)) {
796                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - not ESS network");
797                         continue;
798                 }
799
800                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
801                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
802                                 "allowed");
803                         continue;
804                 }
805
806                 if (!rate_match(wpa_s, bss)) {
807                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - rate sets do "
808                                 "not match");
809                         continue;
810                 }
811
812 #ifdef CONFIG_P2P
813                 /*
814                  * TODO: skip the AP if its P2P IE has Group Formation
815                  * bit set in the P2P Group Capability Bitmap and we
816                  * are not in Group Formation with that device.
817                  */
818 #endif /* CONFIG_P2P */
819
820                 /* Matching configuration found */
821                 return ssid;
822         }
823
824         /* No matching configuration found */
825         return NULL;
826 }
827
828
829 static struct wpa_bss *
830 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
831                           struct wpa_ssid *group,
832                           struct wpa_ssid **selected_ssid)
833 {
834         unsigned int i;
835
836         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
837                 group->priority);
838
839         for (i = 0; i < wpa_s->last_scan_res_used; i++) {
840                 struct wpa_bss *bss = wpa_s->last_scan_res[i];
841                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
842                 if (!*selected_ssid)
843                         continue;
844                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
845                         " ssid='%s'",
846                         MAC2STR(bss->bssid),
847                         wpa_ssid_txt(bss->ssid, bss->ssid_len));
848                 return bss;
849         }
850
851         return NULL;
852 }
853
854
855 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
856                                              struct wpa_ssid **selected_ssid)
857 {
858         struct wpa_bss *selected = NULL;
859         int prio;
860
861         if (wpa_s->last_scan_res == NULL ||
862             wpa_s->last_scan_res_used == 0)
863                 return NULL; /* no scan results from last update */
864
865         while (selected == NULL) {
866                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
867                         selected = wpa_supplicant_select_bss(
868                                 wpa_s, wpa_s->conf->pssid[prio],
869                                 selected_ssid);
870                         if (selected)
871                                 break;
872                 }
873
874                 if (selected == NULL && wpa_s->blacklist &&
875                     !wpa_s->countermeasures) {
876                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
877                                 "blacklist and try again");
878                         wpa_blacklist_clear(wpa_s);
879                         wpa_s->blacklist_cleared++;
880                 } else if (selected == NULL)
881                         break;
882         }
883
884         return selected;
885 }
886
887
888 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
889                                         int timeout_sec, int timeout_usec)
890 {
891         if (!wpa_supplicant_enabled_networks(wpa_s)) {
892                 /*
893                  * No networks are enabled; short-circuit request so
894                  * we don't wait timeout seconds before transitioning
895                  * to INACTIVE state.
896                  */
897                 wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
898                         "since there are no enabled networks");
899                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
900 #ifdef CONFIG_P2P
901                 wpa_s->sta_scan_pending = 0;
902 #endif /* CONFIG_P2P */
903                 return;
904         }
905
906         wpa_s->scan_for_connection = 1;
907         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
908 }
909
910
911 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
912                            struct wpa_bss *selected,
913                            struct wpa_ssid *ssid)
914 {
915         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
916                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
917                         "PBC session overlap");
918 #ifdef CONFIG_P2P
919                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
920                         return -1;
921 #endif /* CONFIG_P2P */
922
923 #ifdef CONFIG_WPS
924                 wpas_wps_cancel(wpa_s);
925 #endif /* CONFIG_WPS */
926                 return -1;
927         }
928
929         /*
930          * Do not trigger new association unless the BSSID has changed or if
931          * reassociation is requested. If we are in process of associating with
932          * the selected BSSID, do not trigger new attempt.
933          */
934         if (wpa_s->reassociate ||
935             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
936              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
937                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
938               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
939               0))) {
940                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
941                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
942                         return 0;
943                 }
944                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
945                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
946                         "  pending: " MACSTR "  wpa_state: %s",
947                         wpa_s->reassociate, MAC2STR(selected->bssid),
948                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
949                         wpa_supplicant_state_txt(wpa_s->wpa_state));
950                 wpa_supplicant_associate(wpa_s, selected, ssid);
951         } else {
952                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
953                         "selected AP");
954         }
955
956         return 0;
957 }
958
959
960 static struct wpa_ssid *
961 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
962 {
963         int prio;
964         struct wpa_ssid *ssid;
965
966         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
967                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
968                 {
969                         if (wpas_network_disabled(wpa_s, ssid))
970                                 continue;
971                         if (ssid->mode == IEEE80211_MODE_IBSS ||
972                             ssid->mode == IEEE80211_MODE_AP)
973                                 return ssid;
974                 }
975         }
976         return NULL;
977 }
978
979
980 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
981  * on BSS added and BSS changed events */
982 static void wpa_supplicant_rsn_preauth_scan_results(
983         struct wpa_supplicant *wpa_s)
984 {
985         struct wpa_bss *bss;
986
987         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
988                 return;
989
990         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
991                 const u8 *ssid, *rsn;
992
993                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
994                 if (ssid == NULL)
995                         continue;
996
997                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
998                 if (rsn == NULL)
999                         continue;
1000
1001                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1002         }
1003
1004 }
1005
1006
1007 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1008                                        struct wpa_bss *selected,
1009                                        struct wpa_ssid *ssid)
1010 {
1011         struct wpa_bss *current_bss = NULL;
1012         int min_diff;
1013
1014         if (wpa_s->reassociate)
1015                 return 1; /* explicit request to reassociate */
1016         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1017                 return 1; /* we are not associated; continue */
1018         if (wpa_s->current_ssid == NULL)
1019                 return 1; /* unknown current SSID */
1020         if (wpa_s->current_ssid != ssid)
1021                 return 1; /* different network block */
1022
1023         if (wpas_driver_bss_selection(wpa_s))
1024                 return 0; /* Driver-based roaming */
1025
1026         if (wpa_s->current_ssid->ssid)
1027                 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1028                                           wpa_s->current_ssid->ssid,
1029                                           wpa_s->current_ssid->ssid_len);
1030         if (!current_bss)
1031                 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1032
1033         if (!current_bss)
1034                 return 1; /* current BSS not seen in scan results */
1035
1036         if (current_bss == selected)
1037                 return 0;
1038
1039         if (selected->last_update_idx > current_bss->last_update_idx)
1040                 return 1; /* current BSS not seen in the last scan */
1041
1042 #ifndef CONFIG_NO_ROAMING
1043         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1044         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1045                 MAC2STR(current_bss->bssid), current_bss->level);
1046         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1047                 MAC2STR(selected->bssid), selected->level);
1048
1049         if (wpa_s->current_ssid->bssid_set &&
1050             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1051             0) {
1052                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1053                         "has preferred BSSID");
1054                 return 1;
1055         }
1056
1057         if (current_bss->level < 0 && current_bss->level > selected->level) {
1058                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1059                         "signal level");
1060                 return 0;
1061         }
1062
1063         min_diff = 2;
1064         if (current_bss->level < 0) {
1065                 if (current_bss->level < -85)
1066                         min_diff = 1;
1067                 else if (current_bss->level < -80)
1068                         min_diff = 2;
1069                 else if (current_bss->level < -75)
1070                         min_diff = 3;
1071                 else if (current_bss->level < -70)
1072                         min_diff = 4;
1073                 else
1074                         min_diff = 5;
1075         }
1076         if (abs(current_bss->level - selected->level) < min_diff) {
1077                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1078                         "in signal level");
1079                 return 0;
1080         }
1081
1082         return 1;
1083 #else /* CONFIG_NO_ROAMING */
1084         return 0;
1085 #endif /* CONFIG_NO_ROAMING */
1086 }
1087
1088
1089 /* Return != 0 if no scan results could be fetched or if scan results should not
1090  * be shared with other virtual interfaces. */
1091 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1092                                               union wpa_event_data *data,
1093                                               int own_request)
1094 {
1095         struct wpa_scan_results *scan_res;
1096         int ap = 0;
1097 #ifndef CONFIG_NO_RANDOM_POOL
1098         size_t i, num;
1099 #endif /* CONFIG_NO_RANDOM_POOL */
1100
1101 #ifdef CONFIG_AP
1102         if (wpa_s->ap_iface)
1103                 ap = 1;
1104 #endif /* CONFIG_AP */
1105
1106         wpa_supplicant_notify_scanning(wpa_s, 0);
1107
1108 #ifdef CONFIG_P2P
1109         if (own_request && wpa_s->global->p2p_cb_on_scan_complete &&
1110             !wpa_s->global->p2p_disabled &&
1111             wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1112             !wpa_s->scan_res_handler) {
1113                 wpa_s->global->p2p_cb_on_scan_complete = 0;
1114                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1115                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1116                                 "stopped scan processing");
1117                         wpa_s->sta_scan_pending = 1;
1118                         wpa_supplicant_req_scan(wpa_s, 5, 0);
1119                         return -1;
1120                 }
1121         }
1122         wpa_s->sta_scan_pending = 0;
1123 #endif /* CONFIG_P2P */
1124
1125         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1126                                                    data ? &data->scan_info :
1127                                                    NULL, 1);
1128         if (scan_res == NULL) {
1129                 if (wpa_s->conf->ap_scan == 2 || ap ||
1130                     wpa_s->scan_res_handler == scan_only_handler)
1131                         return -1;
1132                 if (!own_request)
1133                         return -1;
1134                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1135                         "scanning again");
1136                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1137                 return -1;
1138         }
1139
1140 #ifndef CONFIG_NO_RANDOM_POOL
1141         num = scan_res->num;
1142         if (num > 10)
1143                 num = 10;
1144         for (i = 0; i < num; i++) {
1145                 u8 buf[5];
1146                 struct wpa_scan_res *res = scan_res->res[i];
1147                 buf[0] = res->bssid[5];
1148                 buf[1] = res->qual & 0xff;
1149                 buf[2] = res->noise & 0xff;
1150                 buf[3] = res->level & 0xff;
1151                 buf[4] = res->tsf & 0xff;
1152                 random_add_randomness(buf, sizeof(buf));
1153         }
1154 #endif /* CONFIG_NO_RANDOM_POOL */
1155
1156         if (own_request && wpa_s->scan_res_handler) {
1157                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1158                                          struct wpa_scan_results *scan_res);
1159
1160                 scan_res_handler = wpa_s->scan_res_handler;
1161                 wpa_s->scan_res_handler = NULL;
1162                 scan_res_handler(wpa_s, scan_res);
1163
1164                 wpa_scan_results_free(scan_res);
1165                 return -2;
1166         }
1167
1168         if (ap) {
1169                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1170 #ifdef CONFIG_AP
1171                 if (wpa_s->ap_iface->scan_cb)
1172                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1173 #endif /* CONFIG_AP */
1174                 wpa_scan_results_free(scan_res);
1175                 return 0;
1176         }
1177
1178         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1179         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1180         wpas_notify_scan_results(wpa_s);
1181
1182         wpas_notify_scan_done(wpa_s, 1);
1183
1184         if (sme_proc_obss_scan(wpa_s) > 0) {
1185                 wpa_scan_results_free(scan_res);
1186                 return 0;
1187         }
1188
1189         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1190                 wpa_scan_results_free(scan_res);
1191                 return 0;
1192         }
1193
1194         if (autoscan_notify_scan(wpa_s, scan_res)) {
1195                 wpa_scan_results_free(scan_res);
1196                 return 0;
1197         }
1198
1199         if (wpa_s->disconnected) {
1200                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1201                 wpa_scan_results_free(scan_res);
1202                 return 0;
1203         }
1204
1205         if (!wpas_driver_bss_selection(wpa_s) &&
1206             bgscan_notify_scan(wpa_s, scan_res) == 1) {
1207                 wpa_scan_results_free(scan_res);
1208                 return 0;
1209         }
1210
1211         wpas_wps_update_ap_info(wpa_s, scan_res);
1212
1213         wpa_scan_results_free(scan_res);
1214
1215         return wpas_select_network_from_last_scan(wpa_s, 1);
1216 }
1217
1218
1219 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1220                                               int new_scan)
1221 {
1222         struct wpa_bss *selected;
1223         struct wpa_ssid *ssid = NULL;
1224
1225         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1226
1227         if (selected) {
1228                 int skip;
1229                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1230                 if (skip) {
1231                         if (new_scan)
1232                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1233                         return 0;
1234                 }
1235
1236                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1237                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1238                         return -1;
1239                 }
1240                 if (new_scan)
1241                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1242                 /*
1243                  * Do not notify other virtual radios of scan results since we do not
1244                  * want them to start other associations at the same time.
1245                  */
1246                 return 1;
1247         } else {
1248                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1249                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1250                 if (ssid) {
1251                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1252                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1253                         if (new_scan)
1254                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1255                 } else {
1256                         int timeout_sec = wpa_s->scan_interval;
1257                         int timeout_usec = 0;
1258 #ifdef CONFIG_P2P
1259                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1260                                 return 0;
1261
1262                         if (wpa_s->p2p_in_provisioning) {
1263                                 /*
1264                                  * Use shorter wait during P2P Provisioning
1265                                  * state to speed up group formation.
1266                                  */
1267                                 timeout_sec = 0;
1268                                 timeout_usec = 250000;
1269                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1270                                                             timeout_usec);
1271                                 return 0;
1272                         }
1273 #endif /* CONFIG_P2P */
1274 #ifdef CONFIG_INTERWORKING
1275                         if (wpa_s->conf->auto_interworking &&
1276                             wpa_s->conf->interworking &&
1277                             wpa_s->conf->cred) {
1278                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1279                                         "start ANQP fetch since no matching "
1280                                         "networks found");
1281                                 wpa_s->network_select = 1;
1282                                 wpa_s->auto_network_select = 1;
1283                                 interworking_start_fetch_anqp(wpa_s);
1284                                 return 1;
1285                         }
1286 #endif /* CONFIG_INTERWORKING */
1287                         if (wpa_supplicant_req_sched_scan(wpa_s))
1288                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1289                                                             timeout_usec);
1290                 }
1291         }
1292         return 0;
1293 }
1294
1295
1296 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1297                                               union wpa_event_data *data)
1298 {
1299         const char *rn, *rn2;
1300         struct wpa_supplicant *ifs;
1301
1302         if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
1303                 /*
1304                  * If no scan results could be fetched, then no need to
1305                  * notify those interfaces that did not actually request
1306                  * this scan. Similarly, if scan results started a new operation on this
1307                  * interface, do not notify other interfaces to avoid concurrent
1308                  * operations during a connection attempt.
1309                  */
1310                 return;
1311         }
1312
1313         /*
1314          * Check other interfaces to see if they have the same radio-name. If
1315          * so, they get updated with this same scan info.
1316          */
1317         if (!wpa_s->driver->get_radio_name)
1318                 return;
1319
1320         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1321         if (rn == NULL || rn[0] == '\0')
1322                 return;
1323
1324         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1325                 "sharing same radio (%s) in event_scan_results", rn);
1326
1327         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1328                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1329                         continue;
1330
1331                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1332                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1333                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1334                                    "sibling", ifs->ifname);
1335                         _wpa_supplicant_event_scan_results(ifs, data, 0);
1336                 }
1337         }
1338 }
1339
1340 #endif /* CONFIG_NO_SCAN_PROCESSING */
1341
1342
1343 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1344 {
1345 #ifdef CONFIG_NO_SCAN_PROCESSING
1346         return -1;
1347 #else /* CONFIG_NO_SCAN_PROCESSING */
1348         struct os_time now;
1349
1350         if (wpa_s->last_scan_res_used <= 0)
1351                 return -1;
1352
1353         os_get_time(&now);
1354         if (now.sec - wpa_s->last_scan.sec > 5) {
1355                 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1356                 return -1;
1357         }
1358
1359         return wpas_select_network_from_last_scan(wpa_s, 0);
1360 #endif /* CONFIG_NO_SCAN_PROCESSING */
1361 }
1362
1363 #ifdef CONFIG_WNM
1364
1365 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1366 {
1367         struct wpa_supplicant *wpa_s = eloop_ctx;
1368
1369         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1370                 return;
1371
1372         if (!wpa_s->no_keep_alive) {
1373                 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1374                            MAC2STR(wpa_s->bssid));
1375                 /* TODO: could skip this if normal data traffic has been sent */
1376                 /* TODO: Consider using some more appropriate data frame for
1377                  * this */
1378                 if (wpa_s->l2)
1379                         l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1380                                        (u8 *) "", 0);
1381         }
1382
1383 #ifdef CONFIG_SME
1384         if (wpa_s->sme.bss_max_idle_period) {
1385                 unsigned int msec;
1386                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1387                 if (msec > 100)
1388                         msec -= 100;
1389                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1390                                        wnm_bss_keep_alive, wpa_s, NULL);
1391         }
1392 #endif /* CONFIG_SME */
1393 }
1394
1395
1396 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1397                                    const u8 *ies, size_t ies_len)
1398 {
1399         struct ieee802_11_elems elems;
1400
1401         if (ies == NULL)
1402                 return;
1403
1404         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1405                 return;
1406
1407 #ifdef CONFIG_SME
1408         if (elems.bss_max_idle_period) {
1409                 unsigned int msec;
1410                 wpa_s->sme.bss_max_idle_period =
1411                         WPA_GET_LE16(elems.bss_max_idle_period);
1412                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1413                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1414                            (elems.bss_max_idle_period[2] & 0x01) ?
1415                            " (protected keep-live required)" : "");
1416                 if (wpa_s->sme.bss_max_idle_period == 0)
1417                         wpa_s->sme.bss_max_idle_period = 1;
1418                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1419                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1420                          /* msec times 1000 */
1421                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1422                         if (msec > 100)
1423                                 msec -= 100;
1424                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1425                                                wnm_bss_keep_alive, wpa_s,
1426                                                NULL);
1427                 }
1428         }
1429 #endif /* CONFIG_SME */
1430 }
1431
1432 #endif /* CONFIG_WNM */
1433
1434
1435 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1436 {
1437 #ifdef CONFIG_WNM
1438         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1439 #endif /* CONFIG_WNM */
1440 }
1441
1442
1443 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1444                                           union wpa_event_data *data)
1445 {
1446         int l, len, found = 0, wpa_found, rsn_found;
1447         const u8 *p;
1448 #ifdef CONFIG_IEEE80211R
1449         u8 bssid[ETH_ALEN];
1450 #endif /* CONFIG_IEEE80211R */
1451
1452         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1453         if (data->assoc_info.req_ies)
1454                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1455                             data->assoc_info.req_ies_len);
1456         if (data->assoc_info.resp_ies) {
1457                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1458                             data->assoc_info.resp_ies_len);
1459 #ifdef CONFIG_TDLS
1460                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1461                                         data->assoc_info.resp_ies_len);
1462 #endif /* CONFIG_TDLS */
1463 #ifdef CONFIG_WNM
1464                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1465                                        data->assoc_info.resp_ies_len);
1466 #endif /* CONFIG_WNM */
1467         }
1468         if (data->assoc_info.beacon_ies)
1469                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1470                             data->assoc_info.beacon_ies,
1471                             data->assoc_info.beacon_ies_len);
1472         if (data->assoc_info.freq)
1473                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1474                         data->assoc_info.freq);
1475
1476         p = data->assoc_info.req_ies;
1477         l = data->assoc_info.req_ies_len;
1478
1479         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1480         while (p && l >= 2) {
1481                 len = p[1] + 2;
1482                 if (len > l) {
1483                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1484                                     p, l);
1485                         break;
1486                 }
1487                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1488                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1489                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1490                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1491                                 break;
1492                         found = 1;
1493                         wpa_find_assoc_pmkid(wpa_s);
1494                         break;
1495                 }
1496                 l -= len;
1497                 p += len;
1498         }
1499         if (!found && data->assoc_info.req_ies)
1500                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1501
1502 #ifdef CONFIG_IEEE80211R
1503 #ifdef CONFIG_SME
1504         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1505                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1506                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1507                                                  data->assoc_info.resp_ies,
1508                                                  data->assoc_info.resp_ies_len,
1509                                                  bssid) < 0) {
1510                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1511                                 "Reassociation Response failed");
1512                         wpa_supplicant_deauthenticate(
1513                                 wpa_s, WLAN_REASON_INVALID_IE);
1514                         return -1;
1515                 }
1516         }
1517
1518         p = data->assoc_info.resp_ies;
1519         l = data->assoc_info.resp_ies_len;
1520
1521 #ifdef CONFIG_WPS_STRICT
1522         if (p && wpa_s->current_ssid &&
1523             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1524                 struct wpabuf *wps;
1525                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1526                 if (wps == NULL) {
1527                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1528                                 "include WPS IE in (Re)Association Response");
1529                         return -1;
1530                 }
1531
1532                 if (wps_validate_assoc_resp(wps) < 0) {
1533                         wpabuf_free(wps);
1534                         wpa_supplicant_deauthenticate(
1535                                 wpa_s, WLAN_REASON_INVALID_IE);
1536                         return -1;
1537                 }
1538                 wpabuf_free(wps);
1539         }
1540 #endif /* CONFIG_WPS_STRICT */
1541
1542         /* Go through the IEs and make a copy of the MDIE, if present. */
1543         while (p && l >= 2) {
1544                 len = p[1] + 2;
1545                 if (len > l) {
1546                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1547                                     p, l);
1548                         break;
1549                 }
1550                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1551                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1552                         wpa_s->sme.ft_used = 1;
1553                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1554                                   MOBILITY_DOMAIN_ID_LEN);
1555                         break;
1556                 }
1557                 l -= len;
1558                 p += len;
1559         }
1560 #endif /* CONFIG_SME */
1561
1562         /* Process FT when SME is in the driver */
1563         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1564             wpa_ft_is_completed(wpa_s->wpa)) {
1565                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1566                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1567                                                  data->assoc_info.resp_ies,
1568                                                  data->assoc_info.resp_ies_len,
1569                                                  bssid) < 0) {
1570                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1571                                 "Reassociation Response failed");
1572                         wpa_supplicant_deauthenticate(
1573                                 wpa_s, WLAN_REASON_INVALID_IE);
1574                         return -1;
1575                 }
1576                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1577         }
1578
1579         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1580                              data->assoc_info.resp_ies_len);
1581 #endif /* CONFIG_IEEE80211R */
1582
1583         /* WPA/RSN IE from Beacon/ProbeResp */
1584         p = data->assoc_info.beacon_ies;
1585         l = data->assoc_info.beacon_ies_len;
1586
1587         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1588          */
1589         wpa_found = rsn_found = 0;
1590         while (p && l >= 2) {
1591                 len = p[1] + 2;
1592                 if (len > l) {
1593                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1594                                     p, l);
1595                         break;
1596                 }
1597                 if (!wpa_found &&
1598                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1599                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1600                         wpa_found = 1;
1601                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1602                 }
1603
1604                 if (!rsn_found &&
1605                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1606                         rsn_found = 1;
1607                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1608                 }
1609
1610                 l -= len;
1611                 p += len;
1612         }
1613
1614         if (!wpa_found && data->assoc_info.beacon_ies)
1615                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1616         if (!rsn_found && data->assoc_info.beacon_ies)
1617                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1618         if (wpa_found || rsn_found)
1619                 wpa_s->ap_ies_from_associnfo = 1;
1620
1621         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1622             wpa_s->assoc_freq != data->assoc_info.freq) {
1623                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1624                            "%u to %u MHz",
1625                            wpa_s->assoc_freq, data->assoc_info.freq);
1626                 wpa_supplicant_update_scan_results(wpa_s);
1627         }
1628
1629         wpa_s->assoc_freq = data->assoc_info.freq;
1630
1631         return 0;
1632 }
1633
1634
1635 static struct wpa_bss * wpa_supplicant_get_new_bss(
1636         struct wpa_supplicant *wpa_s, const u8 *bssid)
1637 {
1638         struct wpa_bss *bss = NULL;
1639         struct wpa_ssid *ssid = wpa_s->current_ssid;
1640
1641         if (ssid->ssid_len > 0)
1642                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1643         if (!bss)
1644                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1645
1646         return bss;
1647 }
1648
1649
1650 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1651 {
1652         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1653
1654         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1655                 return -1;
1656
1657         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1658                 return 0;
1659
1660         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1661                                         WPA_IE_VENDOR_TYPE);
1662         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1663
1664         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1665                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1666             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1667                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1668                 return -1;
1669
1670         return 0;
1671 }
1672
1673
1674 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1675                                        union wpa_event_data *data)
1676 {
1677         u8 bssid[ETH_ALEN];
1678         int ft_completed;
1679         struct wpa_driver_capa capa;
1680
1681 #ifdef CONFIG_AP
1682         if (wpa_s->ap_iface) {
1683                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1684                                     data->assoc_info.addr,
1685                                     data->assoc_info.req_ies,
1686                                     data->assoc_info.req_ies_len,
1687                                     data->assoc_info.reassoc);
1688                 return;
1689         }
1690 #endif /* CONFIG_AP */
1691
1692         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1693         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1694                 return;
1695
1696         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1697                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1698                 wpa_supplicant_deauthenticate(
1699                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1700                 return;
1701         }
1702
1703         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1704         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1705                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1706                         MACSTR, MAC2STR(bssid));
1707                 random_add_randomness(bssid, ETH_ALEN);
1708                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1709                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1710                 wpas_notify_bssid_changed(wpa_s);
1711
1712                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1713                         wpa_clear_keys(wpa_s, bssid);
1714                 }
1715                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1716                         wpa_supplicant_deauthenticate(
1717                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1718                         return;
1719                 }
1720                 if (wpa_s->current_ssid) {
1721                         struct wpa_bss *bss = NULL;
1722
1723                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1724                         if (!bss) {
1725                                 wpa_supplicant_update_scan_results(wpa_s);
1726
1727                                 /* Get the BSS from the new scan results */
1728                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1729                         }
1730
1731                         if (bss)
1732                                 wpa_s->current_bss = bss;
1733                 }
1734
1735                 if (wpa_s->conf->ap_scan == 1 &&
1736                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1737                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1738                                 wpa_msg(wpa_s, MSG_WARNING,
1739                                         "WPA/RSN IEs not updated");
1740                 }
1741         }
1742
1743 #ifdef CONFIG_SME
1744         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1745         wpa_s->sme.prev_bssid_set = 1;
1746 #endif /* CONFIG_SME */
1747
1748         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1749         if (wpa_s->current_ssid) {
1750                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1751                  * initialized before association, but for other modes,
1752                  * initialize PC/SC here, if the current configuration needs
1753                  * smartcard or SIM/USIM. */
1754                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1755         }
1756         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1757         if (wpa_s->l2)
1758                 l2_packet_notify_auth_start(wpa_s->l2);
1759
1760         /*
1761          * Set portEnabled first to FALSE in order to get EAP state machine out
1762          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1763          * state machine may transit to AUTHENTICATING state based on obsolete
1764          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1765          * AUTHENTICATED without ever giving chance to EAP state machine to
1766          * reset the state.
1767          */
1768         if (!ft_completed) {
1769                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1770                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1771         }
1772         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1773                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1774         /* 802.1X::portControl = Auto */
1775         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1776         wpa_s->eapol_received = 0;
1777         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1778             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1779             (wpa_s->current_ssid &&
1780              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1781                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1782                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1783         } else if (!ft_completed) {
1784                 /* Timeout for receiving the first EAPOL packet */
1785                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1786         }
1787         wpa_supplicant_cancel_scan(wpa_s);
1788
1789         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1790             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1791                 /*
1792                  * We are done; the driver will take care of RSN 4-way
1793                  * handshake.
1794                  */
1795                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1796                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1797                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1798                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1799         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1800                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1801                 /*
1802                  * The driver will take care of RSN 4-way handshake, so we need
1803                  * to allow EAPOL supplicant to complete its work without
1804                  * waiting for WPA supplicant.
1805                  */
1806                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1807         } else if (ft_completed) {
1808                 /*
1809                  * FT protocol completed - make sure EAPOL state machine ends
1810                  * up in authenticated.
1811                  */
1812                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1813                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1814                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1815                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1816         }
1817
1818         wpa_s->last_eapol_matches_bssid = 0;
1819
1820         if (wpa_s->pending_eapol_rx) {
1821                 struct os_time now, age;
1822                 os_get_time(&now);
1823                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1824                 if (age.sec == 0 && age.usec < 100000 &&
1825                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1826                     0) {
1827                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1828                                 "frame that was received just before "
1829                                 "association notification");
1830                         wpa_supplicant_rx_eapol(
1831                                 wpa_s, wpa_s->pending_eapol_rx_src,
1832                                 wpabuf_head(wpa_s->pending_eapol_rx),
1833                                 wpabuf_len(wpa_s->pending_eapol_rx));
1834                 }
1835                 wpabuf_free(wpa_s->pending_eapol_rx);
1836                 wpa_s->pending_eapol_rx = NULL;
1837         }
1838
1839         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1840              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1841             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1842             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1843                 /* Set static WEP keys again */
1844                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1845         }
1846
1847 #ifdef CONFIG_IBSS_RSN
1848         if (wpa_s->current_ssid &&
1849             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1850             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1851             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1852             wpa_s->ibss_rsn == NULL) {
1853                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1854                 if (!wpa_s->ibss_rsn) {
1855                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1856                         wpa_supplicant_deauthenticate(
1857                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1858                         return;
1859                 }
1860
1861                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1862         }
1863 #endif /* CONFIG_IBSS_RSN */
1864
1865         wpas_wps_notify_assoc(wpa_s, bssid);
1866 }
1867
1868
1869 static int disconnect_reason_recoverable(u16 reason_code)
1870 {
1871         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1872                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1873                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1874 }
1875
1876
1877 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1878                                           u16 reason_code,
1879                                           int locally_generated)
1880 {
1881         const u8 *bssid;
1882
1883         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1884                 /*
1885                  * At least Host AP driver and a Prism3 card seemed to be
1886                  * generating streams of disconnected events when configuring
1887                  * IBSS for WPA-None. Ignore them for now.
1888                  */
1889                 return;
1890         }
1891
1892         bssid = wpa_s->bssid;
1893         if (is_zero_ether_addr(bssid))
1894                 bssid = wpa_s->pending_bssid;
1895
1896         if (!is_zero_ether_addr(bssid) ||
1897             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1898                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1899                         " reason=%d%s",
1900                         MAC2STR(bssid), reason_code,
1901                         locally_generated ? " locally_generated=1" : "");
1902         }
1903 }
1904
1905
1906 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1907                                  int locally_generated)
1908 {
1909         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1910             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1911                 return 0; /* Not in 4-way handshake with PSK */
1912
1913         /*
1914          * It looks like connection was lost while trying to go through PSK
1915          * 4-way handshake. Filter out known disconnection cases that are caused
1916          * by something else than PSK mismatch to avoid confusing reports.
1917          */
1918
1919         if (locally_generated) {
1920                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1921                         return 0;
1922         }
1923
1924         return 1;
1925 }
1926
1927
1928 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1929                                                  u16 reason_code,
1930                                                  int locally_generated)
1931 {
1932         const u8 *bssid;
1933         int authenticating;
1934         u8 prev_pending_bssid[ETH_ALEN];
1935         struct wpa_bss *fast_reconnect = NULL;
1936         struct wpa_ssid *fast_reconnect_ssid = NULL;
1937         struct wpa_ssid *last_ssid;
1938
1939         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1940         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1941
1942         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1943                 /*
1944                  * At least Host AP driver and a Prism3 card seemed to be
1945                  * generating streams of disconnected events when configuring
1946                  * IBSS for WPA-None. Ignore them for now.
1947                  */
1948                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1949                         "IBSS/WPA-None mode");
1950                 return;
1951         }
1952
1953         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
1954                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1955                         "pre-shared key may be incorrect");
1956                 wpas_auth_failed(wpa_s);
1957         }
1958         if (!wpa_s->auto_reconnect_disabled ||
1959             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1960                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1961                         "reconnect (wps=%d wpa_state=%d)",
1962                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1963                         wpa_s->wpa_state);
1964                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1965                     wpa_s->current_ssid &&
1966                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1967                     !locally_generated &&
1968                     disconnect_reason_recoverable(reason_code)) {
1969                         /*
1970                          * It looks like the AP has dropped association with
1971                          * us, but could allow us to get back in. Try to
1972                          * reconnect to the same BSS without full scan to save
1973                          * time for some common cases.
1974                          */
1975                         fast_reconnect = wpa_s->current_bss;
1976                         fast_reconnect_ssid = wpa_s->current_ssid;
1977                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1978                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1979                 else
1980                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1981                                 "immediate scan");
1982         } else {
1983                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1984                         "try to re-connect");
1985                 wpa_s->reassociate = 0;
1986                 wpa_s->disconnected = 1;
1987                 wpa_supplicant_cancel_sched_scan(wpa_s);
1988         }
1989         bssid = wpa_s->bssid;
1990         if (is_zero_ether_addr(bssid))
1991                 bssid = wpa_s->pending_bssid;
1992         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1993                 wpas_connection_failed(wpa_s, bssid);
1994         wpa_sm_notify_disassoc(wpa_s->wpa);
1995         if (locally_generated)
1996                 wpa_s->disconnect_reason = -reason_code;
1997         else
1998                 wpa_s->disconnect_reason = reason_code;
1999         wpas_notify_disconnect_reason(wpa_s);
2000         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2001                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2002                 wpa_s->keys_cleared = 0;
2003                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2004         }
2005         last_ssid = wpa_s->current_ssid;
2006         wpa_supplicant_mark_disassoc(wpa_s);
2007
2008         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2009                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2010                 wpa_s->current_ssid = last_ssid;
2011         }
2012
2013         if (fast_reconnect) {
2014 #ifndef CONFIG_NO_SCAN_PROCESSING
2015                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2016                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2017                                            fast_reconnect_ssid) < 0) {
2018                         /* Recover through full scan */
2019                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2020                 }
2021 #endif /* CONFIG_NO_SCAN_PROCESSING */
2022         }
2023 }
2024
2025
2026 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2027 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2028 {
2029         struct wpa_supplicant *wpa_s = eloop_ctx;
2030
2031         if (!wpa_s->pending_mic_error_report)
2032                 return;
2033
2034         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2035         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2036         wpa_s->pending_mic_error_report = 0;
2037 }
2038 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2039
2040
2041 static void
2042 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2043                                          union wpa_event_data *data)
2044 {
2045         int pairwise;
2046         struct os_time t;
2047
2048         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2049         pairwise = (data && data->michael_mic_failure.unicast);
2050         os_get_time(&t);
2051         if ((wpa_s->last_michael_mic_error &&
2052              t.sec - wpa_s->last_michael_mic_error <= 60) ||
2053             wpa_s->pending_mic_error_report) {
2054                 if (wpa_s->pending_mic_error_report) {
2055                         /*
2056                          * Send the pending MIC error report immediately since
2057                          * we are going to start countermeasures and AP better
2058                          * do the same.
2059                          */
2060                         wpa_sm_key_request(wpa_s->wpa, 1,
2061                                            wpa_s->pending_mic_error_pairwise);
2062                 }
2063
2064                 /* Send the new MIC error report immediately since we are going
2065                  * to start countermeasures and AP better do the same.
2066                  */
2067                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2068
2069                 /* initialize countermeasures */
2070                 wpa_s->countermeasures = 1;
2071
2072                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2073
2074                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2075
2076                 /*
2077                  * Need to wait for completion of request frame. We do not get
2078                  * any callback for the message completion, so just wait a
2079                  * short while and hope for the best. */
2080                 os_sleep(0, 10000);
2081
2082                 wpa_drv_set_countermeasures(wpa_s, 1);
2083                 wpa_supplicant_deauthenticate(wpa_s,
2084                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2085                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2086                                      wpa_s, NULL);
2087                 eloop_register_timeout(60, 0,
2088                                        wpa_supplicant_stop_countermeasures,
2089                                        wpa_s, NULL);
2090                 /* TODO: mark the AP rejected for 60 second. STA is
2091                  * allowed to associate with another AP.. */
2092         } else {
2093 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2094                 if (wpa_s->mic_errors_seen) {
2095                         /*
2096                          * Reduce the effectiveness of Michael MIC error
2097                          * reports as a means for attacking against TKIP if
2098                          * more than one MIC failure is noticed with the same
2099                          * PTK. We delay the transmission of the reports by a
2100                          * random time between 0 and 60 seconds in order to
2101                          * force the attacker wait 60 seconds before getting
2102                          * the information on whether a frame resulted in a MIC
2103                          * failure.
2104                          */
2105                         u8 rval[4];
2106                         int sec;
2107
2108                         if (os_get_random(rval, sizeof(rval)) < 0)
2109                                 sec = os_random() % 60;
2110                         else
2111                                 sec = WPA_GET_BE32(rval) % 60;
2112                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2113                                 "report %d seconds", sec);
2114                         wpa_s->pending_mic_error_report = 1;
2115                         wpa_s->pending_mic_error_pairwise = pairwise;
2116                         eloop_cancel_timeout(
2117                                 wpa_supplicant_delayed_mic_error_report,
2118                                 wpa_s, NULL);
2119                         eloop_register_timeout(
2120                                 sec, os_random() % 1000000,
2121                                 wpa_supplicant_delayed_mic_error_report,
2122                                 wpa_s, NULL);
2123                 } else {
2124                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2125                 }
2126 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2127                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2128 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2129         }
2130         wpa_s->last_michael_mic_error = t.sec;
2131         wpa_s->mic_errors_seen++;
2132 }
2133
2134
2135 #ifdef CONFIG_TERMINATE_ONLASTIF
2136 static int any_interfaces(struct wpa_supplicant *head)
2137 {
2138         struct wpa_supplicant *wpa_s;
2139
2140         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2141                 if (!wpa_s->interface_removed)
2142                         return 1;
2143         return 0;
2144 }
2145 #endif /* CONFIG_TERMINATE_ONLASTIF */
2146
2147
2148 static void
2149 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2150                                       union wpa_event_data *data)
2151 {
2152         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2153                 return;
2154
2155         switch (data->interface_status.ievent) {
2156         case EVENT_INTERFACE_ADDED:
2157                 if (!wpa_s->interface_removed)
2158                         break;
2159                 wpa_s->interface_removed = 0;
2160                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2161                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2162                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2163                                 "driver after interface was added");
2164                 }
2165                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2166                 break;
2167         case EVENT_INTERFACE_REMOVED:
2168                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2169                 wpa_s->interface_removed = 1;
2170                 wpa_supplicant_mark_disassoc(wpa_s);
2171                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2172                 l2_packet_deinit(wpa_s->l2);
2173                 wpa_s->l2 = NULL;
2174 #ifdef CONFIG_IBSS_RSN
2175                 ibss_rsn_deinit(wpa_s->ibss_rsn);
2176                 wpa_s->ibss_rsn = NULL;
2177 #endif /* CONFIG_IBSS_RSN */
2178 #ifdef CONFIG_TERMINATE_ONLASTIF
2179                 /* check if last interface */
2180                 if (!any_interfaces(wpa_s->global->ifaces))
2181                         eloop_terminate();
2182 #endif /* CONFIG_TERMINATE_ONLASTIF */
2183                 break;
2184         }
2185 }
2186
2187
2188 #ifdef CONFIG_PEERKEY
2189 static void
2190 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2191                               union wpa_event_data *data)
2192 {
2193         if (data == NULL)
2194                 return;
2195         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2196 }
2197 #endif /* CONFIG_PEERKEY */
2198
2199
2200 #ifdef CONFIG_TDLS
2201 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2202                                       union wpa_event_data *data)
2203 {
2204         if (data == NULL)
2205                 return;
2206         switch (data->tdls.oper) {
2207         case TDLS_REQUEST_SETUP:
2208                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2209                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2210                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2211                 else
2212                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2213                 break;
2214         case TDLS_REQUEST_TEARDOWN:
2215                 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2216                                        data->tdls.reason_code);
2217                 break;
2218         }
2219 }
2220 #endif /* CONFIG_TDLS */
2221
2222
2223 #ifdef CONFIG_WNM
2224 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2225                                      union wpa_event_data *data)
2226 {
2227         if (data == NULL)
2228                 return;
2229         switch (data->wnm.oper) {
2230         case WNM_OPER_SLEEP:
2231                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2232                            "(action=%d, intval=%d)",
2233                            data->wnm.sleep_action, data->wnm.sleep_intval);
2234                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2235                                              data->wnm.sleep_intval, NULL);
2236                 break;
2237         }
2238 }
2239 #endif /* CONFIG_WNM */
2240
2241
2242 #ifdef CONFIG_IEEE80211R
2243 static void
2244 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2245                                  union wpa_event_data *data)
2246 {
2247         if (data == NULL)
2248                 return;
2249
2250         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2251                                     data->ft_ies.ies_len,
2252                                     data->ft_ies.ft_action,
2253                                     data->ft_ies.target_ap,
2254                                     data->ft_ies.ric_ies,
2255                                     data->ft_ies.ric_ies_len) < 0) {
2256                 /* TODO: prevent MLME/driver from trying to associate? */
2257         }
2258 }
2259 #endif /* CONFIG_IEEE80211R */
2260
2261
2262 #ifdef CONFIG_IBSS_RSN
2263 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2264                                                 union wpa_event_data *data)
2265 {
2266         struct wpa_ssid *ssid;
2267         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2268                 return;
2269         if (data == NULL)
2270                 return;
2271         ssid = wpa_s->current_ssid;
2272         if (ssid == NULL)
2273                 return;
2274         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2275                 return;
2276
2277         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2278 }
2279 #endif /* CONFIG_IBSS_RSN */
2280
2281
2282 #ifdef CONFIG_IEEE80211R
2283 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2284                          size_t len)
2285 {
2286         const u8 *sta_addr, *target_ap_addr;
2287         u16 status;
2288
2289         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2290         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2291                 return; /* only SME case supported for now */
2292         if (len < 1 + 2 * ETH_ALEN + 2)
2293                 return;
2294         if (data[0] != 2)
2295                 return; /* Only FT Action Response is supported for now */
2296         sta_addr = data + 1;
2297         target_ap_addr = data + 1 + ETH_ALEN;
2298         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2299         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2300                 MACSTR " TargetAP " MACSTR " status %u",
2301                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2302
2303         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2304                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2305                         " in FT Action Response", MAC2STR(sta_addr));
2306                 return;
2307         }
2308
2309         if (status) {
2310                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2311                         "failure (status code %d)", status);
2312                 /* TODO: report error to FT code(?) */
2313                 return;
2314         }
2315
2316         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2317                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2318                                     target_ap_addr, NULL, 0) < 0)
2319                 return;
2320
2321 #ifdef CONFIG_SME
2322         {
2323                 struct wpa_bss *bss;
2324                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2325                 if (bss)
2326                         wpa_s->sme.freq = bss->freq;
2327                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2328                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2329                               WLAN_AUTH_FT);
2330         }
2331 #endif /* CONFIG_SME */
2332 }
2333 #endif /* CONFIG_IEEE80211R */
2334
2335
2336 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2337                                                struct unprot_deauth *e)
2338 {
2339 #ifdef CONFIG_IEEE80211W
2340         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2341                    "dropped: " MACSTR " -> " MACSTR
2342                    " (reason code %u)",
2343                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2344         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2345 #endif /* CONFIG_IEEE80211W */
2346 }
2347
2348
2349 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2350                                                  struct unprot_disassoc *e)
2351 {
2352 #ifdef CONFIG_IEEE80211W
2353         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2354                    "dropped: " MACSTR " -> " MACSTR
2355                    " (reason code %u)",
2356                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2357         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2358 #endif /* CONFIG_IEEE80211W */
2359 }
2360
2361
2362 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2363                           union wpa_event_data *data)
2364 {
2365         struct wpa_supplicant *wpa_s = ctx;
2366         u16 reason_code = 0;
2367         int locally_generated = 0;
2368
2369         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2370             event != EVENT_INTERFACE_ENABLED &&
2371             event != EVENT_INTERFACE_STATUS &&
2372             event != EVENT_SCHED_SCAN_STOPPED) {
2373                 wpa_dbg(wpa_s, MSG_DEBUG,
2374                         "Ignore event %s (%d) while interface is disabled",
2375                         event_to_string(event), event);
2376                 return;
2377         }
2378
2379 #ifndef CONFIG_NO_STDOUT_DEBUG
2380 {
2381         int level = MSG_DEBUG;
2382
2383         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2384                 const struct ieee80211_hdr *hdr;
2385                 u16 fc;
2386                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2387                 fc = le_to_host16(hdr->frame_control);
2388                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2389                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2390                         level = MSG_EXCESSIVE;
2391         }
2392
2393         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2394                 event_to_string(event), event);
2395 }
2396 #endif /* CONFIG_NO_STDOUT_DEBUG */
2397
2398         switch (event) {
2399         case EVENT_AUTH:
2400                 sme_event_auth(wpa_s, data);
2401                 break;
2402         case EVENT_ASSOC:
2403                 wpa_supplicant_event_assoc(wpa_s, data);
2404                 break;
2405         case EVENT_DISASSOC:
2406                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2407                 if (data) {
2408                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2409                                 data->disassoc_info.reason_code,
2410                                 data->disassoc_info.locally_generated ?
2411                                 " (locally generated)" : "");
2412                         if (data->disassoc_info.addr)
2413                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2414                                         MAC2STR(data->disassoc_info.addr));
2415                 }
2416 #ifdef CONFIG_AP
2417                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2418                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2419                                                data->disassoc_info.addr);
2420                         break;
2421                 }
2422                 if (wpa_s->ap_iface) {
2423                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2424                                 "AP mode");
2425                         break;
2426                 }
2427 #endif /* CONFIG_AP */
2428                 if (data) {
2429                         reason_code = data->disassoc_info.reason_code;
2430                         locally_generated =
2431                                 data->disassoc_info.locally_generated;
2432                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2433                                     data->disassoc_info.ie,
2434                                     data->disassoc_info.ie_len);
2435 #ifdef CONFIG_P2P
2436                         wpas_p2p_disassoc_notif(
2437                                 wpa_s, data->disassoc_info.addr, reason_code,
2438                                 data->disassoc_info.ie,
2439                                 data->disassoc_info.ie_len,
2440                                 locally_generated);
2441 #endif /* CONFIG_P2P */
2442                 }
2443                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2444                         sme_event_disassoc(wpa_s, data);
2445                 /* fall through */
2446         case EVENT_DEAUTH:
2447                 if (event == EVENT_DEAUTH) {
2448                         wpa_dbg(wpa_s, MSG_DEBUG,
2449                                 "Deauthentication notification");
2450                         if (data) {
2451                                 reason_code = data->deauth_info.reason_code;
2452                                 locally_generated =
2453                                         data->deauth_info.locally_generated;
2454                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2455                                         data->deauth_info.reason_code,
2456                                         data->deauth_info.locally_generated ?
2457                                         " (locally generated)" : "");
2458                                 if (data->deauth_info.addr) {
2459                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2460                                                 MACSTR,
2461                                                 MAC2STR(data->deauth_info.
2462                                                         addr));
2463                                 }
2464                                 wpa_hexdump(MSG_DEBUG,
2465                                             "Deauthentication frame IE(s)",
2466                                             data->deauth_info.ie,
2467                                             data->deauth_info.ie_len);
2468                         }
2469                         wpa_reset_ft_completed(wpa_s->wpa);
2470                 }
2471 #ifdef CONFIG_AP
2472                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2473                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2474                                                data->deauth_info.addr);
2475                         break;
2476                 }
2477                 if (wpa_s->ap_iface) {
2478                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2479                                 "AP mode");
2480                         break;
2481                 }
2482 #endif /* CONFIG_AP */
2483                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2484                                               locally_generated);
2485                 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2486                     ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2487                       (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2488                      eapol_sm_failed(wpa_s->eapol)))
2489                         wpas_auth_failed(wpa_s);
2490 #ifdef CONFIG_P2P
2491                 if (event == EVENT_DEAUTH && data) {
2492                         if (wpas_p2p_deauth_notif(wpa_s,
2493                                                   data->deauth_info.addr,
2494                                                   reason_code,
2495                                                   data->deauth_info.ie,
2496                                                   data->deauth_info.ie_len,
2497                                                   locally_generated) > 0) {
2498                                 /*
2499                                  * The interface was removed, so cannot
2500                                  * continue processing any additional
2501                                  * operations after this.
2502                                  */
2503                                 break;
2504                         }
2505                 }
2506 #endif /* CONFIG_P2P */
2507                 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2508                                                      locally_generated);
2509                 break;
2510         case EVENT_MICHAEL_MIC_FAILURE:
2511                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2512                 break;
2513 #ifndef CONFIG_NO_SCAN_PROCESSING
2514         case EVENT_SCAN_RESULTS:
2515                 wpa_supplicant_event_scan_results(wpa_s, data);
2516                 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2517                     wpa_s->wpa_state != WPA_ASSOCIATING)
2518                         wpas_p2p_continue_after_scan(wpa_s);
2519                 break;
2520 #endif /* CONFIG_NO_SCAN_PROCESSING */
2521         case EVENT_ASSOCINFO:
2522                 wpa_supplicant_event_associnfo(wpa_s, data);
2523                 break;
2524         case EVENT_INTERFACE_STATUS:
2525                 wpa_supplicant_event_interface_status(wpa_s, data);
2526                 break;
2527         case EVENT_PMKID_CANDIDATE:
2528                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2529                 break;
2530 #ifdef CONFIG_PEERKEY
2531         case EVENT_STKSTART:
2532                 wpa_supplicant_event_stkstart(wpa_s, data);
2533                 break;
2534 #endif /* CONFIG_PEERKEY */
2535 #ifdef CONFIG_TDLS
2536         case EVENT_TDLS:
2537                 wpa_supplicant_event_tdls(wpa_s, data);
2538                 break;
2539 #endif /* CONFIG_TDLS */
2540 #ifdef CONFIG_WNM
2541         case EVENT_WNM:
2542                 wpa_supplicant_event_wnm(wpa_s, data);
2543                 break;
2544 #endif /* CONFIG_WNM */
2545 #ifdef CONFIG_IEEE80211R
2546         case EVENT_FT_RESPONSE:
2547                 wpa_supplicant_event_ft_response(wpa_s, data);
2548                 break;
2549 #endif /* CONFIG_IEEE80211R */
2550 #ifdef CONFIG_IBSS_RSN
2551         case EVENT_IBSS_RSN_START:
2552                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2553                 break;
2554 #endif /* CONFIG_IBSS_RSN */
2555         case EVENT_ASSOC_REJECT:
2556                 if (data->assoc_reject.bssid)
2557                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2558                                 "bssid=" MACSTR " status_code=%u",
2559                                 MAC2STR(data->assoc_reject.bssid),
2560                                 data->assoc_reject.status_code);
2561                 else
2562                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2563                                 "status_code=%u",
2564                                 data->assoc_reject.status_code);
2565                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2566                         sme_event_assoc_reject(wpa_s, data);
2567                 else {
2568                         const u8 *bssid = data->assoc_reject.bssid;
2569                         if (bssid == NULL || is_zero_ether_addr(bssid))
2570                                 bssid = wpa_s->pending_bssid;
2571                         wpas_connection_failed(wpa_s, bssid);
2572                         wpa_supplicant_mark_disassoc(wpa_s);
2573                 }
2574                 break;
2575         case EVENT_AUTH_TIMED_OUT:
2576                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2577                         sme_event_auth_timed_out(wpa_s, data);
2578                 break;
2579         case EVENT_ASSOC_TIMED_OUT:
2580                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2581                         sme_event_assoc_timed_out(wpa_s, data);
2582                 break;
2583         case EVENT_TX_STATUS:
2584                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2585                         " type=%d stype=%d",
2586                         MAC2STR(data->tx_status.dst),
2587                         data->tx_status.type, data->tx_status.stype);
2588 #ifdef CONFIG_AP
2589                 if (wpa_s->ap_iface == NULL) {
2590 #ifdef CONFIG_OFFCHANNEL
2591                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2592                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2593                                 offchannel_send_action_tx_status(
2594                                         wpa_s, data->tx_status.dst,
2595                                         data->tx_status.data,
2596                                         data->tx_status.data_len,
2597                                         data->tx_status.ack ?
2598                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2599                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2600 #endif /* CONFIG_OFFCHANNEL */
2601                         break;
2602                 }
2603 #endif /* CONFIG_AP */
2604 #ifdef CONFIG_OFFCHANNEL
2605                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2606                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2607                 /*
2608                  * Catch TX status events for Action frames we sent via group
2609                  * interface in GO mode.
2610                  */
2611                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2612                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2613                     os_memcmp(wpa_s->parent->pending_action_dst,
2614                               data->tx_status.dst, ETH_ALEN) == 0) {
2615                         offchannel_send_action_tx_status(
2616                                 wpa_s->parent, data->tx_status.dst,
2617                                 data->tx_status.data,
2618                                 data->tx_status.data_len,
2619                                 data->tx_status.ack ?
2620                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2621                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2622                         break;
2623                 }
2624 #endif /* CONFIG_OFFCHANNEL */
2625 #ifdef CONFIG_AP
2626                 switch (data->tx_status.type) {
2627                 case WLAN_FC_TYPE_MGMT:
2628                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2629                                       data->tx_status.data_len,
2630                                       data->tx_status.stype,
2631                                       data->tx_status.ack);
2632                         break;
2633                 case WLAN_FC_TYPE_DATA:
2634                         ap_tx_status(wpa_s, data->tx_status.dst,
2635                                      data->tx_status.data,
2636                                      data->tx_status.data_len,
2637                                      data->tx_status.ack);
2638                         break;
2639                 }
2640 #endif /* CONFIG_AP */
2641                 break;
2642 #ifdef CONFIG_AP
2643         case EVENT_EAPOL_TX_STATUS:
2644                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2645                                    data->eapol_tx_status.data,
2646                                    data->eapol_tx_status.data_len,
2647                                    data->eapol_tx_status.ack);
2648                 break;
2649         case EVENT_DRIVER_CLIENT_POLL_OK:
2650                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2651                 break;
2652         case EVENT_RX_FROM_UNKNOWN:
2653                 if (wpa_s->ap_iface == NULL)
2654                         break;
2655                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2656                                        data->rx_from_unknown.wds);
2657                 break;
2658         case EVENT_CH_SWITCH:
2659                 if (!data)
2660                         break;
2661                 if (!wpa_s->ap_iface) {
2662                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2663                                 "event in non-AP mode");
2664                         break;
2665                 }
2666
2667 #ifdef CONFIG_AP
2668                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2669                                   data->ch_switch.ht_enabled,
2670                                   data->ch_switch.ch_offset);
2671 #endif /* CONFIG_AP */
2672                 break;
2673         case EVENT_RX_MGMT: {
2674                 u16 fc, stype;
2675                 const struct ieee80211_mgmt *mgmt;
2676
2677                 mgmt = (const struct ieee80211_mgmt *)
2678                         data->rx_mgmt.frame;
2679                 fc = le_to_host16(mgmt->frame_control);
2680                 stype = WLAN_FC_GET_STYPE(fc);
2681
2682                 if (wpa_s->ap_iface == NULL) {
2683 #ifdef CONFIG_P2P
2684                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2685                             data->rx_mgmt.frame_len > 24) {
2686                                 const u8 *src = mgmt->sa;
2687                                 const u8 *ie = mgmt->u.probe_req.variable;
2688                                 size_t ie_len = data->rx_mgmt.frame_len -
2689                                         (mgmt->u.probe_req.variable -
2690                                          data->rx_mgmt.frame);
2691                                 wpas_p2p_probe_req_rx(
2692                                         wpa_s, src, mgmt->da,
2693                                         mgmt->bssid, ie, ie_len,
2694                                         data->rx_mgmt.ssi_signal);
2695                                 break;
2696                         }
2697 #endif /* CONFIG_P2P */
2698                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2699                                 "management frame in non-AP mode");
2700                         break;
2701                 }
2702
2703                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2704                     data->rx_mgmt.frame_len > 24) {
2705                         const u8 *ie = mgmt->u.probe_req.variable;
2706                         size_t ie_len = data->rx_mgmt.frame_len -
2707                                 (mgmt->u.probe_req.variable -
2708                                  data->rx_mgmt.frame);
2709
2710                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2711                                          mgmt->bssid, ie, ie_len,
2712                                          data->rx_mgmt.ssi_signal);
2713                 }
2714
2715                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2716                 break;
2717                 }
2718 #endif /* CONFIG_AP */
2719         case EVENT_RX_ACTION:
2720                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2721                         " Category=%u DataLen=%d freq=%d MHz",
2722                         MAC2STR(data->rx_action.sa),
2723                         data->rx_action.category, (int) data->rx_action.len,
2724                         data->rx_action.freq);
2725 #ifdef CONFIG_IEEE80211R
2726                 if (data->rx_action.category == WLAN_ACTION_FT) {
2727                         ft_rx_action(wpa_s, data->rx_action.data,
2728                                      data->rx_action.len);
2729                         break;
2730                 }
2731 #endif /* CONFIG_IEEE80211R */
2732 #ifdef CONFIG_IEEE80211W
2733 #ifdef CONFIG_SME
2734                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2735                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2736                                         data->rx_action.data,
2737                                         data->rx_action.len);
2738                         break;
2739                 }
2740 #endif /* CONFIG_SME */
2741 #endif /* CONFIG_IEEE80211W */
2742 #ifdef CONFIG_WNM
2743                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2744                         ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2745                         break;
2746                 }
2747 #endif /* CONFIG_WNM */
2748 #ifdef CONFIG_GAS
2749                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2750                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2751                                  data->rx_action.sa, data->rx_action.bssid,
2752                                  data->rx_action.data, data->rx_action.len,
2753                                  data->rx_action.freq) == 0)
2754                         break;
2755 #endif /* CONFIG_GAS */
2756 #ifdef CONFIG_TDLS
2757                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2758                     data->rx_action.len >= 4 &&
2759                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2760                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2761                                 "Response from " MACSTR,
2762                                 MAC2STR(data->rx_action.sa));
2763                         break;
2764                 }
2765 #endif /* CONFIG_TDLS */
2766 #ifdef CONFIG_P2P
2767                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2768                                    data->rx_action.sa,
2769                                    data->rx_action.bssid,
2770                                    data->rx_action.category,
2771                                    data->rx_action.data,
2772                                    data->rx_action.len, data->rx_action.freq);
2773 #endif /* CONFIG_P2P */
2774                 break;
2775         case EVENT_RX_PROBE_REQ:
2776                 if (data->rx_probe_req.sa == NULL ||
2777                     data->rx_probe_req.ie == NULL)
2778                         break;
2779 #ifdef CONFIG_AP
2780                 if (wpa_s->ap_iface) {
2781                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2782                                              data->rx_probe_req.sa,
2783                                              data->rx_probe_req.da,
2784                                              data->rx_probe_req.bssid,
2785                                              data->rx_probe_req.ie,
2786                                              data->rx_probe_req.ie_len,
2787                                              data->rx_probe_req.ssi_signal);
2788                         break;
2789                 }
2790 #endif /* CONFIG_AP */
2791 #ifdef CONFIG_P2P
2792                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2793                                       data->rx_probe_req.da,
2794                                       data->rx_probe_req.bssid,
2795                                       data->rx_probe_req.ie,
2796                                       data->rx_probe_req.ie_len,
2797                                       data->rx_probe_req.ssi_signal);
2798 #endif /* CONFIG_P2P */
2799                 break;
2800         case EVENT_REMAIN_ON_CHANNEL:
2801 #ifdef CONFIG_OFFCHANNEL
2802                 offchannel_remain_on_channel_cb(
2803                         wpa_s, data->remain_on_channel.freq,
2804                         data->remain_on_channel.duration);
2805 #endif /* CONFIG_OFFCHANNEL */
2806 #ifdef CONFIG_P2P
2807                 wpas_p2p_remain_on_channel_cb(
2808                         wpa_s, data->remain_on_channel.freq,
2809                         data->remain_on_channel.duration);
2810 #endif /* CONFIG_P2P */
2811                 break;
2812         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2813 #ifdef CONFIG_OFFCHANNEL
2814                 offchannel_cancel_remain_on_channel_cb(
2815                         wpa_s, data->remain_on_channel.freq);
2816 #endif /* CONFIG_OFFCHANNEL */
2817 #ifdef CONFIG_P2P
2818                 wpas_p2p_cancel_remain_on_channel_cb(
2819                         wpa_s, data->remain_on_channel.freq);
2820 #endif /* CONFIG_P2P */
2821                 break;
2822 #ifdef CONFIG_P2P
2823         case EVENT_P2P_DEV_FOUND: {
2824                 struct p2p_peer_info peer_info;
2825
2826                 os_memset(&peer_info, 0, sizeof(peer_info));
2827                 if (data->p2p_dev_found.dev_addr)
2828                         os_memcpy(peer_info.p2p_device_addr,
2829                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2830                 if (data->p2p_dev_found.pri_dev_type)
2831                         os_memcpy(peer_info.pri_dev_type,
2832                                   data->p2p_dev_found.pri_dev_type,
2833                                   sizeof(peer_info.pri_dev_type));
2834                 if (data->p2p_dev_found.dev_name)
2835                         os_strlcpy(peer_info.device_name,
2836                                    data->p2p_dev_found.dev_name,
2837                                    sizeof(peer_info.device_name));
2838                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2839                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2840                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2841
2842                 /*
2843                  * FIX: new_device=1 is not necessarily correct. We should
2844                  * maintain a P2P peer database in wpa_supplicant and update
2845                  * this information based on whether the peer is truly new.
2846                  */
2847                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2848                 break;
2849                 }
2850         case EVENT_P2P_GO_NEG_REQ_RX:
2851                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2852                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2853                 break;
2854         case EVENT_P2P_GO_NEG_COMPLETED:
2855                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2856                 break;
2857         case EVENT_P2P_PROV_DISC_REQUEST:
2858                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2859                                    data->p2p_prov_disc_req.config_methods,
2860                                    data->p2p_prov_disc_req.dev_addr,
2861                                    data->p2p_prov_disc_req.pri_dev_type,
2862                                    data->p2p_prov_disc_req.dev_name,
2863                                    data->p2p_prov_disc_req.supp_config_methods,
2864                                    data->p2p_prov_disc_req.dev_capab,
2865                                    data->p2p_prov_disc_req.group_capab,
2866                                    NULL, 0);
2867                 break;
2868         case EVENT_P2P_PROV_DISC_RESPONSE:
2869                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2870                                     data->p2p_prov_disc_resp.config_methods);
2871                 break;
2872         case EVENT_P2P_SD_REQUEST:
2873                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2874                                 data->p2p_sd_req.sa,
2875                                 data->p2p_sd_req.dialog_token,
2876                                 data->p2p_sd_req.update_indic,
2877                                 data->p2p_sd_req.tlvs,
2878                                 data->p2p_sd_req.tlvs_len);
2879                 break;
2880         case EVENT_P2P_SD_RESPONSE:
2881                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2882                                  data->p2p_sd_resp.update_indic,
2883                                  data->p2p_sd_resp.tlvs,
2884                                  data->p2p_sd_resp.tlvs_len);
2885                 break;
2886 #endif /* CONFIG_P2P */
2887         case EVENT_EAPOL_RX:
2888                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2889                                         data->eapol_rx.data,
2890                                         data->eapol_rx.data_len);
2891                 break;
2892         case EVENT_SIGNAL_CHANGE:
2893                 bgscan_notify_signal_change(
2894                         wpa_s, data->signal_change.above_threshold,
2895                         data->signal_change.current_signal,
2896                         data->signal_change.current_noise,
2897                         data->signal_change.current_txrate);
2898                 break;
2899         case EVENT_INTERFACE_ENABLED:
2900                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2901                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2902                         wpa_supplicant_update_mac_addr(wpa_s);
2903 #ifdef CONFIG_AP
2904                         if (!wpa_s->ap_iface) {
2905                                 wpa_supplicant_set_state(wpa_s,
2906                                                          WPA_DISCONNECTED);
2907                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2908                         } else
2909                                 wpa_supplicant_set_state(wpa_s,
2910                                                          WPA_COMPLETED);
2911 #else /* CONFIG_AP */
2912                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2913                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2914 #endif /* CONFIG_AP */
2915                 }
2916                 break;
2917         case EVENT_INTERFACE_DISABLED:
2918                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2919                 wpa_supplicant_mark_disassoc(wpa_s);
2920                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2921                 break;
2922         case EVENT_CHANNEL_LIST_CHANGED:
2923                 if (wpa_s->drv_priv == NULL)
2924                         break; /* Ignore event during drv initialization */
2925
2926                 free_hw_features(wpa_s);
2927                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2928                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2929
2930 #ifdef CONFIG_P2P
2931                 wpas_p2p_update_channel_list(wpa_s);
2932 #endif /* CONFIG_P2P */
2933                 break;
2934         case EVENT_INTERFACE_UNAVAILABLE:
2935 #ifdef CONFIG_P2P
2936                 wpas_p2p_interface_unavailable(wpa_s);
2937 #endif /* CONFIG_P2P */
2938                 break;
2939         case EVENT_BEST_CHANNEL:
2940                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2941                         "(%d %d %d)",
2942                         data->best_chan.freq_24, data->best_chan.freq_5,
2943                         data->best_chan.freq_overall);
2944                 wpa_s->best_24_freq = data->best_chan.freq_24;
2945                 wpa_s->best_5_freq = data->best_chan.freq_5;
2946                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2947 #ifdef CONFIG_P2P
2948                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2949                                               data->best_chan.freq_5,
2950                                               data->best_chan.freq_overall);
2951 #endif /* CONFIG_P2P */
2952                 break;
2953         case EVENT_UNPROT_DEAUTH:
2954                 wpa_supplicant_event_unprot_deauth(wpa_s,
2955                                                    &data->unprot_deauth);
2956                 break;
2957         case EVENT_UNPROT_DISASSOC:
2958                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2959                                                      &data->unprot_disassoc);
2960                 break;
2961         case EVENT_STATION_LOW_ACK:
2962 #ifdef CONFIG_AP
2963                 if (wpa_s->ap_iface && data)
2964                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2965                                                   data->low_ack.addr);
2966 #endif /* CONFIG_AP */
2967 #ifdef CONFIG_TDLS
2968                 if (data)
2969                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2970 #endif /* CONFIG_TDLS */
2971                 break;
2972         case EVENT_IBSS_PEER_LOST:
2973 #ifdef CONFIG_IBSS_RSN
2974                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2975 #endif /* CONFIG_IBSS_RSN */
2976                 break;
2977         case EVENT_DRIVER_GTK_REKEY:
2978                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2979                               wpa_s->bssid, ETH_ALEN))
2980                         break;
2981                 if (!wpa_s->wpa)
2982                         break;
2983                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2984                                          data->driver_gtk_rekey.replay_ctr);
2985                 break;
2986         case EVENT_SCHED_SCAN_STOPPED:
2987                 wpa_s->sched_scanning = 0;
2988                 wpa_supplicant_notify_scanning(wpa_s, 0);
2989
2990                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2991                         break;
2992
2993                 /*
2994                  * If we timed out, start a new sched scan to continue
2995                  * searching for more SSIDs.
2996                  */
2997                 if (wpa_s->sched_scan_timed_out)
2998                         wpa_supplicant_req_sched_scan(wpa_s);
2999                 break;
3000         case EVENT_WPS_BUTTON_PUSHED:
3001 #ifdef CONFIG_WPS
3002                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3003 #endif /* CONFIG_WPS */
3004                 break;
3005         case EVENT_CONNECT_FAILED_REASON:
3006 #ifdef CONFIG_AP
3007                 if (!wpa_s->ap_iface || !data)
3008                         break;
3009                 hostapd_event_connect_failed_reason(
3010                         wpa_s->ap_iface->bss[0],
3011                         data->connect_failed_reason.addr,
3012                         data->connect_failed_reason.code);
3013 #endif /* CONFIG_AP */
3014                 break;
3015         default:
3016                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3017                 break;
3018         }
3019 }