remove @EAP_LDFLAGS@, no longer exists
[mech_eap.orig] / libeap / 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 ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
467                         wpa_printf(MSG_DEBUG, "   skip - blacklisted (WPS)");
468                         continue;
469                 }
470
471                 if (wpa && ssid->ssid_len == 0 &&
472                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
473                         check_ssid = 0;
474
475                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
476                         /* Only allow wildcard SSID match if an AP
477                          * advertises active WPS operation that matches
478                          * with our mode. */
479                         check_ssid = 1;
480                         if (ssid->ssid_len == 0 &&
481                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
482                                 check_ssid = 0;
483                 }
484 #endif /* CONFIG_WPS */
485
486                 if (check_ssid &&
487                     (ssid_len != ssid->ssid_len ||
488                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
489                         wpa_printf(MSG_DEBUG, "   skip - SSID mismatch");
490                         continue;
491                 }
492
493                 if (ssid->bssid_set &&
494                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
495                         wpa_printf(MSG_DEBUG, "   skip - BSSID mismatch");
496                         continue;
497                 }
498
499                 if (wpa && !wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
500                         continue;
501
502                 if (!wpa &&
503                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
504                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
505                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
506                         wpa_printf(MSG_DEBUG, "   skip - non-WPA network not "
507                                    "allowed");
508                         continue;
509                 }
510
511                 if (!wpa && !wpa_supplicant_match_privacy(bss, ssid)) {
512                         wpa_printf(MSG_DEBUG, "   skip - privacy mismatch");
513                         continue;
514                 }
515
516                 if (!wpa && (bss->caps & IEEE80211_CAP_IBSS)) {
517                         wpa_printf(MSG_DEBUG, "   skip - IBSS (adhoc) "
518                                    "network");
519                         continue;
520                 }
521
522                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
523                         wpa_printf(MSG_DEBUG, "   skip - frequency not "
524                                    "allowed");
525                         continue;
526                 }
527
528 #ifdef CONFIG_P2P
529                 /*
530                  * TODO: skip the AP if its P2P IE has Group Formation
531                  * bit set in the P2P Group Capability Bitmap and we
532                  * are not in Group Formation with that device.
533                  */
534 #endif /* CONFIG_P2P */
535
536                 /* Matching configuration found */
537                 return ssid;
538         }
539
540         /* No matching configuration found */
541         return 0;
542 }
543
544
545 static struct wpa_bss *
546 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
547                           struct wpa_scan_results *scan_res,
548                           struct wpa_ssid *group,
549                           struct wpa_ssid **selected_ssid)
550 {
551         size_t i;
552
553         wpa_printf(MSG_DEBUG, "Selecting BSS from priority group %d",
554                    group->priority);
555
556         for (i = 0; i < scan_res->num; i++) {
557                 struct wpa_scan_res *bss = scan_res->res[i];
558                 const u8 *ie, *ssid;
559                 u8 ssid_len;
560
561                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
562                 if (!*selected_ssid)
563                         continue;
564
565                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
566                 ssid = ie ? ie + 2 : (u8 *) "";
567                 ssid_len = ie ? ie[1] : 0;
568
569                 wpa_printf(MSG_DEBUG, "   selected BSS " MACSTR " ssid='%s'",
570                            MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
571                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
572         }
573
574         return NULL;
575 }
576
577
578 static struct wpa_bss *
579 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
580                             struct wpa_scan_results *scan_res,
581                             struct wpa_ssid **selected_ssid)
582 {
583         struct wpa_bss *selected = NULL;
584         int prio;
585
586         while (selected == NULL) {
587                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
588                         selected = wpa_supplicant_select_bss(
589                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
590                                 selected_ssid);
591                         if (selected)
592                                 break;
593                 }
594
595                 if (selected == NULL && wpa_s->blacklist) {
596                         wpa_printf(MSG_DEBUG, "No APs found - clear blacklist "
597                                    "and try again");
598                         wpa_blacklist_clear(wpa_s);
599                         wpa_s->blacklist_cleared++;
600                 } else if (selected == NULL)
601                         break;
602         }
603
604         return selected;
605 }
606
607
608 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
609                                         int timeout_sec, int timeout_usec)
610 {
611         if (!wpa_supplicant_enabled_networks(wpa_s->conf)) {
612                 /*
613                  * No networks are enabled; short-circuit request so
614                  * we don't wait timeout seconds before transitioning
615                  * to INACTIVE state.
616                  */
617                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
618                 return;
619         }
620         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
621 }
622
623
624 void wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
625                             struct wpa_bss *selected,
626                             struct wpa_ssid *ssid)
627 {
628         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
629                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
630                         "PBC session overlap");
631 #ifdef CONFIG_P2P
632                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
633                         return;
634 #endif /* CONFIG_P2P */
635
636 #ifdef CONFIG_WPS
637                 wpas_wps_cancel(wpa_s);
638 #endif /* CONFIG_WPS */
639                 return;
640         }
641
642         /*
643          * Do not trigger new association unless the BSSID has changed or if
644          * reassociation is requested. If we are in process of associating with
645          * the selected BSSID, do not trigger new attempt.
646          */
647         if (wpa_s->reassociate ||
648             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
649              (wpa_s->wpa_state != WPA_ASSOCIATING ||
650               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
651               0))) {
652                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
653                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
654                         return;
655                 }
656                 wpa_supplicant_associate(wpa_s, selected, ssid);
657         } else {
658                 wpa_printf(MSG_DEBUG, "Already associated with the selected "
659                            "AP");
660         }
661 }
662
663
664 static struct wpa_ssid *
665 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
666 {
667         int prio;
668         struct wpa_ssid *ssid;
669
670         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
671                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
672                 {
673                         if (ssid->disabled)
674                                 continue;
675                         if (ssid->mode == IEEE80211_MODE_IBSS ||
676                             ssid->mode == IEEE80211_MODE_AP)
677                                 return ssid;
678                 }
679         }
680         return NULL;
681 }
682
683
684 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
685  * on BSS added and BSS changed events */
686 static void wpa_supplicant_rsn_preauth_scan_results(
687         struct wpa_supplicant *wpa_s, struct wpa_scan_results *scan_res)
688 {
689         int i;
690
691         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
692                 return;
693
694         for (i = scan_res->num - 1; i >= 0; i--) {
695                 const u8 *ssid, *rsn;
696                 struct wpa_scan_res *r;
697
698                 r = scan_res->res[i];
699
700                 ssid = wpa_scan_get_ie(r, WLAN_EID_SSID);
701                 if (ssid == NULL)
702                         continue;
703
704                 rsn = wpa_scan_get_ie(r, WLAN_EID_RSN);
705                 if (rsn == NULL)
706                         continue;
707
708                 rsn_preauth_scan_result(wpa_s->wpa, r->bssid, ssid, rsn);
709         }
710
711 }
712
713
714 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
715                                        struct wpa_bss *selected,
716                                        struct wpa_ssid *ssid,
717                                        struct wpa_scan_results *scan_res)
718 {
719         size_t i;
720         struct wpa_scan_res *current_bss = NULL;
721         int min_diff;
722
723         if (wpa_s->reassociate)
724                 return 1; /* explicit request to reassociate */
725         if (wpa_s->wpa_state < WPA_ASSOCIATED)
726                 return 1; /* we are not associated; continue */
727         if (wpa_s->current_ssid == NULL)
728                 return 1; /* unknown current SSID */
729         if (wpa_s->current_ssid != ssid)
730                 return 1; /* different network block */
731
732         for (i = 0; i < scan_res->num; i++) {
733                 struct wpa_scan_res *res = scan_res->res[i];
734                 const u8 *ie;
735                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
736                         continue;
737
738                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
739                 if (ie == NULL)
740                         continue;
741                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
742                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
743                         continue;
744                 current_bss = res;
745                 break;
746         }
747
748         if (!current_bss)
749                 return 1; /* current BSS not seen in scan results */
750
751         wpa_printf(MSG_DEBUG, "Considering within-ESS reassociation");
752         wpa_printf(MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
753                    MAC2STR(current_bss->bssid), current_bss->level);
754         wpa_printf(MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
755                    MAC2STR(selected->bssid), selected->level);
756
757         if (wpa_s->current_ssid->bssid_set &&
758             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
759             0) {
760                 wpa_printf(MSG_DEBUG, "Allow reassociation - selected BSS has "
761                            "preferred BSSID");
762                 return 1;
763         }
764
765         min_diff = 2;
766         if (current_bss->level < 0) {
767                 if (current_bss->level < -85)
768                         min_diff = 1;
769                 else if (current_bss->level < -80)
770                         min_diff = 2;
771                 else if (current_bss->level < -75)
772                         min_diff = 3;
773                 else if (current_bss->level < -70)
774                         min_diff = 4;
775                 else
776                         min_diff = 5;
777         }
778         if (abs(current_bss->level - selected->level) < min_diff) {
779                 wpa_printf(MSG_DEBUG, "Skip roam - too small difference in "
780                            "signal level");
781                 return 0;
782         }
783
784         return 1;
785 }
786
787
788 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
789                                               union wpa_event_data *data)
790 {
791         struct wpa_bss *selected;
792         struct wpa_ssid *ssid = NULL;
793         struct wpa_scan_results *scan_res;
794         int ap = 0;
795
796 #ifdef CONFIG_AP
797         if (wpa_s->ap_iface)
798                 ap = 1;
799 #endif /* CONFIG_AP */
800
801         wpa_supplicant_notify_scanning(wpa_s, 0);
802
803         scan_res = wpa_supplicant_get_scan_results(wpa_s,
804                                                    data ? &data->scan_info :
805                                                    NULL, 1);
806         if (scan_res == NULL) {
807                 if (wpa_s->conf->ap_scan == 2 || ap)
808                         return;
809                 wpa_printf(MSG_DEBUG, "Failed to get scan results - try "
810                            "scanning again");
811                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
812                 return;
813         }
814
815         if (wpa_s->scan_res_handler) {
816                 wpa_s->scan_res_handler(wpa_s, scan_res);
817                 wpa_s->scan_res_handler = NULL;
818                 wpa_scan_results_free(scan_res);
819                 return;
820         }
821
822         if (ap) {
823                 wpa_printf(MSG_DEBUG, "Ignore scan results in AP mode");
824                 wpa_scan_results_free(scan_res);
825                 return;
826         }
827
828         wpa_printf(MSG_DEBUG, "New scan results available");
829         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
830         wpas_notify_scan_results(wpa_s);
831
832         wpas_notify_scan_done(wpa_s, 1);
833
834         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
835                 wpa_scan_results_free(scan_res);
836                 return;
837         }
838
839         if (wpa_s->disconnected) {
840                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
841                 wpa_scan_results_free(scan_res);
842                 return;
843         }
844
845         if (bgscan_notify_scan(wpa_s, scan_res) == 1) {
846                 wpa_scan_results_free(scan_res);
847                 return;
848         }
849
850         wpa_supplicant_rsn_preauth_scan_results(wpa_s, scan_res);
851
852         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
853
854         if (selected) {
855                 int skip;
856                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
857                                                     scan_res);
858                 wpa_scan_results_free(scan_res);
859                 if (skip)
860                         return;
861                 wpa_supplicant_connect(wpa_s, selected, ssid);
862         } else {
863                 wpa_scan_results_free(scan_res);
864                 wpa_printf(MSG_DEBUG, "No suitable network found");
865                 ssid = wpa_supplicant_pick_new_network(wpa_s);
866                 if (ssid) {
867                         wpa_printf(MSG_DEBUG, "Setup a new network");
868                         wpa_supplicant_associate(wpa_s, NULL, ssid);
869                 } else {
870                         int timeout_sec = 5;
871                         int timeout_usec = 0;
872 #ifdef CONFIG_P2P
873                         if (wpa_s->p2p_in_provisioning) {
874                                 /*
875                                  * Use shorter wait during P2P Provisioning
876                                  * state to speed up group formation.
877                                  */
878                                 timeout_sec = 0;
879                                 timeout_usec = 250000;
880                         }
881 #endif /* CONFIG_P2P */
882                         wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
883                                                     timeout_usec);
884                 }
885         }
886 }
887 #endif /* CONFIG_NO_SCAN_PROCESSING */
888
889
890 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
891                                           union wpa_event_data *data)
892 {
893         int l, len, found = 0, wpa_found, rsn_found;
894         const u8 *p;
895
896         wpa_printf(MSG_DEBUG, "Association info event");
897         if (data->assoc_info.req_ies)
898                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
899                             data->assoc_info.req_ies_len);
900         if (data->assoc_info.resp_ies)
901                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
902                             data->assoc_info.resp_ies_len);
903         if (data->assoc_info.beacon_ies)
904                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
905                             data->assoc_info.beacon_ies,
906                             data->assoc_info.beacon_ies_len);
907         if (data->assoc_info.freq)
908                 wpa_printf(MSG_DEBUG, "freq=%u MHz", data->assoc_info.freq);
909
910         p = data->assoc_info.req_ies;
911         l = data->assoc_info.req_ies_len;
912
913         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
914         while (p && l >= 2) {
915                 len = p[1] + 2;
916                 if (len > l) {
917                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
918                                     p, l);
919                         break;
920                 }
921                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
922                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
923                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
924                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
925                                 break;
926                         found = 1;
927                         wpa_find_assoc_pmkid(wpa_s);
928                         break;
929                 }
930                 l -= len;
931                 p += len;
932         }
933         if (!found && data->assoc_info.req_ies)
934                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
935
936 #ifdef CONFIG_IEEE80211R
937 #ifdef CONFIG_SME
938         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
939                 u8 bssid[ETH_ALEN];
940                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
941                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
942                                                  data->assoc_info.resp_ies,
943                                                  data->assoc_info.resp_ies_len,
944                                                  bssid) < 0) {
945                         wpa_printf(MSG_DEBUG, "FT: Validation of "
946                                    "Reassociation Response failed");
947                         wpa_supplicant_deauthenticate(
948                                 wpa_s, WLAN_REASON_INVALID_IE);
949                         return -1;
950                 }
951         }
952
953         p = data->assoc_info.resp_ies;
954         l = data->assoc_info.resp_ies_len;
955
956 #ifdef CONFIG_WPS_STRICT
957         if (wpa_s->current_ssid &&
958             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
959                 struct wpabuf *wps;
960                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
961                 if (wps == NULL) {
962                         wpa_printf(MSG_INFO, "WPS-STRICT: AP did not include "
963                                    "WPS IE in (Re)Association Response");
964                         return -1;
965                 }
966
967                 if (wps_validate_assoc_resp(wps) < 0) {
968                         wpabuf_free(wps);
969                         wpa_supplicant_deauthenticate(
970                                 wpa_s, WLAN_REASON_INVALID_IE);
971                         return -1;
972                 }
973                 wpabuf_free(wps);
974         }
975 #endif /* CONFIG_WPS_STRICT */
976
977         /* Go through the IEs and make a copy of the MDIE, if present. */
978         while (p && l >= 2) {
979                 len = p[1] + 2;
980                 if (len > l) {
981                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
982                                     p, l);
983                         break;
984                 }
985                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
986                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
987                         wpa_s->sme.ft_used = 1;
988                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
989                                   MOBILITY_DOMAIN_ID_LEN);
990                         break;
991                 }
992                 l -= len;
993                 p += len;
994         }
995 #endif /* CONFIG_SME */
996
997         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
998                              data->assoc_info.resp_ies_len);
999 #endif /* CONFIG_IEEE80211R */
1000
1001         /* WPA/RSN IE from Beacon/ProbeResp */
1002         p = data->assoc_info.beacon_ies;
1003         l = data->assoc_info.beacon_ies_len;
1004
1005         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1006          */
1007         wpa_found = rsn_found = 0;
1008         while (p && l >= 2) {
1009                 len = p[1] + 2;
1010                 if (len > l) {
1011                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1012                                     p, l);
1013                         break;
1014                 }
1015                 if (!wpa_found &&
1016                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1017                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1018                         wpa_found = 1;
1019                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1020                 }
1021
1022                 if (!rsn_found &&
1023                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1024                         rsn_found = 1;
1025                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1026                 }
1027
1028                 l -= len;
1029                 p += len;
1030         }
1031
1032         if (!wpa_found && data->assoc_info.beacon_ies)
1033                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1034         if (!rsn_found && data->assoc_info.beacon_ies)
1035                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1036         if (wpa_found || rsn_found)
1037                 wpa_s->ap_ies_from_associnfo = 1;
1038
1039         wpa_s->assoc_freq = data->assoc_info.freq;
1040
1041         return 0;
1042 }
1043
1044
1045 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1046                                        union wpa_event_data *data)
1047 {
1048         u8 bssid[ETH_ALEN];
1049         int ft_completed;
1050         int bssid_changed;
1051         struct wpa_driver_capa capa;
1052
1053 #ifdef CONFIG_AP
1054         if (wpa_s->ap_iface) {
1055                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1056                                     data->assoc_info.addr,
1057                                     data->assoc_info.req_ies,
1058                                     data->assoc_info.req_ies_len);
1059                 return;
1060         }
1061 #endif /* CONFIG_AP */
1062
1063         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1064         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1065                 return;
1066
1067         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1068         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1069                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1070         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
1071             (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
1072              os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0)) {
1073                 wpa_msg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1074                         MACSTR, MAC2STR(bssid));
1075                 bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
1076                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1077                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1078                 if (bssid_changed)
1079                         wpas_notify_bssid_changed(wpa_s);
1080
1081                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1082                         wpa_clear_keys(wpa_s, bssid);
1083                 }
1084                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1085                         wpa_supplicant_disassociate(
1086                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1087                         return;
1088                 }
1089                 if (wpa_s->current_ssid) {
1090                         struct wpa_bss *bss = NULL;
1091                         struct wpa_ssid *ssid = wpa_s->current_ssid;
1092                         if (ssid->ssid_len > 0)
1093                                 bss = wpa_bss_get(wpa_s, bssid,
1094                                                   ssid->ssid, ssid->ssid_len);
1095                         if (!bss)
1096                                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1097                         if (bss)
1098                                 wpa_s->current_bss = bss;
1099                 }
1100         }
1101
1102 #ifdef CONFIG_SME
1103         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1104         wpa_s->sme.prev_bssid_set = 1;
1105 #endif /* CONFIG_SME */
1106
1107         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1108         if (wpa_s->current_ssid) {
1109                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1110                  * initialized before association, but for other modes,
1111                  * initialize PC/SC here, if the current configuration needs
1112                  * smartcard or SIM/USIM. */
1113                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1114         }
1115         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1116         if (wpa_s->l2)
1117                 l2_packet_notify_auth_start(wpa_s->l2);
1118
1119         /*
1120          * Set portEnabled first to FALSE in order to get EAP state machine out
1121          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1122          * state machine may transit to AUTHENTICATING state based on obsolete
1123          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1124          * AUTHENTICATED without ever giving chance to EAP state machine to
1125          * reset the state.
1126          */
1127         if (!ft_completed) {
1128                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1129                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1130         }
1131         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1132                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1133         /* 802.1X::portControl = Auto */
1134         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1135         wpa_s->eapol_received = 0;
1136         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1137             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1138             (wpa_s->current_ssid &&
1139              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1140                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1141                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1142         } else if (!ft_completed) {
1143                 /* Timeout for receiving the first EAPOL packet */
1144                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1145         }
1146         wpa_supplicant_cancel_scan(wpa_s);
1147
1148         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1149             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1150                 /*
1151                  * We are done; the driver will take care of RSN 4-way
1152                  * handshake.
1153                  */
1154                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1155                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1156                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1157                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1158         }
1159
1160         if (wpa_s->pending_eapol_rx) {
1161                 struct os_time now, age;
1162                 os_get_time(&now);
1163                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1164                 if (age.sec == 0 && age.usec < 100000 &&
1165                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1166                     0) {
1167                         wpa_printf(MSG_DEBUG, "Process pending EAPOL frame "
1168                                    "that was received just before association "
1169                                    "notification");
1170                         wpa_supplicant_rx_eapol(
1171                                 wpa_s, wpa_s->pending_eapol_rx_src,
1172                                 wpabuf_head(wpa_s->pending_eapol_rx),
1173                                 wpabuf_len(wpa_s->pending_eapol_rx));
1174                 }
1175                 wpabuf_free(wpa_s->pending_eapol_rx);
1176                 wpa_s->pending_eapol_rx = NULL;
1177         }
1178
1179 #ifdef CONFIG_BGSCAN
1180         if (wpa_s->current_ssid != wpa_s->bgscan_ssid) {
1181                 bgscan_deinit(wpa_s);
1182                 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
1183                         if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
1184                                 wpa_printf(MSG_DEBUG, "Failed to initialize "
1185                                            "bgscan");
1186                                 /*
1187                                  * Live without bgscan; it is only used as a
1188                                  * roaming optimization, so the initial
1189                                  * connection is not affected.
1190                                  */
1191                         } else
1192                                 wpa_s->bgscan_ssid = wpa_s->current_ssid;
1193                 } else
1194                         wpa_s->bgscan_ssid = NULL;
1195         }
1196 #endif /* CONFIG_BGSCAN */
1197
1198         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1199              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1200             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1201             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1202                 /* Set static WEP keys again */
1203                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1204         }
1205 }
1206
1207
1208 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1209                                           u16 reason_code)
1210 {
1211         const u8 *bssid;
1212 #ifdef CONFIG_SME
1213         int authenticating;
1214         u8 prev_pending_bssid[ETH_ALEN];
1215
1216         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1217         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1218 #endif /* CONFIG_SME */
1219
1220         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1221                 /*
1222                  * At least Host AP driver and a Prism3 card seemed to be
1223                  * generating streams of disconnected events when configuring
1224                  * IBSS for WPA-None. Ignore them for now.
1225                  */
1226                 wpa_printf(MSG_DEBUG, "Disconnect event - ignore in "
1227                            "IBSS/WPA-None mode");
1228                 return;
1229         }
1230
1231         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1232             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1233                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1234                         "pre-shared key may be incorrect");
1235         }
1236         if (!wpa_s->auto_reconnect_disabled ||
1237             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1238                 wpa_printf(MSG_DEBUG, "WPA: Auto connect enabled: try to "
1239                            "reconnect (wps=%d)",
1240                            wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
1241                 if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1242                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1243         } else {
1244                 wpa_printf(MSG_DEBUG, "WPA: Auto connect disabled: do not try "
1245                            "to re-connect");
1246                 wpa_s->reassociate = 0;
1247                 wpa_s->disconnected = 1;
1248         }
1249         bssid = wpa_s->bssid;
1250         if (is_zero_ether_addr(bssid))
1251                 bssid = wpa_s->pending_bssid;
1252         wpa_blacklist_add(wpa_s, bssid);
1253         wpa_sm_notify_disassoc(wpa_s->wpa);
1254         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1255                 " reason=%d",
1256                 MAC2STR(bssid), reason_code);
1257         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1258                 wpa_printf(MSG_DEBUG, "Disconnect event - remove keys");
1259                 wpa_s->keys_cleared = 0;
1260                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1261         }
1262         wpa_supplicant_mark_disassoc(wpa_s);
1263         bgscan_deinit(wpa_s);
1264         wpa_s->bgscan_ssid = NULL;
1265 #ifdef CONFIG_SME
1266         if (authenticating &&
1267             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1268                 /*
1269                  * mac80211-workaround to force deauth on failed auth cmd,
1270                  * requires us to remain in authenticating state to allow the
1271                  * second authentication attempt to be continued properly.
1272                  */
1273                 wpa_printf(MSG_DEBUG, "SME: Allow pending authentication to "
1274                            "proceed after disconnection event");
1275                 wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
1276                 os_memcpy(wpa_s->pending_bssid, prev_pending_bssid, ETH_ALEN);
1277         }
1278 #endif /* CONFIG_SME */
1279 }
1280
1281
1282 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1283 static void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx,
1284                                                     void *sock_ctx)
1285 {
1286         struct wpa_supplicant *wpa_s = eloop_ctx;
1287
1288         if (!wpa_s->pending_mic_error_report)
1289                 return;
1290
1291         wpa_printf(MSG_DEBUG, "WPA: Sending pending MIC error report");
1292         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1293         wpa_s->pending_mic_error_report = 0;
1294 }
1295 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1296
1297
1298 static void
1299 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1300                                          union wpa_event_data *data)
1301 {
1302         int pairwise;
1303         struct os_time t;
1304
1305         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1306         pairwise = (data && data->michael_mic_failure.unicast);
1307         os_get_time(&t);
1308         if ((wpa_s->last_michael_mic_error &&
1309              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1310             wpa_s->pending_mic_error_report) {
1311                 if (wpa_s->pending_mic_error_report) {
1312                         /*
1313                          * Send the pending MIC error report immediately since
1314                          * we are going to start countermeasures and AP better
1315                          * do the same.
1316                          */
1317                         wpa_sm_key_request(wpa_s->wpa, 1,
1318                                            wpa_s->pending_mic_error_pairwise);
1319                 }
1320
1321                 /* Send the new MIC error report immediately since we are going
1322                  * to start countermeasures and AP better do the same.
1323                  */
1324                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1325
1326                 /* initialize countermeasures */
1327                 wpa_s->countermeasures = 1;
1328                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1329
1330                 /*
1331                  * Need to wait for completion of request frame. We do not get
1332                  * any callback for the message completion, so just wait a
1333                  * short while and hope for the best. */
1334                 os_sleep(0, 10000);
1335
1336                 wpa_drv_set_countermeasures(wpa_s, 1);
1337                 wpa_supplicant_deauthenticate(wpa_s,
1338                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1339                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1340                                      wpa_s, NULL);
1341                 eloop_register_timeout(60, 0,
1342                                        wpa_supplicant_stop_countermeasures,
1343                                        wpa_s, NULL);
1344                 /* TODO: mark the AP rejected for 60 second. STA is
1345                  * allowed to associate with another AP.. */
1346         } else {
1347 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1348                 if (wpa_s->mic_errors_seen) {
1349                         /*
1350                          * Reduce the effectiveness of Michael MIC error
1351                          * reports as a means for attacking against TKIP if
1352                          * more than one MIC failure is noticed with the same
1353                          * PTK. We delay the transmission of the reports by a
1354                          * random time between 0 and 60 seconds in order to
1355                          * force the attacker wait 60 seconds before getting
1356                          * the information on whether a frame resulted in a MIC
1357                          * failure.
1358                          */
1359                         u8 rval[4];
1360                         int sec;
1361
1362                         if (os_get_random(rval, sizeof(rval)) < 0)
1363                                 sec = os_random() % 60;
1364                         else
1365                                 sec = WPA_GET_BE32(rval) % 60;
1366                         wpa_printf(MSG_DEBUG, "WPA: Delay MIC error report %d "
1367                                    "seconds", sec);
1368                         wpa_s->pending_mic_error_report = 1;
1369                         wpa_s->pending_mic_error_pairwise = pairwise;
1370                         eloop_cancel_timeout(
1371                                 wpa_supplicant_delayed_mic_error_report,
1372                                 wpa_s, NULL);
1373                         eloop_register_timeout(
1374                                 sec, os_random() % 1000000,
1375                                 wpa_supplicant_delayed_mic_error_report,
1376                                 wpa_s, NULL);
1377                 } else {
1378                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1379                 }
1380 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1381                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1382 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1383         }
1384         wpa_s->last_michael_mic_error = t.sec;
1385         wpa_s->mic_errors_seen++;
1386 }
1387
1388
1389 #ifdef CONFIG_TERMINATE_ONLASTIF
1390 static int any_interfaces(struct wpa_supplicant *head)
1391 {
1392         struct wpa_supplicant *wpa_s;
1393
1394         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1395                 if (!wpa_s->interface_removed)
1396                         return 1;
1397         return 0;
1398 }
1399 #endif /* CONFIG_TERMINATE_ONLASTIF */
1400
1401
1402 static void
1403 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1404                                       union wpa_event_data *data)
1405 {
1406         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1407                 return;
1408
1409         switch (data->interface_status.ievent) {
1410         case EVENT_INTERFACE_ADDED:
1411                 if (!wpa_s->interface_removed)
1412                         break;
1413                 wpa_s->interface_removed = 0;
1414                 wpa_printf(MSG_DEBUG, "Configured interface was added.");
1415                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1416                         wpa_printf(MSG_INFO, "Failed to initialize the driver "
1417                                    "after interface was added.");
1418                 }
1419                 break;
1420         case EVENT_INTERFACE_REMOVED:
1421                 wpa_printf(MSG_DEBUG, "Configured interface was removed.");
1422                 wpa_s->interface_removed = 1;
1423                 wpa_supplicant_mark_disassoc(wpa_s);
1424                 l2_packet_deinit(wpa_s->l2);
1425                 wpa_s->l2 = NULL;
1426 #ifdef CONFIG_TERMINATE_ONLASTIF
1427                 /* check if last interface */
1428                 if (!any_interfaces(wpa_s->global->ifaces))
1429                         eloop_terminate();
1430 #endif /* CONFIG_TERMINATE_ONLASTIF */
1431                 break;
1432         }
1433 }
1434
1435
1436 #ifdef CONFIG_PEERKEY
1437 static void
1438 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
1439                               union wpa_event_data *data)
1440 {
1441         if (data == NULL)
1442                 return;
1443         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
1444 }
1445 #endif /* CONFIG_PEERKEY */
1446
1447
1448 #ifdef CONFIG_IEEE80211R
1449 static void
1450 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
1451                                  union wpa_event_data *data)
1452 {
1453         if (data == NULL)
1454                 return;
1455
1456         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
1457                                     data->ft_ies.ies_len,
1458                                     data->ft_ies.ft_action,
1459                                     data->ft_ies.target_ap,
1460                                     data->ft_ies.ric_ies,
1461                                     data->ft_ies.ric_ies_len) < 0) {
1462                 /* TODO: prevent MLME/driver from trying to associate? */
1463         }
1464 }
1465 #endif /* CONFIG_IEEE80211R */
1466
1467
1468 #ifdef CONFIG_IBSS_RSN
1469 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
1470                                                 union wpa_event_data *data)
1471 {
1472         if (data == NULL)
1473                 return;
1474         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
1475 }
1476 #endif /* CONFIG_IBSS_RSN */
1477
1478
1479 #ifdef CONFIG_IEEE80211R
1480 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
1481                          size_t len)
1482 {
1483         const u8 *sta_addr, *target_ap_addr;
1484         u16 status;
1485
1486         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
1487         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1488                 return; /* only SME case supported for now */
1489         if (len < 1 + 2 * ETH_ALEN + 2)
1490                 return;
1491         if (data[0] != 2)
1492                 return; /* Only FT Action Response is supported for now */
1493         sta_addr = data + 1;
1494         target_ap_addr = data + 1 + ETH_ALEN;
1495         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
1496         wpa_printf(MSG_DEBUG, "FT: Received FT Action Response: STA " MACSTR
1497                    " TargetAP " MACSTR " status %u",
1498                    MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
1499
1500         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
1501                 wpa_printf(MSG_DEBUG, "FT: Foreign STA Address " MACSTR
1502                            " in FT Action Response", MAC2STR(sta_addr));
1503                 return;
1504         }
1505
1506         if (status) {
1507                 wpa_printf(MSG_DEBUG, "FT: FT Action Response indicates "
1508                            "failure (status code %d)", status);
1509                 /* TODO: report error to FT code(?) */
1510                 return;
1511         }
1512
1513         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
1514                                     len - (1 + 2 * ETH_ALEN + 2), 1,
1515                                     target_ap_addr, NULL, 0) < 0)
1516                 return;
1517
1518 #ifdef CONFIG_SME
1519         {
1520                 struct wpa_bss *bss;
1521                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
1522                 if (bss)
1523                         wpa_s->sme.freq = bss->freq;
1524                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
1525                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
1526                               WLAN_AUTH_FT);
1527         }
1528 #endif /* CONFIG_SME */
1529 }
1530 #endif /* CONFIG_IEEE80211R */
1531
1532
1533 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1534                           union wpa_event_data *data)
1535 {
1536         struct wpa_supplicant *wpa_s = ctx;
1537         u16 reason_code = 0;
1538
1539         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
1540             event != EVENT_INTERFACE_ENABLED &&
1541             event != EVENT_INTERFACE_STATUS) {
1542                 wpa_printf(MSG_DEBUG, "Ignore event %d while interface is "
1543                            "disabled", event);
1544                 return;
1545         }
1546
1547         wpa_printf(MSG_DEBUG, "Event %d received on interface %s",
1548                    event, wpa_s->ifname);
1549
1550         switch (event) {
1551         case EVENT_AUTH:
1552                 sme_event_auth(wpa_s, data);
1553                 break;
1554         case EVENT_ASSOC:
1555                 wpa_supplicant_event_assoc(wpa_s, data);
1556                 break;
1557         case EVENT_DISASSOC:
1558                 wpa_printf(MSG_DEBUG, "Disassociation notification");
1559                 if (data) {
1560                         wpa_printf(MSG_DEBUG, " * reason %u",
1561                                    data->disassoc_info.reason_code);
1562                         if (data->disassoc_info.addr)
1563                                 wpa_printf(MSG_DEBUG, " * address " MACSTR,
1564                                            MAC2STR(data->disassoc_info.addr));
1565                 }
1566 #ifdef CONFIG_AP
1567                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
1568                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1569                                                data->disassoc_info.addr);
1570                         break;
1571                 }
1572 #endif /* CONFIG_AP */
1573                 if (data) {
1574                         reason_code = data->disassoc_info.reason_code;
1575                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
1576                                     data->disassoc_info.ie,
1577                                     data->disassoc_info.ie_len);
1578 #ifdef CONFIG_P2P
1579                         wpas_p2p_disassoc_notif(
1580                                 wpa_s, data->disassoc_info.addr, reason_code,
1581                                 data->disassoc_info.ie,
1582                                 data->disassoc_info.ie_len);
1583 #endif /* CONFIG_P2P */
1584                 }
1585                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
1586                         sme_event_disassoc(wpa_s, data);
1587                 /* fall through */
1588         case EVENT_DEAUTH:
1589                 if (event == EVENT_DEAUTH) {
1590                         wpa_printf(MSG_DEBUG, "Deauthentication notification");
1591                         if (data) {
1592                                 reason_code = data->deauth_info.reason_code;
1593                                 wpa_printf(MSG_DEBUG, " * reason %u",
1594                                            data->deauth_info.reason_code);
1595                                 if (data->deauth_info.addr) {
1596                                         wpa_printf(MSG_DEBUG, " * address "
1597                                                    MACSTR,
1598                                                    MAC2STR(data->deauth_info.
1599                                                            addr));
1600                                 }
1601                                 wpa_hexdump(MSG_DEBUG,
1602                                             "Deauthentication frame IE(s)",
1603                                             data->deauth_info.ie,
1604                                             data->deauth_info.ie_len);
1605 #ifdef CONFIG_P2P
1606                                 wpas_p2p_deauth_notif(
1607                                         wpa_s, data->deauth_info.addr,
1608                                         reason_code,
1609                                         data->deauth_info.ie,
1610                                         data->deauth_info.ie_len);
1611 #endif /* CONFIG_P2P */
1612                         }
1613                 }
1614 #ifdef CONFIG_AP
1615                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
1616                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
1617                                                data->deauth_info.addr);
1618                         break;
1619                 }
1620 #endif /* CONFIG_AP */
1621                 wpa_supplicant_event_disassoc(wpa_s, reason_code);
1622                 break;
1623         case EVENT_MICHAEL_MIC_FAILURE:
1624                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
1625                 break;
1626 #ifndef CONFIG_NO_SCAN_PROCESSING
1627         case EVENT_SCAN_RESULTS:
1628                 wpa_supplicant_event_scan_results(wpa_s, data);
1629                 break;
1630 #endif /* CONFIG_NO_SCAN_PROCESSING */
1631         case EVENT_ASSOCINFO:
1632                 wpa_supplicant_event_associnfo(wpa_s, data);
1633                 break;
1634         case EVENT_INTERFACE_STATUS:
1635                 wpa_supplicant_event_interface_status(wpa_s, data);
1636                 break;
1637         case EVENT_PMKID_CANDIDATE:
1638                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
1639                 break;
1640 #ifdef CONFIG_PEERKEY
1641         case EVENT_STKSTART:
1642                 wpa_supplicant_event_stkstart(wpa_s, data);
1643                 break;
1644 #endif /* CONFIG_PEERKEY */
1645 #ifdef CONFIG_IEEE80211R
1646         case EVENT_FT_RESPONSE:
1647                 wpa_supplicant_event_ft_response(wpa_s, data);
1648                 break;
1649 #endif /* CONFIG_IEEE80211R */
1650 #ifdef CONFIG_IBSS_RSN
1651         case EVENT_IBSS_RSN_START:
1652                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
1653                 break;
1654 #endif /* CONFIG_IBSS_RSN */
1655         case EVENT_ASSOC_REJECT:
1656                 sme_event_assoc_reject(wpa_s, data);
1657                 break;
1658         case EVENT_AUTH_TIMED_OUT:
1659                 sme_event_auth_timed_out(wpa_s, data);
1660                 break;
1661         case EVENT_ASSOC_TIMED_OUT:
1662                 sme_event_assoc_timed_out(wpa_s, data);
1663                 break;
1664 #ifdef CONFIG_AP
1665         case EVENT_TX_STATUS:
1666                 wpa_printf(MSG_DEBUG, "EVENT_TX_STATUS on %s dst=" MACSTR
1667                            " type=%d stype=%d pending_dst=" MACSTR,
1668                            wpa_s->ifname, MAC2STR(data->tx_status.dst),
1669                            data->tx_status.type, data->tx_status.stype,
1670                            MAC2STR(wpa_s->parent->pending_action_dst));
1671                 if (wpa_s->ap_iface == NULL) {
1672 #ifdef CONFIG_P2P
1673                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1674                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
1675                                 wpas_send_action_tx_status(
1676                                         wpa_s, data->tx_status.dst,
1677                                         data->tx_status.data,
1678                                         data->tx_status.data_len,
1679                                         data->tx_status.ack);
1680 #endif /* CONFIG_P2P */
1681                         break;
1682                 }
1683 #ifdef CONFIG_P2P
1684                 /*
1685                  * Catch TX status events for Action frames we sent via group
1686                  * interface in GO mode.
1687                  */
1688                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
1689                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
1690                     os_memcmp(wpa_s->parent->pending_action_dst,
1691                               data->tx_status.dst, ETH_ALEN) == 0) {
1692                         wpas_send_action_tx_status(
1693                                 wpa_s->parent, data->tx_status.dst,
1694                                 data->tx_status.data,
1695                                 data->tx_status.data_len,
1696                                 data->tx_status.ack);
1697                         break;
1698                 }
1699 #endif /* CONFIG_P2P */
1700                 switch (data->tx_status.type) {
1701                 case WLAN_FC_TYPE_MGMT:
1702                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
1703                                       data->tx_status.data_len,
1704                                       data->tx_status.stype,
1705                                       data->tx_status.ack);
1706                         break;
1707                 case WLAN_FC_TYPE_DATA:
1708                         ap_tx_status(wpa_s, data->tx_status.dst,
1709                                      data->tx_status.data,
1710                                      data->tx_status.data_len,
1711                                      data->tx_status.ack);
1712                         break;
1713                 }
1714                 break;
1715         case EVENT_RX_FROM_UNKNOWN:
1716                 if (wpa_s->ap_iface == NULL)
1717                         break;
1718                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.frame,
1719                                        data->rx_from_unknown.len);
1720                 break;
1721         case EVENT_RX_MGMT:
1722                 if (wpa_s->ap_iface == NULL) {
1723 #ifdef CONFIG_P2P
1724                         u16 fc, stype;
1725                         const struct ieee80211_mgmt *mgmt;
1726                         mgmt = (const struct ieee80211_mgmt *)
1727                                 data->rx_mgmt.frame;
1728                         fc = le_to_host16(mgmt->frame_control);
1729                         stype = WLAN_FC_GET_STYPE(fc);
1730                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
1731                             data->rx_mgmt.frame_len > 24) {
1732                                 const u8 *src = mgmt->sa;
1733                                 const u8 *ie = mgmt->u.probe_req.variable;
1734                                 size_t ie_len = data->rx_mgmt.frame_len -
1735                                         (mgmt->u.probe_req.variable -
1736                                          data->rx_mgmt.frame);
1737                                 wpas_p2p_probe_req_rx(wpa_s, src, ie, ie_len);
1738                                 break;
1739                         }
1740 #endif /* CONFIG_P2P */
1741                         wpa_printf(MSG_DEBUG, "AP: ignore received management "
1742                                    "frame in non-AP mode");
1743                         break;
1744                 }
1745                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
1746                 break;
1747 #endif /* CONFIG_AP */
1748         case EVENT_RX_ACTION:
1749                 wpa_printf(MSG_DEBUG, "Received Action frame: SA=" MACSTR
1750                            " Category=%u DataLen=%d freq=%d MHz",
1751                            MAC2STR(data->rx_action.sa),
1752                            data->rx_action.category, (int) data->rx_action.len,
1753                            data->rx_action.freq);
1754 #ifdef CONFIG_IEEE80211R
1755                 if (data->rx_action.category == WLAN_ACTION_FT) {
1756                         ft_rx_action(wpa_s, data->rx_action.data,
1757                                      data->rx_action.len);
1758                         break;
1759                 }
1760 #endif /* CONFIG_IEEE80211R */
1761 #ifdef CONFIG_P2P
1762                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
1763                                    data->rx_action.sa,
1764                                    data->rx_action.bssid,
1765                                    data->rx_action.category,
1766                                    data->rx_action.data,
1767                                    data->rx_action.len, data->rx_action.freq);
1768 #endif /* CONFIG_P2P */
1769                 break;
1770 #ifdef CONFIG_P2P
1771         case EVENT_REMAIN_ON_CHANNEL:
1772                 wpas_p2p_remain_on_channel_cb(
1773                         wpa_s, data->remain_on_channel.freq,
1774                         data->remain_on_channel.duration);
1775                 break;
1776         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
1777                 wpas_p2p_cancel_remain_on_channel_cb(
1778                         wpa_s, data->remain_on_channel.freq);
1779                 break;
1780         case EVENT_RX_PROBE_REQ:
1781                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
1782                                       data->rx_probe_req.ie,
1783                                       data->rx_probe_req.ie_len);
1784                 break;
1785 #endif /* CONFIG_P2P */
1786 #ifdef CONFIG_CLIENT_MLME
1787         case EVENT_MLME_RX: {
1788                 struct ieee80211_rx_status rx_status;
1789                 os_memset(&rx_status, 0, sizeof(rx_status));
1790                 rx_status.freq = data->mlme_rx.freq;
1791                 rx_status.channel = data->mlme_rx.channel;
1792                 rx_status.ssi = data->mlme_rx.ssi;
1793                 ieee80211_sta_rx(wpa_s, data->mlme_rx.buf, data->mlme_rx.len,
1794                                  &rx_status);
1795                 break;
1796         }
1797 #endif /* CONFIG_CLIENT_MLME */
1798         case EVENT_EAPOL_RX:
1799                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
1800                                         data->eapol_rx.data,
1801                                         data->eapol_rx.data_len);
1802                 break;
1803         case EVENT_SIGNAL_CHANGE:
1804                 bgscan_notify_signal_change(
1805                         wpa_s, data->signal_change.above_threshold,
1806                         data->signal_change.current_signal,
1807                         data->signal_change.current_noise,
1808                         data->signal_change.current_txrate);
1809                 break;
1810         case EVENT_INTERFACE_ENABLED:
1811                 wpa_printf(MSG_DEBUG, "Interface was enabled");
1812                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
1813 #ifdef CONFIG_AP
1814                         if (!wpa_s->ap_iface) {
1815                                 wpa_supplicant_set_state(wpa_s,
1816                                                          WPA_DISCONNECTED);
1817                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1818                         } else
1819                                 wpa_supplicant_set_state(wpa_s,
1820                                                          WPA_COMPLETED);
1821 #else /* CONFIG_AP */
1822                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1823                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1824 #endif /* CONFIG_AP */
1825                 }
1826                 break;
1827         case EVENT_INTERFACE_DISABLED:
1828                 wpa_printf(MSG_DEBUG, "Interface was disabled");
1829                 wpa_supplicant_mark_disassoc(wpa_s);
1830                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1831                 break;
1832         case EVENT_CHANNEL_LIST_CHANGED:
1833 #ifdef CONFIG_P2P
1834                 wpas_p2p_update_channel_list(wpa_s);
1835 #endif /* CONFIG_P2P */
1836                 break;
1837         default:
1838                 wpa_printf(MSG_INFO, "Unknown event %d", event);
1839                 break;
1840         }
1841 }