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