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