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