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