]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - build-utilities/hostap.git/blob - wpa_supplicant/events.c
FT: Clear SME ft_used/ft_ies when disconnecting
[build-utilities/hostap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
15 #include "includes.h"
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "pcsc_funcs.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "common/wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ap/hostapd.h"
31 #include "p2p/p2p.h"
32 #include "notify.h"
33 #include "common/ieee802_11_defs.h"
34 #include "common/ieee802_11_common.h"
35 #include "crypto/random.h"
36 #include "blacklist.h"
37 #include "wpas_glue.h"
38 #include "wps_supplicant.h"
39 #include "ibss_rsn.h"
40 #include "sme.h"
41 #include "p2p_supplicant.h"
42 #include "bgscan.h"
43 #include "ap.h"
44 #include "bss.h"
45 #include "mlme.h"
46 #include "scan.h"
49 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
50 {
51         struct wpa_ssid *ssid, *old_ssid;
53         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
54                 return 0;
56         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
57                 "information");
58         ssid = wpa_supplicant_get_ssid(wpa_s);
59         if (ssid == NULL) {
60                 wpa_msg(wpa_s, MSG_INFO,
61                         "No network configuration found for the current AP");
62                 return -1;
63         }
65         if (ssid->disabled) {
66                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
67                 return -1;
68         }
70         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
71                 "current AP");
72         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
73                               WPA_KEY_MGMT_WPA_NONE |
74                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
75                               WPA_KEY_MGMT_PSK_SHA256 |
76                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
77                 u8 wpa_ie[80];
78                 size_t wpa_ie_len = sizeof(wpa_ie);
79                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
80                                           wpa_ie, &wpa_ie_len);
81         } else {
82                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
83         }
85         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
86                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
87         old_ssid = wpa_s->current_ssid;
88         wpa_s->current_ssid = ssid;
89         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
90         wpa_supplicant_initiate_eapol(wpa_s);
91         if (old_ssid != wpa_s->current_ssid)
92                 wpas_notify_network_changed(wpa_s);
94         return 0;
95 }
98 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
99                                                 void *sock_ctx)
101         struct wpa_supplicant *wpa_s = eloop_ctx;
103         if (wpa_s->countermeasures) {
104                 wpa_s->countermeasures = 0;
105                 wpa_drv_set_countermeasures(wpa_s, 0);
106                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
107                 wpa_supplicant_req_scan(wpa_s, 0, 0);
108         }
112 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
114         int bssid_changed;
116 #ifdef CONFIG_IBSS_RSN
117         ibss_rsn_deinit(wpa_s->ibss_rsn);
118         wpa_s->ibss_rsn = NULL;
119 #endif /* CONFIG_IBSS_RSN */
121 #ifdef CONFIG_AP
122         wpa_supplicant_ap_deinit(wpa_s);
123 #endif /* CONFIG_AP */
125         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
126                 return;
128         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
129         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
130         os_memset(wpa_s->bssid, 0, ETH_ALEN);
131         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
132         wpa_s->current_bss = NULL;
133         wpa_s->assoc_freq = 0;
134 #ifdef CONFIG_IEEE80211R
135 #ifdef CONFIG_SME
136         if (wpa_s->sme.ft_ies)
137                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
138 #endif /* CONFIG_SME */
139 #endif /* CONFIG_IEEE80211R */
141         if (bssid_changed)
142                 wpas_notify_bssid_changed(wpa_s);
144         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
145         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
146         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
147                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
148         wpa_s->ap_ies_from_associnfo = 0;
152 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
154         struct wpa_ie_data ie;
155         int pmksa_set = -1;
156         size_t i;
158         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
159             ie.pmkid == NULL)
160                 return;
162         for (i = 0; i < ie.num_pmkid; i++) {
163                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
164                                                     ie.pmkid + i * PMKID_LEN,
165                                                     NULL, NULL, 0);
166                 if (pmksa_set == 0) {
167                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
168                         break;
169                 }
170         }
172         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
173                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
177 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
178                                                  union wpa_event_data *data)
180         if (data == NULL) {
181                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
182                         "event");
183                 return;
184         }
185         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
186                 " index=%d preauth=%d",
187                 MAC2STR(data->pmkid_candidate.bssid),
188                 data->pmkid_candidate.index,
189                 data->pmkid_candidate.preauth);
191         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
192                             data->pmkid_candidate.index,
193                             data->pmkid_candidate.preauth);
197 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
199         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
200             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
201                 return 0;
203 #ifdef IEEE8021X_EAPOL
204         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
205             wpa_s->current_ssid &&
206             !(wpa_s->current_ssid->eapol_flags &
207               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
208                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
209                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
210                  * plaintext or static WEP keys). */
211                 return 0;
212         }
213 #endif /* IEEE8021X_EAPOL */
215         return 1;
219 /**
220  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
221  * @wpa_s: pointer to wpa_supplicant data
222  * @ssid: Configuration data for the network
223  * Returns: 0 on success, -1 on failure
224  *
225  * This function is called when starting authentication with a network that is
226  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
227  */
228 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
229                               struct wpa_ssid *ssid)
231 #ifdef IEEE8021X_EAPOL
232         int aka = 0, sim = 0, type;
234         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
235                 return 0;
237         if (ssid->eap.eap_methods == NULL) {
238                 sim = 1;
239                 aka = 1;
240         } else {
241                 struct eap_method_type *eap = ssid->eap.eap_methods;
242                 while (eap->vendor != EAP_VENDOR_IETF ||
243                        eap->method != EAP_TYPE_NONE) {
244                         if (eap->vendor == EAP_VENDOR_IETF) {
245                                 if (eap->method == EAP_TYPE_SIM)
246                                         sim = 1;
247                                 else if (eap->method == EAP_TYPE_AKA)
248                                         aka = 1;
249                         }
250                         eap++;
251                 }
252         }
254         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
255                 sim = 0;
256         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
257                 aka = 0;
259         if (!sim && !aka) {
260                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
261                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
262                         "enabled");
263                 return 0;
264         }
266         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
267                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
268         if (sim && aka)
269                 type = SCARD_TRY_BOTH;
270         else if (aka)
271                 type = SCARD_USIM_ONLY;
272         else
273                 type = SCARD_GSM_SIM_ONLY;
275         wpa_s->scard = scard_init(type);
276         if (wpa_s->scard == NULL) {
277                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
278                         "(pcsc-lite)");
279                 return -1;
280         }
281         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
282         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
283 #endif /* IEEE8021X_EAPOL */
285         return 0;
289 #ifndef CONFIG_NO_SCAN_PROCESSING
290 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
291                                         struct wpa_ssid *ssid)
293         int i, privacy = 0;
295         if (ssid->mixed_cell)
296                 return 1;
298 #ifdef CONFIG_WPS
299         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
300                 return 1;
301 #endif /* CONFIG_WPS */
303         for (i = 0; i < NUM_WEP_KEYS; i++) {
304                 if (ssid->wep_key_len[i]) {
305                         privacy = 1;
306                         break;
307                 }
308         }
309 #ifdef IEEE8021X_EAPOL
310         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
311             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
312                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
313                 privacy = 1;
314 #endif /* IEEE8021X_EAPOL */
316         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
317                 privacy = 1;
319         if (bss->caps & IEEE80211_CAP_PRIVACY)
320                 return privacy;
321         return !privacy;
325 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
326                                          struct wpa_ssid *ssid,
327                                          struct wpa_scan_res *bss)
329         struct wpa_ie_data ie;
330         int proto_match = 0;
331         const u8 *rsn_ie, *wpa_ie;
332         int ret;
333         int wep_ok;
335         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
336         if (ret >= 0)
337                 return ret;
339         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
340         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
341                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
342                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
343                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
345         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
346         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
347                 proto_match++;
349                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
350                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
351                                 "failed");
352                         break;
353                 }
355                 if (wep_ok &&
356                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
357                 {
358                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
359                                 "in RSN IE");
360                         return 1;
361                 }
363                 if (!(ie.proto & ssid->proto)) {
364                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
365                                 "mismatch");
366                         break;
367                 }
369                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
370                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
371                                 "cipher mismatch");
372                         break;
373                 }
375                 if (!(ie.group_cipher & ssid->group_cipher)) {
376                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
377                                 "cipher mismatch");
378                         break;
379                 }
381                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
382                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
383                                 "mismatch");
384                         break;
385                 }
387 #ifdef CONFIG_IEEE80211W
388                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
389                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
390                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
391                                 "frame protection");
392                         break;
393                 }
394 #endif /* CONFIG_IEEE80211W */
396                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
397                 return 1;
398         }
400         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
401         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
402                 proto_match++;
404                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
405                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
406                                 "failed");
407                         break;
408                 }
410                 if (wep_ok &&
411                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
412                 {
413                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
414                                 "in WPA IE");
415                         return 1;
416                 }
418                 if (!(ie.proto & ssid->proto)) {
419                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
420                                 "mismatch");
421                         break;
422                 }
424                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
425                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
426                                 "cipher mismatch");
427                         break;
428                 }
430                 if (!(ie.group_cipher & ssid->group_cipher)) {
431                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
432                                 "cipher mismatch");
433                         break;
434                 }
436                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
437                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
438                                 "mismatch");
439                         break;
440                 }
442                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
443                 return 1;
444         }
446         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
447             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
448                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
449                 return 0;
450         }
452         if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
453                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
454                 return 1;
455         }
457         wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
458                 "WPA/WPA2");
460         return 0;
464 static int freq_allowed(int *freqs, int freq)
466         int i;
468         if (freqs == NULL)
469                 return 1;
471         for (i = 0; freqs[i]; i++)
472                 if (freqs[i] == freq)
473                         return 1;
474         return 0;
478 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
479                                             int i, struct wpa_scan_res *bss,
480                                             struct wpa_ssid *group)
482         const u8 *ssid_;
483         u8 wpa_ie_len, rsn_ie_len, ssid_len;
484         int wpa;
485         struct wpa_blacklist *e;
486         const u8 *ie;
487         struct wpa_ssid *ssid;
489         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
490         ssid_ = ie ? ie + 2 : (u8 *) "";
491         ssid_len = ie ? ie[1] : 0;
493         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
494         wpa_ie_len = ie ? ie[1] : 0;
496         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
497         rsn_ie_len = ie ? ie[1] : 0;
499         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
500                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
501                 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
502                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
503                 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
505         e = wpa_blacklist_get(wpa_s, bss->bssid);
506         if (e) {
507                 int limit = 1;
508                 if (wpa_supplicant_enabled_networks(wpa_s->conf) == 1) {
509                         /*
510                          * When only a single network is enabled, we can
511                          * trigger blacklisting on the first failure. This
512                          * should not be done with multiple enabled networks to
513                          * avoid getting forced to move into a worse ESS on
514                          * single error if there are no other BSSes of the
515                          * current ESS.
516                          */
517                         limit = 0;
518                 }
519                 if (e->count > limit) {
520                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
521                                 "(count=%d limit=%d)", e->count, limit);
522                         return NULL;
523                 }
524         }
526         if (ssid_len == 0) {
527                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
528                 return NULL;
529         }
531         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
533         for (ssid = group; ssid; ssid = ssid->pnext) {
534                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
536                 if (ssid->disabled) {
537                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
538                         continue;
539                 }
541 #ifdef CONFIG_WPS
542                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
543                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
544                                 "(WPS)");
545                         continue;
546                 }
548                 if (wpa && ssid->ssid_len == 0 &&
549                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
550                         check_ssid = 0;
552                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
553                         /* Only allow wildcard SSID match if an AP
554                          * advertises active WPS operation that matches
555                          * with our mode. */
556                         check_ssid = 1;
557                         if (ssid->ssid_len == 0 &&
558                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
559                                 check_ssid = 0;
560                 }
561 #endif /* CONFIG_WPS */
563                 if (check_ssid &&
564                     (ssid_len != ssid->ssid_len ||
565                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
566                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
567                         continue;
568                 }
570                 if (ssid->bssid_set &&
571                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
572                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
573                         continue;
574                 }
576                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
577                         continue;
579                 if (!wpa &&
580                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
581                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
582                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
583                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
584                                 "not allowed");
585                         continue;
586                 }
588                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
589                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
590                                 "mismatch");
591                         continue;
592                 }
594                 if (bss->caps & IEEE80211_CAP_IBSS) {
595                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
596                                 "network");
597                         continue;
598                 }
600                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
601                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
602                                 "allowed");
603                         continue;
604                 }
606 #ifdef CONFIG_P2P
607                 /*
608                  * TODO: skip the AP if its P2P IE has Group Formation
609                  * bit set in the P2P Group Capability Bitmap and we
610                  * are not in Group Formation with that device.
611                  */
612 #endif /* CONFIG_P2P */
614                 /* Matching configuration found */
615                 return ssid;
616         }
618         /* No matching configuration found */
619         return NULL;
623 static struct wpa_bss *
624 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
625                           struct wpa_scan_results *scan_res,
626                           struct wpa_ssid *group,
627                           struct wpa_ssid **selected_ssid)
629         size_t i;
631         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
632                 group->priority);
634         for (i = 0; i < scan_res->num; i++) {
635                 struct wpa_scan_res *bss = scan_res->res[i];
636                 const u8 *ie, *ssid;
637                 u8 ssid_len;
639                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
640                 if (!*selected_ssid)
641                         continue;
643                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
644                 ssid = ie ? ie + 2 : (u8 *) "";
645                 ssid_len = ie ? ie[1] : 0;
647                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
648                         " ssid='%s'",
649                         MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
650                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
651         }
653         return NULL;
657 static struct wpa_bss *
658 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
659                             struct wpa_scan_results *scan_res,
660                             struct wpa_ssid **selected_ssid)
662         struct wpa_bss *selected = NULL;
663         int prio;
665         while (selected == NULL) {
666                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
667                         selected = wpa_supplicant_select_bss(
668                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
669                                 selected_ssid);
670                         if (selected)
671                                 break;
672                 }
674                 if (selected == NULL && wpa_s->blacklist) {
675                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
676                                 "blacklist and try again");
677                         wpa_blacklist_clear(wpa_s);
678                         wpa_s->blacklist_cleared++;
679                 } else if (selected == NULL)
680                         break;
681         }
683         return selected;
687 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
688                                         int timeout_sec, int timeout_usec)
690         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
691                 /*
692                  * No networks are enabled; short-circuit request so
693                  * we don't wait timeout seconds before transitioning
694                  * to INACTIVE state.
695                  */
696                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
697                 return;
698         }
699         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
703 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
704                             struct wpa_bss *selected,
705                             struct wpa_ssid *ssid)
707         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
708                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
709                         "PBC session overlap");
710 #ifdef CONFIG_P2P
711                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
712                         return;
713 #endif /* CONFIG_P2P */
715 #ifdef CONFIG_WPS
716                 wpas_wps_cancel(wpa_s);
717 #endif /* CONFIG_WPS */
718                 return;
719         }
721         /*
722          * Do not trigger new association unless the BSSID has changed or if
723          * reassociation is requested. If we are in process of associating with
724          * the selected BSSID, do not trigger new attempt.
725          */
726         if (wpa_s->reassociate ||
727             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
728              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
729                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
730               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
731               0))) {
732                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
733                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
734                         return;
735                 }
736                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
737                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
738                         "  pending: " MACSTR "  wpa_state: %s",
739                         wpa_s->reassociate, MAC2STR(selected->bssid),
740                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
741                         wpa_supplicant_state_txt(wpa_s->wpa_state));
742                 wpa_supplicant_associate(wpa_s, selected, ssid);
743         } else {
744                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
745                         "selected AP");
746         }
750 static struct wpa_ssid *
751 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
753         int prio;
754         struct wpa_ssid *ssid;
756         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
757                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
758                 {
759                         if (ssid->disabled)
760                                 continue;
761                         if (ssid->mode == IEEE80211_MODE_IBSS ||
762                             ssid->mode == IEEE80211_MODE_AP)
763                                 return ssid;
764                 }
765         }
766         return NULL;
770 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
771  * on BSS added and BSS changed events */
772 static void wpa_supplicant_rsn_preauth_scan_results(
773         struct wpa_supplicant *wpa_s)
775         struct wpa_bss *bss;
777         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
778                 return;
780         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
781                 const u8 *ssid, *rsn;
783                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
784                 if (ssid == NULL)
785                         continue;
787                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
788                 if (rsn == NULL)
789                         continue;
791                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
792         }
797 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
798                                        struct wpa_bss *selected,
799                                        struct wpa_ssid *ssid,
800                                        struct wpa_scan_results *scan_res)
802         size_t i;
803         struct wpa_scan_res *current_bss = NULL;
804         int min_diff;
806         if (wpa_s->reassociate)
807                 return 1; /* explicit request to reassociate */
808         if (wpa_s->wpa_state < WPA_ASSOCIATED)
809                 return 1; /* we are not associated; continue */
810         if (wpa_s->current_ssid == NULL)
811                 return 1; /* unknown current SSID */
812         if (wpa_s->current_ssid != ssid)
813                 return 1; /* different network block */
815         for (i = 0; i < scan_res->num; i++) {
816                 struct wpa_scan_res *res = scan_res->res[i];
817                 const u8 *ie;
818                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
819                         continue;
821                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
822                 if (ie == NULL)
823                         continue;
824                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
825                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
826                         continue;
827                 current_bss = res;
828                 break;
829         }
831         if (!current_bss)
832                 return 1; /* current BSS not seen in scan results */
834         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
835         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
836                 MAC2STR(current_bss->bssid), current_bss->level);
837         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
838                 MAC2STR(selected->bssid), selected->level);
840         if (wpa_s->current_ssid->bssid_set &&
841             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
842             0) {
843                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
844                         "has preferred BSSID");
845                 return 1;
846         }
848         min_diff = 2;
849         if (current_bss->level < 0) {
850                 if (current_bss->level < -85)
851                         min_diff = 1;
852                 else if (current_bss->level < -80)
853                         min_diff = 2;
854                 else if (current_bss->level < -75)
855                         min_diff = 3;
856                 else if (current_bss->level < -70)
857                         min_diff = 4;
858                 else
859                         min_diff = 5;
860         }
861         if (abs(current_bss->level - selected->level) < min_diff) {
862                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
863                         "in signal level");
864                 return 0;
865         }
867         return 1;
870 /* Return < 0 if no scan results could be fetched. */
871 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
872                                               union wpa_event_data *data)
874         struct wpa_bss *selected;
875         struct wpa_ssid *ssid = NULL;
876         struct wpa_scan_results *scan_res;
877         int ap = 0;
879 #ifdef CONFIG_AP
880         if (wpa_s->ap_iface)
881                 ap = 1;
882 #endif /* CONFIG_AP */
884         wpa_supplicant_notify_scanning(wpa_s, 0);
886         scan_res = wpa_supplicant_get_scan_results(wpa_s,
887                                                    data ? &data->scan_info :
888                                                    NULL, 1);
889         if (scan_res == NULL) {
890                 if (wpa_s->conf->ap_scan == 2 || ap)
891                         return -1;
892                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
893                         "scanning again");
894                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
895                 return -1;
896         }
898 #ifndef CONFIG_NO_RANDOM_POOL
899         size_t i, num;
900         num = scan_res->num;
901         if (num > 10)
902                 num = 10;
903         for (i = 0; i < num; i++) {
904                 u8 buf[5];
905                 struct wpa_scan_res *res = scan_res->res[i];
906                 buf[0] = res->bssid[5];
907                 buf[1] = res->qual & 0xff;
908                 buf[2] = res->noise & 0xff;
909                 buf[3] = res->level & 0xff;
910                 buf[4] = res->tsf & 0xff;
911                 random_add_randomness(buf, sizeof(buf));
912         }
913 #endif /* CONFIG_NO_RANDOM_POOL */
915         if (wpa_s->scan_res_handler) {
916                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
917                                          struct wpa_scan_results *scan_res);
919                 scan_res_handler = wpa_s->scan_res_handler;
920                 wpa_s->scan_res_handler = NULL;
921                 scan_res_handler(wpa_s, scan_res);
923                 wpa_scan_results_free(scan_res);
924                 return 0;
925         }
927         if (ap) {
928                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
929 #ifdef CONFIG_AP
930                 if (wpa_s->ap_iface->scan_cb)
931                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
932 #endif /* CONFIG_AP */
933                 wpa_scan_results_free(scan_res);
934                 return 0;
935         }
937         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
938         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
939         wpas_notify_scan_results(wpa_s);
941         wpas_notify_scan_done(wpa_s, 1);
943         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
944                 wpa_scan_results_free(scan_res);
945                 return 0;
946         }
948         if (wpa_s->disconnected) {
949                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
950                 wpa_scan_results_free(scan_res);
951                 return 0;
952         }
954         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
955                 wpa_scan_results_free(scan_res);
956                 return 0;
957         }
959         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
961         if (selected) {
962                 int skip;
963                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
964                                                     scan_res);
965                 wpa_scan_results_free(scan_res);
966                 if (skip)
967                         return 0;
968                 wpa_supplicant_connect(wpa_s, selected, ssid);
969                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
970         } else {
971                 wpa_scan_results_free(scan_res);
972                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
973                 ssid = wpa_supplicant_pick_new_network(wpa_s);
974                 if (ssid) {
975                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
976                         wpa_supplicant_associate(wpa_s, NULL, ssid);
977                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
978                 } else {
979                         int timeout_sec = wpa_s->scan_interval;
980                         int timeout_usec = 0;
981 #ifdef CONFIG_P2P
982                         if (wpa_s->p2p_in_provisioning) {
983                                 /*
984                                  * Use shorter wait during P2P Provisioning
985                                  * state to speed up group formation.
986                                  */
987                                 timeout_sec = 0;
988                                 timeout_usec = 250000;
989                         }
990 #endif /* CONFIG_P2P */
991                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
992                                                     timeout_usec);
993                 }
994         }
995         return 0;
999 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1000                                               union wpa_event_data *data)
1002         const char *rn, *rn2;
1003         struct wpa_supplicant *ifs;
1005         if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1006                 /*
1007                  * If no scan results could be fetched, then no need to
1008                  * notify those interfaces that did not actually request
1009                  * this scan.
1010                  */
1011                 return;
1012         }
1014         /*
1015          * Check other interfaces to see if they have the same radio-name. If
1016          * so, they get updated with this same scan info.
1017          */
1018         if (!wpa_s->driver->get_radio_name)
1019                 return;
1021         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1022         if (rn == NULL || rn[0] == '\0')
1023                 return;
1025         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1026                 "sharing same radio (%s) in event_scan_results", rn);
1028         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1029                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1030                         continue;
1032                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1033                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1034                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1035                                    "sibling", ifs->ifname);
1036                         _wpa_supplicant_event_scan_results(ifs, data);
1037                 }
1038         }
1041 #endif /* CONFIG_NO_SCAN_PROCESSING */
1044 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1045                                           union wpa_event_data *data)
1047         int l, len, found = 0, wpa_found, rsn_found;
1048         const u8 *p;
1050         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1051         if (data->assoc_info.req_ies)
1052                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1053                             data->assoc_info.req_ies_len);
1054         if (data->assoc_info.resp_ies) {
1055                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1056                             data->assoc_info.resp_ies_len);
1057 #ifdef CONFIG_TDLS
1058                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1059                                         data->assoc_info.resp_ies_len);
1060 #endif /* CONFIG_TDLS */
1061         }
1062         if (data->assoc_info.beacon_ies)
1063                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1064                             data->assoc_info.beacon_ies,
1065                             data->assoc_info.beacon_ies_len);
1066         if (data->assoc_info.freq)
1067                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1068                         data->assoc_info.freq);
1070         p = data->assoc_info.req_ies;
1071         l = data->assoc_info.req_ies_len;
1073         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1074         while (p && l >= 2) {
1075                 len = p[1] + 2;
1076                 if (len > l) {
1077                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1078                                     p, l);
1079                         break;
1080                 }
1081                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1082                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1083                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1084                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1085                                 break;
1086                         found = 1;
1087                         wpa_find_assoc_pmkid(wpa_s);
1088                         break;
1089                 }
1090                 l -= len;
1091                 p += len;
1092         }
1093         if (!found && data->assoc_info.req_ies)
1094                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1096 #ifdef CONFIG_IEEE80211R
1097 #ifdef CONFIG_SME
1098         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1099                 u8 bssid[ETH_ALEN];
1100                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1101                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1102                                                  data->assoc_info.resp_ies,
1103                                                  data->assoc_info.resp_ies_len,
1104                                                  bssid) < 0) {
1105                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1106                                 "Reassociation Response failed");
1107                         wpa_supplicant_deauthenticate(
1108                                 wpa_s, WLAN_REASON_INVALID_IE);
1109                         return -1;
1110                 }
1111         }
1113         p = data->assoc_info.resp_ies;
1114         l = data->assoc_info.resp_ies_len;
1116 #ifdef CONFIG_WPS_STRICT
1117         if (p && wpa_s->current_ssid &&
1118             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1119                 struct wpabuf *wps;
1120                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1121                 if (wps == NULL) {
1122                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1123                                 "include WPS IE in (Re)Association Response");
1124                         return -1;
1125                 }
1127                 if (wps_validate_assoc_resp(wps) < 0) {
1128                         wpabuf_free(wps);
1129                         wpa_supplicant_deauthenticate(
1130                                 wpa_s, WLAN_REASON_INVALID_IE);
1131                         return -1;
1132                 }
1133                 wpabuf_free(wps);
1134         }
1135 #endif /* CONFIG_WPS_STRICT */
1137         /* Go through the IEs and make a copy of the MDIE, if present. */
1138         while (p && l >= 2) {
1139                 len = p[1] + 2;
1140                 if (len > l) {
1141                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1142                                     p, l);
1143                         break;
1144                 }
1145                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1146                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1147                         wpa_s->sme.ft_used = 1;
1148                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1149                                   MOBILITY_DOMAIN_ID_LEN);
1150                         break;
1151                 }
1152                 l -= len;
1153                 p += len;
1154         }
1155 #endif /* CONFIG_SME */
1157         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1158                              data->assoc_info.resp_ies_len);
1159 #endif /* CONFIG_IEEE80211R */
1161         /* WPA/RSN IE from Beacon/ProbeResp */
1162         p = data->assoc_info.beacon_ies;
1163         l = data->assoc_info.beacon_ies_len;
1165         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1166          */
1167         wpa_found = rsn_found = 0;
1168         while (p && l >= 2) {
1169                 len = p[1] + 2;
1170                 if (len > l) {
1171                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1172                                     p, l);
1173                         break;
1174                 }
1175                 if (!wpa_found &&
1176                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1177                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1178                         wpa_found = 1;
1179                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1180                 }
1182                 if (!rsn_found &&
1183                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1184                         rsn_found = 1;
1185                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1186                 }
1188                 l -= len;
1189                 p += len;
1190         }
1192         if (!wpa_found && data->assoc_info.beacon_ies)
1193                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1194         if (!rsn_found && data->assoc_info.beacon_ies)
1195                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1196         if (wpa_found || rsn_found)
1197                 wpa_s->ap_ies_from_associnfo = 1;
1199         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1200             wpa_s->assoc_freq != data->assoc_info.freq) {
1201                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1202                            "%u to %u MHz",
1203                            wpa_s->assoc_freq, data->assoc_info.freq);
1204                 wpa_supplicant_update_scan_results(wpa_s);
1205         }
1207         wpa_s->assoc_freq = data->assoc_info.freq;
1209         return 0;
1213 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1214                                        union wpa_event_data *data)
1216         u8 bssid[ETH_ALEN];
1217         int ft_completed;
1218         int bssid_changed;
1219         struct wpa_driver_capa capa;
1221 #ifdef CONFIG_AP
1222         if (wpa_s->ap_iface) {
1223                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1224                                     data->assoc_info.addr,
1225                                     data->assoc_info.req_ies,
1226                                     data->assoc_info.req_ies_len,
1227                                     data->assoc_info.reassoc);
1228                 return;
1229         }
1230 #endif /* CONFIG_AP */
1232         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1233         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1234                 return;
1236         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1237         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1238                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1239         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1240             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1241              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1242                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1243                         MACSTR, MAC2STR(bssid));
1244                 random_add_randomness(bssid, ETH_ALEN);
1245                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1246                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1247                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1248                 if (bssid_changed)
1249                         wpas_notify_bssid_changed(wpa_s);
1251                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1252                         wpa_clear_keys(wpa_s, bssid);
1253                 }
1254                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1255                         wpa_supplicant_disassociate(
1256                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1257                         return;
1258                 }
1259                 if (wpa_s->current_ssid) {
1260                         struct wpa_bss *bss = NULL;
1261                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1262                         if (ssid->ssid_len > 0)
1263                                 bss = wpa_bss_get(wpa_s, bssid,
1264                                                   ssid->ssid, ssid->ssid_len);
1265                         if (!bss)
1266                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1267                         if (bss)
1268                                 wpa_s->current_bss = bss;
1269                 }
1270         }
1272 #ifdef CONFIG_SME
1273         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1274         wpa_s->sme.prev_bssid_set = 1;
1275 #endif /* CONFIG_SME */
1277         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1278         if (wpa_s->current_ssid) {
1279                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1280                  * initialized before association, but for other modes,
1281                  * initialize PC/SC here, if the current configuration needs
1282                  * smartcard or SIM/USIM. */
1283                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1284         }
1285         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1286         if (wpa_s->l2)
1287                 l2_packet_notify_auth_start(wpa_s->l2);
1289         /*
1290          * Set portEnabled first to FALSE in order to get EAP state machine out
1291          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1292          * state machine may transit to AUTHENTICATING state based on obsolete
1293          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1294          * AUTHENTICATED without ever giving chance to EAP state machine to
1295          * reset the state.
1296          */
1297         if (!ft_completed) {
1298                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1299                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1300         }
1301         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1302                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1303         /* 802.1X::portControl = Auto */
1304         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1305         wpa_s->eapol_received = 0;
1306         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1307             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1308             (wpa_s->current_ssid &&
1309              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1310                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1311                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1312         } else if (!ft_completed) {
1313                 /* Timeout for receiving the first EAPOL packet */
1314                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1315         }
1316         wpa_supplicant_cancel_scan(wpa_s);
1318         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1319             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1320                 /*
1321                  * We are done; the driver will take care of RSN 4-way
1322                  * handshake.
1323                  */
1324                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1325                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1326                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1327                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1328         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1329                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1330                 /*
1331                  * The driver will take care of RSN 4-way handshake, so we need
1332                  * to allow EAPOL supplicant to complete its work without
1333                  * waiting for WPA supplicant.
1334                  */
1335                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1336         } else if (ft_completed) {
1337                 /*
1338                  * FT protocol completed - make sure EAPOL state machine ends
1339                  * up in authenticated.
1340                  */
1341                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1342                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1343                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1344                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1345         }
1347         if (wpa_s->pending_eapol_rx) {
1348                 struct os_time now, age;
1349                 os_get_time(&now);
1350                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1351                 if (age.sec == 0 && age.usec < 100000 &&
1352                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1353                     0) {
1354                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1355                                 "frame that was received just before "
1356                                 "association notification");
1357                         wpa_supplicant_rx_eapol(
1358                                 wpa_s, wpa_s->pending_eapol_rx_src,
1359                                 wpabuf_head(wpa_s->pending_eapol_rx),
1360                                 wpabuf_len(wpa_s->pending_eapol_rx));
1361                 }
1362                 wpabuf_free(wpa_s->pending_eapol_rx);
1363                 wpa_s->pending_eapol_rx = NULL;
1364         }
1366         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1367              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1368             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1369             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1370                 /* Set static WEP keys again */
1371                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1372         }
1374 #ifdef CONFIG_IBSS_RSN
1375         if (wpa_s->current_ssid &&
1376             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1377             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1378             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1379             wpa_s->ibss_rsn == NULL) {
1380                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1381                 if (!wpa_s->ibss_rsn) {
1382                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1383                         wpa_supplicant_deauthenticate(
1384                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1385                         return;
1386                 }
1388                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1389         }
1390 #endif /* CONFIG_IBSS_RSN */
1394 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1395                                           u16 reason_code)
1397         const u8 *bssid;
1398         int authenticating;
1399         u8 prev_pending_bssid[ETH_ALEN];
1401         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1402         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1404         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1405                 /*
1406                  * At least Host AP driver and a Prism3 card seemed to be
1407                  * generating streams of disconnected events when configuring
1408                  * IBSS for WPA-None. Ignore them for now.
1409                  */
1410                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1411                         "IBSS/WPA-None mode");
1412                 return;
1413         }
1415         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1416             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1417                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1418                         "pre-shared key may be incorrect");
1419         }
1420         if (!wpa_s->auto_reconnect_disabled ||
1421             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1422                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect enabled: try to "
1423                         "reconnect (wps=%d)",
1424                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1425                 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1426                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1427         } else {
1428                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Auto connect disabled: do not "
1429                         "try to re-connect");
1430                 wpa_s->reassociate = 0;
1431                 wpa_s->disconnected = 1;
1432         }
1433         bssid = wpa_s->bssid;
1434         if (is_zero_ether_addr(bssid))
1435                 bssid = wpa_s->pending_bssid;
1436         wpas_connection_failed(wpa_s, bssid);
1437         wpa_sm_notify_disassoc(wpa_s->wpa);
1438         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1439                 " reason=%d",
1440                 MAC2STR(bssid), reason_code);
1441         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1442                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1443                 wpa_s->keys_cleared = 0;
1444                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1445         }
1446         wpa_supplicant_mark_disassoc(wpa_s);
1448         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1449                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1453 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1454 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1455                                                     void *sock_ctx)
1457         struct wpa_supplicant *wpa_s = eloop_ctx;
1459         if (!wpa_s->pending_mic_error_report)
1460                 return;
1462         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1463         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1464         wpa_s->pending_mic_error_report = 0;
1466 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1469 static void
1470 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1471                                          union wpa_event_data *data)
1473         int pairwise;
1474         struct os_time t;
1476         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1477         pairwise = (data && data->michael_mic_failure.unicast);
1478         os_get_time(&t);
1479         if ((wpa_s->last_michael_mic_error &&
1480              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1481             wpa_s->pending_mic_error_report) {
1482                 if (wpa_s->pending_mic_error_report) {
1483                         /*
1484                          * Send the pending MIC error report immediately since
1485                          * we are going to start countermeasures and AP better
1486                          * do the same.
1487                          */
1488                         wpa_sm_key_request(wpa_s->wpa, 1,
1489                                            wpa_s->pending_mic_error_pairwise);
1490                 }
1492                 /* Send the new MIC error report immediately since we are going
1493                  * to start countermeasures and AP better do the same.
1494                  */
1495                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1497                 /* initialize countermeasures */
1498                 wpa_s->countermeasures = 1;
1499                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1501                 /*
1502                  * Need to wait for completion of request frame. We do not get
1503                  * any callback for the message completion, so just wait a
1504                  * short while and hope for the best. */
1505                 os_sleep(0, 10000);
1507                 wpa_drv_set_countermeasures(wpa_s, 1);
1508                 wpa_supplicant_deauthenticate(wpa_s,
1509                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1510                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1511                                      wpa_s, NULL);
1512                 eloop_register_timeout(60, 0,
1513                                        wpa_supplicant_stop_countermeasures,
1514                                        wpa_s, NULL);
1515                 /* TODO: mark the AP rejected for 60 second. STA is
1516                  * allowed to associate with another AP.. */
1517         } else {
1518 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1519                 if (wpa_s->mic_errors_seen) {
1520                         /*
1521                          * Reduce the effectiveness of Michael MIC error
1522                          * reports as a means for attacking against TKIP if
1523                          * more than one MIC failure is noticed with the same
1524                          * PTK. We delay the transmission of the reports by a
1525                          * random time between 0 and 60 seconds in order to
1526                          * force the attacker wait 60 seconds before getting
1527                          * the information on whether a frame resulted in a MIC
1528                          * failure.
1529                          */
1530                         u8 rval[4];
1531                         int sec;
1533                         if (os_get_random(rval, sizeof(rval)) < 0)
1534                                 sec = os_random() % 60;
1535                         else
1536                                 sec = WPA_GET_BE32(rval) % 60;
1537                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1538                                 "report %d seconds", sec);
1539                         wpa_s->pending_mic_error_report = 1;
1540                         wpa_s->pending_mic_error_pairwise = pairwise;
1541                         eloop_cancel_timeout(
1542                                 wpa_supplicant_delayed_mic_error_report,
1543                                 wpa_s, NULL);
1544                         eloop_register_timeout(
1545                                 sec, os_random() % 1000000,
1546                                 wpa_supplicant_delayed_mic_error_report,
1547                                 wpa_s, NULL);
1548                 } else {
1549                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1550                 }
1551 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1552                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1553 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1554         }
1555         wpa_s->last_michael_mic_error = t.sec;
1556         wpa_s->mic_errors_seen++;
1560 #ifdef CONFIG_TERMINATE_ONLASTIF
1561 static int any_interfaces(struct wpa_supplicant *head)
1563         struct wpa_supplicant *wpa_s;
1565         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1566                 if (!wpa_s->interface_removed)
1567                         return 1;
1568         return 0;
1570 #endif /* CONFIG_TERMINATE_ONLASTIF */
1573 static void
1574 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1575                                       union wpa_event_data *data)
1577         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1578                 return;
1580         switch (data->interface_status.ievent) {
1581         case EVENT_INTERFACE_ADDED:
1582                 if (!wpa_s->interface_removed)
1583                         break;
1584                 wpa_s->interface_removed = 0;
1585                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1586                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1587                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1588                                 "driver after interface was added");
1589                 }
1590                 break;
1591         case EVENT_INTERFACE_REMOVED:
1592                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1593                 wpa_s->interface_removed = 1;
1594                 wpa_supplicant_mark_disassoc(wpa_s);
1595                 l2_packet_deinit(wpa_s->l2);
1596                 wpa_s->l2 = NULL;
1597 #ifdef CONFIG_IBSS_RSN
1598                 ibss_rsn_deinit(wpa_s->ibss_rsn);
1599                 wpa_s->ibss_rsn = NULL;
1600 #endif /* CONFIG_IBSS_RSN */
1601 #ifdef CONFIG_TERMINATE_ONLASTIF
1602                 /* check if last interface */
1603                 if (!any_interfaces(wpa_s->global->ifaces))
1604                         eloop_terminate();
1605 #endif /* CONFIG_TERMINATE_ONLASTIF */
1606                 break;
1607         }
1611 #ifdef CONFIG_PEERKEY
1612 static void
1613 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1614                               union wpa_event_data *data)
1616         if (data == NULL)
1617                 return;
1618         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1620 #endif /* CONFIG_PEERKEY */
1623 #ifdef CONFIG_TDLS
1624 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1625                                       union wpa_event_data *data)
1627         if (data == NULL)
1628                 return;
1629         switch (data->tdls.oper) {
1630         case TDLS_REQUEST_SETUP:
1631                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1632                 break;
1633         case TDLS_REQUEST_TEARDOWN:
1634                 /* request from driver to add FTIE */
1635                 wpa_tdls_recv_teardown_notify(wpa_s->wpa, data->tdls.peer,
1636                                               data->tdls.reason_code);
1637                 break;
1638         }
1640 #endif /* CONFIG_TDLS */
1643 #ifdef CONFIG_IEEE80211R
1644 static void
1645 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1646                                  union wpa_event_data *data)
1648         if (data == NULL)
1649                 return;
1651         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1652                                     data->ft_ies.ies_len,
1653                                     data->ft_ies.ft_action,
1654                                     data->ft_ies.target_ap,
1655                                     data->ft_ies.ric_ies,
1656                                     data->ft_ies.ric_ies_len) < 0) {
1657                 /* TODO: prevent MLME/driver from trying to associate? */
1658         }
1660 #endif /* CONFIG_IEEE80211R */
1663 #ifdef CONFIG_IBSS_RSN
1664 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1665                                                 union wpa_event_data *data)
1667         struct wpa_ssid *ssid;
1668         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1669                 return;
1670         if (data == NULL)
1671                 return;
1672         ssid = wpa_s->current_ssid;
1673         if (ssid == NULL)
1674                 return;
1675         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
1676                 return;
1678         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1680 #endif /* CONFIG_IBSS_RSN */
1683 #ifdef CONFIG_IEEE80211R
1684 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1685                          size_t len)
1687         const u8 *sta_addr, *target_ap_addr;
1688         u16 status;
1690         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1691         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1692                 return; /* only SME case supported for now */
1693         if (len < 1 + 2 * ETH_ALEN + 2)
1694                 return;
1695         if (data[0] != 2)
1696                 return; /* Only FT Action Response is supported for now */
1697         sta_addr = data + 1;
1698         target_ap_addr = data + 1 + ETH_ALEN;
1699         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1700         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
1701                 MACSTR " TargetAP " MACSTR " status %u",
1702                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1704         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1705                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1706                         " in FT Action Response", MAC2STR(sta_addr));
1707                 return;
1708         }
1710         if (status) {
1711                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
1712                         "failure (status code %d)", status);
1713                 /* TODO: report error to FT code(?) */
1714                 return;
1715         }
1717         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1718                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1719                                     target_ap_addr, NULL, 0) < 0)
1720                 return;
1722 #ifdef CONFIG_SME
1723         {
1724                 struct wpa_bss *bss;
1725                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1726                 if (bss)
1727                         wpa_s->sme.freq = bss->freq;
1728                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1729                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1730                               WLAN_AUTH_FT);
1731         }
1732 #endif /* CONFIG_SME */
1734 #endif /* CONFIG_IEEE80211R */
1737 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
1738                                                struct unprot_deauth *e)
1740 #ifdef CONFIG_IEEE80211W
1741         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
1742                    "dropped: " MACSTR " -> " MACSTR
1743                    " (reason code %u)",
1744                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1745         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1746 #endif /* CONFIG_IEEE80211W */
1750 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
1751                                                  struct unprot_disassoc *e)
1753 #ifdef CONFIG_IEEE80211W
1754         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
1755                    "dropped: " MACSTR " -> " MACSTR
1756                    " (reason code %u)",
1757                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
1758         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
1759 #endif /* CONFIG_IEEE80211W */
1763 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1764                           union wpa_event_data *data)
1766         struct wpa_supplicant *wpa_s = ctx;
1767         u16 reason_code = 0;
1769         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1770             event != EVENT_INTERFACE_ENABLED &&
1771             event != EVENT_INTERFACE_STATUS) {
1772                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore event %d while interface is "
1773                         "disabled", event);
1774                 return;
1775         }
1777         wpa_dbg(wpa_s, MSG_DEBUG, "Event %d received on interface %s",
1778                 event, wpa_s->ifname);
1780         switch (event) {
1781         case EVENT_AUTH:
1782                 sme_event_auth(wpa_s, data);
1783                 break;
1784         case EVENT_ASSOC:
1785                 wpa_supplicant_event_assoc(wpa_s, data);
1786                 break;
1787         case EVENT_DISASSOC:
1788                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
1789                 if (data) {
1790                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1791                                 data->disassoc_info.reason_code);
1792                         if (data->disassoc_info.addr)
1793                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
1794                                         MAC2STR(data->disassoc_info.addr));
1795                 }
1796 #ifdef CONFIG_AP
1797                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1798                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1799                                                data->disassoc_info.addr);
1800                         break;
1801                 }
1802 #endif /* CONFIG_AP */
1803                 if (data) {
1804                         reason_code = data->disassoc_info.reason_code;
1805                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1806                                     data->disassoc_info.ie,
1807                                     data->disassoc_info.ie_len);
1808 #ifdef CONFIG_P2P
1809                         wpas_p2p_disassoc_notif(
1810                                 wpa_s, data->disassoc_info.addr, reason_code,
1811                                 data->disassoc_info.ie,
1812                                 data->disassoc_info.ie_len);
1813 #endif /* CONFIG_P2P */
1814                 }
1815                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1816                         sme_event_disassoc(wpa_s, data);
1817                 /* fall through */
1818         case EVENT_DEAUTH:
1819                 if (event == EVENT_DEAUTH) {
1820                         wpa_dbg(wpa_s, MSG_DEBUG,
1821                                 "Deauthentication notification");
1822                         if (data) {
1823                                 reason_code = data->deauth_info.reason_code;
1824                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u",
1825                                         data->deauth_info.reason_code);
1826                                 if (data->deauth_info.addr) {
1827                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
1828                                                 MACSTR,
1829                                                 MAC2STR(data->deauth_info.
1830                                                         addr));
1831                                 }
1832                                 wpa_hexdump(MSG_DEBUG,
1833                                             "Deauthentication frame IE(s)",
1834                                             data->deauth_info.ie,
1835                                             data->deauth_info.ie_len);
1836 #ifdef CONFIG_P2P
1837                                 wpas_p2p_deauth_notif(
1838                                         wpa_s, data->deauth_info.addr,
1839                                         reason_code,
1840                                         data->deauth_info.ie,
1841                                         data->deauth_info.ie_len);
1842 #endif /* CONFIG_P2P */
1843                         }
1844                 }
1845 #ifdef CONFIG_AP
1846                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1847                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1848                                                data->deauth_info.addr);
1849                         break;
1850                 }
1851 #endif /* CONFIG_AP */
1852                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1853                 break;
1854         case EVENT_MICHAEL_MIC_FAILURE:
1855                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1856                 break;
1857 #ifndef CONFIG_NO_SCAN_PROCESSING
1858         case EVENT_SCAN_RESULTS:
1859                 wpa_supplicant_event_scan_results(wpa_s, data);
1860                 break;
1861 #endif /* CONFIG_NO_SCAN_PROCESSING */
1862         case EVENT_ASSOCINFO:
1863                 wpa_supplicant_event_associnfo(wpa_s, data);
1864                 break;
1865         case EVENT_INTERFACE_STATUS:
1866                 wpa_supplicant_event_interface_status(wpa_s, data);
1867                 break;
1868         case EVENT_PMKID_CANDIDATE:
1869                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1870                 break;
1871 #ifdef CONFIG_PEERKEY
1872         case EVENT_STKSTART:
1873                 wpa_supplicant_event_stkstart(wpa_s, data);
1874                 break;
1875 #endif /* CONFIG_PEERKEY */
1876 #ifdef CONFIG_TDLS
1877         case EVENT_TDLS:
1878                 wpa_supplicant_event_tdls(wpa_s, data);
1879                 break;
1880 #endif /* CONFIG_TDLS */
1881 #ifdef CONFIG_IEEE80211R
1882         case EVENT_FT_RESPONSE:
1883                 wpa_supplicant_event_ft_response(wpa_s, data);
1884                 break;
1885 #endif /* CONFIG_IEEE80211R */
1886 #ifdef CONFIG_IBSS_RSN
1887         case EVENT_IBSS_RSN_START:
1888                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1889                 break;
1890 #endif /* CONFIG_IBSS_RSN */
1891         case EVENT_ASSOC_REJECT:
1892                 if (data->assoc_reject.bssid)
1893                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1894                                 "bssid=" MACSTR " status_code=%u",
1895                                 MAC2STR(data->assoc_reject.bssid),
1896                                 data->assoc_reject.status_code);
1897                 else
1898                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
1899                                 "status_code=%u",
1900                                 data->assoc_reject.status_code);
1901                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1902                         sme_event_assoc_reject(wpa_s, data);
1903                 break;
1904         case EVENT_AUTH_TIMED_OUT:
1905                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1906                         sme_event_auth_timed_out(wpa_s, data);
1907                 break;
1908         case EVENT_ASSOC_TIMED_OUT:
1909                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1910                         sme_event_assoc_timed_out(wpa_s, data);
1911                 break;
1912 #ifdef CONFIG_AP
1913         case EVENT_TX_STATUS:
1914                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
1915                         " type=%d stype=%d",
1916                         MAC2STR(data->tx_status.dst),
1917                         data->tx_status.type, data->tx_status.stype);
1918                 if (wpa_s->ap_iface == NULL) {
1919 #ifdef CONFIG_P2P
1920                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1921                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1922                                 wpas_send_action_tx_status(
1923                                         wpa_s, data->tx_status.dst,
1924                                         data->tx_status.data,
1925                                         data->tx_status.data_len,
1926                                         data->tx_status.ack ?
1927                                         P2P_SEND_ACTION_SUCCESS :
1928                                         P2P_SEND_ACTION_NO_ACK);
1929 #endif /* CONFIG_P2P */
1930                         break;
1931                 }
1932 #ifdef CONFIG_P2P
1933                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
1934                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
1935                 /*
1936                  * Catch TX status events for Action frames we sent via group
1937                  * interface in GO mode.
1938                  */
1939                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1940                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1941                     os_memcmp(wpa_s->parent->pending_action_dst,
1942                               data->tx_status.dst, ETH_ALEN) == 0) {
1943                         wpas_send_action_tx_status(
1944                                 wpa_s->parent, data->tx_status.dst,
1945                                 data->tx_status.data,
1946                                 data->tx_status.data_len,
1947                                 data->tx_status.ack ?
1948                                 P2P_SEND_ACTION_SUCCESS :
1949                                 P2P_SEND_ACTION_NO_ACK);
1950                         break;
1951                 }
1952 #endif /* CONFIG_P2P */
1953                 switch (data->tx_status.type) {
1954                 case WLAN_FC_TYPE_MGMT:
1955                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1956                                       data->tx_status.data_len,
1957                                       data->tx_status.stype,
1958                                       data->tx_status.ack);
1959                         break;
1960                 case WLAN_FC_TYPE_DATA:
1961                         ap_tx_status(wpa_s, data->tx_status.dst,
1962                                      data->tx_status.data,
1963                                      data->tx_status.data_len,
1964                                      data->tx_status.ack);
1965                         break;
1966                 }
1967                 break;
1968         case EVENT_RX_FROM_UNKNOWN:
1969                 if (wpa_s->ap_iface == NULL)
1970                         break;
1971                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1972                                        data->rx_from_unknown.len);
1973                 break;
1974         case EVENT_RX_MGMT:
1975                 if (wpa_s->ap_iface == NULL) {
1976 #ifdef CONFIG_P2P
1977                         u16 fc, stype;
1978                         const struct ieee80211_mgmt *mgmt;
1979                         mgmt = (const struct ieee80211_mgmt *)
1980                                 data->rx_mgmt.frame;
1981                         fc = le_to_host16(mgmt->frame_control);
1982                         stype = WLAN_FC_GET_STYPE(fc);
1983                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1984                             data->rx_mgmt.frame_len > 24) {
1985                                 const u8 *src = mgmt->sa;
1986                                 const u8 *ie = mgmt->u.probe_req.variable;
1987                                 size_t ie_len = data->rx_mgmt.frame_len -
1988                                         (mgmt->u.probe_req.variable -
1989                                          data->rx_mgmt.frame);
1990                                 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1991                                 break;
1992                         }
1993 #endif /* CONFIG_P2P */
1994                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
1995                                 "management frame in non-AP mode");
1996                         break;
1997                 }
1998                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1999                 break;
2000 #endif /* CONFIG_AP */
2001         case EVENT_RX_ACTION:
2002                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2003                         " Category=%u DataLen=%d freq=%d MHz",
2004                         MAC2STR(data->rx_action.sa),
2005                         data->rx_action.category, (int) data->rx_action.len,
2006                         data->rx_action.freq);
2007 #ifdef CONFIG_IEEE80211R
2008                 if (data->rx_action.category == WLAN_ACTION_FT) {
2009                         ft_rx_action(wpa_s, data->rx_action.data,
2010                                      data->rx_action.len);
2011                         break;
2012                 }
2013 #endif /* CONFIG_IEEE80211R */
2014 #ifdef CONFIG_IEEE80211W
2015 #ifdef CONFIG_SME
2016                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2017                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2018                                         data->rx_action.data,
2019                                         data->rx_action.len);
2020                         break;
2021                 }
2022 #endif /* CONFIG_SME */
2023 #endif /* CONFIG_IEEE80211W */
2024 #ifdef CONFIG_P2P
2025                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2026                                    data->rx_action.sa,
2027                                    data->rx_action.bssid,
2028                                    data->rx_action.category,
2029                                    data->rx_action.data,
2030                                    data->rx_action.len, data->rx_action.freq);
2031 #endif /* CONFIG_P2P */
2032                 break;
2033         case EVENT_RX_PROBE_REQ:
2034                 if (data->rx_probe_req.sa == NULL ||
2035                     data->rx_probe_req.ie == NULL)
2036                         break;
2037 #ifdef CONFIG_AP
2038                 if (wpa_s->ap_iface) {
2039                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2040                                              data->rx_probe_req.sa,
2041                                              data->rx_probe_req.ie,
2042                                              data->rx_probe_req.ie_len);
2043                         break;
2044                 }
2045 #endif /* CONFIG_AP */
2046 #ifdef CONFIG_P2P
2047                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2048                                       data->rx_probe_req.ie,
2049                                       data->rx_probe_req.ie_len);
2050 #endif /* CONFIG_P2P */
2051                 break;
2052 #ifdef CONFIG_P2P
2053         case EVENT_REMAIN_ON_CHANNEL:
2054                 wpas_p2p_remain_on_channel_cb(
2055                         wpa_s, data->remain_on_channel.freq,
2056                         data->remain_on_channel.duration);
2057                 break;
2058         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2059                 wpas_p2p_cancel_remain_on_channel_cb(
2060                         wpa_s, data->remain_on_channel.freq);
2061                 break;
2062         case EVENT_P2P_DEV_FOUND: {
2063                 struct p2p_peer_info peer_info;
2065                 os_memset(&peer_info, 0, sizeof(peer_info));
2066                 if (data->p2p_dev_found.dev_addr)
2067                         os_memcpy(peer_info.p2p_device_addr,
2068                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2069                 if (data->p2p_dev_found.pri_dev_type)
2070                         os_memcpy(peer_info.pri_dev_type,
2071                                   data->p2p_dev_found.pri_dev_type,
2072                                   sizeof(peer_info.pri_dev_type));
2073                 if (data->p2p_dev_found.dev_name)
2074                         os_strlcpy(peer_info.device_name,
2075                                    data->p2p_dev_found.dev_name,
2076                                    sizeof(peer_info.device_name));
2077                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2078                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2079                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2081                 /*
2082                  * FIX: new_device=1 is not necessarily correct. We should
2083                  * maintain a P2P peer database in wpa_supplicant and update
2084                  * this information based on whether the peer is truly new.
2085                  */
2086                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2087                 break;
2088                 }
2089         case EVENT_P2P_GO_NEG_REQ_RX:
2090                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2091                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2092                 break;
2093         case EVENT_P2P_GO_NEG_COMPLETED:
2094                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2095                 break;
2096         case EVENT_P2P_PROV_DISC_REQUEST:
2097                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2098                                    data->p2p_prov_disc_req.config_methods,
2099                                    data->p2p_prov_disc_req.dev_addr,
2100                                    data->p2p_prov_disc_req.pri_dev_type,
2101                                    data->p2p_prov_disc_req.dev_name,
2102                                    data->p2p_prov_disc_req.supp_config_methods,
2103                                    data->p2p_prov_disc_req.dev_capab,
2104                                    data->p2p_prov_disc_req.group_capab);
2105                 break;
2106         case EVENT_P2P_PROV_DISC_RESPONSE:
2107                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2108                                     data->p2p_prov_disc_resp.config_methods);
2109                 break;
2110         case EVENT_P2P_SD_REQUEST:
2111                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2112                                 data->p2p_sd_req.sa,
2113                                 data->p2p_sd_req.dialog_token,
2114                                 data->p2p_sd_req.update_indic,
2115                                 data->p2p_sd_req.tlvs,
2116                                 data->p2p_sd_req.tlvs_len);
2117                 break;
2118         case EVENT_P2P_SD_RESPONSE:
2119                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2120                                  data->p2p_sd_resp.update_indic,
2121                                  data->p2p_sd_resp.tlvs,
2122                                  data->p2p_sd_resp.tlvs_len);
2123                 break;
2124 #endif /* CONFIG_P2P */
2125 #ifdef CONFIG_CLIENT_MLME
2126         case EVENT_MLME_RX: {
2127                 struct ieee80211_rx_status rx_status;
2128                 os_memset(&rx_status, 0, sizeof(rx_status));
2129                 rx_status.freq = data->mlme_rx.freq;
2130                 rx_status.channel = data->mlme_rx.channel;
2131                 rx_status.ssi = data->mlme_rx.ssi;
2132                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
2133                                  &rx_status);
2134                 break;
2135         }
2136 #endif /* CONFIG_CLIENT_MLME */
2137         case EVENT_EAPOL_RX:
2138                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2139                                         data->eapol_rx.data,
2140                                         data->eapol_rx.data_len);
2141                 break;
2142         case EVENT_SIGNAL_CHANGE:
2143                 bgscan_notify_signal_change(
2144                         wpa_s, data->signal_change.above_threshold,
2145                         data->signal_change.current_signal,
2146                         data->signal_change.current_noise,
2147                         data->signal_change.current_txrate);
2148                 break;
2149         case EVENT_INTERFACE_ENABLED:
2150                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2151                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2152 #ifdef CONFIG_AP
2153                         if (!wpa_s->ap_iface) {
2154                                 wpa_supplicant_set_state(wpa_s,
2155                                                          WPA_DISCONNECTED);
2156                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2157                         } else
2158                                 wpa_supplicant_set_state(wpa_s,
2159                                                          WPA_COMPLETED);
2160 #else /* CONFIG_AP */
2161                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2162                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2163 #endif /* CONFIG_AP */
2164                 }
2165                 break;
2166         case EVENT_INTERFACE_DISABLED:
2167                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2168                 wpa_supplicant_mark_disassoc(wpa_s);
2169                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2170                 break;
2171         case EVENT_CHANNEL_LIST_CHANGED:
2172                 if (wpa_s->drv_priv == NULL)
2173                         break; /* Ignore event during drv initialization */
2174 #ifdef CONFIG_P2P
2175                 wpas_p2p_update_channel_list(wpa_s);
2176 #endif /* CONFIG_P2P */
2177                 break;
2178         case EVENT_INTERFACE_UNAVAILABLE:
2179 #ifdef CONFIG_P2P
2180                 wpas_p2p_interface_unavailable(wpa_s);
2181 #endif /* CONFIG_P2P */
2182                 break;
2183         case EVENT_BEST_CHANNEL:
2184                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2185                         "(%d %d %d)",
2186                         data->best_chan.freq_24, data->best_chan.freq_5,
2187                         data->best_chan.freq_overall);
2188                 wpa_s->best_24_freq = data->best_chan.freq_24;
2189                 wpa_s->best_5_freq = data->best_chan.freq_5;
2190                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2191 #ifdef CONFIG_P2P
2192                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2193                                               data->best_chan.freq_5,
2194                                               data->best_chan.freq_overall);
2195 #endif /* CONFIG_P2P */
2196                 break;
2197         case EVENT_UNPROT_DEAUTH:
2198                 wpa_supplicant_event_unprot_deauth(wpa_s,
2199                                                    &data->unprot_deauth);
2200                 break;
2201         case EVENT_UNPROT_DISASSOC:
2202                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2203                                                      &data->unprot_disassoc);
2204                 break;
2205         case EVENT_STATION_LOW_ACK:
2206 #ifdef CONFIG_AP
2207                 if (wpa_s->ap_iface && data)
2208                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2209                                                   data->low_ack.addr);
2210 #endif /* CONFIG_AP */
2211                 break;
2212         case EVENT_IBSS_PEER_LOST:
2213 #ifdef CONFIG_IBSS_RSN
2214                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2215 #endif /* CONFIG_IBSS_RSN */
2216                 break;
2217         default:
2218                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2219                 break;
2220         }