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