WNM: Send empty IPv4 packet as keep-alive for now
[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 to AP " MACSTR,
1215                    MAC2STR(wpa_s->bssid));
1216         /* TODO: could skip this if normal data traffic has been sent */
1217         /* TODO: Consider using some more appropriate data frame for this */
1218         if (wpa_s->l2)
1219                 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800, (u8 *) "", 0);
1220
1221 #ifdef CONFIG_SME
1222         if (wpa_s->sme.bss_max_idle_period) {
1223                 unsigned int msec;
1224                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1225                 if (msec > 100)
1226                         msec -= 100;
1227                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1228                                        wnm_bss_keep_alive, wpa_s, NULL);
1229         }
1230 #endif /* CONFIG_SME */
1231 }
1232
1233
1234 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1235                                    const u8 *ies, size_t ies_len)
1236 {
1237         struct ieee802_11_elems elems;
1238
1239         if (ies == NULL)
1240                 return;
1241
1242         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1243                 return;
1244
1245 #ifdef CONFIG_SME
1246         if (elems.bss_max_idle_period) {
1247                 unsigned int msec;
1248                 wpa_s->sme.bss_max_idle_period =
1249                         WPA_GET_LE16(elems.bss_max_idle_period);
1250                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1251                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1252                            (elems.bss_max_idle_period[2] & 0x01) ?
1253                            " (protected keep-live required)" : "");
1254                 if (wpa_s->sme.bss_max_idle_period == 0)
1255                         wpa_s->sme.bss_max_idle_period = 1;
1256                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1257                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1258                          /* msec times 1000 */
1259                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1260                         if (msec > 100)
1261                                 msec -= 100;
1262                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1263                                                wnm_bss_keep_alive, wpa_s,
1264                                                NULL);
1265                 }
1266         }
1267 #endif /* CONFIG_SME */
1268 }
1269
1270 #endif /* CONFIG_WNM */
1271
1272
1273 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1274 {
1275 #ifdef CONFIG_WNM
1276         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1277 #endif /* CONFIG_WNM */
1278 }
1279
1280
1281 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1282                                           union wpa_event_data *data)
1283 {
1284         int l, len, found = 0, wpa_found, rsn_found;
1285         const u8 *p;
1286
1287         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1288         if (data->assoc_info.req_ies)
1289                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1290                             data->assoc_info.req_ies_len);
1291         if (data->assoc_info.resp_ies) {
1292                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1293                             data->assoc_info.resp_ies_len);
1294 #ifdef CONFIG_TDLS
1295                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1296                                         data->assoc_info.resp_ies_len);
1297 #endif /* CONFIG_TDLS */
1298 #ifdef CONFIG_WNM
1299                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1300                                        data->assoc_info.resp_ies_len);
1301 #endif /* CONFIG_WNM */
1302         }
1303         if (data->assoc_info.beacon_ies)
1304                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1305                             data->assoc_info.beacon_ies,
1306                             data->assoc_info.beacon_ies_len);
1307         if (data->assoc_info.freq)
1308                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1309                         data->assoc_info.freq);
1310
1311         p = data->assoc_info.req_ies;
1312         l = data->assoc_info.req_ies_len;
1313
1314         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1315         while (p && l >= 2) {
1316                 len = p[1] + 2;
1317                 if (len > l) {
1318                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1319                                     p, l);
1320                         break;
1321                 }
1322                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1323                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1324                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1325                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1326                                 break;
1327                         found = 1;
1328                         wpa_find_assoc_pmkid(wpa_s);
1329                         break;
1330                 }
1331                 l -= len;
1332                 p += len;
1333         }
1334         if (!found && data->assoc_info.req_ies)
1335                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1336
1337 #ifdef CONFIG_IEEE80211R
1338 #ifdef CONFIG_SME
1339         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1340                 u8 bssid[ETH_ALEN];
1341                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1342                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1343                                                  data->assoc_info.resp_ies,
1344                                                  data->assoc_info.resp_ies_len,
1345                                                  bssid) < 0) {
1346                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1347                                 "Reassociation Response failed");
1348                         wpa_supplicant_deauthenticate(
1349                                 wpa_s, WLAN_REASON_INVALID_IE);
1350                         return -1;
1351                 }
1352         }
1353
1354         p = data->assoc_info.resp_ies;
1355         l = data->assoc_info.resp_ies_len;
1356
1357 #ifdef CONFIG_WPS_STRICT
1358         if (p && wpa_s->current_ssid &&
1359             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1360                 struct wpabuf *wps;
1361                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1362                 if (wps == NULL) {
1363                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1364                                 "include WPS IE in (Re)Association Response");
1365                         return -1;
1366                 }
1367
1368                 if (wps_validate_assoc_resp(wps) < 0) {
1369                         wpabuf_free(wps);
1370                         wpa_supplicant_deauthenticate(
1371                                 wpa_s, WLAN_REASON_INVALID_IE);
1372                         return -1;
1373                 }
1374                 wpabuf_free(wps);
1375         }
1376 #endif /* CONFIG_WPS_STRICT */
1377
1378         /* Go through the IEs and make a copy of the MDIE, if present. */
1379         while (p && l >= 2) {
1380                 len = p[1] + 2;
1381                 if (len > l) {
1382                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1383                                     p, l);
1384                         break;
1385                 }
1386                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1387                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1388                         wpa_s->sme.ft_used = 1;
1389                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1390                                   MOBILITY_DOMAIN_ID_LEN);
1391                         break;
1392                 }
1393                 l -= len;
1394                 p += len;
1395         }
1396 #endif /* CONFIG_SME */
1397
1398         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1399                              data->assoc_info.resp_ies_len);
1400 #endif /* CONFIG_IEEE80211R */
1401
1402         /* WPA/RSN IE from Beacon/ProbeResp */
1403         p = data->assoc_info.beacon_ies;
1404         l = data->assoc_info.beacon_ies_len;
1405
1406         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1407          */
1408         wpa_found = rsn_found = 0;
1409         while (p && l >= 2) {
1410                 len = p[1] + 2;
1411                 if (len > l) {
1412                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1413                                     p, l);
1414                         break;
1415                 }
1416                 if (!wpa_found &&
1417                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1418                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1419                         wpa_found = 1;
1420                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1421                 }
1422
1423                 if (!rsn_found &&
1424                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1425                         rsn_found = 1;
1426                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1427                 }
1428
1429                 l -= len;
1430                 p += len;
1431         }
1432
1433         if (!wpa_found && data->assoc_info.beacon_ies)
1434                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1435         if (!rsn_found && data->assoc_info.beacon_ies)
1436                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1437         if (wpa_found || rsn_found)
1438                 wpa_s->ap_ies_from_associnfo = 1;
1439
1440         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1441             wpa_s->assoc_freq != data->assoc_info.freq) {
1442                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1443                            "%u to %u MHz",
1444                            wpa_s->assoc_freq, data->assoc_info.freq);
1445                 wpa_supplicant_update_scan_results(wpa_s);
1446         }
1447
1448         wpa_s->assoc_freq = data->assoc_info.freq;
1449
1450         return 0;
1451 }
1452
1453
1454 static struct wpa_bss * wpa_supplicant_get_new_bss(
1455         struct wpa_supplicant *wpa_s, const u8 *bssid)
1456 {
1457         struct wpa_bss *bss = NULL;
1458         struct wpa_ssid *ssid = wpa_s->current_ssid;
1459
1460         if (ssid->ssid_len > 0)
1461                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1462         if (!bss)
1463                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1464
1465         return bss;
1466 }
1467
1468
1469 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1470 {
1471         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1472
1473         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1474                 return -1;
1475
1476         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1477                 return 0;
1478
1479         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1480                                         WPA_IE_VENDOR_TYPE);
1481         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1482
1483         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1484                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1485             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1486                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1487                 return -1;
1488
1489         return 0;
1490 }
1491
1492
1493 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1494                                        union wpa_event_data *data)
1495 {
1496         u8 bssid[ETH_ALEN];
1497         int ft_completed;
1498         int bssid_changed;
1499         struct wpa_driver_capa capa;
1500
1501 #ifdef CONFIG_AP
1502         if (wpa_s->ap_iface) {
1503                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1504                                     data->assoc_info.addr,
1505                                     data->assoc_info.req_ies,
1506                                     data->assoc_info.req_ies_len,
1507                                     data->assoc_info.reassoc);
1508                 return;
1509         }
1510 #endif /* CONFIG_AP */
1511
1512         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1513         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1514                 return;
1515
1516         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1517         if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1518             os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1519                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1520                         MACSTR, MAC2STR(bssid));
1521                 random_add_randomness(bssid, ETH_ALEN);
1522                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1523                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1524                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1525                 if (bssid_changed)
1526                         wpas_notify_bssid_changed(wpa_s);
1527
1528                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1529                         wpa_clear_keys(wpa_s, bssid);
1530                 }
1531                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1532                         wpa_supplicant_disassociate(
1533                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1534                         return;
1535                 }
1536                 if (wpa_s->current_ssid) {
1537                         struct wpa_bss *bss = NULL;
1538
1539                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1540                         if (!bss) {
1541                                 wpa_supplicant_update_scan_results(wpa_s);
1542
1543                                 /* Get the BSS from the new scan results */
1544                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1545                         }
1546
1547                         if (bss)
1548                                 wpa_s->current_bss = bss;
1549                 }
1550
1551                 if (wpa_s->conf->ap_scan == 1 &&
1552                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1553                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1554                                 wpa_msg(wpa_s, MSG_WARNING,
1555                                         "WPA/RSN IEs not updated");
1556                 }
1557         }
1558
1559 #ifdef CONFIG_SME
1560         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1561         wpa_s->sme.prev_bssid_set = 1;
1562 #endif /* CONFIG_SME */
1563
1564         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1565         if (wpa_s->current_ssid) {
1566                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1567                  * initialized before association, but for other modes,
1568                  * initialize PC/SC here, if the current configuration needs
1569                  * smartcard or SIM/USIM. */
1570                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1571         }
1572         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1573         if (wpa_s->l2)
1574                 l2_packet_notify_auth_start(wpa_s->l2);
1575
1576         /*
1577          * Set portEnabled first to FALSE in order to get EAP state machine out
1578          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1579          * state machine may transit to AUTHENTICATING state based on obsolete
1580          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1581          * AUTHENTICATED without ever giving chance to EAP state machine to
1582          * reset the state.
1583          */
1584         if (!ft_completed) {
1585                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1586                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1587         }
1588         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1589                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1590         /* 802.1X::portControl = Auto */
1591         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1592         wpa_s->eapol_received = 0;
1593         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1594             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1595             (wpa_s->current_ssid &&
1596              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1597                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1598                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1599         } else if (!ft_completed) {
1600                 /* Timeout for receiving the first EAPOL packet */
1601                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1602         }
1603         wpa_supplicant_cancel_scan(wpa_s);
1604
1605         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1606             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1607                 /*
1608                  * We are done; the driver will take care of RSN 4-way
1609                  * handshake.
1610                  */
1611                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1612                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1613                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1614                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1615         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1616                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1617                 /*
1618                  * The driver will take care of RSN 4-way handshake, so we need
1619                  * to allow EAPOL supplicant to complete its work without
1620                  * waiting for WPA supplicant.
1621                  */
1622                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1623         } else if (ft_completed) {
1624                 /*
1625                  * FT protocol completed - make sure EAPOL state machine ends
1626                  * up in authenticated.
1627                  */
1628                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1629                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1630                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1631                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1632         }
1633
1634         if (wpa_s->pending_eapol_rx) {
1635                 struct os_time now, age;
1636                 os_get_time(&now);
1637                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1638                 if (age.sec == 0 && age.usec < 100000 &&
1639                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1640                     0) {
1641                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1642                                 "frame that was received just before "
1643                                 "association notification");
1644                         wpa_supplicant_rx_eapol(
1645                                 wpa_s, wpa_s->pending_eapol_rx_src,
1646                                 wpabuf_head(wpa_s->pending_eapol_rx),
1647                                 wpabuf_len(wpa_s->pending_eapol_rx));
1648                 }
1649                 wpabuf_free(wpa_s->pending_eapol_rx);
1650                 wpa_s->pending_eapol_rx = NULL;
1651         }
1652
1653         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1654              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1655             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1656             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1657                 /* Set static WEP keys again */
1658                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1659         }
1660
1661 #ifdef CONFIG_IBSS_RSN
1662         if (wpa_s->current_ssid &&
1663             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1664             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1665             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1666             wpa_s->ibss_rsn == NULL) {
1667                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1668                 if (!wpa_s->ibss_rsn) {
1669                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1670                         wpa_supplicant_deauthenticate(
1671                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1672                         return;
1673                 }
1674
1675                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1676         }
1677 #endif /* CONFIG_IBSS_RSN */
1678 }
1679
1680
1681 static int disconnect_reason_recoverable(u16 reason_code)
1682 {
1683         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1684                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1685                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1686 }
1687
1688
1689 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1690                                           u16 reason_code,
1691                                           int locally_generated)
1692 {
1693         const u8 *bssid;
1694         int authenticating;
1695         u8 prev_pending_bssid[ETH_ALEN];
1696         struct wpa_bss *fast_reconnect = NULL;
1697         struct wpa_ssid *fast_reconnect_ssid = NULL;
1698
1699         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1700         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1701
1702         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1703                 /*
1704                  * At least Host AP driver and a Prism3 card seemed to be
1705                  * generating streams of disconnected events when configuring
1706                  * IBSS for WPA-None. Ignore them for now.
1707                  */
1708                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1709                         "IBSS/WPA-None mode");
1710                 return;
1711         }
1712
1713         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1714             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1715                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1716                         "pre-shared key may be incorrect");
1717         }
1718         if (!wpa_s->auto_reconnect_disabled ||
1719             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1720                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1721                         "reconnect (wps=%d wpa_state=%d)",
1722                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1723                         wpa_s->wpa_state);
1724                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1725                     wpa_s->current_ssid &&
1726                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1727                     !locally_generated &&
1728                     disconnect_reason_recoverable(reason_code)) {
1729                         /*
1730                          * It looks like the AP has dropped association with
1731                          * us, but could allow us to get back in. Try to
1732                          * reconnect to the same BSS without full scan to save
1733                          * time for some common cases.
1734                          */
1735                         fast_reconnect = wpa_s->current_bss;
1736                         fast_reconnect_ssid = wpa_s->current_ssid;
1737                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1738                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1739                 else
1740                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1741                                 "immediate scan");
1742         } else {
1743                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1744                         "try to re-connect");
1745                 wpa_s->reassociate = 0;
1746                 wpa_s->disconnected = 1;
1747                 wpa_supplicant_cancel_sched_scan(wpa_s);
1748         }
1749         bssid = wpa_s->bssid;
1750         if (is_zero_ether_addr(bssid))
1751                 bssid = wpa_s->pending_bssid;
1752         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1753                 wpas_connection_failed(wpa_s, bssid);
1754         wpa_sm_notify_disassoc(wpa_s->wpa);
1755         if (!is_zero_ether_addr(bssid) ||
1756             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1757                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1758                         " reason=%d%s",
1759                         MAC2STR(bssid), reason_code,
1760                         locally_generated ? " locally_generated=1" : "");
1761         }
1762         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1763                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1764                 wpa_s->keys_cleared = 0;
1765                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1766         }
1767         wpa_supplicant_mark_disassoc(wpa_s);
1768
1769         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1770                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1771
1772         if (fast_reconnect) {
1773 #ifndef CONFIG_NO_SCAN_PROCESSING
1774                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1775                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1776                                            fast_reconnect_ssid) < 0) {
1777                         /* Recover through full scan */
1778                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1779                 }
1780 #endif /* CONFIG_NO_SCAN_PROCESSING */
1781         }
1782 }
1783
1784
1785 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1786 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1787 {
1788         struct wpa_supplicant *wpa_s = eloop_ctx;
1789
1790         if (!wpa_s->pending_mic_error_report)
1791                 return;
1792
1793         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1794         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1795         wpa_s->pending_mic_error_report = 0;
1796 }
1797 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1798
1799
1800 static void
1801 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1802                                          union wpa_event_data *data)
1803 {
1804         int pairwise;
1805         struct os_time t;
1806
1807         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1808         pairwise = (data && data->michael_mic_failure.unicast);
1809         os_get_time(&t);
1810         if ((wpa_s->last_michael_mic_error &&
1811              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1812             wpa_s->pending_mic_error_report) {
1813                 if (wpa_s->pending_mic_error_report) {
1814                         /*
1815                          * Send the pending MIC error report immediately since
1816                          * we are going to start countermeasures and AP better
1817                          * do the same.
1818                          */
1819                         wpa_sm_key_request(wpa_s->wpa, 1,
1820                                            wpa_s->pending_mic_error_pairwise);
1821                 }
1822
1823                 /* Send the new MIC error report immediately since we are going
1824                  * to start countermeasures and AP better do the same.
1825                  */
1826                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1827
1828                 /* initialize countermeasures */
1829                 wpa_s->countermeasures = 1;
1830
1831                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1832
1833                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1834
1835                 /*
1836                  * Need to wait for completion of request frame. We do not get
1837                  * any callback for the message completion, so just wait a
1838                  * short while and hope for the best. */
1839                 os_sleep(0, 10000);
1840
1841                 wpa_drv_set_countermeasures(wpa_s, 1);
1842                 wpa_supplicant_deauthenticate(wpa_s,
1843                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1844                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1845                                      wpa_s, NULL);
1846                 eloop_register_timeout(60, 0,
1847                                        wpa_supplicant_stop_countermeasures,
1848                                        wpa_s, NULL);
1849                 /* TODO: mark the AP rejected for 60 second. STA is
1850                  * allowed to associate with another AP.. */
1851         } else {
1852 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1853                 if (wpa_s->mic_errors_seen) {
1854                         /*
1855                          * Reduce the effectiveness of Michael MIC error
1856                          * reports as a means for attacking against TKIP if
1857                          * more than one MIC failure is noticed with the same
1858                          * PTK. We delay the transmission of the reports by a
1859                          * random time between 0 and 60 seconds in order to
1860                          * force the attacker wait 60 seconds before getting
1861                          * the information on whether a frame resulted in a MIC
1862                          * failure.
1863                          */
1864                         u8 rval[4];
1865                         int sec;
1866
1867                         if (os_get_random(rval, sizeof(rval)) < 0)
1868                                 sec = os_random() % 60;
1869                         else
1870                                 sec = WPA_GET_BE32(rval) % 60;
1871                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1872                                 "report %d seconds", sec);
1873                         wpa_s->pending_mic_error_report = 1;
1874                         wpa_s->pending_mic_error_pairwise = pairwise;
1875                         eloop_cancel_timeout(
1876                                 wpa_supplicant_delayed_mic_error_report,
1877                                 wpa_s, NULL);
1878                         eloop_register_timeout(
1879                                 sec, os_random() % 1000000,
1880                                 wpa_supplicant_delayed_mic_error_report,
1881                                 wpa_s, NULL);
1882                 } else {
1883                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1884                 }
1885 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1886                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1887 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1888         }
1889         wpa_s->last_michael_mic_error = t.sec;
1890         wpa_s->mic_errors_seen++;
1891 }
1892
1893
1894 #ifdef CONFIG_TERMINATE_ONLASTIF
1895 static int any_interfaces(struct wpa_supplicant *head)
1896 {
1897         struct wpa_supplicant *wpa_s;
1898
1899         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1900                 if (!wpa_s->interface_removed)
1901                         return 1;
1902         return 0;
1903 }
1904 #endif /* CONFIG_TERMINATE_ONLASTIF */
1905
1906
1907 static void
1908 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1909                                       union wpa_event_data *data)
1910 {
1911         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1912                 return;
1913
1914         switch (data->interface_status.ievent) {
1915         case EVENT_INTERFACE_ADDED:
1916                 if (!wpa_s->interface_removed)
1917                         break;
1918                 wpa_s->interface_removed = 0;
1919                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1920                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1921                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1922                                 "driver after interface was added");
1923                 }
1924                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1925                 break;
1926         case EVENT_INTERFACE_REMOVED:
1927                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1928                 wpa_s->interface_removed = 1;
1929                 wpa_supplicant_mark_disassoc(wpa_s);
1930                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1931                 l2_packet_deinit(wpa_s->l2);
1932                 wpa_s->l2 = NULL;
1933 #ifdef CONFIG_IBSS_RSN
1934                 ibss_rsn_deinit(wpa_s->ibss_rsn);
1935                 wpa_s->ibss_rsn = NULL;
1936 #endif /* CONFIG_IBSS_RSN */
1937 #ifdef CONFIG_TERMINATE_ONLASTIF
1938                 /* check if last interface */
1939                 if (!any_interfaces(wpa_s->global->ifaces))
1940                         eloop_terminate();
1941 #endif /* CONFIG_TERMINATE_ONLASTIF */
1942                 break;
1943         }
1944 }
1945
1946
1947 #ifdef CONFIG_PEERKEY
1948 static void
1949 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1950                               union wpa_event_data *data)
1951 {
1952         if (data == NULL)
1953                 return;
1954         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1955 }
1956 #endif /* CONFIG_PEERKEY */
1957
1958
1959 #ifdef CONFIG_TDLS
1960 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
1961                                       union wpa_event_data *data)
1962 {
1963         if (data == NULL)
1964                 return;
1965         switch (data->tdls.oper) {
1966         case TDLS_REQUEST_SETUP:
1967                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
1968                 break;
1969         case TDLS_REQUEST_TEARDOWN:
1970                 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
1971                                        data->tdls.reason_code);
1972                 break;
1973         }
1974 }
1975 #endif /* CONFIG_TDLS */
1976
1977
1978 #ifdef CONFIG_IEEE80211R
1979 static void
1980 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1981                                  union wpa_event_data *data)
1982 {
1983         if (data == NULL)
1984                 return;
1985
1986         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1987                                     data->ft_ies.ies_len,
1988                                     data->ft_ies.ft_action,
1989                                     data->ft_ies.target_ap,
1990                                     data->ft_ies.ric_ies,
1991                                     data->ft_ies.ric_ies_len) < 0) {
1992                 /* TODO: prevent MLME/driver from trying to associate? */
1993         }
1994 }
1995 #endif /* CONFIG_IEEE80211R */
1996
1997
1998 #ifdef CONFIG_IBSS_RSN
1999 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2000                                                 union wpa_event_data *data)
2001 {
2002         struct wpa_ssid *ssid;
2003         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2004                 return;
2005         if (data == NULL)
2006                 return;
2007         ssid = wpa_s->current_ssid;
2008         if (ssid == NULL)
2009                 return;
2010         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2011                 return;
2012
2013         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2014 }
2015 #endif /* CONFIG_IBSS_RSN */
2016
2017
2018 #ifdef CONFIG_IEEE80211R
2019 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2020                          size_t len)
2021 {
2022         const u8 *sta_addr, *target_ap_addr;
2023         u16 status;
2024
2025         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2026         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2027                 return; /* only SME case supported for now */
2028         if (len < 1 + 2 * ETH_ALEN + 2)
2029                 return;
2030         if (data[0] != 2)
2031                 return; /* Only FT Action Response is supported for now */
2032         sta_addr = data + 1;
2033         target_ap_addr = data + 1 + ETH_ALEN;
2034         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2035         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2036                 MACSTR " TargetAP " MACSTR " status %u",
2037                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2038
2039         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2040                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2041                         " in FT Action Response", MAC2STR(sta_addr));
2042                 return;
2043         }
2044
2045         if (status) {
2046                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2047                         "failure (status code %d)", status);
2048                 /* TODO: report error to FT code(?) */
2049                 return;
2050         }
2051
2052         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2053                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2054                                     target_ap_addr, NULL, 0) < 0)
2055                 return;
2056
2057 #ifdef CONFIG_SME
2058         {
2059                 struct wpa_bss *bss;
2060                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2061                 if (bss)
2062                         wpa_s->sme.freq = bss->freq;
2063                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2064                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2065                               WLAN_AUTH_FT);
2066         }
2067 #endif /* CONFIG_SME */
2068 }
2069 #endif /* CONFIG_IEEE80211R */
2070
2071
2072 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2073                                                struct unprot_deauth *e)
2074 {
2075 #ifdef CONFIG_IEEE80211W
2076         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2077                    "dropped: " MACSTR " -> " MACSTR
2078                    " (reason code %u)",
2079                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2080         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2081 #endif /* CONFIG_IEEE80211W */
2082 }
2083
2084
2085 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2086                                                  struct unprot_disassoc *e)
2087 {
2088 #ifdef CONFIG_IEEE80211W
2089         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2090                    "dropped: " MACSTR " -> " MACSTR
2091                    " (reason code %u)",
2092                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2093         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2094 #endif /* CONFIG_IEEE80211W */
2095 }
2096
2097
2098 static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2099 {
2100         u8 action, mode;
2101         const u8 *pos, *end;
2102
2103         if (rx->data == NULL || rx->len == 0)
2104                 return;
2105
2106         pos = rx->data;
2107         end = pos + rx->len;
2108         action = *pos++;
2109
2110         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2111                    action, MAC2STR(rx->sa));
2112         switch (action) {
2113         case WNM_BSS_TRANS_MGMT_REQ:
2114                 if (pos + 5 > end)
2115                         break;
2116                 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2117                            "Request: dialog_token=%u request_mode=0x%x "
2118                            "disassoc_timer=%u validity_interval=%u",
2119                            pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2120                 mode = pos[1];
2121                 pos += 5;
2122                 if (mode & 0x08)
2123                         pos += 12; /* BSS Termination Duration */
2124                 if (mode & 0x10) {
2125                         char url[256];
2126                         if (pos + 1 > end || pos + 1 + pos[0] > end) {
2127                                 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2128                                            "Transition Management Request "
2129                                            "(URL)");
2130                                 break;
2131                         }
2132                         os_memcpy(url, pos + 1, pos[0]);
2133                         url[pos[0]] = '\0';
2134                         wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2135                                 "Imminent - session_info_url=%s", url);
2136                 }
2137                 break;
2138         }
2139 }
2140
2141
2142 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2143                           union wpa_event_data *data)
2144 {
2145         struct wpa_supplicant *wpa_s = ctx;
2146         u16 reason_code = 0;
2147         int locally_generated = 0;
2148
2149         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2150             event != EVENT_INTERFACE_ENABLED &&
2151             event != EVENT_INTERFACE_STATUS &&
2152             event != EVENT_SCHED_SCAN_STOPPED) {
2153                 wpa_dbg(wpa_s, MSG_DEBUG,
2154                         "Ignore event %s (%d) while interface is disabled",
2155                         event_to_string(event), event);
2156                 return;
2157         }
2158
2159 #ifndef CONFIG_NO_STDOUT_DEBUG
2160 {
2161         int level = MSG_DEBUG;
2162
2163         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2164                 const struct ieee80211_hdr *hdr;
2165                 u16 fc;
2166                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2167                 fc = le_to_host16(hdr->frame_control);
2168                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2169                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2170                         level = MSG_EXCESSIVE;
2171         }
2172
2173         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2174                 event_to_string(event), event);
2175 }
2176 #endif /* CONFIG_NO_STDOUT_DEBUG */
2177
2178         switch (event) {
2179         case EVENT_AUTH:
2180                 sme_event_auth(wpa_s, data);
2181                 break;
2182         case EVENT_ASSOC:
2183                 wpa_supplicant_event_assoc(wpa_s, data);
2184                 break;
2185         case EVENT_DISASSOC:
2186                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2187                 if (data) {
2188                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2189                                 data->disassoc_info.reason_code,
2190                                 data->disassoc_info.locally_generated ?
2191                                 " (locally generated)" : "");
2192                         if (data->disassoc_info.addr)
2193                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2194                                         MAC2STR(data->disassoc_info.addr));
2195                 }
2196 #ifdef CONFIG_AP
2197                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2198                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2199                                                data->disassoc_info.addr);
2200                         break;
2201                 }
2202                 if (wpa_s->ap_iface) {
2203                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2204                                 "AP mode");
2205                         break;
2206                 }
2207 #endif /* CONFIG_AP */
2208                 if (data) {
2209                         reason_code = data->disassoc_info.reason_code;
2210                         locally_generated =
2211                                 data->disassoc_info.locally_generated;
2212                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2213                                     data->disassoc_info.ie,
2214                                     data->disassoc_info.ie_len);
2215 #ifdef CONFIG_P2P
2216                         wpas_p2p_disassoc_notif(
2217                                 wpa_s, data->disassoc_info.addr, reason_code,
2218                                 data->disassoc_info.ie,
2219                                 data->disassoc_info.ie_len,
2220                                 locally_generated);
2221 #endif /* CONFIG_P2P */
2222                 }
2223                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2224                         sme_event_disassoc(wpa_s, data);
2225                 /* fall through */
2226         case EVENT_DEAUTH:
2227                 if (event == EVENT_DEAUTH) {
2228                         wpa_dbg(wpa_s, MSG_DEBUG,
2229                                 "Deauthentication notification");
2230                         if (data) {
2231                                 reason_code = data->deauth_info.reason_code;
2232                                 locally_generated =
2233                                         data->deauth_info.locally_generated;
2234                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2235                                         data->deauth_info.reason_code,
2236                                         data->deauth_info.locally_generated ?
2237                                         " (locally generated)" : "");
2238                                 if (data->deauth_info.addr) {
2239                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2240                                                 MACSTR,
2241                                                 MAC2STR(data->deauth_info.
2242                                                         addr));
2243                                 }
2244                                 wpa_hexdump(MSG_DEBUG,
2245                                             "Deauthentication frame IE(s)",
2246                                             data->deauth_info.ie,
2247                                             data->deauth_info.ie_len);
2248                         }
2249                 }
2250 #ifdef CONFIG_AP
2251                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2252                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2253                                                data->deauth_info.addr);
2254                         break;
2255                 }
2256                 if (wpa_s->ap_iface) {
2257                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2258                                 "AP mode");
2259                         break;
2260                 }
2261 #endif /* CONFIG_AP */
2262                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2263                                               locally_generated);
2264 #ifdef CONFIG_P2P
2265                 if (event == EVENT_DEAUTH && data) {
2266                         wpas_p2p_deauth_notif(wpa_s, data->deauth_info.addr,
2267                                               reason_code,
2268                                               data->deauth_info.ie,
2269                                               data->deauth_info.ie_len,
2270                                               locally_generated);
2271                 }
2272 #endif /* CONFIG_P2P */
2273                 break;
2274         case EVENT_MICHAEL_MIC_FAILURE:
2275                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2276                 break;
2277 #ifndef CONFIG_NO_SCAN_PROCESSING
2278         case EVENT_SCAN_RESULTS:
2279                 wpa_supplicant_event_scan_results(wpa_s, data);
2280                 break;
2281 #endif /* CONFIG_NO_SCAN_PROCESSING */
2282         case EVENT_ASSOCINFO:
2283                 wpa_supplicant_event_associnfo(wpa_s, data);
2284                 break;
2285         case EVENT_INTERFACE_STATUS:
2286                 wpa_supplicant_event_interface_status(wpa_s, data);
2287                 break;
2288         case EVENT_PMKID_CANDIDATE:
2289                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2290                 break;
2291 #ifdef CONFIG_PEERKEY
2292         case EVENT_STKSTART:
2293                 wpa_supplicant_event_stkstart(wpa_s, data);
2294                 break;
2295 #endif /* CONFIG_PEERKEY */
2296 #ifdef CONFIG_TDLS
2297         case EVENT_TDLS:
2298                 wpa_supplicant_event_tdls(wpa_s, data);
2299                 break;
2300 #endif /* CONFIG_TDLS */
2301 #ifdef CONFIG_IEEE80211R
2302         case EVENT_FT_RESPONSE:
2303                 wpa_supplicant_event_ft_response(wpa_s, data);
2304                 break;
2305 #endif /* CONFIG_IEEE80211R */
2306 #ifdef CONFIG_IBSS_RSN
2307         case EVENT_IBSS_RSN_START:
2308                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2309                 break;
2310 #endif /* CONFIG_IBSS_RSN */
2311         case EVENT_ASSOC_REJECT:
2312                 if (data->assoc_reject.bssid)
2313                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2314                                 "bssid=" MACSTR " status_code=%u",
2315                                 MAC2STR(data->assoc_reject.bssid),
2316                                 data->assoc_reject.status_code);
2317                 else
2318                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2319                                 "status_code=%u",
2320                                 data->assoc_reject.status_code);
2321                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2322                         sme_event_assoc_reject(wpa_s, data);
2323                 break;
2324         case EVENT_AUTH_TIMED_OUT:
2325                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2326                         sme_event_auth_timed_out(wpa_s, data);
2327                 break;
2328         case EVENT_ASSOC_TIMED_OUT:
2329                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2330                         sme_event_assoc_timed_out(wpa_s, data);
2331                 break;
2332         case EVENT_TX_STATUS:
2333                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2334                         " type=%d stype=%d",
2335                         MAC2STR(data->tx_status.dst),
2336                         data->tx_status.type, data->tx_status.stype);
2337 #ifdef CONFIG_AP
2338                 if (wpa_s->ap_iface == NULL) {
2339 #ifdef CONFIG_OFFCHANNEL
2340                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2341                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2342                                 offchannel_send_action_tx_status(
2343                                         wpa_s, data->tx_status.dst,
2344                                         data->tx_status.data,
2345                                         data->tx_status.data_len,
2346                                         data->tx_status.ack ?
2347                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2348                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2349 #endif /* CONFIG_OFFCHANNEL */
2350                         break;
2351                 }
2352 #endif /* CONFIG_AP */
2353 #ifdef CONFIG_OFFCHANNEL
2354                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2355                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2356                 /*
2357                  * Catch TX status events for Action frames we sent via group
2358                  * interface in GO mode.
2359                  */
2360                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2361                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2362                     os_memcmp(wpa_s->parent->pending_action_dst,
2363                               data->tx_status.dst, ETH_ALEN) == 0) {
2364                         offchannel_send_action_tx_status(
2365                                 wpa_s->parent, data->tx_status.dst,
2366                                 data->tx_status.data,
2367                                 data->tx_status.data_len,
2368                                 data->tx_status.ack ?
2369                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2370                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2371                         break;
2372                 }
2373 #endif /* CONFIG_OFFCHANNEL */
2374 #ifdef CONFIG_AP
2375                 switch (data->tx_status.type) {
2376                 case WLAN_FC_TYPE_MGMT:
2377                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2378                                       data->tx_status.data_len,
2379                                       data->tx_status.stype,
2380                                       data->tx_status.ack);
2381                         break;
2382                 case WLAN_FC_TYPE_DATA:
2383                         ap_tx_status(wpa_s, data->tx_status.dst,
2384                                      data->tx_status.data,
2385                                      data->tx_status.data_len,
2386                                      data->tx_status.ack);
2387                         break;
2388                 }
2389 #endif /* CONFIG_AP */
2390                 break;
2391 #ifdef CONFIG_AP
2392         case EVENT_EAPOL_TX_STATUS:
2393                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2394                                    data->eapol_tx_status.data,
2395                                    data->eapol_tx_status.data_len,
2396                                    data->eapol_tx_status.ack);
2397                 break;
2398         case EVENT_DRIVER_CLIENT_POLL_OK:
2399                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2400                 break;
2401         case EVENT_RX_FROM_UNKNOWN:
2402                 if (wpa_s->ap_iface == NULL)
2403                         break;
2404                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2405                                        data->rx_from_unknown.wds);
2406                 break;
2407         case EVENT_RX_MGMT: {
2408                 u16 fc, stype;
2409                 const struct ieee80211_mgmt *mgmt;
2410
2411                 mgmt = (const struct ieee80211_mgmt *)
2412                         data->rx_mgmt.frame;
2413                 fc = le_to_host16(mgmt->frame_control);
2414                 stype = WLAN_FC_GET_STYPE(fc);
2415
2416                 if (wpa_s->ap_iface == NULL) {
2417 #ifdef CONFIG_P2P
2418                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2419                             data->rx_mgmt.frame_len > 24) {
2420                                 const u8 *src = mgmt->sa;
2421                                 const u8 *ie = mgmt->u.probe_req.variable;
2422                                 size_t ie_len = data->rx_mgmt.frame_len -
2423                                         (mgmt->u.probe_req.variable -
2424                                          data->rx_mgmt.frame);
2425                                 wpas_p2p_probe_req_rx(
2426                                         wpa_s, src, mgmt->da,
2427                                         mgmt->bssid, ie, ie_len,
2428                                         data->rx_mgmt.ssi_signal);
2429                                 break;
2430                         }
2431 #endif /* CONFIG_P2P */
2432                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2433                                 "management frame in non-AP mode");
2434                         break;
2435                 }
2436
2437                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2438                     data->rx_mgmt.frame_len > 24) {
2439                         const u8 *ie = mgmt->u.probe_req.variable;
2440                         size_t ie_len = data->rx_mgmt.frame_len -
2441                                 (mgmt->u.probe_req.variable -
2442                                  data->rx_mgmt.frame);
2443
2444                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2445                                          mgmt->bssid, ie, ie_len,
2446                                          data->rx_mgmt.ssi_signal);
2447                 }
2448
2449                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2450                 break;
2451                 }
2452 #endif /* CONFIG_AP */
2453         case EVENT_RX_ACTION:
2454                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2455                         " Category=%u DataLen=%d freq=%d MHz",
2456                         MAC2STR(data->rx_action.sa),
2457                         data->rx_action.category, (int) data->rx_action.len,
2458                         data->rx_action.freq);
2459 #ifdef CONFIG_IEEE80211R
2460                 if (data->rx_action.category == WLAN_ACTION_FT) {
2461                         ft_rx_action(wpa_s, data->rx_action.data,
2462                                      data->rx_action.len);
2463                         break;
2464                 }
2465 #endif /* CONFIG_IEEE80211R */
2466 #ifdef CONFIG_IEEE80211W
2467 #ifdef CONFIG_SME
2468                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2469                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2470                                         data->rx_action.data,
2471                                         data->rx_action.len);
2472                         break;
2473                 }
2474 #endif /* CONFIG_SME */
2475 #endif /* CONFIG_IEEE80211W */
2476 #ifdef CONFIG_GAS
2477                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2478                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2479                                  data->rx_action.sa, data->rx_action.bssid,
2480                                  data->rx_action.data, data->rx_action.len,
2481                                  data->rx_action.freq) == 0)
2482                         break;
2483 #endif /* CONFIG_GAS */
2484                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2485                         wnm_action_rx(wpa_s, &data->rx_action);
2486                         break;
2487                 }
2488 #ifdef CONFIG_TDLS
2489                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2490                     data->rx_action.len >= 4 &&
2491                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2492                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2493                                 "Response from " MACSTR,
2494                                 MAC2STR(data->rx_action.sa));
2495                         break;
2496                 }
2497 #endif /* CONFIG_TDLS */
2498 #ifdef CONFIG_P2P
2499                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2500                                    data->rx_action.sa,
2501                                    data->rx_action.bssid,
2502                                    data->rx_action.category,
2503                                    data->rx_action.data,
2504                                    data->rx_action.len, data->rx_action.freq);
2505 #endif /* CONFIG_P2P */
2506                 break;
2507         case EVENT_RX_PROBE_REQ:
2508                 if (data->rx_probe_req.sa == NULL ||
2509                     data->rx_probe_req.ie == NULL)
2510                         break;
2511 #ifdef CONFIG_AP
2512                 if (wpa_s->ap_iface) {
2513                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2514                                              data->rx_probe_req.sa,
2515                                              data->rx_probe_req.da,
2516                                              data->rx_probe_req.bssid,
2517                                              data->rx_probe_req.ie,
2518                                              data->rx_probe_req.ie_len,
2519                                              data->rx_probe_req.ssi_signal);
2520                         break;
2521                 }
2522 #endif /* CONFIG_AP */
2523 #ifdef CONFIG_P2P
2524                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2525                                       data->rx_probe_req.da,
2526                                       data->rx_probe_req.bssid,
2527                                       data->rx_probe_req.ie,
2528                                       data->rx_probe_req.ie_len,
2529                                       data->rx_probe_req.ssi_signal);
2530 #endif /* CONFIG_P2P */
2531                 break;
2532         case EVENT_REMAIN_ON_CHANNEL:
2533 #ifdef CONFIG_OFFCHANNEL
2534                 offchannel_remain_on_channel_cb(
2535                         wpa_s, data->remain_on_channel.freq,
2536                         data->remain_on_channel.duration);
2537 #endif /* CONFIG_OFFCHANNEL */
2538 #ifdef CONFIG_P2P
2539                 wpas_p2p_remain_on_channel_cb(
2540                         wpa_s, data->remain_on_channel.freq,
2541                         data->remain_on_channel.duration);
2542 #endif /* CONFIG_P2P */
2543                 break;
2544         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2545 #ifdef CONFIG_OFFCHANNEL
2546                 offchannel_cancel_remain_on_channel_cb(
2547                         wpa_s, data->remain_on_channel.freq);
2548 #endif /* CONFIG_OFFCHANNEL */
2549 #ifdef CONFIG_P2P
2550                 wpas_p2p_cancel_remain_on_channel_cb(
2551                         wpa_s, data->remain_on_channel.freq);
2552 #endif /* CONFIG_P2P */
2553                 break;
2554 #ifdef CONFIG_P2P
2555         case EVENT_P2P_DEV_FOUND: {
2556                 struct p2p_peer_info peer_info;
2557
2558                 os_memset(&peer_info, 0, sizeof(peer_info));
2559                 if (data->p2p_dev_found.dev_addr)
2560                         os_memcpy(peer_info.p2p_device_addr,
2561                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2562                 if (data->p2p_dev_found.pri_dev_type)
2563                         os_memcpy(peer_info.pri_dev_type,
2564                                   data->p2p_dev_found.pri_dev_type,
2565                                   sizeof(peer_info.pri_dev_type));
2566                 if (data->p2p_dev_found.dev_name)
2567                         os_strlcpy(peer_info.device_name,
2568                                    data->p2p_dev_found.dev_name,
2569                                    sizeof(peer_info.device_name));
2570                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2571                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2572                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2573
2574                 /*
2575                  * FIX: new_device=1 is not necessarily correct. We should
2576                  * maintain a P2P peer database in wpa_supplicant and update
2577                  * this information based on whether the peer is truly new.
2578                  */
2579                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2580                 break;
2581                 }
2582         case EVENT_P2P_GO_NEG_REQ_RX:
2583                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2584                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2585                 break;
2586         case EVENT_P2P_GO_NEG_COMPLETED:
2587                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2588                 break;
2589         case EVENT_P2P_PROV_DISC_REQUEST:
2590                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2591                                    data->p2p_prov_disc_req.config_methods,
2592                                    data->p2p_prov_disc_req.dev_addr,
2593                                    data->p2p_prov_disc_req.pri_dev_type,
2594                                    data->p2p_prov_disc_req.dev_name,
2595                                    data->p2p_prov_disc_req.supp_config_methods,
2596                                    data->p2p_prov_disc_req.dev_capab,
2597                                    data->p2p_prov_disc_req.group_capab,
2598                                    NULL, 0);
2599                 break;
2600         case EVENT_P2P_PROV_DISC_RESPONSE:
2601                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2602                                     data->p2p_prov_disc_resp.config_methods);
2603                 break;
2604         case EVENT_P2P_SD_REQUEST:
2605                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2606                                 data->p2p_sd_req.sa,
2607                                 data->p2p_sd_req.dialog_token,
2608                                 data->p2p_sd_req.update_indic,
2609                                 data->p2p_sd_req.tlvs,
2610                                 data->p2p_sd_req.tlvs_len);
2611                 break;
2612         case EVENT_P2P_SD_RESPONSE:
2613                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2614                                  data->p2p_sd_resp.update_indic,
2615                                  data->p2p_sd_resp.tlvs,
2616                                  data->p2p_sd_resp.tlvs_len);
2617                 break;
2618 #endif /* CONFIG_P2P */
2619         case EVENT_EAPOL_RX:
2620                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2621                                         data->eapol_rx.data,
2622                                         data->eapol_rx.data_len);
2623                 break;
2624         case EVENT_SIGNAL_CHANGE:
2625                 bgscan_notify_signal_change(
2626                         wpa_s, data->signal_change.above_threshold,
2627                         data->signal_change.current_signal,
2628                         data->signal_change.current_noise,
2629                         data->signal_change.current_txrate);
2630                 break;
2631         case EVENT_INTERFACE_ENABLED:
2632                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2633                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2634                         wpa_supplicant_update_mac_addr(wpa_s);
2635 #ifdef CONFIG_AP
2636                         if (!wpa_s->ap_iface) {
2637                                 wpa_supplicant_set_state(wpa_s,
2638                                                          WPA_DISCONNECTED);
2639                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2640                         } else
2641                                 wpa_supplicant_set_state(wpa_s,
2642                                                          WPA_COMPLETED);
2643 #else /* CONFIG_AP */
2644                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2645                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2646 #endif /* CONFIG_AP */
2647                 }
2648                 break;
2649         case EVENT_INTERFACE_DISABLED:
2650                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2651                 wpa_supplicant_mark_disassoc(wpa_s);
2652                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2653                 break;
2654         case EVENT_CHANNEL_LIST_CHANGED:
2655                 if (wpa_s->drv_priv == NULL)
2656                         break; /* Ignore event during drv initialization */
2657
2658                 free_hw_features(wpa_s);
2659                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2660                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2661
2662 #ifdef CONFIG_P2P
2663                 wpas_p2p_update_channel_list(wpa_s);
2664 #endif /* CONFIG_P2P */
2665                 break;
2666         case EVENT_INTERFACE_UNAVAILABLE:
2667 #ifdef CONFIG_P2P
2668                 wpas_p2p_interface_unavailable(wpa_s);
2669 #endif /* CONFIG_P2P */
2670                 break;
2671         case EVENT_BEST_CHANNEL:
2672                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2673                         "(%d %d %d)",
2674                         data->best_chan.freq_24, data->best_chan.freq_5,
2675                         data->best_chan.freq_overall);
2676                 wpa_s->best_24_freq = data->best_chan.freq_24;
2677                 wpa_s->best_5_freq = data->best_chan.freq_5;
2678                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2679 #ifdef CONFIG_P2P
2680                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2681                                               data->best_chan.freq_5,
2682                                               data->best_chan.freq_overall);
2683 #endif /* CONFIG_P2P */
2684                 break;
2685         case EVENT_UNPROT_DEAUTH:
2686                 wpa_supplicant_event_unprot_deauth(wpa_s,
2687                                                    &data->unprot_deauth);
2688                 break;
2689         case EVENT_UNPROT_DISASSOC:
2690                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2691                                                      &data->unprot_disassoc);
2692                 break;
2693         case EVENT_STATION_LOW_ACK:
2694 #ifdef CONFIG_AP
2695                 if (wpa_s->ap_iface && data)
2696                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2697                                                   data->low_ack.addr);
2698 #endif /* CONFIG_AP */
2699 #ifdef CONFIG_TDLS
2700                 if (data)
2701                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2702 #endif /* CONFIG_TDLS */
2703                 break;
2704         case EVENT_IBSS_PEER_LOST:
2705 #ifdef CONFIG_IBSS_RSN
2706                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2707 #endif /* CONFIG_IBSS_RSN */
2708                 break;
2709         case EVENT_DRIVER_GTK_REKEY:
2710                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2711                               wpa_s->bssid, ETH_ALEN))
2712                         break;
2713                 if (!wpa_s->wpa)
2714                         break;
2715                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2716                                          data->driver_gtk_rekey.replay_ctr);
2717                 break;
2718         case EVENT_SCHED_SCAN_STOPPED:
2719                 wpa_s->sched_scanning = 0;
2720                 wpa_supplicant_notify_scanning(wpa_s, 0);
2721
2722                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2723                         break;
2724
2725                 /*
2726                  * If we timed out, start a new sched scan to continue
2727                  * searching for more SSIDs.
2728                  */
2729                 if (wpa_s->sched_scan_timed_out)
2730                         wpa_supplicant_req_sched_scan(wpa_s);
2731                 break;
2732         case EVENT_WPS_BUTTON_PUSHED:
2733 #ifdef CONFIG_WPS
2734                 wpas_wps_start_pbc(wpa_s, NULL, 0);
2735 #endif /* CONFIG_WPS */
2736                 break;
2737         default:
2738                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2739                 break;
2740         }
2741 }