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