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