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