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