0fad8f7b3edd34942fc60257956904a6e7838ac0
[libeap.git] / wpa_supplicant / events.c
1 /*
2  * WPA Supplicant - Driver event processing
3  * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "rsn_supp/wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "pcsc_funcs.h"
26 #include "rsn_supp/preauth.h"
27 #include "rsn_supp/pmksa_cache.h"
28 #include "common/wpa_ctrl.h"
29 #include "eap_peer/eap.h"
30 #include "ap/hostapd.h"
31 #include "notify.h"
32 #include "common/ieee802_11_defs.h"
33 #include "common/ieee802_11_common.h"
34 #include "blacklist.h"
35 #include "wpas_glue.h"
36 #include "wps_supplicant.h"
37 #include "ibss_rsn.h"
38 #include "sme.h"
39 #include "p2p_supplicant.h"
40 #include "bgscan.h"
41 #include "ap.h"
42 #include "bss.h"
43 #include "mlme.h"
44 #include "scan.h"
45
46
47 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
48 {
49         struct wpa_ssid *ssid, *old_ssid;
50
51         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
52                 return 0;
53
54         wpa_printf(MSG_DEBUG, "Select network based on association "
55                    "information");
56         ssid = wpa_supplicant_get_ssid(wpa_s);
57         if (ssid == NULL) {
58                 wpa_printf(MSG_INFO, "No network configuration found for the "
59                            "current AP");
60                 return -1;
61         }
62
63         if (ssid->disabled) {
64                 wpa_printf(MSG_DEBUG, "Selected network is disabled");
65                 return -1;
66         }
67
68         wpa_printf(MSG_DEBUG, "Network configuration found for the current "
69                    "AP");
70         if (ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
71                               WPA_KEY_MGMT_WPA_NONE |
72                               WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_FT_IEEE8021X |
73                               WPA_KEY_MGMT_PSK_SHA256 |
74                               WPA_KEY_MGMT_IEEE8021X_SHA256)) {
75                 u8 wpa_ie[80];
76                 size_t wpa_ie_len = sizeof(wpa_ie);
77                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
78                                           wpa_ie, &wpa_ie_len);
79         } else {
80                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
81         }
82
83         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
84                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
85         old_ssid = wpa_s->current_ssid;
86         wpa_s->current_ssid = ssid;
87         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
88         wpa_supplicant_initiate_eapol(wpa_s);
89         if (old_ssid != wpa_s->current_ssid)
90                 wpas_notify_network_changed(wpa_s);
91
92         return 0;
93 }
94
95
96 static void wpa_supplicant_stop_countermeasures(void *eloop_ctx,
97                                                 void *sock_ctx)
98 {
99         struct wpa_supplicant *wpa_s = eloop_ctx;
100
101         if (wpa_s->countermeasures) {
102                 wpa_s->countermeasures = 0;
103                 wpa_drv_set_countermeasures(wpa_s, 0);
104                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
105                 wpa_supplicant_req_scan(wpa_s, 0, 0);
106         }
107 }
108
109
110 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
111 {
112         int bssid_changed;
113
114         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
115                 return;
116
117         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
118         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
119         os_memset(wpa_s->bssid, 0, ETH_ALEN);
120         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
121         wpa_s->current_bss = NULL;
122         if (bssid_changed)
123                 wpas_notify_bssid_changed(wpa_s);
124
125         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
126         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
127         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
128                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
129         wpa_s->ap_ies_from_associnfo = 0;
130 }
131
132
133 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
134 {
135         struct wpa_ie_data ie;
136         int pmksa_set = -1;
137         size_t i;
138
139         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
140             ie.pmkid == NULL)
141                 return;
142
143         for (i = 0; i < ie.num_pmkid; i++) {
144                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
145                                                     ie.pmkid + i * PMKID_LEN,
146                                                     NULL, NULL, 0);
147                 if (pmksa_set == 0) {
148                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
149                         break;
150                 }
151         }
152
153         wpa_printf(MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from PMKSA "
154                    "cache", pmksa_set == 0 ? "" : "not ");
155 }
156
157
158 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
159                                                  union wpa_event_data *data)
160 {
161         if (data == NULL) {
162                 wpa_printf(MSG_DEBUG, "RSN: No data in PMKID candidate event");
163                 return;
164         }
165         wpa_printf(MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
166                    " index=%d preauth=%d",
167                    MAC2STR(data->pmkid_candidate.bssid),
168                    data->pmkid_candidate.index,
169                    data->pmkid_candidate.preauth);
170
171         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
172                             data->pmkid_candidate.index,
173                             data->pmkid_candidate.preauth);
174 }
175
176
177 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
178 {
179         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
180             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
181                 return 0;
182
183 #ifdef IEEE8021X_EAPOL
184         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
185             wpa_s->current_ssid &&
186             !(wpa_s->current_ssid->eapol_flags &
187               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
188                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
189                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
190                  * plaintext or static WEP keys). */
191                 return 0;
192         }
193 #endif /* IEEE8021X_EAPOL */
194
195         return 1;
196 }
197
198
199 /**
200  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
201  * @wpa_s: pointer to wpa_supplicant data
202  * @ssid: Configuration data for the network
203  * Returns: 0 on success, -1 on failure
204  *
205  * This function is called when starting authentication with a network that is
206  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
207  */
208 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
209                               struct wpa_ssid *ssid)
210 {
211 #ifdef IEEE8021X_EAPOL
212         int aka = 0, sim = 0, type;
213
214         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
215                 return 0;
216
217         if (ssid->eap.eap_methods == NULL) {
218                 sim = 1;
219                 aka = 1;
220         } else {
221                 struct eap_method_type *eap = ssid->eap.eap_methods;
222                 while (eap->vendor != EAP_VENDOR_IETF ||
223                        eap->method != EAP_TYPE_NONE) {
224                         if (eap->vendor == EAP_VENDOR_IETF) {
225                                 if (eap->method == EAP_TYPE_SIM)
226                                         sim = 1;
227                                 else if (eap->method == EAP_TYPE_AKA)
228                                         aka = 1;
229                         }
230                         eap++;
231                 }
232         }
233
234         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
235                 sim = 0;
236         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL)
237                 aka = 0;
238
239         if (!sim && !aka) {
240                 wpa_printf(MSG_DEBUG, "Selected network is configured to use "
241                            "SIM, but neither EAP-SIM nor EAP-AKA are enabled");
242                 return 0;
243         }
244
245         wpa_printf(MSG_DEBUG, "Selected network is configured to use SIM "
246                    "(sim=%d aka=%d) - initialize PCSC", sim, aka);
247         if (sim && aka)
248                 type = SCARD_TRY_BOTH;
249         else if (aka)
250                 type = SCARD_USIM_ONLY;
251         else
252                 type = SCARD_GSM_SIM_ONLY;
253
254         wpa_s->scard = scard_init(type);
255         if (wpa_s->scard == NULL) {
256                 wpa_printf(MSG_WARNING, "Failed to initialize SIM "
257                            "(pcsc-lite)");
258                 return -1;
259         }
260         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
261         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
262 #endif /* IEEE8021X_EAPOL */
263
264         return 0;
265 }
266
267
268 #ifndef CONFIG_NO_SCAN_PROCESSING
269 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
270                                         struct wpa_ssid *ssid)
271 {
272         int i, privacy = 0;
273
274         if (ssid->mixed_cell)
275                 return 1;
276
277 #ifdef CONFIG_WPS
278         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
279                 return 1;
280 #endif /* CONFIG_WPS */
281
282         for (i = 0; i < NUM_WEP_KEYS; i++) {
283                 if (ssid->wep_key_len[i]) {
284                         privacy = 1;
285                         break;
286                 }
287         }
288 #ifdef IEEE8021X_EAPOL
289         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
290             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
291                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
292                 privacy = 1;
293 #endif /* IEEE8021X_EAPOL */
294
295         if (bss->caps & IEEE80211_CAP_PRIVACY)
296                 return privacy;
297         return !privacy;
298 }
299
300
301 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
302                                          struct wpa_ssid *ssid,
303                                          struct wpa_scan_res *bss)
304 {
305         struct wpa_ie_data ie;
306         int proto_match = 0;
307         const u8 *rsn_ie, *wpa_ie;
308         int ret;
309
310         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
311         if (ret >= 0)
312                 return ret;
313
314         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
315         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
316                 proto_match++;
317
318                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
319                         wpa_printf(MSG_DEBUG, "   skip RSN IE - parse failed");
320                         break;
321                 }
322                 if (!(ie.proto & ssid->proto)) {
323                         wpa_printf(MSG_DEBUG, "   skip RSN IE - proto "
324                                    "mismatch");
325                         break;
326                 }
327
328                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
329                         wpa_printf(MSG_DEBUG, "   skip RSN IE - PTK cipher "
330                                    "mismatch");
331                         break;
332                 }
333
334                 if (!(ie.group_cipher & ssid->group_cipher)) {
335                         wpa_printf(MSG_DEBUG, "   skip RSN IE - GTK cipher "
336                                    "mismatch");
337                         break;
338                 }
339
340                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
341                         wpa_printf(MSG_DEBUG, "   skip RSN IE - key mgmt "
342                                    "mismatch");
343                         break;
344                 }
345
346 #ifdef CONFIG_IEEE80211W
347                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
348                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
349                         wpa_printf(MSG_DEBUG, "   skip RSN IE - no mgmt frame "
350                                    "protection");
351                         break;
352                 }
353 #endif /* CONFIG_IEEE80211W */
354
355                 wpa_printf(MSG_DEBUG, "   selected based on RSN IE");
356                 return 1;
357         }
358
359         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
360         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
361                 proto_match++;
362
363                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
364                         wpa_printf(MSG_DEBUG, "   skip WPA IE - parse failed");
365                         break;
366                 }
367                 if (!(ie.proto & ssid->proto)) {
368                         wpa_printf(MSG_DEBUG, "   skip WPA IE - proto "
369                                    "mismatch");
370                         break;
371                 }
372
373                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
374                         wpa_printf(MSG_DEBUG, "   skip WPA IE - PTK cipher "
375                                    "mismatch");
376                         break;
377                 }
378
379                 if (!(ie.group_cipher & ssid->group_cipher)) {
380                         wpa_printf(MSG_DEBUG, "   skip WPA IE - GTK cipher "
381                                    "mismatch");
382                         break;
383                 }
384
385                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
386                         wpa_printf(MSG_DEBUG, "   skip WPA IE - key mgmt "
387                                    "mismatch");
388                         break;
389                 }
390
391                 wpa_printf(MSG_DEBUG, "   selected based on WPA IE");
392                 return 1;
393         }
394
395         if (proto_match == 0)
396                 wpa_printf(MSG_DEBUG, "   skip - no WPA/RSN proto match");
397
398         return 0;
399 }
400
401
402 static int freq_allowed(int *freqs, int freq)
403 {
404         int i;
405
406         if (freqs == NULL)
407                 return 1;
408
409         for (i = 0; freqs[i]; i++)
410                 if (freqs[i] == freq)
411                         return 1;
412         return 0;
413 }
414
415
416 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
417                                             int i, struct wpa_scan_res *bss,
418                                             struct wpa_ssid *group)
419 {
420         const u8 *ssid_;
421         u8 wpa_ie_len, rsn_ie_len, ssid_len;
422         int wpa;
423         struct wpa_blacklist *e;
424         const u8 *ie;
425         struct wpa_ssid *ssid;
426
427         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
428         ssid_ = ie ? ie + 2 : (u8 *) "";
429         ssid_len = ie ? ie[1] : 0;
430
431         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
432         wpa_ie_len = ie ? ie[1] : 0;
433
434         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
435         rsn_ie_len = ie ? ie[1] : 0;
436
437         wpa_printf(MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
438                    "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
439                    i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
440                    wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
441                    wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
442                    " wps" : "");
443
444         e = wpa_blacklist_get(wpa_s, bss->bssid);
445         if (e && e->count > 1) {
446                 wpa_printf(MSG_DEBUG, "   skip - blacklisted");
447                 return 0;
448         }
449
450         if (ssid_len == 0) {
451                 wpa_printf(MSG_DEBUG, "   skip - SSID not known");
452                 return 0;
453         }
454
455         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
456
457         for (ssid = group; ssid; ssid = ssid->pnext) {
458                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
459
460                 if (ssid->disabled) {
461                         wpa_printf(MSG_DEBUG, "   skip - disabled");
462                         continue;
463                 }
464
465 #ifdef CONFIG_WPS
466                 if (wpa && ssid->ssid_len == 0 &&
467                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
468                         check_ssid = 0;
469
470                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
471                         /* Only allow wildcard SSID match if an AP
472                          * advertises active WPS operation that matches
473                          * with our mode. */
474                         check_ssid = 1;
475                         if (ssid->ssid_len == 0 &&
476                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
477                                 check_ssid = 0;
478                 }
479 #endif /* CONFIG_WPS */
480
481                 if (check_ssid &&
482                     (ssid_len != ssid->ssid_len ||
483                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
484                         wpa_printf(MSG_DEBUG, "   skip - SSID mismatch");
485                         continue;
486                 }
487
488                 if (ssid->bssid_set &&
489                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
490                         wpa_printf(MSG_DEBUG, "   skip - BSSID mismatch");
491                         continue;
492                 }
493
494                 if (wpa && !wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
495                         continue;
496
497                 if (!wpa &&
498                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
499                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
500                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
501                         wpa_printf(MSG_DEBUG, "   skip - non-WPA network not "
502                                    "allowed");
503                         continue;
504                 }
505
506                 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
507                         wpa_printf(MSG_DEBUG, "   skip - privacy mismatch");
508                         continue;
509                 }
510
511                 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
512                         wpa_printf(MSG_DEBUG, "   skip - IBSS (adhoc) "
513                                    "network");
514                         continue;
515                 }
516
517                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
518                         wpa_printf(MSG_DEBUG, "   skip - frequency not "
519                                    "allowed");
520                         continue;
521                 }
522
523 #ifdef CONFIG_P2P
524                 /*
525                  * TODO: skip the AP if its P2P IE has Group Formation
526                  * bit set in the P2P Group Capability Bitmap and we
527                  * are not in Group Formation with that device.
528                  */
529 #endif /* CONFIG_P2P */
530
531                 /* Matching configuration found */
532                 return ssid;
533         }
534
535         /* No matching configuration found */
536         return 0;
537 }
538
539
540 static struct wpa_bss *
541 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
542                           struct wpa_scan_results *scan_res,
543                           struct wpa_ssid *group,
544                           struct wpa_ssid **selected_ssid)
545 {
546         size_t i;
547
548         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
549                    group->priority);
550
551         for (i = 0; i < scan_res->num; i++) {
552                 struct wpa_scan_res *bss = scan_res->res[i];
553                 const u8 *ie, *ssid;
554                 u8 ssid_len;
555
556                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
557                 if (!*selected_ssid)
558                         continue;
559
560                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
561                 ssid = ie ? ie + 2 : (u8 *) "";
562                 ssid_len = ie ? ie[1] : 0;
563
564                 wpa_printf(MSG_DEBUG, "   selected BSS " MACSTR " ssid='%s'",
565                            MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
566                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
567         }
568
569         return NULL;
570 }
571
572
573 static struct wpa_bss *
574 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
575                             struct wpa_scan_results *scan_res,
576                             struct wpa_ssid **selected_ssid)
577 {
578         struct wpa_bss *selected = NULL;
579         int prio;
580
581         while (selected == NULL) {
582                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
583                         selected = wpa_supplicant_select_bss(
584                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
585                                 selected_ssid);
586                         if (selected)
587                                 break;
588                 }
589
590                 if (selected == NULL && wpa_s->blacklist) {
591                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
592                                    "and try again");
593                         wpa_blacklist_clear(wpa_s);
594                         wpa_s->blacklist_cleared++;
595                 } else if (selected == NULL)
596                         break;
597         }
598
599         return selected;
600 }
601
602
603 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
604                                         int timeout_sec, int timeout_usec)
605 {
606         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
607                 /*
608                  * No networks are enabled; short-circuit request so
609                  * we don't wait timeout seconds before transitioning
610                  * to INACTIVE state.
611                  */
612                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
613                 return;
614         }
615         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
616 }
617
618
619 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
620                             struct wpa_bss *selected,
621                             struct wpa_ssid *ssid)
622 {
623         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
624                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
625                         "PBC session overlap");
626                 wpa_supplicant_req_new_scan(wpa_s, 10, 0);
627                 return;
628         }
629
630         /*
631          * Do not trigger new association unless the BSSID has changed or if
632          * reassociation is requested. If we are in process of associating with
633          * the selected BSSID, do not trigger new attempt.
634          */
635         if (wpa_s->reassociate ||
636             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
637              (wpa_s->wpa_state != WPA_ASSOCIATING ||
638               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
639               0))) {
640                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
641                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
642                         return;
643                 }
644                 wpa_supplicant_associate(wpa_s, selected, ssid);
645         } else {
646                 wpa_printf(MSG_DEBUG, "Already associated with the selected "
647                            "AP");
648         }
649 }
650
651
652 static struct wpa_ssid *
653 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
654 {
655         int prio;
656         struct wpa_ssid *ssid;
657
658         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
659                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
660                 {
661                         if (ssid->disabled)
662                                 continue;
663                         if (ssid->mode == IEEE80211_MODE_IBSS ||
664                             ssid->mode == IEEE80211_MODE_AP)
665                                 return ssid;
666                 }
667         }
668         return NULL;
669 }
670
671
672 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
673  * on BSS added and BSS changed events */
674 static void wpa_supplicant_rsn_preauth_scan_results(
675         struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
676 {
677         int i;
678
679         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
680                 return;
681
682         for (i = scan_res->num - 1; i >= 0; i--) {
683                 const u8 *ssid, *rsn;
684                 struct wpa_scan_res *r;
685
686                 r = scan_res->res[i];
687
688                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
689                 if (ssid == NULL)
690                         continue;
691
692                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
693                 if (rsn == NULL)
694                         continue;
695
696                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
697         }
698
699 }
700
701
702 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
703                                        struct wpa_bss *selected,
704                                        struct wpa_ssid *ssid,
705                                        struct wpa_scan_results *scan_res)
706 {
707         size_t i;
708         struct wpa_scan_res *current_bss = NULL;
709         int min_diff;
710
711         if (wpa_s->reassociate)
712                 return 1; /* explicit request to reassociate */
713         if (wpa_s->wpa_state < WPA_ASSOCIATED)
714                 return 1; /* we are not associated; continue */
715         if (wpa_s->current_ssid == NULL)
716                 return 1; /* unknown current SSID */
717         if (wpa_s->current_ssid != ssid)
718                 return 1; /* different network block */
719
720         for (i = 0; i < scan_res->num; i++) {
721                 struct wpa_scan_res *res = scan_res->res[i];
722                 const u8 *ie;
723                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
724                         continue;
725
726                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
727                 if (ie == NULL)
728                         continue;
729                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
730                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
731                         continue;
732                 current_bss = res;
733                 break;
734         }
735
736         if (!current_bss)
737                 return 1; /* current BSS not seen in scan results */
738
739         wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
740         wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
741                    MAC2STR(current_bss->bssid), current_bss->level);
742         wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
743                    MAC2STR(selected->bssid), selected->level);
744
745         if (wpa_s->current_ssid->bssid_set &&
746             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
747             0) {
748                 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
749                            "preferred BSSID");
750                 return 1;
751         }
752
753         min_diff = 2;
754         if (current_bss->level < 0) {
755                 if (current_bss->level < -85)
756                         min_diff = 1;
757                 else if (current_bss->level < -80)
758                         min_diff = 2;
759                 else if (current_bss->level < -75)
760                         min_diff = 3;
761                 else if (current_bss->level < -70)
762                         min_diff = 4;
763                 else
764                         min_diff = 5;
765         }
766         if (abs(current_bss->level - selected->level) < min_diff) {
767                 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
768                            "signal level");
769                 return 0;
770         }
771
772         return 1;
773 }
774
775
776 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
777                                               union wpa_event_data *data)
778 {
779         struct wpa_bss *selected;
780         struct wpa_ssid *ssid = NULL;
781         struct wpa_scan_results *scan_res;
782         int ap = 0;
783
784 #ifdef CONFIG_AP
785         if (wpa_s->ap_iface)
786                 ap = 1;
787 #endif /* CONFIG_AP */
788
789         wpa_supplicant_notify_scanning(wpa_s, 0);
790
791         scan_res = wpa_supplicant_get_scan_results(wpa_s,
792                                                    data ? &data->scan_info :
793                                                    NULL, 1);
794         if (scan_res == NULL) {
795                 if (wpa_s->conf->ap_scan == 2 || ap)
796                         return;
797                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
798                            "scanning again");
799                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
800                 return;
801         }
802
803         if (wpa_s->scan_res_handler) {
804                 wpa_s->scan_res_handler(wpa_s, scan_res);
805                 wpa_s->scan_res_handler = NULL;
806                 wpa_scan_results_free(scan_res);
807                 return;
808         }
809
810         if (ap) {
811                 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
812                 wpa_scan_results_free(scan_res);
813                 return;
814         }
815
816         wpa_printf(MSG_DEBUG, "New scan results available");
817         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
818         wpas_notify_scan_results(wpa_s);
819
820         wpas_notify_scan_done(wpa_s, 1);
821
822         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
823                 wpa_scan_results_free(scan_res);
824                 return;
825         }
826
827         if (wpa_s->disconnected) {
828                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
829                 wpa_scan_results_free(scan_res);
830                 return;
831         }
832
833         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
834                 wpa_scan_results_free(scan_res);
835                 return;
836         }
837
838         wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
839
840         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
841
842         if (selected) {
843                 int skip;
844                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
845                                                     scan_res);
846                 wpa_scan_results_free(scan_res);
847                 if (skip)
848                         return;
849                 wpa_supplicant_connect(wpa_s, selected, ssid);
850         } else {
851                 wpa_scan_results_free(scan_res);
852                 wpa_printf(MSG_DEBUG, "No suitable network found");
853                 ssid = wpa_supplicant_pick_new_network(wpa_s);
854                 if (ssid) {
855                         wpa_printf(MSG_DEBUG, "Setup a new network");
856                         wpa_supplicant_associate(wpa_s, NULL, ssid);
857                 } else {
858                         int timeout_sec = 5;
859                         int timeout_usec = 0;
860 #ifdef CONFIG_P2P
861                         if (wpa_s->p2p_in_provisioning) {
862                                 /*
863                                  * Use shorter wait during P2P Provisioning
864                                  * state to speed up group formation.
865                                  */
866                                 timeout_sec = 0;
867                                 timeout_usec = 250000;
868                         }
869 #endif /* CONFIG_P2P */
870                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
871                                                     timeout_usec);
872                 }
873         }
874 }
875 #endif /* CONFIG_NO_SCAN_PROCESSING */
876
877
878 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
879                                           union wpa_event_data *data)
880 {
881         int l, len, found = 0, wpa_found, rsn_found;
882         const u8 *p;
883
884         wpa_printf(MSG_DEBUG, "Association info event");
885         if (data->assoc_info.req_ies)
886                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
887                             data->assoc_info.req_ies_len);
888         if (data->assoc_info.resp_ies)
889                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
890                             data->assoc_info.resp_ies_len);
891         if (data->assoc_info.beacon_ies)
892                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
893                             data->assoc_info.beacon_ies,
894                             data->assoc_info.beacon_ies_len);
895         if (data->assoc_info.freq)
896                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
897
898         p = data->assoc_info.req_ies;
899         l = data->assoc_info.req_ies_len;
900
901         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
902         while (p && l >= 2) {
903                 len = p[1] + 2;
904                 if (len > l) {
905                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
906                                     p, l);
907                         break;
908                 }
909                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
910                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
911                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
912                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
913                                 break;
914                         found = 1;
915                         wpa_find_assoc_pmkid(wpa_s);
916                         break;
917                 }
918                 l -= len;
919                 p += len;
920         }
921         if (!found && data->assoc_info.req_ies)
922                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
923
924 #ifdef CONFIG_IEEE80211R
925 #ifdef CONFIG_SME
926         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
927                 u8 bssid[ETH_ALEN];
928                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
929                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
930                                                  data->assoc_info.resp_ies,
931                                                  data->assoc_info.resp_ies_len,
932                                                  bssid) < 0) {
933                         wpa_printf(MSG_DEBUG, "FT: Validation of "
934                                    "Reassociation Response failed");
935                         wpa_supplicant_deauthenticate(
936                                 wpa_s, WLAN_REASON_INVALID_IE);
937                         return -1;
938                 }
939         }
940
941         p = data->assoc_info.resp_ies;
942         l = data->assoc_info.resp_ies_len;
943
944 #ifdef CONFIG_WPS_STRICT
945         if (wpa_s->current_ssid &&
946             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
947                 struct wpabuf *wps;
948                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
949                 if (wps == NULL) {
950                         wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
951                                    "WPS IE in (Re)Association Response");
952                         return -1;
953                 }
954
955                 if (wps_validate_assoc_resp(wps) < 0) {
956                         wpabuf_free(wps);
957                         wpa_supplicant_deauthenticate(
958                                 wpa_s, WLAN_REASON_INVALID_IE);
959                         return -1;
960                 }
961                 wpabuf_free(wps);
962         }
963 #endif /* CONFIG_WPS_STRICT */
964
965         /* Go through the IEs and make a copy of the MDIE, if present. */
966         while (p && l >= 2) {
967                 len = p[1] + 2;
968                 if (len > l) {
969                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
970                                     p, l);
971                         break;
972                 }
973                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
974                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
975                         wpa_s->sme.ft_used = 1;
976                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
977                                   MOBILITY_DOMAIN_ID_LEN);
978                         break;
979                 }
980                 l -= len;
981                 p += len;
982         }
983 #endif /* CONFIG_SME */
984
985         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
986                              data->assoc_info.resp_ies_len);
987 #endif /* CONFIG_IEEE80211R */
988
989         /* WPA/RSN IE from Beacon/ProbeResp */
990         p = data->assoc_info.beacon_ies;
991         l = data->assoc_info.beacon_ies_len;
992
993         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
994          */
995         wpa_found = rsn_found = 0;
996         while (p && l >= 2) {
997                 len = p[1] + 2;
998                 if (len > l) {
999                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1000                                     p, l);
1001                         break;
1002                 }
1003                 if (!wpa_found &&
1004                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1005                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1006                         wpa_found = 1;
1007                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1008                 }
1009
1010                 if (!rsn_found &&
1011                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1012                         rsn_found = 1;
1013                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1014                 }
1015
1016                 l -= len;
1017                 p += len;
1018         }
1019
1020         if (!wpa_found && data->assoc_info.beacon_ies)
1021                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1022         if (!rsn_found && data->assoc_info.beacon_ies)
1023                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1024         if (wpa_found || rsn_found)
1025                 wpa_s->ap_ies_from_associnfo = 1;
1026
1027         wpa_s->assoc_freq = data->assoc_info.freq;
1028
1029         return 0;
1030 }
1031
1032
1033 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1034                                        union wpa_event_data *data)
1035 {
1036         u8 bssid[ETH_ALEN];
1037         int ft_completed;
1038         int bssid_changed;
1039         struct wpa_driver_capa capa;
1040
1041 #ifdef CONFIG_AP
1042         if (wpa_s->ap_iface) {
1043                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1044                                     data->assoc_info.addr,
1045                                     data->assoc_info.req_ies,
1046                                     data->assoc_info.req_ies_len);
1047                 return;
1048         }
1049 #endif /* CONFIG_AP */
1050
1051         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1052         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1053                 return;
1054
1055         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1056         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1057                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1058         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1059             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1060              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1061                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1062                         MACSTR, MAC2STR(bssid));
1063                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1064                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1065                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1066                 if (bssid_changed)
1067                         wpas_notify_bssid_changed(wpa_s);
1068
1069                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1070                         wpa_clear_keys(wpa_s, bssid);
1071                 }
1072                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1073                         wpa_supplicant_disassociate(
1074                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1075                         return;
1076                 }
1077                 if (wpa_s->current_ssid) {
1078                         struct wpa_bss *bss = NULL;
1079                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1080                         if (ssid->ssid_len > 0)
1081                                 bss = wpa_bss_get(wpa_s, bssid,
1082                                                   ssid->ssid, ssid->ssid_len);
1083                         if (!bss)
1084                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1085                         if (bss)
1086                                 wpa_s->current_bss = bss;
1087                 }
1088         }
1089
1090 #ifdef CONFIG_SME
1091         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1092         wpa_s->sme.prev_bssid_set = 1;
1093 #endif /* CONFIG_SME */
1094
1095         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1096         if (wpa_s->current_ssid) {
1097                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1098                  * initialized before association, but for other modes,
1099                  * initialize PC/SC here, if the current configuration needs
1100                  * smartcard or SIM/USIM. */
1101                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1102         }
1103         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1104         if (wpa_s->l2)
1105                 l2_packet_notify_auth_start(wpa_s->l2);
1106
1107         /*
1108          * Set portEnabled first to FALSE in order to get EAP state machine out
1109          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1110          * state machine may transit to AUTHENTICATING state based on obsolete
1111          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1112          * AUTHENTICATED without ever giving chance to EAP state machine to
1113          * reset the state.
1114          */
1115         if (!ft_completed) {
1116                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1117                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1118         }
1119         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1120                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1121         /* 802.1X::portControl = Auto */
1122         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1123         wpa_s->eapol_received = 0;
1124         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1125             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1126             (wpa_s->current_ssid &&
1127              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1128                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1129                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1130         } else if (!ft_completed) {
1131                 /* Timeout for receiving the first EAPOL packet */
1132                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1133         }
1134         wpa_supplicant_cancel_scan(wpa_s);
1135
1136         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1137             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1138                 /*
1139                  * We are done; the driver will take care of RSN 4-way
1140                  * handshake.
1141                  */
1142                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1143                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1144                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1145                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1146         }
1147
1148         if (wpa_s->pending_eapol_rx) {
1149                 struct os_time now, age;
1150                 os_get_time(&now);
1151                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1152                 if (age.sec == 0 && age.usec < 100000 &&
1153                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1154                     0) {
1155                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1156                                    "that was received just before association "
1157                                    "notification");
1158                         wpa_supplicant_rx_eapol(
1159                                 wpa_s, wpa_s->pending_eapol_rx_src,
1160                                 wpabuf_head(wpa_s->pending_eapol_rx),
1161                                 wpabuf_len(wpa_s->pending_eapol_rx));
1162                 }
1163                 wpabuf_free(wpa_s->pending_eapol_rx);
1164                 wpa_s->pending_eapol_rx = NULL;
1165         }
1166
1167 #ifdef CONFIG_BGSCAN
1168         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1169                 bgscan_deinit(wpa_s);
1170                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1171                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1172                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1173                                            "bgscan");
1174                                 /*
1175                                  * Live without bgscan; it is only used as a
1176                                  * roaming optimization, so the initial
1177                                  * connection is not affected.
1178                                  */
1179                         } else
1180                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1181                 } else
1182                         wpa_s->bgscan_ssid = NULL;
1183         }
1184 #endif /* CONFIG_BGSCAN */
1185
1186         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1187              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1188             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1189             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1190                 /* Set static WEP keys again */
1191                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1192         }
1193 }
1194
1195
1196 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1197                                           u16 reason_code)
1198 {
1199         const u8 *bssid;
1200 #ifdef CONFIG_SME
1201         int authenticating;
1202         u8 prev_pending_bssid[ETH_ALEN];
1203
1204         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1205         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1206 #endif /* CONFIG_SME */
1207
1208         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1209                 /*
1210                  * At least Host AP driver and a Prism3 card seemed to be
1211                  * generating streams of disconnected events when configuring
1212                  * IBSS for WPA-None. Ignore them for now.
1213                  */
1214                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1215                            "IBSS/WPA-None mode");
1216                 return;
1217         }
1218
1219         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1220             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1221                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1222                         "pre-shared key may be incorrect");
1223         }
1224         if (wpa_s->wpa_state >= WPA_ASSOCIATED)
1225                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
1226         bssid = wpa_s->bssid;
1227         if (is_zero_ether_addr(bssid))
1228                 bssid = wpa_s->pending_bssid;
1229         wpa_blacklist_add(wpa_s, bssid);
1230         wpa_sm_notify_disassoc(wpa_s->wpa);
1231         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1232                 " reason=%d",
1233                 MAC2STR(bssid), reason_code);
1234         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1235                 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1236                 wpa_s->keys_cleared = 0;
1237                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1238         }
1239         wpa_supplicant_mark_disassoc(wpa_s);
1240         bgscan_deinit(wpa_s);
1241         wpa_s->bgscan_ssid = NULL;
1242 #ifdef CONFIG_SME
1243         if (authenticating &&
1244             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1245                 /*
1246                  * mac80211-workaround to force deauth on failed auth cmd,
1247                  * requires us to remain in authenticating state to allow the
1248                  * second authentication attempt to be continued properly.
1249                  */
1250                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1251                            "proceed after disconnection event");
1252                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1253                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1254         }
1255 #endif /* CONFIG_SME */
1256 }
1257
1258
1259 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1260 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1261                                                     void *sock_ctx)
1262 {
1263         struct wpa_supplicant *wpa_s = eloop_ctx;
1264
1265         if (!wpa_s->pending_mic_error_report)
1266                 return;
1267
1268         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1269         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1270         wpa_s->pending_mic_error_report = 0;
1271 }
1272 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1273
1274
1275 static void
1276 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1277                                          union wpa_event_data *data)
1278 {
1279         int pairwise;
1280         struct os_time t;
1281
1282         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1283         pairwise = (data && data->michael_mic_failure.unicast);
1284         os_get_time(&t);
1285         if ((wpa_s->last_michael_mic_error &&
1286              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1287             wpa_s->pending_mic_error_report) {
1288                 if (wpa_s->pending_mic_error_report) {
1289                         /*
1290                          * Send the pending MIC error report immediately since
1291                          * we are going to start countermeasures and AP better
1292                          * do the same.
1293                          */
1294                         wpa_sm_key_request(wpa_s->wpa, 1,
1295                                            wpa_s->pending_mic_error_pairwise);
1296                 }
1297
1298                 /* Send the new MIC error report immediately since we are going
1299                  * to start countermeasures and AP better do the same.
1300                  */
1301                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1302
1303                 /* initialize countermeasures */
1304                 wpa_s->countermeasures = 1;
1305                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1306
1307                 /*
1308                  * Need to wait for completion of request frame. We do not get
1309                  * any callback for the message completion, so just wait a
1310                  * short while and hope for the best. */
1311                 os_sleep(0, 10000);
1312
1313                 wpa_drv_set_countermeasures(wpa_s, 1);
1314                 wpa_supplicant_deauthenticate(wpa_s,
1315                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1316                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1317                                      wpa_s, NULL);
1318                 eloop_register_timeout(60, 0,
1319                                        wpa_supplicant_stop_countermeasures,
1320                                        wpa_s, NULL);
1321                 /* TODO: mark the AP rejected for 60 second. STA is
1322                  * allowed to associate with another AP.. */
1323         } else {
1324 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1325                 if (wpa_s->mic_errors_seen) {
1326                         /*
1327                          * Reduce the effectiveness of Michael MIC error
1328                          * reports as a means for attacking against TKIP if
1329                          * more than one MIC failure is noticed with the same
1330                          * PTK. We delay the transmission of the reports by a
1331                          * random time between 0 and 60 seconds in order to
1332                          * force the attacker wait 60 seconds before getting
1333                          * the information on whether a frame resulted in a MIC
1334                          * failure.
1335                          */
1336                         u8 rval[4];
1337                         int sec;
1338
1339                         if (os_get_random(rval, sizeof(rval)) < 0)
1340                                 sec = os_random() % 60;
1341                         else
1342                                 sec = WPA_GET_BE32(rval) % 60;
1343                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1344                                    "seconds", sec);
1345                         wpa_s->pending_mic_error_report = 1;
1346                         wpa_s->pending_mic_error_pairwise = pairwise;
1347                         eloop_cancel_timeout(
1348                                 wpa_supplicant_delayed_mic_error_report,
1349                                 wpa_s, NULL);
1350                         eloop_register_timeout(
1351                                 sec, os_random() % 1000000,
1352                                 wpa_supplicant_delayed_mic_error_report,
1353                                 wpa_s, NULL);
1354                 } else {
1355                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1356                 }
1357 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1358                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1359 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1360         }
1361         wpa_s->last_michael_mic_error = t.sec;
1362         wpa_s->mic_errors_seen++;
1363 }
1364
1365
1366 #ifdef CONFIG_TERMINATE_ONLASTIF
1367 static int any_interfaces(struct wpa_supplicant *head)
1368 {
1369         struct wpa_supplicant *wpa_s;
1370
1371         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1372                 if (!wpa_s->interface_removed)
1373                         return 1;
1374         return 0;
1375 }
1376 #endif /* CONFIG_TERMINATE_ONLASTIF */
1377
1378
1379 static void
1380 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1381                                       union wpa_event_data *data)
1382 {
1383         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1384                 return;
1385
1386         switch (data->interface_status.ievent) {
1387         case EVENT_INTERFACE_ADDED:
1388                 if (!wpa_s->interface_removed)
1389                         break;
1390                 wpa_s->interface_removed = 0;
1391                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1392                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1393                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1394                                    "after interface was added.");
1395                 }
1396                 break;
1397         case EVENT_INTERFACE_REMOVED:
1398                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1399                 wpa_s->interface_removed = 1;
1400                 wpa_supplicant_mark_disassoc(wpa_s);
1401                 l2_packet_deinit(wpa_s->l2);
1402                 wpa_s->l2 = NULL;
1403 #ifdef CONFIG_TERMINATE_ONLASTIF
1404                 /* check if last interface */
1405                 if (!any_interfaces(wpa_s->global->ifaces))
1406                         eloop_terminate();
1407 #endif /* CONFIG_TERMINATE_ONLASTIF */
1408                 break;
1409         }
1410 }
1411
1412
1413 #ifdef CONFIG_PEERKEY
1414 static void
1415 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1416                               union wpa_event_data *data)
1417 {
1418         if (data == NULL)
1419                 return;
1420         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1421 }
1422 #endif /* CONFIG_PEERKEY */
1423
1424
1425 #ifdef CONFIG_IEEE80211R
1426 static void
1427 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1428                                  union wpa_event_data *data)
1429 {
1430         if (data == NULL)
1431                 return;
1432
1433         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1434                                     data->ft_ies.ies_len,
1435                                     data->ft_ies.ft_action,
1436                                     data->ft_ies.target_ap,
1437                                     data->ft_ies.ric_ies,
1438                                     data->ft_ies.ric_ies_len) < 0) {
1439                 /* TODO: prevent MLME/driver from trying to associate? */
1440         }
1441 }
1442 #endif /* CONFIG_IEEE80211R */
1443
1444
1445 #ifdef CONFIG_IBSS_RSN
1446 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1447                                                 union wpa_event_data *data)
1448 {
1449         if (data == NULL)
1450                 return;
1451         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1452 }
1453 #endif /* CONFIG_IBSS_RSN */
1454
1455
1456 #ifdef CONFIG_IEEE80211R
1457 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1458                          size_t len)
1459 {
1460         const u8 *sta_addr, *target_ap_addr;
1461         u16 status;
1462
1463         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1464         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1465                 return; /* only SME case supported for now */
1466         if (len < 1 + 2 * ETH_ALEN + 2)
1467                 return;
1468         if (data[0] != 2)
1469                 return; /* Only FT Action Response is supported for now */
1470         sta_addr = data + 1;
1471         target_ap_addr = data + 1 + ETH_ALEN;
1472         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1473         wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1474                    " TargetAP " MACSTR " status %u",
1475                    MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1476
1477         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1478                 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1479                            " in FT Action Response", MAC2STR(sta_addr));
1480                 return;
1481         }
1482
1483         if (status) {
1484                 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1485                            "failure (status code %d)", status);
1486                 /* TODO: report error to FT code(?) */
1487                 return;
1488         }
1489
1490         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1491                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1492                                     target_ap_addr, NULL, 0) < 0)
1493                 return;
1494
1495 #ifdef CONFIG_SME
1496         {
1497                 struct wpa_bss *bss;
1498                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1499                 if (bss)
1500                         wpa_s->sme.freq = bss->freq;
1501                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1502                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1503                               WLAN_AUTH_FT);
1504         }
1505 #endif /* CONFIG_SME */
1506 }
1507 #endif /* CONFIG_IEEE80211R */
1508
1509
1510 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1511                           union wpa_event_data *data)
1512 {
1513         struct wpa_supplicant *wpa_s = ctx;
1514         u16 reason_code = 0;
1515
1516         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1517             event != EVENT_INTERFACE_ENABLED &&
1518             event != EVENT_INTERFACE_STATUS) {
1519                 wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
1520                            "disabled", event);
1521                 return;
1522         }
1523
1524         wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
1525                    event, wpa_s->ifname);
1526
1527         switch (event) {
1528         case EVENT_AUTH:
1529                 sme_event_auth(wpa_s, data);
1530                 break;
1531         case EVENT_ASSOC:
1532                 wpa_supplicant_event_assoc(wpa_s, data);
1533                 break;
1534         case EVENT_DISASSOC:
1535                 wpa_printf(MSG_DEBUG, "Disassociation notification");
1536                 if (data) {
1537                         wpa_printf(MSG_DEBUG, " * reason %u",
1538                                    data->disassoc_info.reason_code);
1539                         if (data->disassoc_info.addr)
1540                                 wpa_printf(MSG_DEBUG, " * address " MACSTR,
1541                                            MAC2STR(data->disassoc_info.addr));
1542                 }
1543 #ifdef CONFIG_AP
1544                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1545                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1546                                                data->disassoc_info.addr);
1547                         break;
1548                 }
1549 #endif /* CONFIG_AP */
1550                 if (data) {
1551                         reason_code = data->disassoc_info.reason_code;
1552                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1553                                     data->disassoc_info.ie,
1554                                     data->disassoc_info.ie_len);
1555 #ifdef CONFIG_P2P
1556                         wpas_p2p_disassoc_notif(
1557                                 wpa_s, data->disassoc_info.addr, reason_code,
1558                                 data->disassoc_info.ie,
1559                                 data->disassoc_info.ie_len);
1560 #endif /* CONFIG_P2P */
1561                 }
1562                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1563                         sme_event_disassoc(wpa_s, data);
1564                 /* fall through */
1565         case EVENT_DEAUTH:
1566                 if (event == EVENT_DEAUTH) {
1567                         wpa_printf(MSG_DEBUG, "Deauthentication notification");
1568                         if (data) {
1569                                 reason_code = data->deauth_info.reason_code;
1570                                 wpa_printf(MSG_DEBUG, " * reason %u",
1571                                            data->deauth_info.reason_code);
1572                                 if (data->deauth_info.addr) {
1573                                         wpa_printf(MSG_DEBUG, " * address "
1574                                                    MACSTR,
1575                                                    MAC2STR(data->deauth_info.
1576                                                            addr));
1577                                 }
1578                                 wpa_hexdump(MSG_DEBUG,
1579                                             "Deauthentication frame IE(s)",
1580                                             data->deauth_info.ie,
1581                                             data->deauth_info.ie_len);
1582 #ifdef CONFIG_P2P
1583                                 wpas_p2p_deauth_notif(
1584                                         wpa_s, data->deauth_info.addr,
1585                                         reason_code,
1586                                         data->deauth_info.ie,
1587                                         data->deauth_info.ie_len);
1588 #endif /* CONFIG_P2P */
1589                         }
1590                 }
1591 #ifdef CONFIG_AP
1592                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1593                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1594                                                data->deauth_info.addr);
1595                         break;
1596                 }
1597 #endif /* CONFIG_AP */
1598                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1599                 break;
1600         case EVENT_MICHAEL_MIC_FAILURE:
1601                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1602                 break;
1603 #ifndef CONFIG_NO_SCAN_PROCESSING
1604         case EVENT_SCAN_RESULTS:
1605                 wpa_supplicant_event_scan_results(wpa_s, data);
1606                 break;
1607 #endif /* CONFIG_NO_SCAN_PROCESSING */
1608         case EVENT_ASSOCINFO:
1609                 wpa_supplicant_event_associnfo(wpa_s, data);
1610                 break;
1611         case EVENT_INTERFACE_STATUS:
1612                 wpa_supplicant_event_interface_status(wpa_s, data);
1613                 break;
1614         case EVENT_PMKID_CANDIDATE:
1615                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1616                 break;
1617 #ifdef CONFIG_PEERKEY
1618         case EVENT_STKSTART:
1619                 wpa_supplicant_event_stkstart(wpa_s, data);
1620                 break;
1621 #endif /* CONFIG_PEERKEY */
1622 #ifdef CONFIG_IEEE80211R
1623         case EVENT_FT_RESPONSE:
1624                 wpa_supplicant_event_ft_response(wpa_s, data);
1625                 break;
1626 #endif /* CONFIG_IEEE80211R */
1627 #ifdef CONFIG_IBSS_RSN
1628         case EVENT_IBSS_RSN_START:
1629                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1630                 break;
1631 #endif /* CONFIG_IBSS_RSN */
1632         case EVENT_ASSOC_REJECT:
1633                 sme_event_assoc_reject(wpa_s, data);
1634                 break;
1635         case EVENT_AUTH_TIMED_OUT:
1636                 sme_event_auth_timed_out(wpa_s, data);
1637                 break;
1638         case EVENT_ASSOC_TIMED_OUT:
1639                 sme_event_assoc_timed_out(wpa_s, data);
1640                 break;
1641 #ifdef CONFIG_AP
1642         case EVENT_TX_STATUS:
1643                 wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
1644                            " type=%d stype=%d pending_dst=" MACSTR,
1645                            wpa_s->ifname, MAC2STR(data->tx_status.dst),
1646                            data->tx_status.type, data->tx_status.stype,
1647                            MAC2STR(wpa_s->parent->pending_action_dst));
1648                 if (wpa_s->ap_iface == NULL) {
1649 #ifdef CONFIG_P2P
1650                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1651                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1652                                 wpas_send_action_tx_status(
1653                                         wpa_s, data->tx_status.dst,
1654                                         data->tx_status.data,
1655                                         data->tx_status.data_len,
1656                                         data->tx_status.ack);
1657 #endif /* CONFIG_P2P */
1658                         break;
1659                 }
1660 #ifdef CONFIG_P2P
1661                 /*
1662                  * Catch TX status events for Action frames we sent via group
1663                  * interface in GO mode.
1664                  */
1665                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1666                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1667                     os_memcmp(wpa_s->parent->pending_action_dst,
1668                               data->tx_status.dst, ETH_ALEN) == 0) {
1669                         wpas_send_action_tx_status(
1670                                 wpa_s->parent, data->tx_status.dst,
1671                                 data->tx_status.data,
1672                                 data->tx_status.data_len,
1673                                 data->tx_status.ack);
1674                         break;
1675                 }
1676 #endif /* CONFIG_P2P */
1677                 switch (data->tx_status.type) {
1678                 case WLAN_FC_TYPE_MGMT:
1679                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1680                                       data->tx_status.data_len,
1681                                       data->tx_status.stype,
1682                                       data->tx_status.ack);
1683                         break;
1684                 case WLAN_FC_TYPE_DATA:
1685                         ap_tx_status(wpa_s, data->tx_status.dst,
1686                                      data->tx_status.data,
1687                                      data->tx_status.data_len,
1688                                      data->tx_status.ack);
1689                         break;
1690                 }
1691                 break;
1692         case EVENT_RX_FROM_UNKNOWN:
1693                 if (wpa_s->ap_iface == NULL)
1694                         break;
1695                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1696                                        data->rx_from_unknown.len);
1697                 break;
1698         case EVENT_RX_MGMT:
1699                 if (wpa_s->ap_iface == NULL) {
1700 #ifdef CONFIG_P2P
1701                         u16 fc, stype;
1702                         const struct ieee80211_mgmt *mgmt;
1703                         mgmt = (const struct ieee80211_mgmt *)
1704                                 data->rx_mgmt.frame;
1705                         fc = le_to_host16(mgmt->frame_control);
1706                         stype = WLAN_FC_GET_STYPE(fc);
1707                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1708                             data->rx_mgmt.frame_len > 24) {
1709                                 const u8 *src = mgmt->sa;
1710                                 const u8 *ie = mgmt->u.probe_req.variable;
1711                                 size_t ie_len = data->rx_mgmt.frame_len -
1712                                         (mgmt->u.probe_req.variable -
1713                                          data->rx_mgmt.frame);
1714                                 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1715                                 break;
1716                         }
1717 #endif /* CONFIG_P2P */
1718                         wpa_printf(MSG_DEBUG, "AP: ignore received management "
1719                                    "frame in non-AP mode");
1720                         break;
1721                 }
1722                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1723                 break;
1724 #endif /* CONFIG_AP */
1725         case EVENT_RX_ACTION:
1726                 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1727                            " Category=%u DataLen=%d freq=%d MHz",
1728                            MAC2STR(data->rx_action.sa),
1729                            data->rx_action.category, (int) data->rx_action.len,
1730                            data->rx_action.freq);
1731 #ifdef CONFIG_IEEE80211R
1732                 if (data->rx_action.category == WLAN_ACTION_FT) {
1733                         ft_rx_action(wpa_s, data->rx_action.data,
1734                                      data->rx_action.len);
1735                         break;
1736                 }
1737 #endif /* CONFIG_IEEE80211R */
1738 #ifdef CONFIG_P2P
1739                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
1740                                    data->rx_action.sa,
1741                                    data->rx_action.bssid,
1742                                    data->rx_action.category,
1743                                    data->rx_action.data,
1744                                    data->rx_action.len, data->rx_action.freq);
1745 #endif /* CONFIG_P2P */
1746                 break;
1747 #ifdef CONFIG_P2P
1748         case EVENT_REMAIN_ON_CHANNEL:
1749                 wpas_p2p_remain_on_channel_cb(
1750                         wpa_s, data->remain_on_channel.freq,
1751                         data->remain_on_channel.duration);
1752                 break;
1753         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
1754                 wpas_p2p_cancel_remain_on_channel_cb(
1755                         wpa_s, data->remain_on_channel.freq);
1756                 break;
1757         case EVENT_RX_PROBE_REQ:
1758                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
1759                                       data->rx_probe_req.ie,
1760                                       data->rx_probe_req.ie_len);
1761                 break;
1762 #endif /* CONFIG_P2P */
1763 #ifdef CONFIG_CLIENT_MLME
1764         case EVENT_MLME_RX: {
1765                 struct ieee80211_rx_status rx_status;
1766                 os_memset(&rx_status, 0, sizeof(rx_status));
1767                 rx_status.freq = data->mlme_rx.freq;
1768                 rx_status.channel = data->mlme_rx.channel;
1769                 rx_status.ssi = data->mlme_rx.ssi;
1770                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1771                                  &rx_status);
1772                 break;
1773         }
1774 #endif /* CONFIG_CLIENT_MLME */
1775         case EVENT_EAPOL_RX:
1776                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1777                                         data->eapol_rx.data,
1778                                         data->eapol_rx.data_len);
1779                 break;
1780         case EVENT_SIGNAL_CHANGE:
1781                 bgscan_notify_signal_change(
1782                         wpa_s, data->signal_change.above_threshold,
1783                         data->signal_change.current_signal);
1784                 break;
1785         case EVENT_INTERFACE_ENABLED:
1786                 wpa_printf(MSG_DEBUG, "Interface was enabled");
1787                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1788                         wpa_supplicant_set_state(wpa_s,
1789                                                  WPA_DISCONNECTED);
1790                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1791                 }
1792                 break;
1793         case EVENT_INTERFACE_DISABLED:
1794                 wpa_printf(MSG_DEBUG, "Interface was disabled");
1795                 wpa_supplicant_mark_disassoc(wpa_s);
1796                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1797                 break;
1798         default:
1799                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1800                 break;
1801         }
1802 }