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