Do not try auto connect mechanism in disconnected state
[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
1680 #ifdef CONFIG_AP
1681         if (wpa_s->ap_iface) {
1682                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1683                                     data->assoc_info.addr,
1684                                     data->assoc_info.req_ies,
1685                                     data->assoc_info.req_ies_len,
1686                                     data->assoc_info.reassoc);
1687                 return;
1688         }
1689 #endif /* CONFIG_AP */
1690
1691         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1692         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1693                 return;
1694
1695         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1696                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1697                 wpa_supplicant_deauthenticate(
1698                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1699                 return;
1700         }
1701
1702         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1703         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1704                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1705                         MACSTR, MAC2STR(bssid));
1706                 random_add_randomness(bssid, ETH_ALEN);
1707                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1708                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1709                 wpas_notify_bssid_changed(wpa_s);
1710
1711                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1712                         wpa_clear_keys(wpa_s, bssid);
1713                 }
1714                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1715                         wpa_supplicant_deauthenticate(
1716                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1717                         return;
1718                 }
1719                 if (wpa_s->current_ssid) {
1720                         struct wpa_bss *bss = NULL;
1721
1722                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1723                         if (!bss) {
1724                                 wpa_supplicant_update_scan_results(wpa_s);
1725
1726                                 /* Get the BSS from the new scan results */
1727                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1728                         }
1729
1730                         if (bss)
1731                                 wpa_s->current_bss = bss;
1732                 }
1733
1734                 if (wpa_s->conf->ap_scan == 1 &&
1735                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1736                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1737                                 wpa_msg(wpa_s, MSG_WARNING,
1738                                         "WPA/RSN IEs not updated");
1739                 }
1740         }
1741
1742 #ifdef CONFIG_SME
1743         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1744         wpa_s->sme.prev_bssid_set = 1;
1745 #endif /* CONFIG_SME */
1746
1747         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1748         if (wpa_s->current_ssid) {
1749                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1750                  * initialized before association, but for other modes,
1751                  * initialize PC/SC here, if the current configuration needs
1752                  * smartcard or SIM/USIM. */
1753                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1754         }
1755         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1756         if (wpa_s->l2)
1757                 l2_packet_notify_auth_start(wpa_s->l2);
1758
1759         /*
1760          * Set portEnabled first to FALSE in order to get EAP state machine out
1761          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1762          * state machine may transit to AUTHENTICATING state based on obsolete
1763          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1764          * AUTHENTICATED without ever giving chance to EAP state machine to
1765          * reset the state.
1766          */
1767         if (!ft_completed) {
1768                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1769                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1770         }
1771         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1772                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1773         /* 802.1X::portControl = Auto */
1774         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1775         wpa_s->eapol_received = 0;
1776         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1777             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1778             (wpa_s->current_ssid &&
1779              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1780                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1781                     (wpa_s->drv_flags &
1782                      WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1783                         /*
1784                          * Set the key after having received joined-IBSS event
1785                          * from the driver.
1786                          */
1787                         wpa_supplicant_set_wpa_none_key(wpa_s,
1788                                                         wpa_s->current_ssid);
1789                 }
1790                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1791                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1792         } else if (!ft_completed) {
1793                 /* Timeout for receiving the first EAPOL packet */
1794                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1795         }
1796         wpa_supplicant_cancel_scan(wpa_s);
1797
1798         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1799             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1800                 /*
1801                  * We are done; the driver will take care of RSN 4-way
1802                  * handshake.
1803                  */
1804                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1805                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1806                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1807                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1808         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1809                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1810                 /*
1811                  * The driver will take care of RSN 4-way handshake, so we need
1812                  * to allow EAPOL supplicant to complete its work without
1813                  * waiting for WPA supplicant.
1814                  */
1815                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1816         } else if (ft_completed) {
1817                 /*
1818                  * FT protocol completed - make sure EAPOL state machine ends
1819                  * up in authenticated.
1820                  */
1821                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1822                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1823                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1824                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1825         }
1826
1827         wpa_s->last_eapol_matches_bssid = 0;
1828
1829         if (wpa_s->pending_eapol_rx) {
1830                 struct os_time now, age;
1831                 os_get_time(&now);
1832                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1833                 if (age.sec == 0 && age.usec < 100000 &&
1834                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1835                     0) {
1836                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1837                                 "frame that was received just before "
1838                                 "association notification");
1839                         wpa_supplicant_rx_eapol(
1840                                 wpa_s, wpa_s->pending_eapol_rx_src,
1841                                 wpabuf_head(wpa_s->pending_eapol_rx),
1842                                 wpabuf_len(wpa_s->pending_eapol_rx));
1843                 }
1844                 wpabuf_free(wpa_s->pending_eapol_rx);
1845                 wpa_s->pending_eapol_rx = NULL;
1846         }
1847
1848         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1849              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1850             wpa_s->current_ssid &&
1851             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1852                 /* Set static WEP keys again */
1853                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1854         }
1855
1856 #ifdef CONFIG_IBSS_RSN
1857         if (wpa_s->current_ssid &&
1858             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1859             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1860             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1861             wpa_s->ibss_rsn == NULL) {
1862                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1863                 if (!wpa_s->ibss_rsn) {
1864                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1865                         wpa_supplicant_deauthenticate(
1866                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1867                         return;
1868                 }
1869
1870                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1871         }
1872 #endif /* CONFIG_IBSS_RSN */
1873
1874         wpas_wps_notify_assoc(wpa_s, bssid);
1875 }
1876
1877
1878 static int disconnect_reason_recoverable(u16 reason_code)
1879 {
1880         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1881                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1882                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1883 }
1884
1885
1886 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1887                                           u16 reason_code,
1888                                           int locally_generated)
1889 {
1890         const u8 *bssid;
1891
1892         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1893                 /*
1894                  * At least Host AP driver and a Prism3 card seemed to be
1895                  * generating streams of disconnected events when configuring
1896                  * IBSS for WPA-None. Ignore them for now.
1897                  */
1898                 return;
1899         }
1900
1901         bssid = wpa_s->bssid;
1902         if (is_zero_ether_addr(bssid))
1903                 bssid = wpa_s->pending_bssid;
1904
1905         if (!is_zero_ether_addr(bssid) ||
1906             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1907                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1908                         " reason=%d%s",
1909                         MAC2STR(bssid), reason_code,
1910                         locally_generated ? " locally_generated=1" : "");
1911         }
1912 }
1913
1914
1915 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1916                                  int locally_generated)
1917 {
1918         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1919             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1920                 return 0; /* Not in 4-way handshake with PSK */
1921
1922         /*
1923          * It looks like connection was lost while trying to go through PSK
1924          * 4-way handshake. Filter out known disconnection cases that are caused
1925          * by something else than PSK mismatch to avoid confusing reports.
1926          */
1927
1928         if (locally_generated) {
1929                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1930                         return 0;
1931         }
1932
1933         return 1;
1934 }
1935
1936
1937 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1938                                                  u16 reason_code,
1939                                                  int locally_generated)
1940 {
1941         const u8 *bssid;
1942         int authenticating;
1943         u8 prev_pending_bssid[ETH_ALEN];
1944         struct wpa_bss *fast_reconnect = NULL;
1945         struct wpa_ssid *fast_reconnect_ssid = NULL;
1946         struct wpa_ssid *last_ssid;
1947
1948         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1949         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1950
1951         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1952                 /*
1953                  * At least Host AP driver and a Prism3 card seemed to be
1954                  * generating streams of disconnected events when configuring
1955                  * IBSS for WPA-None. Ignore them for now.
1956                  */
1957                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1958                         "IBSS/WPA-None mode");
1959                 return;
1960         }
1961
1962         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
1963                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1964                         "pre-shared key may be incorrect");
1965                 wpas_auth_failed(wpa_s);
1966         }
1967         if (!wpa_s->disconnected &&
1968             (!wpa_s->auto_reconnect_disabled ||
1969              wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)) {
1970                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1971                         "reconnect (wps=%d wpa_state=%d)",
1972                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1973                         wpa_s->wpa_state);
1974                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1975                     wpa_s->current_ssid &&
1976                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1977                     !locally_generated &&
1978                     disconnect_reason_recoverable(reason_code)) {
1979                         /*
1980                          * It looks like the AP has dropped association with
1981                          * us, but could allow us to get back in. Try to
1982                          * reconnect to the same BSS without full scan to save
1983                          * time for some common cases.
1984                          */
1985                         fast_reconnect = wpa_s->current_bss;
1986                         fast_reconnect_ssid = wpa_s->current_ssid;
1987                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1988                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1989                 else
1990                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1991                                 "immediate scan");
1992         } else {
1993                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1994                         "try to re-connect");
1995                 wpa_s->reassociate = 0;
1996                 wpa_s->disconnected = 1;
1997                 wpa_supplicant_cancel_sched_scan(wpa_s);
1998         }
1999         bssid = wpa_s->bssid;
2000         if (is_zero_ether_addr(bssid))
2001                 bssid = wpa_s->pending_bssid;
2002         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2003                 wpas_connection_failed(wpa_s, bssid);
2004         wpa_sm_notify_disassoc(wpa_s->wpa);
2005         if (locally_generated)
2006                 wpa_s->disconnect_reason = -reason_code;
2007         else
2008                 wpa_s->disconnect_reason = reason_code;
2009         wpas_notify_disconnect_reason(wpa_s);
2010         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2011                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2012                 wpa_s->keys_cleared = 0;
2013                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2014         }
2015         last_ssid = wpa_s->current_ssid;
2016         wpa_supplicant_mark_disassoc(wpa_s);
2017
2018         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2019                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2020                 wpa_s->current_ssid = last_ssid;
2021         }
2022
2023         if (fast_reconnect) {
2024 #ifndef CONFIG_NO_SCAN_PROCESSING
2025                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2026                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2027                                            fast_reconnect_ssid) < 0) {
2028                         /* Recover through full scan */
2029                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2030                 }
2031 #endif /* CONFIG_NO_SCAN_PROCESSING */
2032         }
2033 }
2034
2035
2036 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2037 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2038 {
2039         struct wpa_supplicant *wpa_s = eloop_ctx;
2040
2041         if (!wpa_s->pending_mic_error_report)
2042                 return;
2043
2044         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2045         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2046         wpa_s->pending_mic_error_report = 0;
2047 }
2048 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2049
2050
2051 static void
2052 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2053                                          union wpa_event_data *data)
2054 {
2055         int pairwise;
2056         struct os_time t;
2057
2058         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2059         pairwise = (data && data->michael_mic_failure.unicast);
2060         os_get_time(&t);
2061         if ((wpa_s->last_michael_mic_error &&
2062              t.sec - wpa_s->last_michael_mic_error <= 60) ||
2063             wpa_s->pending_mic_error_report) {
2064                 if (wpa_s->pending_mic_error_report) {
2065                         /*
2066                          * Send the pending MIC error report immediately since
2067                          * we are going to start countermeasures and AP better
2068                          * do the same.
2069                          */
2070                         wpa_sm_key_request(wpa_s->wpa, 1,
2071                                            wpa_s->pending_mic_error_pairwise);
2072                 }
2073
2074                 /* Send the new MIC error report immediately since we are going
2075                  * to start countermeasures and AP better do the same.
2076                  */
2077                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2078
2079                 /* initialize countermeasures */
2080                 wpa_s->countermeasures = 1;
2081
2082                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2083
2084                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2085
2086                 /*
2087                  * Need to wait for completion of request frame. We do not get
2088                  * any callback for the message completion, so just wait a
2089                  * short while and hope for the best. */
2090                 os_sleep(0, 10000);
2091
2092                 wpa_drv_set_countermeasures(wpa_s, 1);
2093                 wpa_supplicant_deauthenticate(wpa_s,
2094                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2095                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2096                                      wpa_s, NULL);
2097                 eloop_register_timeout(60, 0,
2098                                        wpa_supplicant_stop_countermeasures,
2099                                        wpa_s, NULL);
2100                 /* TODO: mark the AP rejected for 60 second. STA is
2101                  * allowed to associate with another AP.. */
2102         } else {
2103 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2104                 if (wpa_s->mic_errors_seen) {
2105                         /*
2106                          * Reduce the effectiveness of Michael MIC error
2107                          * reports as a means for attacking against TKIP if
2108                          * more than one MIC failure is noticed with the same
2109                          * PTK. We delay the transmission of the reports by a
2110                          * random time between 0 and 60 seconds in order to
2111                          * force the attacker wait 60 seconds before getting
2112                          * the information on whether a frame resulted in a MIC
2113                          * failure.
2114                          */
2115                         u8 rval[4];
2116                         int sec;
2117
2118                         if (os_get_random(rval, sizeof(rval)) < 0)
2119                                 sec = os_random() % 60;
2120                         else
2121                                 sec = WPA_GET_BE32(rval) % 60;
2122                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2123                                 "report %d seconds", sec);
2124                         wpa_s->pending_mic_error_report = 1;
2125                         wpa_s->pending_mic_error_pairwise = pairwise;
2126                         eloop_cancel_timeout(
2127                                 wpa_supplicant_delayed_mic_error_report,
2128                                 wpa_s, NULL);
2129                         eloop_register_timeout(
2130                                 sec, os_random() % 1000000,
2131                                 wpa_supplicant_delayed_mic_error_report,
2132                                 wpa_s, NULL);
2133                 } else {
2134                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2135                 }
2136 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2137                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2138 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2139         }
2140         wpa_s->last_michael_mic_error = t.sec;
2141         wpa_s->mic_errors_seen++;
2142 }
2143
2144
2145 #ifdef CONFIG_TERMINATE_ONLASTIF
2146 static int any_interfaces(struct wpa_supplicant *head)
2147 {
2148         struct wpa_supplicant *wpa_s;
2149
2150         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2151                 if (!wpa_s->interface_removed)
2152                         return 1;
2153         return 0;
2154 }
2155 #endif /* CONFIG_TERMINATE_ONLASTIF */
2156
2157
2158 static void
2159 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2160                                       union wpa_event_data *data)
2161 {
2162         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2163                 return;
2164
2165         switch (data->interface_status.ievent) {
2166         case EVENT_INTERFACE_ADDED:
2167                 if (!wpa_s->interface_removed)
2168                         break;
2169                 wpa_s->interface_removed = 0;
2170                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2171                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2172                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2173                                 "driver after interface was added");
2174                 }
2175                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2176                 break;
2177         case EVENT_INTERFACE_REMOVED:
2178                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2179                 wpa_s->interface_removed = 1;
2180                 wpa_supplicant_mark_disassoc(wpa_s);
2181                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2182                 l2_packet_deinit(wpa_s->l2);
2183                 wpa_s->l2 = NULL;
2184 #ifdef CONFIG_IBSS_RSN
2185                 ibss_rsn_deinit(wpa_s->ibss_rsn);
2186                 wpa_s->ibss_rsn = NULL;
2187 #endif /* CONFIG_IBSS_RSN */
2188 #ifdef CONFIG_TERMINATE_ONLASTIF
2189                 /* check if last interface */
2190                 if (!any_interfaces(wpa_s->global->ifaces))
2191                         eloop_terminate();
2192 #endif /* CONFIG_TERMINATE_ONLASTIF */
2193                 break;
2194         }
2195 }
2196
2197
2198 #ifdef CONFIG_PEERKEY
2199 static void
2200 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2201                               union wpa_event_data *data)
2202 {
2203         if (data == NULL)
2204                 return;
2205         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2206 }
2207 #endif /* CONFIG_PEERKEY */
2208
2209
2210 #ifdef CONFIG_TDLS
2211 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2212                                       union wpa_event_data *data)
2213 {
2214         if (data == NULL)
2215                 return;
2216         switch (data->tdls.oper) {
2217         case TDLS_REQUEST_SETUP:
2218                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2219                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2220                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2221                 else
2222                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2223                 break;
2224         case TDLS_REQUEST_TEARDOWN:
2225                 wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2226                                        data->tdls.reason_code);
2227                 break;
2228         }
2229 }
2230 #endif /* CONFIG_TDLS */
2231
2232
2233 #ifdef CONFIG_WNM
2234 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2235                                      union wpa_event_data *data)
2236 {
2237         if (data == NULL)
2238                 return;
2239         switch (data->wnm.oper) {
2240         case WNM_OPER_SLEEP:
2241                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2242                            "(action=%d, intval=%d)",
2243                            data->wnm.sleep_action, data->wnm.sleep_intval);
2244                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2245                                              data->wnm.sleep_intval, NULL);
2246                 break;
2247         }
2248 }
2249 #endif /* CONFIG_WNM */
2250
2251
2252 #ifdef CONFIG_IEEE80211R
2253 static void
2254 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2255                                  union wpa_event_data *data)
2256 {
2257         if (data == NULL)
2258                 return;
2259
2260         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2261                                     data->ft_ies.ies_len,
2262                                     data->ft_ies.ft_action,
2263                                     data->ft_ies.target_ap,
2264                                     data->ft_ies.ric_ies,
2265                                     data->ft_ies.ric_ies_len) < 0) {
2266                 /* TODO: prevent MLME/driver from trying to associate? */
2267         }
2268 }
2269 #endif /* CONFIG_IEEE80211R */
2270
2271
2272 #ifdef CONFIG_IBSS_RSN
2273 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2274                                                 union wpa_event_data *data)
2275 {
2276         struct wpa_ssid *ssid;
2277         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2278                 return;
2279         if (data == NULL)
2280                 return;
2281         ssid = wpa_s->current_ssid;
2282         if (ssid == NULL)
2283                 return;
2284         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2285                 return;
2286
2287         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2288 }
2289 #endif /* CONFIG_IBSS_RSN */
2290
2291
2292 #ifdef CONFIG_IEEE80211R
2293 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2294                          size_t len)
2295 {
2296         const u8 *sta_addr, *target_ap_addr;
2297         u16 status;
2298
2299         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2300         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2301                 return; /* only SME case supported for now */
2302         if (len < 1 + 2 * ETH_ALEN + 2)
2303                 return;
2304         if (data[0] != 2)
2305                 return; /* Only FT Action Response is supported for now */
2306         sta_addr = data + 1;
2307         target_ap_addr = data + 1 + ETH_ALEN;
2308         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2309         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2310                 MACSTR " TargetAP " MACSTR " status %u",
2311                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2312
2313         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2314                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2315                         " in FT Action Response", MAC2STR(sta_addr));
2316                 return;
2317         }
2318
2319         if (status) {
2320                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2321                         "failure (status code %d)", status);
2322                 /* TODO: report error to FT code(?) */
2323                 return;
2324         }
2325
2326         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2327                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2328                                     target_ap_addr, NULL, 0) < 0)
2329                 return;
2330
2331 #ifdef CONFIG_SME
2332         {
2333                 struct wpa_bss *bss;
2334                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2335                 if (bss)
2336                         wpa_s->sme.freq = bss->freq;
2337                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2338                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2339                               WLAN_AUTH_FT);
2340         }
2341 #endif /* CONFIG_SME */
2342 }
2343 #endif /* CONFIG_IEEE80211R */
2344
2345
2346 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2347                                                struct unprot_deauth *e)
2348 {
2349 #ifdef CONFIG_IEEE80211W
2350         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2351                    "dropped: " MACSTR " -> " MACSTR
2352                    " (reason code %u)",
2353                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2354         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2355 #endif /* CONFIG_IEEE80211W */
2356 }
2357
2358
2359 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2360                                                  struct unprot_disassoc *e)
2361 {
2362 #ifdef CONFIG_IEEE80211W
2363         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2364                    "dropped: " MACSTR " -> " MACSTR
2365                    " (reason code %u)",
2366                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2367         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2368 #endif /* CONFIG_IEEE80211W */
2369 }
2370
2371
2372 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2373                           union wpa_event_data *data)
2374 {
2375         struct wpa_supplicant *wpa_s = ctx;
2376         u16 reason_code = 0;
2377         int locally_generated = 0;
2378
2379         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2380             event != EVENT_INTERFACE_ENABLED &&
2381             event != EVENT_INTERFACE_STATUS &&
2382             event != EVENT_SCHED_SCAN_STOPPED) {
2383                 wpa_dbg(wpa_s, MSG_DEBUG,
2384                         "Ignore event %s (%d) while interface is disabled",
2385                         event_to_string(event), event);
2386                 return;
2387         }
2388
2389 #ifndef CONFIG_NO_STDOUT_DEBUG
2390 {
2391         int level = MSG_DEBUG;
2392
2393         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2394                 const struct ieee80211_hdr *hdr;
2395                 u16 fc;
2396                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2397                 fc = le_to_host16(hdr->frame_control);
2398                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2399                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2400                         level = MSG_EXCESSIVE;
2401         }
2402
2403         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2404                 event_to_string(event), event);
2405 }
2406 #endif /* CONFIG_NO_STDOUT_DEBUG */
2407
2408         switch (event) {
2409         case EVENT_AUTH:
2410                 sme_event_auth(wpa_s, data);
2411                 break;
2412         case EVENT_ASSOC:
2413                 wpa_supplicant_event_assoc(wpa_s, data);
2414                 break;
2415         case EVENT_DISASSOC:
2416                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2417                 if (data) {
2418                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2419                                 data->disassoc_info.reason_code,
2420                                 data->disassoc_info.locally_generated ?
2421                                 " (locally generated)" : "");
2422                         if (data->disassoc_info.addr)
2423                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2424                                         MAC2STR(data->disassoc_info.addr));
2425                 }
2426 #ifdef CONFIG_AP
2427                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2428                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2429                                                data->disassoc_info.addr);
2430                         break;
2431                 }
2432                 if (wpa_s->ap_iface) {
2433                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2434                                 "AP mode");
2435                         break;
2436                 }
2437 #endif /* CONFIG_AP */
2438                 if (data) {
2439                         reason_code = data->disassoc_info.reason_code;
2440                         locally_generated =
2441                                 data->disassoc_info.locally_generated;
2442                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2443                                     data->disassoc_info.ie,
2444                                     data->disassoc_info.ie_len);
2445 #ifdef CONFIG_P2P
2446                         wpas_p2p_disassoc_notif(
2447                                 wpa_s, data->disassoc_info.addr, reason_code,
2448                                 data->disassoc_info.ie,
2449                                 data->disassoc_info.ie_len,
2450                                 locally_generated);
2451 #endif /* CONFIG_P2P */
2452                 }
2453                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2454                         sme_event_disassoc(wpa_s, data);
2455                 /* fall through */
2456         case EVENT_DEAUTH:
2457                 if (event == EVENT_DEAUTH) {
2458                         wpa_dbg(wpa_s, MSG_DEBUG,
2459                                 "Deauthentication notification");
2460                         if (data) {
2461                                 reason_code = data->deauth_info.reason_code;
2462                                 locally_generated =
2463                                         data->deauth_info.locally_generated;
2464                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2465                                         data->deauth_info.reason_code,
2466                                         data->deauth_info.locally_generated ?
2467                                         " (locally generated)" : "");
2468                                 if (data->deauth_info.addr) {
2469                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2470                                                 MACSTR,
2471                                                 MAC2STR(data->deauth_info.
2472                                                         addr));
2473                                 }
2474                                 wpa_hexdump(MSG_DEBUG,
2475                                             "Deauthentication frame IE(s)",
2476                                             data->deauth_info.ie,
2477                                             data->deauth_info.ie_len);
2478                         }
2479                         wpa_reset_ft_completed(wpa_s->wpa);
2480                 }
2481 #ifdef CONFIG_AP
2482                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2483                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2484                                                data->deauth_info.addr);
2485                         break;
2486                 }
2487                 if (wpa_s->ap_iface) {
2488                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2489                                 "AP mode");
2490                         break;
2491                 }
2492 #endif /* CONFIG_AP */
2493                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2494                                               locally_generated);
2495                 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2496                     ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2497                       (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2498                      eapol_sm_failed(wpa_s->eapol)))
2499                         wpas_auth_failed(wpa_s);
2500 #ifdef CONFIG_P2P
2501                 if (event == EVENT_DEAUTH && data) {
2502                         if (wpas_p2p_deauth_notif(wpa_s,
2503                                                   data->deauth_info.addr,
2504                                                   reason_code,
2505                                                   data->deauth_info.ie,
2506                                                   data->deauth_info.ie_len,
2507                                                   locally_generated) > 0) {
2508                                 /*
2509                                  * The interface was removed, so cannot
2510                                  * continue processing any additional
2511                                  * operations after this.
2512                                  */
2513                                 break;
2514                         }
2515                 }
2516 #endif /* CONFIG_P2P */
2517                 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2518                                                      locally_generated);
2519                 break;
2520         case EVENT_MICHAEL_MIC_FAILURE:
2521                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2522                 break;
2523 #ifndef CONFIG_NO_SCAN_PROCESSING
2524         case EVENT_SCAN_RESULTS:
2525                 wpa_supplicant_event_scan_results(wpa_s, data);
2526                 if (wpa_s->wpa_state != WPA_AUTHENTICATING &&
2527                     wpa_s->wpa_state != WPA_ASSOCIATING)
2528                         wpas_p2p_continue_after_scan(wpa_s);
2529                 break;
2530 #endif /* CONFIG_NO_SCAN_PROCESSING */
2531         case EVENT_ASSOCINFO:
2532                 wpa_supplicant_event_associnfo(wpa_s, data);
2533                 break;
2534         case EVENT_INTERFACE_STATUS:
2535                 wpa_supplicant_event_interface_status(wpa_s, data);
2536                 break;
2537         case EVENT_PMKID_CANDIDATE:
2538                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2539                 break;
2540 #ifdef CONFIG_PEERKEY
2541         case EVENT_STKSTART:
2542                 wpa_supplicant_event_stkstart(wpa_s, data);
2543                 break;
2544 #endif /* CONFIG_PEERKEY */
2545 #ifdef CONFIG_TDLS
2546         case EVENT_TDLS:
2547                 wpa_supplicant_event_tdls(wpa_s, data);
2548                 break;
2549 #endif /* CONFIG_TDLS */
2550 #ifdef CONFIG_WNM
2551         case EVENT_WNM:
2552                 wpa_supplicant_event_wnm(wpa_s, data);
2553                 break;
2554 #endif /* CONFIG_WNM */
2555 #ifdef CONFIG_IEEE80211R
2556         case EVENT_FT_RESPONSE:
2557                 wpa_supplicant_event_ft_response(wpa_s, data);
2558                 break;
2559 #endif /* CONFIG_IEEE80211R */
2560 #ifdef CONFIG_IBSS_RSN
2561         case EVENT_IBSS_RSN_START:
2562                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2563                 break;
2564 #endif /* CONFIG_IBSS_RSN */
2565         case EVENT_ASSOC_REJECT:
2566                 if (data->assoc_reject.bssid)
2567                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2568                                 "bssid=" MACSTR " status_code=%u",
2569                                 MAC2STR(data->assoc_reject.bssid),
2570                                 data->assoc_reject.status_code);
2571                 else
2572                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2573                                 "status_code=%u",
2574                                 data->assoc_reject.status_code);
2575                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2576                         sme_event_assoc_reject(wpa_s, data);
2577                 else {
2578                         const u8 *bssid = data->assoc_reject.bssid;
2579                         if (bssid == NULL || is_zero_ether_addr(bssid))
2580                                 bssid = wpa_s->pending_bssid;
2581                         wpas_connection_failed(wpa_s, bssid);
2582                         wpa_supplicant_mark_disassoc(wpa_s);
2583                 }
2584                 break;
2585         case EVENT_AUTH_TIMED_OUT:
2586                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2587                         sme_event_auth_timed_out(wpa_s, data);
2588                 break;
2589         case EVENT_ASSOC_TIMED_OUT:
2590                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2591                         sme_event_assoc_timed_out(wpa_s, data);
2592                 break;
2593         case EVENT_TX_STATUS:
2594                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2595                         " type=%d stype=%d",
2596                         MAC2STR(data->tx_status.dst),
2597                         data->tx_status.type, data->tx_status.stype);
2598 #ifdef CONFIG_AP
2599                 if (wpa_s->ap_iface == NULL) {
2600 #ifdef CONFIG_OFFCHANNEL
2601                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2602                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2603                                 offchannel_send_action_tx_status(
2604                                         wpa_s, data->tx_status.dst,
2605                                         data->tx_status.data,
2606                                         data->tx_status.data_len,
2607                                         data->tx_status.ack ?
2608                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2609                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2610 #endif /* CONFIG_OFFCHANNEL */
2611                         break;
2612                 }
2613 #endif /* CONFIG_AP */
2614 #ifdef CONFIG_OFFCHANNEL
2615                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2616                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2617                 /*
2618                  * Catch TX status events for Action frames we sent via group
2619                  * interface in GO mode.
2620                  */
2621                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2622                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2623                     os_memcmp(wpa_s->parent->pending_action_dst,
2624                               data->tx_status.dst, ETH_ALEN) == 0) {
2625                         offchannel_send_action_tx_status(
2626                                 wpa_s->parent, data->tx_status.dst,
2627                                 data->tx_status.data,
2628                                 data->tx_status.data_len,
2629                                 data->tx_status.ack ?
2630                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2631                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2632                         break;
2633                 }
2634 #endif /* CONFIG_OFFCHANNEL */
2635 #ifdef CONFIG_AP
2636                 switch (data->tx_status.type) {
2637                 case WLAN_FC_TYPE_MGMT:
2638                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2639                                       data->tx_status.data_len,
2640                                       data->tx_status.stype,
2641                                       data->tx_status.ack);
2642                         break;
2643                 case WLAN_FC_TYPE_DATA:
2644                         ap_tx_status(wpa_s, data->tx_status.dst,
2645                                      data->tx_status.data,
2646                                      data->tx_status.data_len,
2647                                      data->tx_status.ack);
2648                         break;
2649                 }
2650 #endif /* CONFIG_AP */
2651                 break;
2652 #ifdef CONFIG_AP
2653         case EVENT_EAPOL_TX_STATUS:
2654                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2655                                    data->eapol_tx_status.data,
2656                                    data->eapol_tx_status.data_len,
2657                                    data->eapol_tx_status.ack);
2658                 break;
2659         case EVENT_DRIVER_CLIENT_POLL_OK:
2660                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2661                 break;
2662         case EVENT_RX_FROM_UNKNOWN:
2663                 if (wpa_s->ap_iface == NULL)
2664                         break;
2665                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2666                                        data->rx_from_unknown.wds);
2667                 break;
2668         case EVENT_CH_SWITCH:
2669                 if (!data)
2670                         break;
2671                 if (!wpa_s->ap_iface) {
2672                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2673                                 "event in non-AP mode");
2674                         break;
2675                 }
2676
2677 #ifdef CONFIG_AP
2678                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2679                                   data->ch_switch.ht_enabled,
2680                                   data->ch_switch.ch_offset);
2681 #endif /* CONFIG_AP */
2682                 break;
2683         case EVENT_RX_MGMT: {
2684                 u16 fc, stype;
2685                 const struct ieee80211_mgmt *mgmt;
2686
2687                 mgmt = (const struct ieee80211_mgmt *)
2688                         data->rx_mgmt.frame;
2689                 fc = le_to_host16(mgmt->frame_control);
2690                 stype = WLAN_FC_GET_STYPE(fc);
2691
2692                 if (wpa_s->ap_iface == NULL) {
2693 #ifdef CONFIG_P2P
2694                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2695                             data->rx_mgmt.frame_len > 24) {
2696                                 const u8 *src = mgmt->sa;
2697                                 const u8 *ie = mgmt->u.probe_req.variable;
2698                                 size_t ie_len = data->rx_mgmt.frame_len -
2699                                         (mgmt->u.probe_req.variable -
2700                                          data->rx_mgmt.frame);
2701                                 wpas_p2p_probe_req_rx(
2702                                         wpa_s, src, mgmt->da,
2703                                         mgmt->bssid, ie, ie_len,
2704                                         data->rx_mgmt.ssi_signal);
2705                                 break;
2706                         }
2707 #endif /* CONFIG_P2P */
2708                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2709                                 "management frame in non-AP mode");
2710                         break;
2711                 }
2712
2713                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2714                     data->rx_mgmt.frame_len > 24) {
2715                         const u8 *ie = mgmt->u.probe_req.variable;
2716                         size_t ie_len = data->rx_mgmt.frame_len -
2717                                 (mgmt->u.probe_req.variable -
2718                                  data->rx_mgmt.frame);
2719
2720                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2721                                          mgmt->bssid, ie, ie_len,
2722                                          data->rx_mgmt.ssi_signal);
2723                 }
2724
2725                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2726                 break;
2727                 }
2728 #endif /* CONFIG_AP */
2729         case EVENT_RX_ACTION:
2730                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2731                         " Category=%u DataLen=%d freq=%d MHz",
2732                         MAC2STR(data->rx_action.sa),
2733                         data->rx_action.category, (int) data->rx_action.len,
2734                         data->rx_action.freq);
2735 #ifdef CONFIG_IEEE80211R
2736                 if (data->rx_action.category == WLAN_ACTION_FT) {
2737                         ft_rx_action(wpa_s, data->rx_action.data,
2738                                      data->rx_action.len);
2739                         break;
2740                 }
2741 #endif /* CONFIG_IEEE80211R */
2742 #ifdef CONFIG_IEEE80211W
2743 #ifdef CONFIG_SME
2744                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2745                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2746                                         data->rx_action.data,
2747                                         data->rx_action.len);
2748                         break;
2749                 }
2750 #endif /* CONFIG_SME */
2751 #endif /* CONFIG_IEEE80211W */
2752 #ifdef CONFIG_WNM
2753                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2754                         ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2755                         break;
2756                 }
2757 #endif /* CONFIG_WNM */
2758 #ifdef CONFIG_GAS
2759                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2760                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2761                                  data->rx_action.sa, data->rx_action.bssid,
2762                                  data->rx_action.data, data->rx_action.len,
2763                                  data->rx_action.freq) == 0)
2764                         break;
2765 #endif /* CONFIG_GAS */
2766 #ifdef CONFIG_TDLS
2767                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2768                     data->rx_action.len >= 4 &&
2769                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2770                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2771                                 "Response from " MACSTR,
2772                                 MAC2STR(data->rx_action.sa));
2773                         break;
2774                 }
2775 #endif /* CONFIG_TDLS */
2776 #ifdef CONFIG_P2P
2777                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2778                                    data->rx_action.sa,
2779                                    data->rx_action.bssid,
2780                                    data->rx_action.category,
2781                                    data->rx_action.data,
2782                                    data->rx_action.len, data->rx_action.freq);
2783 #endif /* CONFIG_P2P */
2784                 break;
2785         case EVENT_RX_PROBE_REQ:
2786                 if (data->rx_probe_req.sa == NULL ||
2787                     data->rx_probe_req.ie == NULL)
2788                         break;
2789 #ifdef CONFIG_AP
2790                 if (wpa_s->ap_iface) {
2791                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2792                                              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                         break;
2799                 }
2800 #endif /* CONFIG_AP */
2801 #ifdef CONFIG_P2P
2802                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2803                                       data->rx_probe_req.da,
2804                                       data->rx_probe_req.bssid,
2805                                       data->rx_probe_req.ie,
2806                                       data->rx_probe_req.ie_len,
2807                                       data->rx_probe_req.ssi_signal);
2808 #endif /* CONFIG_P2P */
2809                 break;
2810         case EVENT_REMAIN_ON_CHANNEL:
2811 #ifdef CONFIG_OFFCHANNEL
2812                 offchannel_remain_on_channel_cb(
2813                         wpa_s, data->remain_on_channel.freq,
2814                         data->remain_on_channel.duration);
2815 #endif /* CONFIG_OFFCHANNEL */
2816 #ifdef CONFIG_P2P
2817                 wpas_p2p_remain_on_channel_cb(
2818                         wpa_s, data->remain_on_channel.freq,
2819                         data->remain_on_channel.duration);
2820 #endif /* CONFIG_P2P */
2821                 break;
2822         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2823 #ifdef CONFIG_OFFCHANNEL
2824                 offchannel_cancel_remain_on_channel_cb(
2825                         wpa_s, data->remain_on_channel.freq);
2826 #endif /* CONFIG_OFFCHANNEL */
2827 #ifdef CONFIG_P2P
2828                 wpas_p2p_cancel_remain_on_channel_cb(
2829                         wpa_s, data->remain_on_channel.freq);
2830 #endif /* CONFIG_P2P */
2831                 break;
2832 #ifdef CONFIG_P2P
2833         case EVENT_P2P_DEV_FOUND: {
2834                 struct p2p_peer_info peer_info;
2835
2836                 os_memset(&peer_info, 0, sizeof(peer_info));
2837                 if (data->p2p_dev_found.dev_addr)
2838                         os_memcpy(peer_info.p2p_device_addr,
2839                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2840                 if (data->p2p_dev_found.pri_dev_type)
2841                         os_memcpy(peer_info.pri_dev_type,
2842                                   data->p2p_dev_found.pri_dev_type,
2843                                   sizeof(peer_info.pri_dev_type));
2844                 if (data->p2p_dev_found.dev_name)
2845                         os_strlcpy(peer_info.device_name,
2846                                    data->p2p_dev_found.dev_name,
2847                                    sizeof(peer_info.device_name));
2848                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2849                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2850                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2851
2852                 /*
2853                  * FIX: new_device=1 is not necessarily correct. We should
2854                  * maintain a P2P peer database in wpa_supplicant and update
2855                  * this information based on whether the peer is truly new.
2856                  */
2857                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2858                 break;
2859                 }
2860         case EVENT_P2P_GO_NEG_REQ_RX:
2861                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2862                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2863                 break;
2864         case EVENT_P2P_GO_NEG_COMPLETED:
2865                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2866                 break;
2867         case EVENT_P2P_PROV_DISC_REQUEST:
2868                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2869                                    data->p2p_prov_disc_req.config_methods,
2870                                    data->p2p_prov_disc_req.dev_addr,
2871                                    data->p2p_prov_disc_req.pri_dev_type,
2872                                    data->p2p_prov_disc_req.dev_name,
2873                                    data->p2p_prov_disc_req.supp_config_methods,
2874                                    data->p2p_prov_disc_req.dev_capab,
2875                                    data->p2p_prov_disc_req.group_capab,
2876                                    NULL, 0);
2877                 break;
2878         case EVENT_P2P_PROV_DISC_RESPONSE:
2879                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2880                                     data->p2p_prov_disc_resp.config_methods);
2881                 break;
2882         case EVENT_P2P_SD_REQUEST:
2883                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2884                                 data->p2p_sd_req.sa,
2885                                 data->p2p_sd_req.dialog_token,
2886                                 data->p2p_sd_req.update_indic,
2887                                 data->p2p_sd_req.tlvs,
2888                                 data->p2p_sd_req.tlvs_len);
2889                 break;
2890         case EVENT_P2P_SD_RESPONSE:
2891                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2892                                  data->p2p_sd_resp.update_indic,
2893                                  data->p2p_sd_resp.tlvs,
2894                                  data->p2p_sd_resp.tlvs_len);
2895                 break;
2896 #endif /* CONFIG_P2P */
2897         case EVENT_EAPOL_RX:
2898                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2899                                         data->eapol_rx.data,
2900                                         data->eapol_rx.data_len);
2901                 break;
2902         case EVENT_SIGNAL_CHANGE:
2903                 bgscan_notify_signal_change(
2904                         wpa_s, data->signal_change.above_threshold,
2905                         data->signal_change.current_signal,
2906                         data->signal_change.current_noise,
2907                         data->signal_change.current_txrate);
2908                 break;
2909         case EVENT_INTERFACE_ENABLED:
2910                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2911                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2912                         wpa_supplicant_update_mac_addr(wpa_s);
2913 #ifdef CONFIG_AP
2914                         if (!wpa_s->ap_iface) {
2915                                 wpa_supplicant_set_state(wpa_s,
2916                                                          WPA_DISCONNECTED);
2917                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2918                         } else
2919                                 wpa_supplicant_set_state(wpa_s,
2920                                                          WPA_COMPLETED);
2921 #else /* CONFIG_AP */
2922                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2923                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2924 #endif /* CONFIG_AP */
2925                 }
2926                 break;
2927         case EVENT_INTERFACE_DISABLED:
2928                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2929                 wpa_supplicant_mark_disassoc(wpa_s);
2930                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2931                 break;
2932         case EVENT_CHANNEL_LIST_CHANGED:
2933                 if (wpa_s->drv_priv == NULL)
2934                         break; /* Ignore event during drv initialization */
2935
2936                 free_hw_features(wpa_s);
2937                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2938                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2939
2940 #ifdef CONFIG_P2P
2941                 wpas_p2p_update_channel_list(wpa_s);
2942 #endif /* CONFIG_P2P */
2943                 break;
2944         case EVENT_INTERFACE_UNAVAILABLE:
2945 #ifdef CONFIG_P2P
2946                 wpas_p2p_interface_unavailable(wpa_s);
2947 #endif /* CONFIG_P2P */
2948                 break;
2949         case EVENT_BEST_CHANNEL:
2950                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2951                         "(%d %d %d)",
2952                         data->best_chan.freq_24, data->best_chan.freq_5,
2953                         data->best_chan.freq_overall);
2954                 wpa_s->best_24_freq = data->best_chan.freq_24;
2955                 wpa_s->best_5_freq = data->best_chan.freq_5;
2956                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2957 #ifdef CONFIG_P2P
2958                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2959                                               data->best_chan.freq_5,
2960                                               data->best_chan.freq_overall);
2961 #endif /* CONFIG_P2P */
2962                 break;
2963         case EVENT_UNPROT_DEAUTH:
2964                 wpa_supplicant_event_unprot_deauth(wpa_s,
2965                                                    &data->unprot_deauth);
2966                 break;
2967         case EVENT_UNPROT_DISASSOC:
2968                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2969                                                      &data->unprot_disassoc);
2970                 break;
2971         case EVENT_STATION_LOW_ACK:
2972 #ifdef CONFIG_AP
2973                 if (wpa_s->ap_iface && data)
2974                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2975                                                   data->low_ack.addr);
2976 #endif /* CONFIG_AP */
2977 #ifdef CONFIG_TDLS
2978                 if (data)
2979                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2980 #endif /* CONFIG_TDLS */
2981                 break;
2982         case EVENT_IBSS_PEER_LOST:
2983 #ifdef CONFIG_IBSS_RSN
2984                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2985 #endif /* CONFIG_IBSS_RSN */
2986                 break;
2987         case EVENT_DRIVER_GTK_REKEY:
2988                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2989                               wpa_s->bssid, ETH_ALEN))
2990                         break;
2991                 if (!wpa_s->wpa)
2992                         break;
2993                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2994                                          data->driver_gtk_rekey.replay_ctr);
2995                 break;
2996         case EVENT_SCHED_SCAN_STOPPED:
2997                 wpa_s->sched_scanning = 0;
2998                 wpa_supplicant_notify_scanning(wpa_s, 0);
2999
3000                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3001                         break;
3002
3003                 /*
3004                  * If we timed out, start a new sched scan to continue
3005                  * searching for more SSIDs.
3006                  */
3007                 if (wpa_s->sched_scan_timed_out)
3008                         wpa_supplicant_req_sched_scan(wpa_s);
3009                 break;
3010         case EVENT_WPS_BUTTON_PUSHED:
3011 #ifdef CONFIG_WPS
3012                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3013 #endif /* CONFIG_WPS */
3014                 break;
3015         case EVENT_CONNECT_FAILED_REASON:
3016 #ifdef CONFIG_AP
3017                 if (!wpa_s->ap_iface || !data)
3018                         break;
3019                 hostapd_event_connect_failed_reason(
3020                         wpa_s->ap_iface->bss[0],
3021                         data->connect_failed_reason.addr,
3022                         data->connect_failed_reason.code);
3023 #endif /* CONFIG_AP */
3024                 break;
3025         default:
3026                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3027                 break;
3028         }
3029 }