WNM: Fix build without CONFIG_SME=y
[mech_eap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eapol_supp/eapol_supp_sm.h"
13 #include "rsn_supp/wpa.h"
14 #include "eloop.h"
15 #include "config.h"
16 #include "l2_packet/l2_packet.h"
17 #include "wpa_supplicant_i.h"
18 #include "driver_i.h"
19 #include "pcsc_funcs.h"
20 #include "rsn_supp/preauth.h"
21 #include "rsn_supp/pmksa_cache.h"
22 #include "common/wpa_ctrl.h"
23 #include "eap_peer/eap.h"
24 #include "ap/hostapd.h"
25 #include "p2p/p2p.h"
26 #include "notify.h"
27 #include "common/ieee802_11_defs.h"
28 #include "common/ieee802_11_common.h"
29 #include "crypto/random.h"
30 #include "blacklist.h"
31 #include "wpas_glue.h"
32 #include "wps_supplicant.h"
33 #include "ibss_rsn.h"
34 #include "sme.h"
35 #include "gas_query.h"
36 #include "p2p_supplicant.h"
37 #include "bgscan.h"
38 #include "ap.h"
39 #include "bss.h"
40 #include "scan.h"
41 #include "offchannel.h"
42
43
44 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
45 {
46         struct wpa_ssid *ssid, *old_ssid;
47
48         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
49                 return 0;
50
51         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
52                 "information");
53         ssid = wpa_supplicant_get_ssid(wpa_s);
54         if (ssid == NULL) {
55                 wpa_msg(wpa_s, MSG_INFO,
56                         "No network configuration found for the current AP");
57                 return -1;
58         }
59
60         if (wpas_network_disabled(wpa_s, ssid)) {
61                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
62                 return -1;
63         }
64
65         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
66                 "current AP");
67         if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
68                 u8 wpa_ie[80];
69                 size_t wpa_ie_len = sizeof(wpa_ie);
70                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
71                                           wpa_ie, &wpa_ie_len);
72         } else {
73                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
74         }
75
76         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
77                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
78         old_ssid = wpa_s->current_ssid;
79         wpa_s->current_ssid = ssid;
80         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
81         wpa_supplicant_initiate_eapol(wpa_s);
82         if (old_ssid != wpa_s->current_ssid)
83                 wpas_notify_network_changed(wpa_s);
84
85         return 0;
86 }
87
88
89 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
90 {
91         struct wpa_supplicant *wpa_s = eloop_ctx;
92
93         if (wpa_s->countermeasures) {
94                 wpa_s->countermeasures = 0;
95                 wpa_drv_set_countermeasures(wpa_s, 0);
96                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
97                 wpa_supplicant_req_scan(wpa_s, 0, 0);
98         }
99 }
100
101
102 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
103 {
104         int bssid_changed;
105
106         wnm_bss_keep_alive_deinit(wpa_s);
107
108 #ifdef CONFIG_IBSS_RSN
109         ibss_rsn_deinit(wpa_s->ibss_rsn);
110         wpa_s->ibss_rsn = NULL;
111 #endif /* CONFIG_IBSS_RSN */
112
113 #ifdef CONFIG_AP
114         wpa_supplicant_ap_deinit(wpa_s);
115 #endif /* CONFIG_AP */
116
117         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
118                 return;
119
120         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
121         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
122         os_memset(wpa_s->bssid, 0, ETH_ALEN);
123         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
124 #ifdef CONFIG_P2P
125         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
126 #endif /* CONFIG_P2P */
127         wpa_s->current_bss = NULL;
128         wpa_s->assoc_freq = 0;
129 #ifdef CONFIG_IEEE80211R
130 #ifdef CONFIG_SME
131         if (wpa_s->sme.ft_ies)
132                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
133 #endif /* CONFIG_SME */
134 #endif /* CONFIG_IEEE80211R */
135
136         if (bssid_changed)
137                 wpas_notify_bssid_changed(wpa_s);
138
139         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
140         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
141         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
142                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
143         wpa_s->ap_ies_from_associnfo = 0;
144 }
145
146
147 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
148 {
149         struct wpa_ie_data ie;
150         int pmksa_set = -1;
151         size_t i;
152
153         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
154             ie.pmkid == NULL)
155                 return;
156
157         for (i = 0; i < ie.num_pmkid; i++) {
158                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
159                                                     ie.pmkid + i * PMKID_LEN,
160                                                     NULL, NULL, 0);
161                 if (pmksa_set == 0) {
162                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
163                         break;
164                 }
165         }
166
167         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
168                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
169 }
170
171
172 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
173                                                  union wpa_event_data *data)
174 {
175         if (data == NULL) {
176                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
177                         "event");
178                 return;
179         }
180         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
181                 " index=%d preauth=%d",
182                 MAC2STR(data->pmkid_candidate.bssid),
183                 data->pmkid_candidate.index,
184                 data->pmkid_candidate.preauth);
185
186         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
187                             data->pmkid_candidate.index,
188                             data->pmkid_candidate.preauth);
189 }
190
191
192 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
193 {
194         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
195             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
196                 return 0;
197
198 #ifdef IEEE8021X_EAPOL
199         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
200             wpa_s->current_ssid &&
201             !(wpa_s->current_ssid->eapol_flags &
202               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
203                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
204                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
205                  * plaintext or static WEP keys). */
206                 return 0;
207         }
208 #endif /* IEEE8021X_EAPOL */
209
210         return 1;
211 }
212
213
214 /**
215  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
216  * @wpa_s: pointer to wpa_supplicant data
217  * @ssid: Configuration data for the network
218  * Returns: 0 on success, -1 on failure
219  *
220  * This function is called when starting authentication with a network that is
221  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
222  */
223 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
224                               struct wpa_ssid *ssid)
225 {
226 #ifdef IEEE8021X_EAPOL
227 #ifdef PCSC_FUNCS
228         int aka = 0, sim = 0, type;
229
230         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
231                 return 0;
232
233         if (ssid->eap.eap_methods == NULL) {
234                 sim = 1;
235                 aka = 1;
236         } else {
237                 struct eap_method_type *eap = ssid->eap.eap_methods;
238                 while (eap->vendor != EAP_VENDOR_IETF ||
239                        eap->method != EAP_TYPE_NONE) {
240                         if (eap->vendor == EAP_VENDOR_IETF) {
241                                 if (eap->method == EAP_TYPE_SIM)
242                                         sim = 1;
243                                 else if (eap->method == EAP_TYPE_AKA ||
244                                          eap->method == EAP_TYPE_AKA_PRIME)
245                                         aka = 1;
246                         }
247                         eap++;
248                 }
249         }
250
251         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
252                 sim = 0;
253         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
254             eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
255             NULL)
256                 aka = 0;
257
258         if (!sim && !aka) {
259                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
260                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
261                         "enabled");
262                 return 0;
263         }
264
265         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
266                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
267         if (sim && aka)
268                 type = SCARD_TRY_BOTH;
269         else if (aka)
270                 type = SCARD_USIM_ONLY;
271         else
272                 type = SCARD_GSM_SIM_ONLY;
273
274         wpa_s->scard = scard_init(type, NULL);
275         if (wpa_s->scard == NULL) {
276                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
277                         "(pcsc-lite)");
278                 return -1;
279         }
280         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
281         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
282 #endif /* PCSC_FUNCS */
283 #endif /* IEEE8021X_EAPOL */
284
285         return 0;
286 }
287
288
289 #ifndef CONFIG_NO_SCAN_PROCESSING
290 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
291                                         struct wpa_ssid *ssid)
292 {
293         int i, privacy = 0;
294
295         if (ssid->mixed_cell)
296                 return 1;
297
298 #ifdef CONFIG_WPS
299         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
300                 return 1;
301 #endif /* CONFIG_WPS */
302
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 */
315
316         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
317                 privacy = 1;
318
319         if (bss->caps & IEEE80211_CAP_PRIVACY)
320                 return privacy;
321         return !privacy;
322 }
323
324
325 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
326                                          struct wpa_ssid *ssid,
327                                          struct wpa_scan_res *bss)
328 {
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;
334
335         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
336         if (ret >= 0)
337                 return ret;
338
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));
344
345         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
346         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
347                 proto_match++;
348
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                 }
354
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                 }
362
363                 if (!(ie.proto & ssid->proto)) {
364                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
365                                 "mismatch");
366                         break;
367                 }
368
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                 }
374
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                 }
380
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                 }
386
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 */
395
396                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
397                 return 1;
398         }
399
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++;
403
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                 }
409
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                 }
417
418                 if (!(ie.proto & ssid->proto)) {
419                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
420                                 "mismatch");
421                         break;
422                 }
423
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                 }
429
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                 }
435
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                 }
441
442                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
443                 return 1;
444         }
445
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         }
451
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         }
456
457         wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
458                 "WPA/WPA2");
459
460         return 0;
461 }
462
463
464 static int freq_allowed(int *freqs, int freq)
465 {
466         int i;
467
468         if (freqs == NULL)
469                 return 1;
470
471         for (i = 0; freqs[i]; i++)
472                 if (freqs[i] == freq)
473                         return 1;
474         return 0;
475 }
476
477
478 static int ht_supported(const struct hostapd_hw_modes *mode)
479 {
480         if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
481                 /*
482                  * The driver did not indicate whether it supports HT. Assume
483                  * it does to avoid connection issues.
484                  */
485                 return 1;
486         }
487
488         /*
489          * IEEE Std 802.11n-2009 20.1.1:
490          * An HT non-AP STA shall support all EQM rates for one spatial stream.
491          */
492         return mode->mcs_set[0] == 0xff;
493 }
494
495
496 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
497 {
498         const struct hostapd_hw_modes *mode = NULL, *modes;
499         const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
500         const u8 *rate_ie;
501         int i, j, k;
502
503         if (bss->freq == 0)
504                 return 1; /* Cannot do matching without knowing band */
505
506         modes = wpa_s->hw.modes;
507         if (modes == NULL) {
508                 /*
509                  * The driver does not provide any additional information
510                  * about the utilized hardware, so allow the connection attempt
511                  * to continue.
512                  */
513                 return 1;
514         }
515
516         for (i = 0; i < wpa_s->hw.num_modes; i++) {
517                 for (j = 0; j < modes[i].num_channels; j++) {
518                         int freq = modes[i].channels[j].freq;
519                         if (freq == bss->freq) {
520                                 if (mode &&
521                                     mode->mode == HOSTAPD_MODE_IEEE80211G)
522                                         break; /* do not allow 802.11b replace
523                                                 * 802.11g */
524                                 mode = &modes[i];
525                                 break;
526                         }
527                 }
528         }
529
530         if (mode == NULL)
531                 return 0;
532
533         for (i = 0; i < (int) sizeof(scan_ie); i++) {
534                 rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
535                 if (rate_ie == NULL)
536                         continue;
537
538                 for (j = 2; j < rate_ie[1] + 2; j++) {
539                         int flagged = !!(rate_ie[j] & 0x80);
540                         int r = (rate_ie[j] & 0x7f) * 5;
541
542                         /*
543                          * IEEE Std 802.11n-2009 7.3.2.2:
544                          * The new BSS Membership selector value is encoded
545                          * like a legacy basic rate, but it is not a rate and
546                          * only indicates if the BSS members are required to
547                          * support the mandatory features of Clause 20 [HT PHY]
548                          * in order to join the BSS.
549                          */
550                         if (flagged && ((rate_ie[j] & 0x7f) ==
551                                         BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
552                                 if (!ht_supported(mode)) {
553                                         wpa_dbg(wpa_s, MSG_DEBUG,
554                                                 "   hardware does not support "
555                                                 "HT PHY");
556                                         return 0;
557                                 }
558                                 continue;
559                         }
560
561                         if (!flagged)
562                                 continue;
563
564                         /* check for legacy basic rates */
565                         for (k = 0; k < mode->num_rates; k++) {
566                                 if (mode->rates[k] == r)
567                                         break;
568                         }
569                         if (k == mode->num_rates) {
570                                 /*
571                                  * IEEE Std 802.11-2007 7.3.2.2 demands that in
572                                  * order to join a BSS all required rates
573                                  * have to be supported by the hardware.
574                                  */
575                                 wpa_dbg(wpa_s, MSG_DEBUG, "   hardware does "
576                                         "not support required rate %d.%d Mbps",
577                                         r / 10, r % 10);
578                                 return 0;
579                         }
580                 }
581         }
582
583         return 1;
584 }
585
586
587 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
588                                             int i, struct wpa_scan_res *bss,
589                                             struct wpa_ssid *group)
590 {
591         const u8 *ssid_;
592         u8 wpa_ie_len, rsn_ie_len, ssid_len;
593         int wpa;
594         struct wpa_blacklist *e;
595         const u8 *ie;
596         struct wpa_ssid *ssid;
597
598         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
599         ssid_ = ie ? ie + 2 : (u8 *) "";
600         ssid_len = ie ? ie[1] : 0;
601
602         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
603         wpa_ie_len = ie ? ie[1] : 0;
604
605         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
606         rsn_ie_len = ie ? ie[1] : 0;
607
608         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
609                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
610                 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
611                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
612                 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
613
614         e = wpa_blacklist_get(wpa_s, bss->bssid);
615         if (e) {
616                 int limit = 1;
617                 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
618                         /*
619                          * When only a single network is enabled, we can
620                          * trigger blacklisting on the first failure. This
621                          * should not be done with multiple enabled networks to
622                          * avoid getting forced to move into a worse ESS on
623                          * single error if there are no other BSSes of the
624                          * current ESS.
625                          */
626                         limit = 0;
627                 }
628                 if (e->count > limit) {
629                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
630                                 "(count=%d limit=%d)", e->count, limit);
631                         return NULL;
632                 }
633         }
634
635         if (ssid_len == 0) {
636                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
637                 return NULL;
638         }
639
640         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
641
642         for (ssid = group; ssid; ssid = ssid->pnext) {
643                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
644
645                 if (wpas_network_disabled(wpa_s, ssid)) {
646                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
647                         continue;
648                 }
649
650 #ifdef CONFIG_WPS
651                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
652                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
653                                 "(WPS)");
654                         continue;
655                 }
656
657                 if (wpa && ssid->ssid_len == 0 &&
658                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
659                         check_ssid = 0;
660
661                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
662                         /* Only allow wildcard SSID match if an AP
663                          * advertises active WPS operation that matches
664                          * with our mode. */
665                         check_ssid = 1;
666                         if (ssid->ssid_len == 0 &&
667                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
668                                 check_ssid = 0;
669                 }
670 #endif /* CONFIG_WPS */
671
672                 if (ssid->bssid_set && ssid->ssid_len == 0 &&
673                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
674                         check_ssid = 0;
675
676                 if (check_ssid &&
677                     (ssid_len != ssid->ssid_len ||
678                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
679                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
680                         continue;
681                 }
682
683                 if (ssid->bssid_set &&
684                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
685                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
686                         continue;
687                 }
688
689                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
690                         continue;
691
692                 if (!wpa &&
693                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
694                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
695                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
696                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
697                                 "not allowed");
698                         continue;
699                 }
700
701                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
702                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
703                                 "mismatch");
704                         continue;
705                 }
706
707                 if (bss->caps & IEEE80211_CAP_IBSS) {
708                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
709                                 "network");
710                         continue;
711                 }
712
713                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
714                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
715                                 "allowed");
716                         continue;
717                 }
718
719                 if (!rate_match(wpa_s, bss)) {
720                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - rate sets do "
721                                 "not match");
722                         continue;
723                 }
724
725 #ifdef CONFIG_P2P
726                 /*
727                  * TODO: skip the AP if its P2P IE has Group Formation
728                  * bit set in the P2P Group Capability Bitmap and we
729                  * are not in Group Formation with that device.
730                  */
731 #endif /* CONFIG_P2P */
732
733                 /* Matching configuration found */
734                 return ssid;
735         }
736
737         /* No matching configuration found */
738         return NULL;
739 }
740
741
742 static struct wpa_bss *
743 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
744                           struct wpa_scan_results *scan_res,
745                           struct wpa_ssid *group,
746                           struct wpa_ssid **selected_ssid)
747 {
748         size_t i;
749
750         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
751                 group->priority);
752
753         for (i = 0; i < scan_res->num; i++) {
754                 struct wpa_scan_res *bss = scan_res->res[i];
755                 const u8 *ie, *ssid;
756                 u8 ssid_len;
757
758                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
759                 if (!*selected_ssid)
760                         continue;
761
762                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
763                 ssid = ie ? ie + 2 : (u8 *) "";
764                 ssid_len = ie ? ie[1] : 0;
765
766                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
767                         " ssid='%s'",
768                         MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
769                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
770         }
771
772         return NULL;
773 }
774
775
776 static struct wpa_bss *
777 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
778                             struct wpa_scan_results *scan_res,
779                             struct wpa_ssid **selected_ssid)
780 {
781         struct wpa_bss *selected = NULL;
782         int prio;
783
784         while (selected == NULL) {
785                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
786                         selected = wpa_supplicant_select_bss(
787                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
788                                 selected_ssid);
789                         if (selected)
790                                 break;
791                 }
792
793                 if (selected == NULL && wpa_s->blacklist &&
794                     !wpa_s->countermeasures) {
795                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
796                                 "blacklist and try again");
797                         wpa_blacklist_clear(wpa_s);
798                         wpa_s->blacklist_cleared++;
799                 } else if (selected == NULL)
800                         break;
801         }
802
803         return selected;
804 }
805
806
807 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
808                                         int timeout_sec, int timeout_usec)
809 {
810         if (!wpa_supplicant_enabled_networks(wpa_s)) {
811                 /*
812                  * No networks are enabled; short-circuit request so
813                  * we don't wait timeout seconds before transitioning
814                  * to INACTIVE state.
815                  */
816                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
817                 return;
818         }
819         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
820 }
821
822
823 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
824                            struct wpa_bss *selected,
825                            struct wpa_ssid *ssid)
826 {
827         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
828                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
829                         "PBC session overlap");
830 #ifdef CONFIG_P2P
831                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
832                         return -1;
833 #endif /* CONFIG_P2P */
834
835 #ifdef CONFIG_WPS
836                 wpas_wps_cancel(wpa_s);
837 #endif /* CONFIG_WPS */
838                 return -1;
839         }
840
841         /*
842          * Do not trigger new association unless the BSSID has changed or if
843          * reassociation is requested. If we are in process of associating with
844          * the selected BSSID, do not trigger new attempt.
845          */
846         if (wpa_s->reassociate ||
847             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
848              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
849                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
850               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
851               0))) {
852                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
853                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
854                         return 0;
855                 }
856                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
857                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
858                         "  pending: " MACSTR "  wpa_state: %s",
859                         wpa_s->reassociate, MAC2STR(selected->bssid),
860                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
861                         wpa_supplicant_state_txt(wpa_s->wpa_state));
862                 wpa_supplicant_associate(wpa_s, selected, ssid);
863         } else {
864                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
865                         "selected AP");
866         }
867
868         return 0;
869 }
870
871
872 static struct wpa_ssid *
873 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
874 {
875         int prio;
876         struct wpa_ssid *ssid;
877
878         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
879                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
880                 {
881                         if (wpas_network_disabled(wpa_s, ssid))
882                                 continue;
883                         if (ssid->mode == IEEE80211_MODE_IBSS ||
884                             ssid->mode == IEEE80211_MODE_AP)
885                                 return ssid;
886                 }
887         }
888         return NULL;
889 }
890
891
892 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
893  * on BSS added and BSS changed events */
894 static void wpa_supplicant_rsn_preauth_scan_results(
895         struct wpa_supplicant *wpa_s)
896 {
897         struct wpa_bss *bss;
898
899         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
900                 return;
901
902         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
903                 const u8 *ssid, *rsn;
904
905                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
906                 if (ssid == NULL)
907                         continue;
908
909                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
910                 if (rsn == NULL)
911                         continue;
912
913                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
914         }
915
916 }
917
918
919 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
920                                        struct wpa_bss *selected,
921                                        struct wpa_ssid *ssid,
922                                        struct wpa_scan_results *scan_res)
923 {
924         size_t i;
925         struct wpa_scan_res *current_bss = NULL;
926         int min_diff;
927
928         if (wpa_s->reassociate)
929                 return 1; /* explicit request to reassociate */
930         if (wpa_s->wpa_state < WPA_ASSOCIATED)
931                 return 1; /* we are not associated; continue */
932         if (wpa_s->current_ssid == NULL)
933                 return 1; /* unknown current SSID */
934         if (wpa_s->current_ssid != ssid)
935                 return 1; /* different network block */
936
937         if (wpas_driver_bss_selection(wpa_s))
938                 return 0; /* Driver-based roaming */
939
940         for (i = 0; i < scan_res->num; i++) {
941                 struct wpa_scan_res *res = scan_res->res[i];
942                 const u8 *ie;
943                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
944                         continue;
945
946                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
947                 if (ie == NULL)
948                         continue;
949                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
950                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
951                         continue;
952                 current_bss = res;
953                 break;
954         }
955
956         if (!current_bss)
957                 return 1; /* current BSS not seen in scan results */
958
959 #ifndef CONFIG_NO_ROAMING
960         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
961         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
962                 MAC2STR(current_bss->bssid), current_bss->level);
963         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
964                 MAC2STR(selected->bssid), selected->level);
965
966         if (wpa_s->current_ssid->bssid_set &&
967             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
968             0) {
969                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
970                         "has preferred BSSID");
971                 return 1;
972         }
973
974         min_diff = 2;
975         if (current_bss->level < 0) {
976                 if (current_bss->level < -85)
977                         min_diff = 1;
978                 else if (current_bss->level < -80)
979                         min_diff = 2;
980                 else if (current_bss->level < -75)
981                         min_diff = 3;
982                 else if (current_bss->level < -70)
983                         min_diff = 4;
984                 else
985                         min_diff = 5;
986         }
987         if (abs(current_bss->level - selected->level) < min_diff) {
988                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
989                         "in signal level");
990                 return 0;
991         }
992
993         return 1;
994 #else /* CONFIG_NO_ROAMING */
995         return 0;
996 #endif /* CONFIG_NO_ROAMING */
997 }
998
999
1000 /* Return < 0 if no scan results could be fetched. */
1001 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1002                                               union wpa_event_data *data)
1003 {
1004         struct wpa_bss *selected;
1005         struct wpa_ssid *ssid = NULL;
1006         struct wpa_scan_results *scan_res;
1007         int ap = 0;
1008
1009 #ifdef CONFIG_AP
1010         if (wpa_s->ap_iface)
1011                 ap = 1;
1012 #endif /* CONFIG_AP */
1013
1014         wpa_supplicant_notify_scanning(wpa_s, 0);
1015
1016 #ifdef CONFIG_P2P
1017         if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
1018             wpa_s->global->p2p != NULL) {
1019                 wpa_s->p2p_cb_on_scan_complete = 0;
1020                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1021                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1022                                 "stopped scan processing");
1023                         return -1;
1024                 }
1025         }
1026 #endif /* CONFIG_P2P */
1027
1028         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1029                                                    data ? &data->scan_info :
1030                                                    NULL, 1);
1031         if (scan_res == NULL) {
1032                 if (wpa_s->conf->ap_scan == 2 || ap)
1033                         return -1;
1034                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1035                         "scanning again");
1036                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1037                 return -1;
1038         }
1039
1040 #ifndef CONFIG_NO_RANDOM_POOL
1041         size_t i, num;
1042         num = scan_res->num;
1043         if (num > 10)
1044                 num = 10;
1045         for (i = 0; i < num; i++) {
1046                 u8 buf[5];
1047                 struct wpa_scan_res *res = scan_res->res[i];
1048                 buf[0] = res->bssid[5];
1049                 buf[1] = res->qual & 0xff;
1050                 buf[2] = res->noise & 0xff;
1051                 buf[3] = res->level & 0xff;
1052                 buf[4] = res->tsf & 0xff;
1053                 random_add_randomness(buf, sizeof(buf));
1054         }
1055 #endif /* CONFIG_NO_RANDOM_POOL */
1056
1057         if (wpa_s->scan_res_handler) {
1058                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1059                                          struct wpa_scan_results *scan_res);
1060
1061                 scan_res_handler = wpa_s->scan_res_handler;
1062                 wpa_s->scan_res_handler = NULL;
1063                 scan_res_handler(wpa_s, scan_res);
1064
1065                 wpa_scan_results_free(scan_res);
1066                 return 0;
1067         }
1068
1069         if (ap) {
1070                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1071 #ifdef CONFIG_AP
1072                 if (wpa_s->ap_iface->scan_cb)
1073                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1074 #endif /* CONFIG_AP */
1075                 wpa_scan_results_free(scan_res);
1076                 return 0;
1077         }
1078
1079         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1080         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1081         wpas_notify_scan_results(wpa_s);
1082
1083         wpas_notify_scan_done(wpa_s, 1);
1084
1085         if (sme_proc_obss_scan(wpa_s) > 0) {
1086                 wpa_scan_results_free(scan_res);
1087                 return 0;
1088         }
1089
1090         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1091                 wpa_scan_results_free(scan_res);
1092                 return 0;
1093         }
1094
1095         if (wpa_s->disconnected) {
1096                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1097                 wpa_scan_results_free(scan_res);
1098                 return 0;
1099         }
1100
1101         if (!wpas_driver_bss_selection(wpa_s) &&
1102             bgscan_notify_scan(wpa_s, scan_res) == 1) {
1103                 wpa_scan_results_free(scan_res);
1104                 return 0;
1105         }
1106
1107         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
1108
1109         if (selected) {
1110                 int skip;
1111                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
1112                                                     scan_res);
1113                 wpa_scan_results_free(scan_res);
1114                 if (skip) {
1115                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1116                         return 0;
1117                 }
1118
1119                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1120                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1121                         return -1;
1122                 }
1123                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1124         } else {
1125                 wpa_scan_results_free(scan_res);
1126                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1127                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1128                 if (ssid) {
1129                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1130                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1131                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1132                 } else {
1133                         int timeout_sec = wpa_s->scan_interval;
1134                         int timeout_usec = 0;
1135 #ifdef CONFIG_P2P
1136                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1137                                 return 0;
1138
1139                         if (wpa_s->p2p_in_provisioning) {
1140                                 /*
1141                                  * Use shorter wait during P2P Provisioning
1142                                  * state to speed up group formation.
1143                                  */
1144                                 timeout_sec = 0;
1145                                 timeout_usec = 250000;
1146                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1147                                                             timeout_usec);
1148                                 return 0;
1149                         }
1150 #endif /* CONFIG_P2P */
1151                         if (wpa_supplicant_req_sched_scan(wpa_s))
1152                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1153                                                             timeout_usec);
1154                 }
1155         }
1156         return 0;
1157 }
1158
1159
1160 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1161                                               union wpa_event_data *data)
1162 {
1163         const char *rn, *rn2;
1164         struct wpa_supplicant *ifs;
1165
1166         if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
1167                 /*
1168                  * If no scan results could be fetched, then no need to
1169                  * notify those interfaces that did not actually request
1170                  * this scan.
1171                  */
1172                 return;
1173         }
1174
1175         /*
1176          * Check other interfaces to see if they have the same radio-name. If
1177          * so, they get updated with this same scan info.
1178          */
1179         if (!wpa_s->driver->get_radio_name)
1180                 return;
1181
1182         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1183         if (rn == NULL || rn[0] == '\0')
1184                 return;
1185
1186         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1187                 "sharing same radio (%s) in event_scan_results", rn);
1188
1189         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1190                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1191                         continue;
1192
1193                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1194                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1195                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1196                                    "sibling", ifs->ifname);
1197                         _wpa_supplicant_event_scan_results(ifs, data);
1198                 }
1199         }
1200 }
1201
1202 #endif /* CONFIG_NO_SCAN_PROCESSING */
1203
1204
1205 #ifdef CONFIG_WNM
1206
1207 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1208 {
1209         struct wpa_supplicant *wpa_s = eloop_ctx;
1210
1211         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1212                 return;
1213
1214         wpa_printf(MSG_DEBUG, "WNM: Send keep-alive");
1215         /* TODO: could skip this if normal data traffic has been sent */
1216         /* TODO: send keep alive frame - better use some short unicast data
1217          * frame that gets protected if PTK is set */
1218
1219 #ifdef CONFIG_SME
1220         if (wpa_s->sme.bss_max_idle_period) {
1221                 unsigned int msec;
1222                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1223                 if (msec > 100)
1224                         msec -= 100;
1225                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1226                                        wnm_bss_keep_alive, wpa_s, NULL);
1227         }
1228 #endif /* CONFIG_SME */
1229 }
1230
1231
1232 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1233                                    const u8 *ies, size_t ies_len)
1234 {
1235         struct ieee802_11_elems elems;
1236
1237         if (ies == NULL)
1238                 return;
1239
1240         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1241                 return;
1242
1243 #ifdef CONFIG_SME
1244         if (elems.bss_max_idle_period) {
1245                 unsigned int msec;
1246                 wpa_s->sme.bss_max_idle_period =
1247                         WPA_GET_LE16(elems.bss_max_idle_period);
1248                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1249                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1250                            (elems.bss_max_idle_period[2] & 0x01) ?
1251                            " (protected keep-live required)" : "");
1252                 if (wpa_s->sme.bss_max_idle_period == 0)
1253                         wpa_s->sme.bss_max_idle_period = 1;
1254                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1255                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1256                          /* msec times 1000 */
1257                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1258                         if (msec > 100)
1259                                 msec -= 100;
1260                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1261                                                wnm_bss_keep_alive, wpa_s,
1262                                                NULL);
1263                 }
1264         }
1265 #endif /* CONFIG_SME */
1266 }
1267
1268 #endif /* CONFIG_WNM */
1269
1270
1271 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1272 {
1273 #ifdef CONFIG_WNM
1274         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1275 #endif /* CONFIG_WNM */
1276 }
1277
1278
1279 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1280                                           union wpa_event_data *data)
1281 {
1282         int l, len, found = 0, wpa_found, rsn_found;
1283         const u8 *p;
1284
1285         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1286         if (data->assoc_info.req_ies)
1287                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1288                             data->assoc_info.req_ies_len);
1289         if (data->assoc_info.resp_ies) {
1290                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1291                             data->assoc_info.resp_ies_len);
1292 #ifdef CONFIG_TDLS
1293                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1294                                         data->assoc_info.resp_ies_len);
1295 #endif /* CONFIG_TDLS */
1296 #ifdef CONFIG_WNM
1297                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1298                                        data->assoc_info.resp_ies_len);
1299 #endif /* CONFIG_WNM */
1300         }
1301         if (data->assoc_info.beacon_ies)
1302                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1303                             data->assoc_info.beacon_ies,
1304                             data->assoc_info.beacon_ies_len);
1305         if (data->assoc_info.freq)
1306                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1307                         data->assoc_info.freq);
1308
1309         p = data->assoc_info.req_ies;
1310         l = data->assoc_info.req_ies_len;
1311
1312         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1313         while (p && l >= 2) {
1314                 len = p[1] + 2;
1315                 if (len > l) {
1316                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1317                                     p, l);
1318                         break;
1319                 }
1320                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1321                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1322                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1323                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1324                                 break;
1325                         found = 1;
1326                         wpa_find_assoc_pmkid(wpa_s);
1327                         break;
1328                 }
1329                 l -= len;
1330                 p += len;
1331         }
1332         if (!found && data->assoc_info.req_ies)
1333                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1334
1335 #ifdef CONFIG_IEEE80211R
1336 #ifdef CONFIG_SME
1337         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1338                 u8 bssid[ETH_ALEN];
1339                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1340                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1341                                                  data->assoc_info.resp_ies,
1342                                                  data->assoc_info.resp_ies_len,
1343                                                  bssid) < 0) {
1344                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1345                                 "Reassociation Response failed");
1346                         wpa_supplicant_deauthenticate(
1347                                 wpa_s, WLAN_REASON_INVALID_IE);
1348                         return -1;
1349                 }
1350         }
1351
1352         p = data->assoc_info.resp_ies;
1353         l = data->assoc_info.resp_ies_len;
1354
1355 #ifdef CONFIG_WPS_STRICT
1356         if (p && wpa_s->current_ssid &&
1357             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1358                 struct wpabuf *wps;
1359                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1360                 if (wps == NULL) {
1361                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1362                                 "include WPS IE in (Re)Association Response");
1363                         return -1;
1364                 }
1365
1366                 if (wps_validate_assoc_resp(wps) < 0) {
1367                         wpabuf_free(wps);
1368                         wpa_supplicant_deauthenticate(
1369                                 wpa_s, WLAN_REASON_INVALID_IE);
1370                         return -1;
1371                 }
1372                 wpabuf_free(wps);
1373         }
1374 #endif /* CONFIG_WPS_STRICT */
1375
1376         /* Go through the IEs and make a copy of the MDIE, if present. */
1377         while (p && l >= 2) {
1378                 len = p[1] + 2;
1379                 if (len > l) {
1380                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1381                                     p, l);
1382                         break;
1383                 }
1384                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1385                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1386                         wpa_s->sme.ft_used = 1;
1387                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1388                                   MOBILITY_DOMAIN_ID_LEN);
1389                         break;
1390                 }
1391                 l -= len;
1392                 p += len;
1393         }
1394 #endif /* CONFIG_SME */
1395
1396         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1397                              data->assoc_info.resp_ies_len);
1398 #endif /* CONFIG_IEEE80211R */
1399
1400         /* WPA/RSN IE from Beacon/ProbeResp */
1401         p = data->assoc_info.beacon_ies;
1402         l = data->assoc_info.beacon_ies_len;
1403
1404         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1405          */
1406         wpa_found = rsn_found = 0;
1407         while (p && l >= 2) {
1408                 len = p[1] + 2;
1409                 if (len > l) {
1410                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1411                                     p, l);
1412                         break;
1413                 }
1414                 if (!wpa_found &&
1415                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1416                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1417                         wpa_found = 1;
1418                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1419                 }
1420
1421                 if (!rsn_found &&
1422                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1423                         rsn_found = 1;
1424                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1425                 }
1426
1427                 l -= len;
1428                 p += len;
1429         }
1430
1431         if (!wpa_found && data->assoc_info.beacon_ies)
1432                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1433         if (!rsn_found && data->assoc_info.beacon_ies)
1434                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1435         if (wpa_found || rsn_found)
1436                 wpa_s->ap_ies_from_associnfo = 1;
1437
1438         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1439             wpa_s->assoc_freq != data->assoc_info.freq) {
1440                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1441                            "%u to %u MHz",
1442                            wpa_s->assoc_freq, data->assoc_info.freq);
1443                 wpa_supplicant_update_scan_results(wpa_s);
1444         }
1445
1446         wpa_s->assoc_freq = data->assoc_info.freq;
1447
1448         return 0;
1449 }
1450
1451
1452 static struct wpa_bss * wpa_supplicant_get_new_bss(
1453         struct wpa_supplicant *wpa_s, const u8 *bssid)
1454 {
1455         struct wpa_bss *bss = NULL;
1456         struct wpa_ssid *ssid = wpa_s->current_ssid;
1457
1458         if (ssid->ssid_len > 0)
1459                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1460         if (!bss)
1461                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1462
1463         return bss;
1464 }
1465
1466
1467 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1468 {
1469         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1470
1471         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1472                 return -1;
1473
1474         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1475                 return 0;
1476
1477         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1478                                         WPA_IE_VENDOR_TYPE);
1479         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1480
1481         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1482                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1483             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1484                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1485                 return -1;
1486
1487         return 0;
1488 }
1489
1490
1491 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1492                                        union wpa_event_data *data)
1493 {
1494         u8 bssid[ETH_ALEN];
1495         int ft_completed;
1496         int bssid_changed;
1497         struct wpa_driver_capa capa;
1498
1499 #ifdef CONFIG_AP
1500         if (wpa_s->ap_iface) {
1501                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1502                                     data->assoc_info.addr,
1503                                     data->assoc_info.req_ies,
1504                                     data->assoc_info.req_ies_len,
1505                                     data->assoc_info.reassoc);
1506                 return;
1507         }
1508 #endif /* CONFIG_AP */
1509
1510         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1511         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1512                 return;
1513
1514         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1515         if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1516             os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1517                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1518                         MACSTR, MAC2STR(bssid));
1519                 random_add_randomness(bssid, ETH_ALEN);
1520                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1521                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1522                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1523                 if (bssid_changed)
1524                         wpas_notify_bssid_changed(wpa_s);
1525
1526                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1527                         wpa_clear_keys(wpa_s, bssid);
1528                 }
1529                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1530                         wpa_supplicant_disassociate(
1531                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1532                         return;
1533                 }
1534                 if (wpa_s->current_ssid) {
1535                         struct wpa_bss *bss = NULL;
1536
1537                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1538                         if (!bss) {
1539                                 wpa_supplicant_update_scan_results(wpa_s);
1540
1541                                 /* Get the BSS from the new scan results */
1542                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1543                         }
1544
1545                         if (bss)
1546                                 wpa_s->current_bss = bss;
1547                 }
1548
1549                 if (wpa_s->conf->ap_scan == 1 &&
1550                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1551                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1552                                 wpa_msg(wpa_s, MSG_WARNING,
1553                                         "WPA/RSN IEs not updated");
1554                 }
1555         }
1556
1557 #ifdef CONFIG_SME
1558         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1559         wpa_s->sme.prev_bssid_set = 1;
1560 #endif /* CONFIG_SME */
1561
1562         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1563         if (wpa_s->current_ssid) {
1564                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1565                  * initialized before association, but for other modes,
1566                  * initialize PC/SC here, if the current configuration needs
1567                  * smartcard or SIM/USIM. */
1568                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1569         }
1570         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1571         if (wpa_s->l2)
1572                 l2_packet_notify_auth_start(wpa_s->l2);
1573
1574         /*
1575          * Set portEnabled first to FALSE in order to get EAP state machine out
1576          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1577          * state machine may transit to AUTHENTICATING state based on obsolete
1578          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1579          * AUTHENTICATED without ever giving chance to EAP state machine to
1580          * reset the state.
1581          */
1582         if (!ft_completed) {
1583                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1584                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1585         }
1586         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1587                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1588         /* 802.1X::portControl = Auto */
1589         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1590         wpa_s->eapol_received = 0;
1591         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1592             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1593             (wpa_s->current_ssid &&
1594              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1595                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1596                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1597         } else if (!ft_completed) {
1598                 /* Timeout for receiving the first EAPOL packet */
1599                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1600         }
1601         wpa_supplicant_cancel_scan(wpa_s);
1602
1603         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1604             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1605                 /*
1606                  * We are done; the driver will take care of RSN 4-way
1607                  * handshake.
1608                  */
1609                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1610                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1611                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1612                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1613         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1614                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1615                 /*
1616                  * The driver will take care of RSN 4-way handshake, so we need
1617                  * to allow EAPOL supplicant to complete its work without
1618                  * waiting for WPA supplicant.
1619                  */
1620                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1621         } else if (ft_completed) {
1622                 /*
1623                  * FT protocol completed - make sure EAPOL state machine ends
1624                  * up in authenticated.
1625                  */
1626                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1627                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1628                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1629                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1630         }
1631
1632         if (wpa_s->pending_eapol_rx) {
1633                 struct os_time now, age;
1634                 os_get_time(&now);
1635                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1636                 if (age.sec == 0 && age.usec < 100000 &&
1637                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1638                     0) {
1639                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1640                                 "frame that was received just before "
1641                                 "association notification");
1642                         wpa_supplicant_rx_eapol(
1643                                 wpa_s, wpa_s->pending_eapol_rx_src,
1644                                 wpabuf_head(wpa_s->pending_eapol_rx),
1645                                 wpabuf_len(wpa_s->pending_eapol_rx));
1646                 }
1647                 wpabuf_free(wpa_s->pending_eapol_rx);
1648                 wpa_s->pending_eapol_rx = NULL;
1649         }
1650
1651         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1652              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1653             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1654             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1655                 /* Set static WEP keys again */
1656                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1657         }
1658
1659 #ifdef CONFIG_IBSS_RSN
1660         if (wpa_s->current_ssid &&
1661             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1662             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1663             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1664             wpa_s->ibss_rsn == NULL) {
1665                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1666                 if (!wpa_s->ibss_rsn) {
1667                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1668                         wpa_supplicant_deauthenticate(
1669                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1670                         return;
1671                 }
1672
1673                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1674         }
1675 #endif /* CONFIG_IBSS_RSN */
1676 }
1677
1678
1679 static int disconnect_reason_recoverable(u16 reason_code)
1680 {
1681         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1682                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1683                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1684 }
1685
1686
1687 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1688                                           u16 reason_code,
1689                                           int locally_generated)
1690 {
1691         const u8 *bssid;
1692         int authenticating;
1693         u8 prev_pending_bssid[ETH_ALEN];
1694         struct wpa_bss *fast_reconnect = NULL;
1695         struct wpa_ssid *fast_reconnect_ssid = NULL;
1696
1697         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1698         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1699
1700         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1701                 /*
1702                  * At least Host AP driver and a Prism3 card seemed to be
1703                  * generating streams of disconnected events when configuring
1704                  * IBSS for WPA-None. Ignore them for now.
1705                  */
1706                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1707                         "IBSS/WPA-None mode");
1708                 return;
1709         }
1710
1711         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1712             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1713                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1714                         "pre-shared key may be incorrect");
1715         }
1716         if (!wpa_s->auto_reconnect_disabled ||
1717             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1718                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1719                         "reconnect (wps=%d wpa_state=%d)",
1720                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1721                         wpa_s->wpa_state);
1722                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1723                     wpa_s->current_ssid &&
1724                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1725                     !locally_generated &&
1726                     disconnect_reason_recoverable(reason_code)) {
1727                         /*
1728                          * It looks like the AP has dropped association with
1729                          * us, but could allow us to get back in. Try to
1730                          * reconnect to the same BSS without full scan to save
1731                          * time for some common cases.
1732                          */
1733                         fast_reconnect = wpa_s->current_bss;
1734                         fast_reconnect_ssid = wpa_s->current_ssid;
1735                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1736                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1737                 else
1738                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1739                                 "immediate scan");
1740         } else {
1741                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1742                         "try to re-connect");
1743                 wpa_s->reassociate = 0;
1744                 wpa_s->disconnected = 1;
1745                 wpa_supplicant_cancel_sched_scan(wpa_s);
1746         }
1747         bssid = wpa_s->bssid;
1748         if (is_zero_ether_addr(bssid))
1749                 bssid = wpa_s->pending_bssid;
1750         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1751                 wpas_connection_failed(wpa_s, bssid);
1752         wpa_sm_notify_disassoc(wpa_s->wpa);
1753         if (!is_zero_ether_addr(bssid) ||
1754             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1755                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1756                         " reason=%d%s",
1757                         MAC2STR(bssid), reason_code,
1758                         locally_generated ? " locally_generated=1" : "");
1759         }
1760         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1761                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1762                 wpa_s->keys_cleared = 0;
1763                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1764         }
1765         wpa_supplicant_mark_disassoc(wpa_s);
1766
1767         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1768                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1769
1770         if (fast_reconnect) {
1771 #ifndef CONFIG_NO_SCAN_PROCESSING
1772                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1773                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1774                                            fast_reconnect_ssid) < 0) {
1775                         /* Recover through full scan */
1776                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1777                 }
1778 #endif /* CONFIG_NO_SCAN_PROCESSING */
1779         }
1780 }
1781
1782
1783 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1784 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1785 {
1786         struct wpa_supplicant *wpa_s = eloop_ctx;
1787
1788         if (!wpa_s->pending_mic_error_report)
1789                 return;
1790
1791         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1792         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1793         wpa_s->pending_mic_error_report = 0;
1794 }
1795 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1796
1797
1798 static void
1799 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1800                                          union wpa_event_data *data)
1801 {
1802         int pairwise;
1803         struct os_time t;
1804
1805         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1806         pairwise = (data && data->michael_mic_failure.unicast);
1807         os_get_time(&t);
1808         if ((wpa_s->last_michael_mic_error &&
1809              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1810             wpa_s->pending_mic_error_report) {
1811                 if (wpa_s->pending_mic_error_report) {
1812                         /*
1813                          * Send the pending MIC error report immediately since
1814                          * we are going to start countermeasures and AP better
1815                          * do the same.
1816                          */
1817                         wpa_sm_key_request(wpa_s->wpa, 1,
1818                                            wpa_s->pending_mic_error_pairwise);
1819                 }
1820
1821                 /* Send the new MIC error report immediately since we are going
1822                  * to start countermeasures and AP better do the same.
1823                  */
1824                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1825
1826                 /* initialize countermeasures */
1827                 wpa_s->countermeasures = 1;
1828
1829                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1830
1831                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1832
1833                 /*
1834                  * Need to wait for completion of request frame. We do not get
1835                  * any callback for the message completion, so just wait a
1836                  * short while and hope for the best. */
1837                 os_sleep(0, 10000);
1838
1839                 wpa_drv_set_countermeasures(wpa_s, 1);
1840                 wpa_supplicant_deauthenticate(wpa_s,
1841                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1842                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1843                                      wpa_s, NULL);
1844                 eloop_register_timeout(60, 0,
1845                                        wpa_supplicant_stop_countermeasures,
1846                                        wpa_s, NULL);
1847                 /* TODO: mark the AP rejected for 60 second. STA is
1848                  * allowed to associate with another AP.. */
1849         } else {
1850 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1851                 if (wpa_s->mic_errors_seen) {
1852                         /*
1853                          * Reduce the effectiveness of Michael MIC error
1854                          * reports as a means for attacking against TKIP if
1855                          * more than one MIC failure is noticed with the same
1856                          * PTK. We delay the transmission of the reports by a
1857                          * random time between 0 and 60 seconds in order to
1858                          * force the attacker wait 60 seconds before getting
1859                          * the information on whether a frame resulted in a MIC
1860                          * failure.
1861                          */
1862                         u8 rval[4];
1863                         int sec;
1864
1865                         if (os_get_random(rval, sizeof(rval)) < 0)
1866                                 sec = os_random() % 60;
1867                         else
1868                                 sec = WPA_GET_BE32(rval) % 60;
1869                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1870                                 "report %d seconds", sec);
1871                         wpa_s->pending_mic_error_report = 1;
1872                         wpa_s->pending_mic_error_pairwise = pairwise;
1873                         eloop_cancel_timeout(
1874                                 wpa_supplicant_delayed_mic_error_report,
1875                                 wpa_s, NULL);
1876                         eloop_register_timeout(
1877                                 sec, os_random() % 1000000,
1878                                 wpa_supplicant_delayed_mic_error_report,
1879                                 wpa_s, NULL);
1880                 } else {
1881                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1882                 }
1883 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1884                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1885 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1886         }
1887         wpa_s->last_michael_mic_error = t.sec;
1888         wpa_s->mic_errors_seen++;
1889 }
1890
1891
1892 #ifdef CONFIG_TERMINATE_ONLASTIF
1893 static int any_interfaces(struct wpa_supplicant *head)
1894 {
1895         struct wpa_supplicant *wpa_s;
1896
1897         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1898                 if (!wpa_s->interface_removed)
1899                         return 1;
1900         return 0;
1901 }
1902 #endif /* CONFIG_TERMINATE_ONLASTIF */
1903
1904
1905 static void
1906 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1907                                       union wpa_event_data *data)
1908 {
1909         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1910                 return;
1911
1912         switch (data->interface_status.ievent) {
1913         case EVENT_INTERFACE_ADDED:
1914                 if (!wpa_s->interface_removed)
1915                         break;
1916                 wpa_s->interface_removed = 0;
1917                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1918                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1919                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1920                                 "driver after interface was added");
1921                 }
1922                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1923                 break;
1924         case EVENT_INTERFACE_REMOVED:
1925                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1926                 wpa_s->interface_removed = 1;
1927                 wpa_supplicant_mark_disassoc(wpa_s);
1928                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1929                 l2_packet_deinit(wpa_s->l2);
1930                 wpa_s->l2 = NULL;
1931 #ifdef CONFIG_IBSS_RSN
1932                 ibss_rsn_deinit(wpa_s->ibss_rsn);
1933                 wpa_s->ibss_rsn = NULL;
1934 #endif /* CONFIG_IBSS_RSN */
1935 #ifdef CONFIG_TERMINATE_ONLASTIF
1936                 /* check if last interface */
1937                 if (!any_interfaces(wpa_s->global->ifaces))
1938                         eloop_terminate();
1939 #endif /* CONFIG_TERMINATE_ONLASTIF */
1940                 break;
1941         }
1942 }
1943
1944
1945 #ifdef CONFIG_PEERKEY
1946 static void
1947 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1948                               union wpa_event_data *data)
1949 {
1950         if (data == NULL)
1951                 return;
1952         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1953 }
1954 #endif /* CONFIG_PEERKEY */
1955
1956
1957 #ifdef CONFIG_TDLS
1958 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1959                                       union wpa_event_data *data)
1960 {
1961         if (data == NULL)
1962                 return;
1963         switch (data->tdls.oper) {
1964         case TDLS_REQUEST_SETUP:
1965                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1966                 break;
1967         case TDLS_REQUEST_TEARDOWN:
1968                 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
1969                                        data->tdls.reason_code);
1970                 break;
1971         }
1972 }
1973 #endif /* CONFIG_TDLS */
1974
1975
1976 #ifdef CONFIG_IEEE80211R
1977 static void
1978 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1979                                  union wpa_event_data *data)
1980 {
1981         if (data == NULL)
1982                 return;
1983
1984         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1985                                     data->ft_ies.ies_len,
1986                                     data->ft_ies.ft_action,
1987                                     data->ft_ies.target_ap,
1988                                     data->ft_ies.ric_ies,
1989                                     data->ft_ies.ric_ies_len) < 0) {
1990                 /* TODO: prevent MLME/driver from trying to associate? */
1991         }
1992 }
1993 #endif /* CONFIG_IEEE80211R */
1994
1995
1996 #ifdef CONFIG_IBSS_RSN
1997 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1998                                                 union wpa_event_data *data)
1999 {
2000         struct wpa_ssid *ssid;
2001         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2002                 return;
2003         if (data == NULL)
2004                 return;
2005         ssid = wpa_s->current_ssid;
2006         if (ssid == NULL)
2007                 return;
2008         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2009                 return;
2010
2011         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2012 }
2013 #endif /* CONFIG_IBSS_RSN */
2014
2015
2016 #ifdef CONFIG_IEEE80211R
2017 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2018                          size_t len)
2019 {
2020         const u8 *sta_addr, *target_ap_addr;
2021         u16 status;
2022
2023         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2024         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2025                 return; /* only SME case supported for now */
2026         if (len < 1 + 2 * ETH_ALEN + 2)
2027                 return;
2028         if (data[0] != 2)
2029                 return; /* Only FT Action Response is supported for now */
2030         sta_addr = data + 1;
2031         target_ap_addr = data + 1 + ETH_ALEN;
2032         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2033         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2034                 MACSTR " TargetAP " MACSTR " status %u",
2035                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2036
2037         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2038                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2039                         " in FT Action Response", MAC2STR(sta_addr));
2040                 return;
2041         }
2042
2043         if (status) {
2044                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2045                         "failure (status code %d)", status);
2046                 /* TODO: report error to FT code(?) */
2047                 return;
2048         }
2049
2050         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2051                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2052                                     target_ap_addr, NULL, 0) < 0)
2053                 return;
2054
2055 #ifdef CONFIG_SME
2056         {
2057                 struct wpa_bss *bss;
2058                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2059                 if (bss)
2060                         wpa_s->sme.freq = bss->freq;
2061                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2062                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2063                               WLAN_AUTH_FT);
2064         }
2065 #endif /* CONFIG_SME */
2066 }
2067 #endif /* CONFIG_IEEE80211R */
2068
2069
2070 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2071                                                struct unprot_deauth *e)
2072 {
2073 #ifdef CONFIG_IEEE80211W
2074         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2075                    "dropped: " MACSTR " -> " MACSTR
2076                    " (reason code %u)",
2077                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2078         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2079 #endif /* CONFIG_IEEE80211W */
2080 }
2081
2082
2083 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2084                                                  struct unprot_disassoc *e)
2085 {
2086 #ifdef CONFIG_IEEE80211W
2087         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2088                    "dropped: " MACSTR " -> " MACSTR
2089                    " (reason code %u)",
2090                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2091         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2092 #endif /* CONFIG_IEEE80211W */
2093 }
2094
2095
2096 static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2097 {
2098         u8 action, mode;
2099         const u8 *pos, *end;
2100
2101         if (rx->data == NULL || rx->len == 0)
2102                 return;
2103
2104         pos = rx->data;
2105         end = pos + rx->len;
2106         action = *pos++;
2107
2108         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2109                    action, MAC2STR(rx->sa));
2110         switch (action) {
2111         case WNM_BSS_TRANS_MGMT_REQ:
2112                 if (pos + 5 > end)
2113                         break;
2114                 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2115                            "Request: dialog_token=%u request_mode=0x%x "
2116                            "disassoc_timer=%u validity_interval=%u",
2117                            pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2118                 mode = pos[1];
2119                 pos += 5;
2120                 if (mode & 0x08)
2121                         pos += 12; /* BSS Termination Duration */
2122                 if (mode & 0x10) {
2123                         char url[256];
2124                         if (pos + 1 > end || pos + 1 + pos[0] > end) {
2125                                 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2126                                            "Transition Management Request "
2127                                            "(URL)");
2128                                 break;
2129                         }
2130                         os_memcpy(url, pos + 1, pos[0]);
2131                         url[pos[0]] = '\0';
2132                         wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2133                                 "Imminent - session_info_url=%s", url);
2134                 }
2135                 break;
2136         }
2137 }
2138
2139
2140 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2141                           union wpa_event_data *data)
2142 {
2143         struct wpa_supplicant *wpa_s = ctx;
2144         u16 reason_code = 0;
2145         int locally_generated = 0;
2146
2147         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2148             event != EVENT_INTERFACE_ENABLED &&
2149             event != EVENT_INTERFACE_STATUS &&
2150             event != EVENT_SCHED_SCAN_STOPPED) {
2151                 wpa_dbg(wpa_s, MSG_DEBUG,
2152                         "Ignore event %s (%d) while interface is disabled",
2153                         event_to_string(event), event);
2154                 return;
2155         }
2156
2157 #ifndef CONFIG_NO_STDOUT_DEBUG
2158 {
2159         int level = MSG_DEBUG;
2160
2161         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2162                 const struct ieee80211_hdr *hdr;
2163                 u16 fc;
2164                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2165                 fc = le_to_host16(hdr->frame_control);
2166                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2167                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2168                         level = MSG_EXCESSIVE;
2169         }
2170
2171         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2172                 event_to_string(event), event);
2173 }
2174 #endif /* CONFIG_NO_STDOUT_DEBUG */
2175
2176         switch (event) {
2177         case EVENT_AUTH:
2178                 sme_event_auth(wpa_s, data);
2179                 break;
2180         case EVENT_ASSOC:
2181                 wpa_supplicant_event_assoc(wpa_s, data);
2182                 break;
2183         case EVENT_DISASSOC:
2184                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2185                 if (data) {
2186                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2187                                 data->disassoc_info.reason_code,
2188                                 data->disassoc_info.locally_generated ?
2189                                 " (locally generated)" : "");
2190                         if (data->disassoc_info.addr)
2191                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2192                                         MAC2STR(data->disassoc_info.addr));
2193                 }
2194 #ifdef CONFIG_AP
2195                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2196                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2197                                                data->disassoc_info.addr);
2198                         break;
2199                 }
2200                 if (wpa_s->ap_iface) {
2201                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2202                                 "AP mode");
2203                         break;
2204                 }
2205 #endif /* CONFIG_AP */
2206                 if (data) {
2207                         reason_code = data->disassoc_info.reason_code;
2208                         locally_generated =
2209                                 data->disassoc_info.locally_generated;
2210                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2211                                     data->disassoc_info.ie,
2212                                     data->disassoc_info.ie_len);
2213 #ifdef CONFIG_P2P
2214                         wpas_p2p_disassoc_notif(
2215                                 wpa_s, data->disassoc_info.addr, reason_code,
2216                                 data->disassoc_info.ie,
2217                                 data->disassoc_info.ie_len,
2218                                 locally_generated);
2219 #endif /* CONFIG_P2P */
2220                 }
2221                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2222                         sme_event_disassoc(wpa_s, data);
2223                 /* fall through */
2224         case EVENT_DEAUTH:
2225                 if (event == EVENT_DEAUTH) {
2226                         wpa_dbg(wpa_s, MSG_DEBUG,
2227                                 "Deauthentication notification");
2228                         if (data) {
2229                                 reason_code = data->deauth_info.reason_code;
2230                                 locally_generated =
2231                                         data->deauth_info.locally_generated;
2232                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2233                                         data->deauth_info.reason_code,
2234                                         data->deauth_info.locally_generated ?
2235                                         " (locally generated)" : "");
2236                                 if (data->deauth_info.addr) {
2237                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2238                                                 MACSTR,
2239                                                 MAC2STR(data->deauth_info.
2240                                                         addr));
2241                                 }
2242                                 wpa_hexdump(MSG_DEBUG,
2243                                             "Deauthentication frame IE(s)",
2244                                             data->deauth_info.ie,
2245                                             data->deauth_info.ie_len);
2246                         }
2247                 }
2248 #ifdef CONFIG_AP
2249                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2250                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2251                                                data->deauth_info.addr);
2252                         break;
2253                 }
2254                 if (wpa_s->ap_iface) {
2255                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2256                                 "AP mode");
2257                         break;
2258                 }
2259 #endif /* CONFIG_AP */
2260                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2261                                               locally_generated);
2262 #ifdef CONFIG_P2P
2263                 if (event == EVENT_DEAUTH && data) {
2264                         wpas_p2p_deauth_notif(wpa_s, data->deauth_info.addr,
2265                                               reason_code,
2266                                               data->deauth_info.ie,
2267                                               data->deauth_info.ie_len,
2268                                               locally_generated);
2269                 }
2270 #endif /* CONFIG_P2P */
2271                 break;
2272         case EVENT_MICHAEL_MIC_FAILURE:
2273                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2274                 break;
2275 #ifndef CONFIG_NO_SCAN_PROCESSING
2276         case EVENT_SCAN_RESULTS:
2277                 wpa_supplicant_event_scan_results(wpa_s, data);
2278                 break;
2279 #endif /* CONFIG_NO_SCAN_PROCESSING */
2280         case EVENT_ASSOCINFO:
2281                 wpa_supplicant_event_associnfo(wpa_s, data);
2282                 break;
2283         case EVENT_INTERFACE_STATUS:
2284                 wpa_supplicant_event_interface_status(wpa_s, data);
2285                 break;
2286         case EVENT_PMKID_CANDIDATE:
2287                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2288                 break;
2289 #ifdef CONFIG_PEERKEY
2290         case EVENT_STKSTART:
2291                 wpa_supplicant_event_stkstart(wpa_s, data);
2292                 break;
2293 #endif /* CONFIG_PEERKEY */
2294 #ifdef CONFIG_TDLS
2295         case EVENT_TDLS:
2296                 wpa_supplicant_event_tdls(wpa_s, data);
2297                 break;
2298 #endif /* CONFIG_TDLS */
2299 #ifdef CONFIG_IEEE80211R
2300         case EVENT_FT_RESPONSE:
2301                 wpa_supplicant_event_ft_response(wpa_s, data);
2302                 break;
2303 #endif /* CONFIG_IEEE80211R */
2304 #ifdef CONFIG_IBSS_RSN
2305         case EVENT_IBSS_RSN_START:
2306                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2307                 break;
2308 #endif /* CONFIG_IBSS_RSN */
2309         case EVENT_ASSOC_REJECT:
2310                 if (data->assoc_reject.bssid)
2311                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2312                                 "bssid=" MACSTR " status_code=%u",
2313                                 MAC2STR(data->assoc_reject.bssid),
2314                                 data->assoc_reject.status_code);
2315                 else
2316                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2317                                 "status_code=%u",
2318                                 data->assoc_reject.status_code);
2319                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2320                         sme_event_assoc_reject(wpa_s, data);
2321                 break;
2322         case EVENT_AUTH_TIMED_OUT:
2323                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2324                         sme_event_auth_timed_out(wpa_s, data);
2325                 break;
2326         case EVENT_ASSOC_TIMED_OUT:
2327                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2328                         sme_event_assoc_timed_out(wpa_s, data);
2329                 break;
2330         case EVENT_TX_STATUS:
2331                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2332                         " type=%d stype=%d",
2333                         MAC2STR(data->tx_status.dst),
2334                         data->tx_status.type, data->tx_status.stype);
2335 #ifdef CONFIG_AP
2336                 if (wpa_s->ap_iface == NULL) {
2337 #ifdef CONFIG_OFFCHANNEL
2338                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2339                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2340                                 offchannel_send_action_tx_status(
2341                                         wpa_s, data->tx_status.dst,
2342                                         data->tx_status.data,
2343                                         data->tx_status.data_len,
2344                                         data->tx_status.ack ?
2345                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2346                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2347 #endif /* CONFIG_OFFCHANNEL */
2348                         break;
2349                 }
2350 #endif /* CONFIG_AP */
2351 #ifdef CONFIG_OFFCHANNEL
2352                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2353                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2354                 /*
2355                  * Catch TX status events for Action frames we sent via group
2356                  * interface in GO mode.
2357                  */
2358                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2359                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2360                     os_memcmp(wpa_s->parent->pending_action_dst,
2361                               data->tx_status.dst, ETH_ALEN) == 0) {
2362                         offchannel_send_action_tx_status(
2363                                 wpa_s->parent, data->tx_status.dst,
2364                                 data->tx_status.data,
2365                                 data->tx_status.data_len,
2366                                 data->tx_status.ack ?
2367                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2368                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2369                         break;
2370                 }
2371 #endif /* CONFIG_OFFCHANNEL */
2372 #ifdef CONFIG_AP
2373                 switch (data->tx_status.type) {
2374                 case WLAN_FC_TYPE_MGMT:
2375                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2376                                       data->tx_status.data_len,
2377                                       data->tx_status.stype,
2378                                       data->tx_status.ack);
2379                         break;
2380                 case WLAN_FC_TYPE_DATA:
2381                         ap_tx_status(wpa_s, data->tx_status.dst,
2382                                      data->tx_status.data,
2383                                      data->tx_status.data_len,
2384                                      data->tx_status.ack);
2385                         break;
2386                 }
2387 #endif /* CONFIG_AP */
2388                 break;
2389 #ifdef CONFIG_AP
2390         case EVENT_EAPOL_TX_STATUS:
2391                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2392                                    data->eapol_tx_status.data,
2393                                    data->eapol_tx_status.data_len,
2394                                    data->eapol_tx_status.ack);
2395                 break;
2396         case EVENT_DRIVER_CLIENT_POLL_OK:
2397                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2398                 break;
2399         case EVENT_RX_FROM_UNKNOWN:
2400                 if (wpa_s->ap_iface == NULL)
2401                         break;
2402                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2403                                        data->rx_from_unknown.wds);
2404                 break;
2405         case EVENT_RX_MGMT: {
2406                 u16 fc, stype;
2407                 const struct ieee80211_mgmt *mgmt;
2408
2409                 mgmt = (const struct ieee80211_mgmt *)
2410                         data->rx_mgmt.frame;
2411                 fc = le_to_host16(mgmt->frame_control);
2412                 stype = WLAN_FC_GET_STYPE(fc);
2413
2414                 if (wpa_s->ap_iface == NULL) {
2415 #ifdef CONFIG_P2P
2416                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2417                             data->rx_mgmt.frame_len > 24) {
2418                                 const u8 *src = mgmt->sa;
2419                                 const u8 *ie = mgmt->u.probe_req.variable;
2420                                 size_t ie_len = data->rx_mgmt.frame_len -
2421                                         (mgmt->u.probe_req.variable -
2422                                          data->rx_mgmt.frame);
2423                                 wpas_p2p_probe_req_rx(
2424                                         wpa_s, src, mgmt->da,
2425                                         mgmt->bssid, ie, ie_len,
2426                                         data->rx_mgmt.ssi_signal);
2427                                 break;
2428                         }
2429 #endif /* CONFIG_P2P */
2430                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2431                                 "management frame in non-AP mode");
2432                         break;
2433                 }
2434
2435                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2436                     data->rx_mgmt.frame_len > 24) {
2437                         const u8 *ie = mgmt->u.probe_req.variable;
2438                         size_t ie_len = data->rx_mgmt.frame_len -
2439                                 (mgmt->u.probe_req.variable -
2440                                  data->rx_mgmt.frame);
2441
2442                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2443                                          mgmt->bssid, ie, ie_len,
2444                                          data->rx_mgmt.ssi_signal);
2445                 }
2446
2447                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2448                 break;
2449                 }
2450 #endif /* CONFIG_AP */
2451         case EVENT_RX_ACTION:
2452                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2453                         " Category=%u DataLen=%d freq=%d MHz",
2454                         MAC2STR(data->rx_action.sa),
2455                         data->rx_action.category, (int) data->rx_action.len,
2456                         data->rx_action.freq);
2457 #ifdef CONFIG_IEEE80211R
2458                 if (data->rx_action.category == WLAN_ACTION_FT) {
2459                         ft_rx_action(wpa_s, data->rx_action.data,
2460                                      data->rx_action.len);
2461                         break;
2462                 }
2463 #endif /* CONFIG_IEEE80211R */
2464 #ifdef CONFIG_IEEE80211W
2465 #ifdef CONFIG_SME
2466                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2467                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2468                                         data->rx_action.data,
2469                                         data->rx_action.len);
2470                         break;
2471                 }
2472 #endif /* CONFIG_SME */
2473 #endif /* CONFIG_IEEE80211W */
2474 #ifdef CONFIG_GAS
2475                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2476                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2477                                  data->rx_action.sa, data->rx_action.bssid,
2478                                  data->rx_action.data, data->rx_action.len,
2479                                  data->rx_action.freq) == 0)
2480                         break;
2481 #endif /* CONFIG_GAS */
2482                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2483                         wnm_action_rx(wpa_s, &data->rx_action);
2484                         break;
2485                 }
2486 #ifdef CONFIG_TDLS
2487                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2488                     data->rx_action.len >= 4 &&
2489                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2490                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2491                                 "Response from " MACSTR,
2492                                 MAC2STR(data->rx_action.sa));
2493                         break;
2494                 }
2495 #endif /* CONFIG_TDLS */
2496 #ifdef CONFIG_P2P
2497                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2498                                    data->rx_action.sa,
2499                                    data->rx_action.bssid,
2500                                    data->rx_action.category,
2501                                    data->rx_action.data,
2502                                    data->rx_action.len, data->rx_action.freq);
2503 #endif /* CONFIG_P2P */
2504                 break;
2505         case EVENT_RX_PROBE_REQ:
2506                 if (data->rx_probe_req.sa == NULL ||
2507                     data->rx_probe_req.ie == NULL)
2508                         break;
2509 #ifdef CONFIG_AP
2510                 if (wpa_s->ap_iface) {
2511                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2512                                              data->rx_probe_req.sa,
2513                                              data->rx_probe_req.da,
2514                                              data->rx_probe_req.bssid,
2515                                              data->rx_probe_req.ie,
2516                                              data->rx_probe_req.ie_len,
2517                                              data->rx_probe_req.ssi_signal);
2518                         break;
2519                 }
2520 #endif /* CONFIG_AP */
2521 #ifdef CONFIG_P2P
2522                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2523                                       data->rx_probe_req.da,
2524                                       data->rx_probe_req.bssid,
2525                                       data->rx_probe_req.ie,
2526                                       data->rx_probe_req.ie_len,
2527                                       data->rx_probe_req.ssi_signal);
2528 #endif /* CONFIG_P2P */
2529                 break;
2530         case EVENT_REMAIN_ON_CHANNEL:
2531 #ifdef CONFIG_OFFCHANNEL
2532                 offchannel_remain_on_channel_cb(
2533                         wpa_s, data->remain_on_channel.freq,
2534                         data->remain_on_channel.duration);
2535 #endif /* CONFIG_OFFCHANNEL */
2536 #ifdef CONFIG_P2P
2537                 wpas_p2p_remain_on_channel_cb(
2538                         wpa_s, data->remain_on_channel.freq,
2539                         data->remain_on_channel.duration);
2540 #endif /* CONFIG_P2P */
2541                 break;
2542         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2543 #ifdef CONFIG_OFFCHANNEL
2544                 offchannel_cancel_remain_on_channel_cb(
2545                         wpa_s, data->remain_on_channel.freq);
2546 #endif /* CONFIG_OFFCHANNEL */
2547 #ifdef CONFIG_P2P
2548                 wpas_p2p_cancel_remain_on_channel_cb(
2549                         wpa_s, data->remain_on_channel.freq);
2550 #endif /* CONFIG_P2P */
2551                 break;
2552 #ifdef CONFIG_P2P
2553         case EVENT_P2P_DEV_FOUND: {
2554                 struct p2p_peer_info peer_info;
2555
2556                 os_memset(&peer_info, 0, sizeof(peer_info));
2557                 if (data->p2p_dev_found.dev_addr)
2558                         os_memcpy(peer_info.p2p_device_addr,
2559                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2560                 if (data->p2p_dev_found.pri_dev_type)
2561                         os_memcpy(peer_info.pri_dev_type,
2562                                   data->p2p_dev_found.pri_dev_type,
2563                                   sizeof(peer_info.pri_dev_type));
2564                 if (data->p2p_dev_found.dev_name)
2565                         os_strlcpy(peer_info.device_name,
2566                                    data->p2p_dev_found.dev_name,
2567                                    sizeof(peer_info.device_name));
2568                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2569                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2570                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2571
2572                 /*
2573                  * FIX: new_device=1 is not necessarily correct. We should
2574                  * maintain a P2P peer database in wpa_supplicant and update
2575                  * this information based on whether the peer is truly new.
2576                  */
2577                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2578                 break;
2579                 }
2580         case EVENT_P2P_GO_NEG_REQ_RX:
2581                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2582                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2583                 break;
2584         case EVENT_P2P_GO_NEG_COMPLETED:
2585                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2586                 break;
2587         case EVENT_P2P_PROV_DISC_REQUEST:
2588                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2589                                    data->p2p_prov_disc_req.config_methods,
2590                                    data->p2p_prov_disc_req.dev_addr,
2591                                    data->p2p_prov_disc_req.pri_dev_type,
2592                                    data->p2p_prov_disc_req.dev_name,
2593                                    data->p2p_prov_disc_req.supp_config_methods,
2594                                    data->p2p_prov_disc_req.dev_capab,
2595                                    data->p2p_prov_disc_req.group_capab,
2596                                    NULL, 0);
2597                 break;
2598         case EVENT_P2P_PROV_DISC_RESPONSE:
2599                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2600                                     data->p2p_prov_disc_resp.config_methods);
2601                 break;
2602         case EVENT_P2P_SD_REQUEST:
2603                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2604                                 data->p2p_sd_req.sa,
2605                                 data->p2p_sd_req.dialog_token,
2606                                 data->p2p_sd_req.update_indic,
2607                                 data->p2p_sd_req.tlvs,
2608                                 data->p2p_sd_req.tlvs_len);
2609                 break;
2610         case EVENT_P2P_SD_RESPONSE:
2611                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2612                                  data->p2p_sd_resp.update_indic,
2613                                  data->p2p_sd_resp.tlvs,
2614                                  data->p2p_sd_resp.tlvs_len);
2615                 break;
2616 #endif /* CONFIG_P2P */
2617         case EVENT_EAPOL_RX:
2618                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2619                                         data->eapol_rx.data,
2620                                         data->eapol_rx.data_len);
2621                 break;
2622         case EVENT_SIGNAL_CHANGE:
2623                 bgscan_notify_signal_change(
2624                         wpa_s, data->signal_change.above_threshold,
2625                         data->signal_change.current_signal,
2626                         data->signal_change.current_noise,
2627                         data->signal_change.current_txrate);
2628                 break;
2629         case EVENT_INTERFACE_ENABLED:
2630                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2631                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2632                         wpa_supplicant_update_mac_addr(wpa_s);
2633 #ifdef CONFIG_AP
2634                         if (!wpa_s->ap_iface) {
2635                                 wpa_supplicant_set_state(wpa_s,
2636                                                          WPA_DISCONNECTED);
2637                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2638                         } else
2639                                 wpa_supplicant_set_state(wpa_s,
2640                                                          WPA_COMPLETED);
2641 #else /* CONFIG_AP */
2642                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2643                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2644 #endif /* CONFIG_AP */
2645                 }
2646                 break;
2647         case EVENT_INTERFACE_DISABLED:
2648                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2649                 wpa_supplicant_mark_disassoc(wpa_s);
2650                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2651                 break;
2652         case EVENT_CHANNEL_LIST_CHANGED:
2653                 if (wpa_s->drv_priv == NULL)
2654                         break; /* Ignore event during drv initialization */
2655
2656                 free_hw_features(wpa_s);
2657                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2658                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2659
2660 #ifdef CONFIG_P2P
2661                 wpas_p2p_update_channel_list(wpa_s);
2662 #endif /* CONFIG_P2P */
2663                 break;
2664         case EVENT_INTERFACE_UNAVAILABLE:
2665 #ifdef CONFIG_P2P
2666                 wpas_p2p_interface_unavailable(wpa_s);
2667 #endif /* CONFIG_P2P */
2668                 break;
2669         case EVENT_BEST_CHANNEL:
2670                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2671                         "(%d %d %d)",
2672                         data->best_chan.freq_24, data->best_chan.freq_5,
2673                         data->best_chan.freq_overall);
2674                 wpa_s->best_24_freq = data->best_chan.freq_24;
2675                 wpa_s->best_5_freq = data->best_chan.freq_5;
2676                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2677 #ifdef CONFIG_P2P
2678                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2679                                               data->best_chan.freq_5,
2680                                               data->best_chan.freq_overall);
2681 #endif /* CONFIG_P2P */
2682                 break;
2683         case EVENT_UNPROT_DEAUTH:
2684                 wpa_supplicant_event_unprot_deauth(wpa_s,
2685                                                    &data->unprot_deauth);
2686                 break;
2687         case EVENT_UNPROT_DISASSOC:
2688                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2689                                                      &data->unprot_disassoc);
2690                 break;
2691         case EVENT_STATION_LOW_ACK:
2692 #ifdef CONFIG_AP
2693                 if (wpa_s->ap_iface && data)
2694                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2695                                                   data->low_ack.addr);
2696 #endif /* CONFIG_AP */
2697 #ifdef CONFIG_TDLS
2698                 if (data)
2699                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2700 #endif /* CONFIG_TDLS */
2701                 break;
2702         case EVENT_IBSS_PEER_LOST:
2703 #ifdef CONFIG_IBSS_RSN
2704                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2705 #endif /* CONFIG_IBSS_RSN */
2706                 break;
2707         case EVENT_DRIVER_GTK_REKEY:
2708                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2709                               wpa_s->bssid, ETH_ALEN))
2710                         break;
2711                 if (!wpa_s->wpa)
2712                         break;
2713                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2714                                          data->driver_gtk_rekey.replay_ctr);
2715                 break;
2716         case EVENT_SCHED_SCAN_STOPPED:
2717                 wpa_s->sched_scanning = 0;
2718                 wpa_supplicant_notify_scanning(wpa_s, 0);
2719
2720                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2721                         break;
2722
2723                 /*
2724                  * If we timed out, start a new sched scan to continue
2725                  * searching for more SSIDs.
2726                  */
2727                 if (wpa_s->sched_scan_timed_out)
2728                         wpa_supplicant_req_sched_scan(wpa_s);
2729                 break;
2730         case EVENT_WPS_BUTTON_PUSHED:
2731 #ifdef CONFIG_WPS
2732                 wpas_wps_start_pbc(wpa_s, NULL, 0);
2733 #endif /* CONFIG_WPS */
2734                 break;
2735         default:
2736                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2737                 break;
2738         }
2739 }