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