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