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