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