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