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