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