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