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