WPS: Maintain more AP state during WPS PIN iteration
[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
45
46 static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
47                               struct wpa_ssid *ssid)
48 {
49         struct os_time now;
50
51         if (ssid == NULL || ssid->disabled_until.sec == 0)
52                 return 0;
53
54         os_get_time(&now);
55         if (ssid->disabled_until.sec > now.sec)
56                 return ssid->disabled_until.sec - now.sec;
57
58         wpas_clear_temp_disabled(wpa_s, ssid, 0);
59
60         return 0;
61 }
62
63
64 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
65 {
66         struct wpa_ssid *ssid, *old_ssid;
67         int res;
68
69         if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
70                 return 0;
71
72         wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
73                 "information");
74         ssid = wpa_supplicant_get_ssid(wpa_s);
75         if (ssid == NULL) {
76                 wpa_msg(wpa_s, MSG_INFO,
77                         "No network configuration found for the current AP");
78                 return -1;
79         }
80
81         if (wpas_network_disabled(wpa_s, ssid)) {
82                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
83                 return -1;
84         }
85
86         res = wpas_temp_disabled(wpa_s, ssid);
87         if (res > 0) {
88                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
89                         "disabled for %d second(s)", res);
90                 return -1;
91         }
92
93         wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
94                 "current AP");
95         if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
96                 u8 wpa_ie[80];
97                 size_t wpa_ie_len = sizeof(wpa_ie);
98                 wpa_supplicant_set_suites(wpa_s, NULL, ssid,
99                                           wpa_ie, &wpa_ie_len);
100         } else {
101                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
102         }
103
104         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
105                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
106         old_ssid = wpa_s->current_ssid;
107         wpa_s->current_ssid = ssid;
108         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
109         wpa_supplicant_initiate_eapol(wpa_s);
110         if (old_ssid != wpa_s->current_ssid)
111                 wpas_notify_network_changed(wpa_s);
112
113         return 0;
114 }
115
116
117 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
118 {
119         struct wpa_supplicant *wpa_s = eloop_ctx;
120
121         if (wpa_s->countermeasures) {
122                 wpa_s->countermeasures = 0;
123                 wpa_drv_set_countermeasures(wpa_s, 0);
124                 wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
125                 wpa_supplicant_req_scan(wpa_s, 0, 0);
126         }
127 }
128
129
130 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
131 {
132         int bssid_changed;
133
134         wnm_bss_keep_alive_deinit(wpa_s);
135
136 #ifdef CONFIG_IBSS_RSN
137         ibss_rsn_deinit(wpa_s->ibss_rsn);
138         wpa_s->ibss_rsn = NULL;
139 #endif /* CONFIG_IBSS_RSN */
140
141 #ifdef CONFIG_AP
142         wpa_supplicant_ap_deinit(wpa_s);
143 #endif /* CONFIG_AP */
144
145         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
146                 return;
147
148         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
149         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
150         os_memset(wpa_s->bssid, 0, ETH_ALEN);
151         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
152 #ifdef CONFIG_SME
153         wpa_s->sme.prev_bssid_set = 0;
154 #endif /* CONFIG_SME */
155 #ifdef CONFIG_P2P
156         os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
157 #endif /* CONFIG_P2P */
158         wpa_s->current_bss = NULL;
159         wpa_s->assoc_freq = 0;
160 #ifdef CONFIG_IEEE80211R
161 #ifdef CONFIG_SME
162         if (wpa_s->sme.ft_ies)
163                 sme_update_ft_ies(wpa_s, NULL, NULL, 0);
164 #endif /* CONFIG_SME */
165 #endif /* CONFIG_IEEE80211R */
166
167         if (bssid_changed)
168                 wpas_notify_bssid_changed(wpa_s);
169
170         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
171         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
172         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
173                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
174         wpa_s->ap_ies_from_associnfo = 0;
175         wpa_s->current_ssid = NULL;
176         wpa_s->key_mgmt = 0;
177 }
178
179
180 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
181 {
182         struct wpa_ie_data ie;
183         int pmksa_set = -1;
184         size_t i;
185
186         if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
187             ie.pmkid == NULL)
188                 return;
189
190         for (i = 0; i < ie.num_pmkid; i++) {
191                 pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
192                                                     ie.pmkid + i * PMKID_LEN,
193                                                     NULL, NULL, 0);
194                 if (pmksa_set == 0) {
195                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
196                         break;
197                 }
198         }
199
200         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
201                 "PMKSA cache", pmksa_set == 0 ? "" : "not ");
202 }
203
204
205 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
206                                                  union wpa_event_data *data)
207 {
208         if (data == NULL) {
209                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
210                         "event");
211                 return;
212         }
213         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
214                 " index=%d preauth=%d",
215                 MAC2STR(data->pmkid_candidate.bssid),
216                 data->pmkid_candidate.index,
217                 data->pmkid_candidate.preauth);
218
219         pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
220                             data->pmkid_candidate.index,
221                             data->pmkid_candidate.preauth);
222 }
223
224
225 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
226 {
227         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
228             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
229                 return 0;
230
231 #ifdef IEEE8021X_EAPOL
232         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
233             wpa_s->current_ssid &&
234             !(wpa_s->current_ssid->eapol_flags &
235               (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
236                EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
237                 /* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
238                  * plaintext or static WEP keys). */
239                 return 0;
240         }
241 #endif /* IEEE8021X_EAPOL */
242
243         return 1;
244 }
245
246
247 /**
248  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
249  * @wpa_s: pointer to wpa_supplicant data
250  * @ssid: Configuration data for the network
251  * Returns: 0 on success, -1 on failure
252  *
253  * This function is called when starting authentication with a network that is
254  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
255  */
256 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
257                               struct wpa_ssid *ssid)
258 {
259 #ifdef IEEE8021X_EAPOL
260 #ifdef PCSC_FUNCS
261         int aka = 0, sim = 0, type;
262
263         if (ssid->eap.pcsc == NULL || wpa_s->scard != NULL)
264                 return 0;
265
266         if (ssid->eap.eap_methods == NULL) {
267                 sim = 1;
268                 aka = 1;
269         } else {
270                 struct eap_method_type *eap = ssid->eap.eap_methods;
271                 while (eap->vendor != EAP_VENDOR_IETF ||
272                        eap->method != EAP_TYPE_NONE) {
273                         if (eap->vendor == EAP_VENDOR_IETF) {
274                                 if (eap->method == EAP_TYPE_SIM)
275                                         sim = 1;
276                                 else if (eap->method == EAP_TYPE_AKA ||
277                                          eap->method == EAP_TYPE_AKA_PRIME)
278                                         aka = 1;
279                         }
280                         eap++;
281                 }
282         }
283
284         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
285                 sim = 0;
286         if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
287             eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
288             NULL)
289                 aka = 0;
290
291         if (!sim && !aka) {
292                 wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
293                         "use SIM, but neither EAP-SIM nor EAP-AKA are "
294                         "enabled");
295                 return 0;
296         }
297
298         wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
299                 "(sim=%d aka=%d) - initialize PCSC", sim, aka);
300         if (sim && aka)
301                 type = SCARD_TRY_BOTH;
302         else if (aka)
303                 type = SCARD_USIM_ONLY;
304         else
305                 type = SCARD_GSM_SIM_ONLY;
306
307         wpa_s->scard = scard_init(type, NULL);
308         if (wpa_s->scard == NULL) {
309                 wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
310                         "(pcsc-lite)");
311                 return -1;
312         }
313         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
314         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
315 #endif /* PCSC_FUNCS */
316 #endif /* IEEE8021X_EAPOL */
317
318         return 0;
319 }
320
321
322 #ifndef CONFIG_NO_SCAN_PROCESSING
323 static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
324                                         struct wpa_ssid *ssid)
325 {
326         int i, privacy = 0;
327
328         if (ssid->mixed_cell)
329                 return 1;
330
331 #ifdef CONFIG_WPS
332         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
333                 return 1;
334 #endif /* CONFIG_WPS */
335
336         for (i = 0; i < NUM_WEP_KEYS; i++) {
337                 if (ssid->wep_key_len[i]) {
338                         privacy = 1;
339                         break;
340                 }
341         }
342 #ifdef IEEE8021X_EAPOL
343         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
344             ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
345                                  EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
346                 privacy = 1;
347 #endif /* IEEE8021X_EAPOL */
348
349         if (wpa_key_mgmt_wpa(ssid->key_mgmt))
350                 privacy = 1;
351
352         if (bss->caps & IEEE80211_CAP_PRIVACY)
353                 return privacy;
354         return !privacy;
355 }
356
357
358 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
359                                          struct wpa_ssid *ssid,
360                                          struct wpa_scan_res *bss)
361 {
362         struct wpa_ie_data ie;
363         int proto_match = 0;
364         const u8 *rsn_ie, *wpa_ie;
365         int ret;
366         int wep_ok;
367
368         ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
369         if (ret >= 0)
370                 return ret;
371
372         /* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
373         wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
374                 (((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
375                   ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
376                  (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
377
378         rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
379         while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
380                 proto_match++;
381
382                 if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
383                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - parse "
384                                 "failed");
385                         break;
386                 }
387
388                 if (wep_ok &&
389                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
390                 {
391                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
392                                 "in RSN IE");
393                         return 1;
394                 }
395
396                 if (!(ie.proto & ssid->proto)) {
397                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - proto "
398                                 "mismatch");
399                         break;
400                 }
401
402                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
403                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - PTK "
404                                 "cipher mismatch");
405                         break;
406                 }
407
408                 if (!(ie.group_cipher & ssid->group_cipher)) {
409                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - GTK "
410                                 "cipher mismatch");
411                         break;
412                 }
413
414                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
415                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - key mgmt "
416                                 "mismatch");
417                         break;
418                 }
419
420 #ifdef CONFIG_IEEE80211W
421                 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
422                     ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
423                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip RSN IE - no mgmt "
424                                 "frame protection");
425                         break;
426                 }
427 #endif /* CONFIG_IEEE80211W */
428
429                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on RSN IE");
430                 return 1;
431         }
432
433         wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
434         while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
435                 proto_match++;
436
437                 if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
438                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - parse "
439                                 "failed");
440                         break;
441                 }
442
443                 if (wep_ok &&
444                     (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
445                 {
446                         wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on TSN "
447                                 "in WPA IE");
448                         return 1;
449                 }
450
451                 if (!(ie.proto & ssid->proto)) {
452                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - proto "
453                                 "mismatch");
454                         break;
455                 }
456
457                 if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
458                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - PTK "
459                                 "cipher mismatch");
460                         break;
461                 }
462
463                 if (!(ie.group_cipher & ssid->group_cipher)) {
464                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - GTK "
465                                 "cipher mismatch");
466                         break;
467                 }
468
469                 if (!(ie.key_mgmt & ssid->key_mgmt)) {
470                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip WPA IE - key mgmt "
471                                 "mismatch");
472                         break;
473                 }
474
475                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected based on WPA IE");
476                 return 1;
477         }
478
479         if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
480             !rsn_ie) {
481                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow for non-WPA IEEE 802.1X");
482                 return 1;
483         }
484
485         if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
486             wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
487                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
488                 return 0;
489         }
490
491         if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
492                 wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
493                 return 1;
494         }
495
496         wpa_dbg(wpa_s, MSG_DEBUG, "   reject due to mismatch with "
497                 "WPA/WPA2");
498
499         return 0;
500 }
501
502
503 static int freq_allowed(int *freqs, int freq)
504 {
505         int i;
506
507         if (freqs == NULL)
508                 return 1;
509
510         for (i = 0; freqs[i]; i++)
511                 if (freqs[i] == freq)
512                         return 1;
513         return 0;
514 }
515
516
517 static int ht_supported(const struct hostapd_hw_modes *mode)
518 {
519         if (!(mode->flags & HOSTAPD_MODE_FLAG_HT_INFO_KNOWN)) {
520                 /*
521                  * The driver did not indicate whether it supports HT. Assume
522                  * it does to avoid connection issues.
523                  */
524                 return 1;
525         }
526
527         /*
528          * IEEE Std 802.11n-2009 20.1.1:
529          * An HT non-AP STA shall support all EQM rates for one spatial stream.
530          */
531         return mode->mcs_set[0] == 0xff;
532 }
533
534
535 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
536 {
537         const struct hostapd_hw_modes *mode = NULL, *modes;
538         const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
539         const u8 *rate_ie;
540         int i, j, k;
541
542         if (bss->freq == 0)
543                 return 1; /* Cannot do matching without knowing band */
544
545         modes = wpa_s->hw.modes;
546         if (modes == NULL) {
547                 /*
548                  * The driver does not provide any additional information
549                  * about the utilized hardware, so allow the connection attempt
550                  * to continue.
551                  */
552                 return 1;
553         }
554
555         for (i = 0; i < wpa_s->hw.num_modes; i++) {
556                 for (j = 0; j < modes[i].num_channels; j++) {
557                         int freq = modes[i].channels[j].freq;
558                         if (freq == bss->freq) {
559                                 if (mode &&
560                                     mode->mode == HOSTAPD_MODE_IEEE80211G)
561                                         break; /* do not allow 802.11b replace
562                                                 * 802.11g */
563                                 mode = &modes[i];
564                                 break;
565                         }
566                 }
567         }
568
569         if (mode == NULL)
570                 return 0;
571
572         for (i = 0; i < (int) sizeof(scan_ie); i++) {
573                 rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
574                 if (rate_ie == NULL)
575                         continue;
576
577                 for (j = 2; j < rate_ie[1] + 2; j++) {
578                         int flagged = !!(rate_ie[j] & 0x80);
579                         int r = (rate_ie[j] & 0x7f) * 5;
580
581                         /*
582                          * IEEE Std 802.11n-2009 7.3.2.2:
583                          * The new BSS Membership selector value is encoded
584                          * like a legacy basic rate, but it is not a rate and
585                          * only indicates if the BSS members are required to
586                          * support the mandatory features of Clause 20 [HT PHY]
587                          * in order to join the BSS.
588                          */
589                         if (flagged && ((rate_ie[j] & 0x7f) ==
590                                         BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
591                                 if (!ht_supported(mode)) {
592                                         wpa_dbg(wpa_s, MSG_DEBUG,
593                                                 "   hardware does not support "
594                                                 "HT PHY");
595                                         return 0;
596                                 }
597                                 continue;
598                         }
599
600                         if (!flagged)
601                                 continue;
602
603                         /* check for legacy basic rates */
604                         for (k = 0; k < mode->num_rates; k++) {
605                                 if (mode->rates[k] == r)
606                                         break;
607                         }
608                         if (k == mode->num_rates) {
609                                 /*
610                                  * IEEE Std 802.11-2007 7.3.2.2 demands that in
611                                  * order to join a BSS all required rates
612                                  * have to be supported by the hardware.
613                                  */
614                                 wpa_dbg(wpa_s, MSG_DEBUG, "   hardware does "
615                                         "not support required rate %d.%d Mbps",
616                                         r / 10, r % 10);
617                                 return 0;
618                         }
619                 }
620         }
621
622         return 1;
623 }
624
625
626 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
627                                             int i, struct wpa_scan_res *bss,
628                                             struct wpa_ssid *group)
629 {
630         const u8 *ssid_;
631         u8 wpa_ie_len, rsn_ie_len, ssid_len;
632         int wpa;
633         struct wpa_blacklist *e;
634         const u8 *ie;
635         struct wpa_ssid *ssid;
636
637         ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
638         ssid_ = ie ? ie + 2 : (u8 *) "";
639         ssid_len = ie ? ie[1] : 0;
640
641         ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
642         wpa_ie_len = ie ? ie[1] : 0;
643
644         ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
645         rsn_ie_len = ie ? ie[1] : 0;
646
647         wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
648                 "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
649                 i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
650                 wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
651                 wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
652
653         e = wpa_blacklist_get(wpa_s, bss->bssid);
654         if (e) {
655                 int limit = 1;
656                 if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
657                         /*
658                          * When only a single network is enabled, we can
659                          * trigger blacklisting on the first failure. This
660                          * should not be done with multiple enabled networks to
661                          * avoid getting forced to move into a worse ESS on
662                          * single error if there are no other BSSes of the
663                          * current ESS.
664                          */
665                         limit = 0;
666                 }
667                 if (e->count > limit) {
668                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
669                                 "(count=%d limit=%d)", e->count, limit);
670                         return NULL;
671                 }
672         }
673
674         if (ssid_len == 0) {
675                 wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
676                 return NULL;
677         }
678
679         wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
680
681         for (ssid = group; ssid; ssid = ssid->pnext) {
682                 int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
683                 int res;
684
685                 if (wpas_network_disabled(wpa_s, ssid)) {
686                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
687                         continue;
688                 }
689
690                 res = wpas_temp_disabled(wpa_s, ssid);
691                 if (res > 0) {
692                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled "
693                                 "temporarily for %d second(s)", res);
694                         continue;
695                 }
696
697 #ifdef CONFIG_WPS
698                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
699                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
700                                 "(WPS)");
701                         continue;
702                 }
703
704                 if (wpa && ssid->ssid_len == 0 &&
705                     wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
706                         check_ssid = 0;
707
708                 if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
709                         /* Only allow wildcard SSID match if an AP
710                          * advertises active WPS operation that matches
711                          * with our mode. */
712                         check_ssid = 1;
713                         if (ssid->ssid_len == 0 &&
714                             wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
715                                 check_ssid = 0;
716                 }
717 #endif /* CONFIG_WPS */
718
719                 if (ssid->bssid_set && ssid->ssid_len == 0 &&
720                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
721                         check_ssid = 0;
722
723                 if (check_ssid &&
724                     (ssid_len != ssid->ssid_len ||
725                      os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
726                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
727                         continue;
728                 }
729
730                 if (ssid->bssid_set &&
731                     os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
732                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
733                         continue;
734                 }
735
736                 if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss))
737                         continue;
738
739                 if (!wpa &&
740                     !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
741                     !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
742                     !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
743                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - non-WPA network "
744                                 "not allowed");
745                         continue;
746                 }
747
748                 if (!wpa_supplicant_match_privacy(bss, ssid)) {
749                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy "
750                                 "mismatch");
751                         continue;
752                 }
753
754                 if (bss->caps & IEEE80211_CAP_IBSS) {
755                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - IBSS (adhoc) "
756                                 "network");
757                         continue;
758                 }
759
760                 if (!freq_allowed(ssid->freq_list, bss->freq)) {
761                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - frequency not "
762                                 "allowed");
763                         continue;
764                 }
765
766                 if (!rate_match(wpa_s, bss)) {
767                         wpa_dbg(wpa_s, MSG_DEBUG, "   skip - rate sets do "
768                                 "not match");
769                         continue;
770                 }
771
772 #ifdef CONFIG_P2P
773                 /*
774                  * TODO: skip the AP if its P2P IE has Group Formation
775                  * bit set in the P2P Group Capability Bitmap and we
776                  * are not in Group Formation with that device.
777                  */
778 #endif /* CONFIG_P2P */
779
780                 /* Matching configuration found */
781                 return ssid;
782         }
783
784         /* No matching configuration found */
785         return NULL;
786 }
787
788
789 static struct wpa_bss *
790 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
791                           struct wpa_scan_results *scan_res,
792                           struct wpa_ssid *group,
793                           struct wpa_ssid **selected_ssid)
794 {
795         size_t i;
796
797         wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
798                 group->priority);
799
800         for (i = 0; i < scan_res->num; i++) {
801                 struct wpa_scan_res *bss = scan_res->res[i];
802                 const u8 *ie, *ssid;
803                 u8 ssid_len;
804
805                 *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
806                 if (!*selected_ssid)
807                         continue;
808
809                 ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
810                 ssid = ie ? ie + 2 : (u8 *) "";
811                 ssid_len = ie ? ie[1] : 0;
812
813                 wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
814                         " ssid='%s'",
815                         MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
816                 return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
817         }
818
819         return NULL;
820 }
821
822
823 static struct wpa_bss *
824 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
825                             struct wpa_scan_results *scan_res,
826                             struct wpa_ssid **selected_ssid)
827 {
828         struct wpa_bss *selected = NULL;
829         int prio;
830
831         while (selected == NULL) {
832                 for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
833                         selected = wpa_supplicant_select_bss(
834                                 wpa_s, scan_res, wpa_s->conf->pssid[prio],
835                                 selected_ssid);
836                         if (selected)
837                                 break;
838                 }
839
840                 if (selected == NULL && wpa_s->blacklist &&
841                     !wpa_s->countermeasures) {
842                         wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
843                                 "blacklist and try again");
844                         wpa_blacklist_clear(wpa_s);
845                         wpa_s->blacklist_cleared++;
846                 } else if (selected == NULL)
847                         break;
848         }
849
850         return selected;
851 }
852
853
854 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
855                                         int timeout_sec, int timeout_usec)
856 {
857         if (!wpa_supplicant_enabled_networks(wpa_s)) {
858                 /*
859                  * No networks are enabled; short-circuit request so
860                  * we don't wait timeout seconds before transitioning
861                  * to INACTIVE state.
862                  */
863                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
864                 return;
865         }
866         wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
867 }
868
869
870 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
871                            struct wpa_bss *selected,
872                            struct wpa_ssid *ssid)
873 {
874         if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
875                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
876                         "PBC session overlap");
877 #ifdef CONFIG_P2P
878                 if (wpas_p2p_notif_pbc_overlap(wpa_s) == 1)
879                         return -1;
880 #endif /* CONFIG_P2P */
881
882 #ifdef CONFIG_WPS
883                 wpas_wps_cancel(wpa_s);
884 #endif /* CONFIG_WPS */
885                 return -1;
886         }
887
888         /*
889          * Do not trigger new association unless the BSSID has changed or if
890          * reassociation is requested. If we are in process of associating with
891          * the selected BSSID, do not trigger new attempt.
892          */
893         if (wpa_s->reassociate ||
894             (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
895              ((wpa_s->wpa_state != WPA_ASSOCIATING &&
896                wpa_s->wpa_state != WPA_AUTHENTICATING) ||
897               os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
898               0))) {
899                 if (wpa_supplicant_scard_init(wpa_s, ssid)) {
900                         wpa_supplicant_req_new_scan(wpa_s, 10, 0);
901                         return 0;
902                 }
903                 wpa_msg(wpa_s, MSG_DEBUG, "Request association: "
904                         "reassociate: %d  selected: "MACSTR "  bssid: " MACSTR
905                         "  pending: " MACSTR "  wpa_state: %s",
906                         wpa_s->reassociate, MAC2STR(selected->bssid),
907                         MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
908                         wpa_supplicant_state_txt(wpa_s->wpa_state));
909                 wpa_supplicant_associate(wpa_s, selected, ssid);
910         } else {
911                 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with the "
912                         "selected AP");
913         }
914
915         return 0;
916 }
917
918
919 static struct wpa_ssid *
920 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
921 {
922         int prio;
923         struct wpa_ssid *ssid;
924
925         for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
926                 for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
927                 {
928                         if (wpas_network_disabled(wpa_s, ssid))
929                                 continue;
930                         if (ssid->mode == IEEE80211_MODE_IBSS ||
931                             ssid->mode == IEEE80211_MODE_AP)
932                                 return ssid;
933                 }
934         }
935         return NULL;
936 }
937
938
939 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
940  * on BSS added and BSS changed events */
941 static void wpa_supplicant_rsn_preauth_scan_results(
942         struct wpa_supplicant *wpa_s)
943 {
944         struct wpa_bss *bss;
945
946         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
947                 return;
948
949         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
950                 const u8 *ssid, *rsn;
951
952                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
953                 if (ssid == NULL)
954                         continue;
955
956                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
957                 if (rsn == NULL)
958                         continue;
959
960                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
961         }
962
963 }
964
965
966 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
967                                        struct wpa_bss *selected,
968                                        struct wpa_ssid *ssid,
969                                        struct wpa_scan_results *scan_res)
970 {
971         size_t i;
972         struct wpa_scan_res *current_bss = NULL;
973         int min_diff;
974
975         if (wpa_s->reassociate)
976                 return 1; /* explicit request to reassociate */
977         if (wpa_s->wpa_state < WPA_ASSOCIATED)
978                 return 1; /* we are not associated; continue */
979         if (wpa_s->current_ssid == NULL)
980                 return 1; /* unknown current SSID */
981         if (wpa_s->current_ssid != ssid)
982                 return 1; /* different network block */
983
984         if (wpas_driver_bss_selection(wpa_s))
985                 return 0; /* Driver-based roaming */
986
987         for (i = 0; i < scan_res->num; i++) {
988                 struct wpa_scan_res *res = scan_res->res[i];
989                 const u8 *ie;
990                 if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
991                         continue;
992
993                 ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
994                 if (ie == NULL)
995                         continue;
996                 if (ie[1] != wpa_s->current_ssid->ssid_len ||
997                     os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
998                         continue;
999                 current_bss = res;
1000                 break;
1001         }
1002
1003         if (!current_bss)
1004                 return 1; /* current BSS not seen in scan results */
1005
1006 #ifndef CONFIG_NO_ROAMING
1007         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1008         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1009                 MAC2STR(current_bss->bssid), current_bss->level);
1010         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1011                 MAC2STR(selected->bssid), selected->level);
1012
1013         if (wpa_s->current_ssid->bssid_set &&
1014             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1015             0) {
1016                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1017                         "has preferred BSSID");
1018                 return 1;
1019         }
1020
1021         min_diff = 2;
1022         if (current_bss->level < 0) {
1023                 if (current_bss->level < -85)
1024                         min_diff = 1;
1025                 else if (current_bss->level < -80)
1026                         min_diff = 2;
1027                 else if (current_bss->level < -75)
1028                         min_diff = 3;
1029                 else if (current_bss->level < -70)
1030                         min_diff = 4;
1031                 else
1032                         min_diff = 5;
1033         }
1034         if (abs(current_bss->level - selected->level) < min_diff) {
1035                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1036                         "in signal level");
1037                 return 0;
1038         }
1039
1040         return 1;
1041 #else /* CONFIG_NO_ROAMING */
1042         return 0;
1043 #endif /* CONFIG_NO_ROAMING */
1044 }
1045
1046
1047 /* Return < 0 if no scan results could be fetched or if scan results should not
1048  * be shared with other virtual interfaces. */
1049 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1050                                               union wpa_event_data *data)
1051 {
1052         struct wpa_bss *selected;
1053         struct wpa_ssid *ssid = NULL;
1054         struct wpa_scan_results *scan_res;
1055         int ap = 0;
1056
1057 #ifdef CONFIG_AP
1058         if (wpa_s->ap_iface)
1059                 ap = 1;
1060 #endif /* CONFIG_AP */
1061
1062         wpa_supplicant_notify_scanning(wpa_s, 0);
1063
1064 #ifdef CONFIG_P2P
1065         if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
1066             wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending) {
1067                 wpa_s->p2p_cb_on_scan_complete = 0;
1068                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1069                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1070                                 "stopped scan processing");
1071                         return -1;
1072                 }
1073         }
1074         wpa_s->sta_scan_pending = 0;
1075 #endif /* CONFIG_P2P */
1076
1077         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1078                                                    data ? &data->scan_info :
1079                                                    NULL, 1);
1080         if (scan_res == NULL) {
1081                 if (wpa_s->conf->ap_scan == 2 || ap)
1082                         return -1;
1083                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1084                         "scanning again");
1085                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1086                 return -1;
1087         }
1088
1089 #ifndef CONFIG_NO_RANDOM_POOL
1090         size_t i, num;
1091         num = scan_res->num;
1092         if (num > 10)
1093                 num = 10;
1094         for (i = 0; i < num; i++) {
1095                 u8 buf[5];
1096                 struct wpa_scan_res *res = scan_res->res[i];
1097                 buf[0] = res->bssid[5];
1098                 buf[1] = res->qual & 0xff;
1099                 buf[2] = res->noise & 0xff;
1100                 buf[3] = res->level & 0xff;
1101                 buf[4] = res->tsf & 0xff;
1102                 random_add_randomness(buf, sizeof(buf));
1103         }
1104 #endif /* CONFIG_NO_RANDOM_POOL */
1105
1106         if (wpa_s->scan_res_handler) {
1107                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1108                                          struct wpa_scan_results *scan_res);
1109
1110                 scan_res_handler = wpa_s->scan_res_handler;
1111                 wpa_s->scan_res_handler = NULL;
1112                 scan_res_handler(wpa_s, scan_res);
1113
1114                 wpa_scan_results_free(scan_res);
1115                 return -2;
1116         }
1117
1118         if (ap) {
1119                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1120 #ifdef CONFIG_AP
1121                 if (wpa_s->ap_iface->scan_cb)
1122                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1123 #endif /* CONFIG_AP */
1124                 wpa_scan_results_free(scan_res);
1125                 return 0;
1126         }
1127
1128         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1129         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1130         wpas_notify_scan_results(wpa_s);
1131
1132         wpas_notify_scan_done(wpa_s, 1);
1133
1134         if (sme_proc_obss_scan(wpa_s) > 0) {
1135                 wpa_scan_results_free(scan_res);
1136                 return 0;
1137         }
1138
1139         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1140                 wpa_scan_results_free(scan_res);
1141                 return 0;
1142         }
1143
1144         if (autoscan_notify_scan(wpa_s, scan_res)) {
1145                 wpa_scan_results_free(scan_res);
1146                 return 0;
1147         }
1148
1149         if (wpa_s->disconnected) {
1150                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1151                 wpa_scan_results_free(scan_res);
1152                 return 0;
1153         }
1154
1155         if (!wpas_driver_bss_selection(wpa_s) &&
1156             bgscan_notify_scan(wpa_s, scan_res) == 1) {
1157                 wpa_scan_results_free(scan_res);
1158                 return 0;
1159         }
1160
1161         wpas_wps_update_ap_info(wpa_s, scan_res);
1162
1163         selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
1164
1165         if (selected) {
1166                 int skip;
1167                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
1168                                                     scan_res);
1169                 wpa_scan_results_free(scan_res);
1170                 if (skip) {
1171                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1172                         return 0;
1173                 }
1174
1175                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1176                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1177                         return -1;
1178                 }
1179                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1180         } else {
1181                 wpa_scan_results_free(scan_res);
1182                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1183                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1184                 if (ssid) {
1185                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1186                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1187                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1188                 } else {
1189                         int timeout_sec = wpa_s->scan_interval;
1190                         int timeout_usec = 0;
1191 #ifdef CONFIG_P2P
1192                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1193                                 return 0;
1194
1195                         if (wpa_s->p2p_in_provisioning) {
1196                                 /*
1197                                  * Use shorter wait during P2P Provisioning
1198                                  * state to speed up group formation.
1199                                  */
1200                                 timeout_sec = 0;
1201                                 timeout_usec = 250000;
1202                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1203                                                             timeout_usec);
1204                                 return 0;
1205                         }
1206 #endif /* CONFIG_P2P */
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         int authenticating;
1756         u8 prev_pending_bssid[ETH_ALEN];
1757         struct wpa_bss *fast_reconnect = NULL;
1758         struct wpa_ssid *fast_reconnect_ssid = NULL;
1759
1760         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1761         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1762
1763         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1764                 /*
1765                  * At least Host AP driver and a Prism3 card seemed to be
1766                  * generating streams of disconnected events when configuring
1767                  * IBSS for WPA-None. Ignore them for now.
1768                  */
1769                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1770                         "IBSS/WPA-None mode");
1771                 return;
1772         }
1773
1774         if (wpa_s->wpa_state == WPA_4WAY_HANDSHAKE &&
1775             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1776                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1777                         "pre-shared key may be incorrect");
1778                 wpas_auth_failed(wpa_s);
1779         }
1780         if (!wpa_s->auto_reconnect_disabled ||
1781             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1782                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1783                         "reconnect (wps=%d wpa_state=%d)",
1784                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1785                         wpa_s->wpa_state);
1786                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1787                     wpa_s->current_ssid &&
1788                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1789                     !locally_generated &&
1790                     disconnect_reason_recoverable(reason_code)) {
1791                         /*
1792                          * It looks like the AP has dropped association with
1793                          * us, but could allow us to get back in. Try to
1794                          * reconnect to the same BSS without full scan to save
1795                          * time for some common cases.
1796                          */
1797                         fast_reconnect = wpa_s->current_bss;
1798                         fast_reconnect_ssid = wpa_s->current_ssid;
1799                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1800                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1801                 else
1802                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1803                                 "immediate scan");
1804         } else {
1805                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1806                         "try to re-connect");
1807                 wpa_s->reassociate = 0;
1808                 wpa_s->disconnected = 1;
1809                 wpa_supplicant_cancel_sched_scan(wpa_s);
1810         }
1811         bssid = wpa_s->bssid;
1812         if (is_zero_ether_addr(bssid))
1813                 bssid = wpa_s->pending_bssid;
1814         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1815                 wpas_connection_failed(wpa_s, bssid);
1816         wpa_sm_notify_disassoc(wpa_s->wpa);
1817         if (locally_generated)
1818                 wpa_s->disconnect_reason = -reason_code;
1819         else
1820                 wpa_s->disconnect_reason = reason_code;
1821         wpas_notify_disconnect_reason(wpa_s);
1822         if (!is_zero_ether_addr(bssid) ||
1823             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1824                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1825                         " reason=%d%s",
1826                         MAC2STR(bssid), reason_code,
1827                         locally_generated ? " locally_generated=1" : "");
1828         }
1829         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1830                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1831                 wpa_s->keys_cleared = 0;
1832                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1833         }
1834         wpa_supplicant_mark_disassoc(wpa_s);
1835
1836         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
1837                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1838
1839         if (fast_reconnect) {
1840 #ifndef CONFIG_NO_SCAN_PROCESSING
1841                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1842                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1843                                            fast_reconnect_ssid) < 0) {
1844                         /* Recover through full scan */
1845                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1846                 }
1847 #endif /* CONFIG_NO_SCAN_PROCESSING */
1848         }
1849 }
1850
1851
1852 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1853 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1854 {
1855         struct wpa_supplicant *wpa_s = eloop_ctx;
1856
1857         if (!wpa_s->pending_mic_error_report)
1858                 return;
1859
1860         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1861         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1862         wpa_s->pending_mic_error_report = 0;
1863 }
1864 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1865
1866
1867 static void
1868 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1869                                          union wpa_event_data *data)
1870 {
1871         int pairwise;
1872         struct os_time t;
1873
1874         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1875         pairwise = (data && data->michael_mic_failure.unicast);
1876         os_get_time(&t);
1877         if ((wpa_s->last_michael_mic_error &&
1878              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1879             wpa_s->pending_mic_error_report) {
1880                 if (wpa_s->pending_mic_error_report) {
1881                         /*
1882                          * Send the pending MIC error report immediately since
1883                          * we are going to start countermeasures and AP better
1884                          * do the same.
1885                          */
1886                         wpa_sm_key_request(wpa_s->wpa, 1,
1887                                            wpa_s->pending_mic_error_pairwise);
1888                 }
1889
1890                 /* Send the new MIC error report immediately since we are going
1891                  * to start countermeasures and AP better do the same.
1892                  */
1893                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1894
1895                 /* initialize countermeasures */
1896                 wpa_s->countermeasures = 1;
1897
1898                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1899
1900                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1901
1902                 /*
1903                  * Need to wait for completion of request frame. We do not get
1904                  * any callback for the message completion, so just wait a
1905                  * short while and hope for the best. */
1906                 os_sleep(0, 10000);
1907
1908                 wpa_drv_set_countermeasures(wpa_s, 1);
1909                 wpa_supplicant_deauthenticate(wpa_s,
1910                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1911                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1912                                      wpa_s, NULL);
1913                 eloop_register_timeout(60, 0,
1914                                        wpa_supplicant_stop_countermeasures,
1915                                        wpa_s, NULL);
1916                 /* TODO: mark the AP rejected for 60 second. STA is
1917                  * allowed to associate with another AP.. */
1918         } else {
1919 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1920                 if (wpa_s->mic_errors_seen) {
1921                         /*
1922                          * Reduce the effectiveness of Michael MIC error
1923                          * reports as a means for attacking against TKIP if
1924                          * more than one MIC failure is noticed with the same
1925                          * PTK. We delay the transmission of the reports by a
1926                          * random time between 0 and 60 seconds in order to
1927                          * force the attacker wait 60 seconds before getting
1928                          * the information on whether a frame resulted in a MIC
1929                          * failure.
1930                          */
1931                         u8 rval[4];
1932                         int sec;
1933
1934                         if (os_get_random(rval, sizeof(rval)) < 0)
1935                                 sec = os_random() % 60;
1936                         else
1937                                 sec = WPA_GET_BE32(rval) % 60;
1938                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
1939                                 "report %d seconds", sec);
1940                         wpa_s->pending_mic_error_report = 1;
1941                         wpa_s->pending_mic_error_pairwise = pairwise;
1942                         eloop_cancel_timeout(
1943                                 wpa_supplicant_delayed_mic_error_report,
1944                                 wpa_s, NULL);
1945                         eloop_register_timeout(
1946                                 sec, os_random() % 1000000,
1947                                 wpa_supplicant_delayed_mic_error_report,
1948                                 wpa_s, NULL);
1949                 } else {
1950                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1951                 }
1952 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1953                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1954 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1955         }
1956         wpa_s->last_michael_mic_error = t.sec;
1957         wpa_s->mic_errors_seen++;
1958 }
1959
1960
1961 #ifdef CONFIG_TERMINATE_ONLASTIF
1962 static int any_interfaces(struct wpa_supplicant *head)
1963 {
1964         struct wpa_supplicant *wpa_s;
1965
1966         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
1967                 if (!wpa_s->interface_removed)
1968                         return 1;
1969         return 0;
1970 }
1971 #endif /* CONFIG_TERMINATE_ONLASTIF */
1972
1973
1974 static void
1975 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
1976                                       union wpa_event_data *data)
1977 {
1978         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
1979                 return;
1980
1981         switch (data->interface_status.ievent) {
1982         case EVENT_INTERFACE_ADDED:
1983                 if (!wpa_s->interface_removed)
1984                         break;
1985                 wpa_s->interface_removed = 0;
1986                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
1987                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
1988                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
1989                                 "driver after interface was added");
1990                 }
1991                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1992                 break;
1993         case EVENT_INTERFACE_REMOVED:
1994                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
1995                 wpa_s->interface_removed = 1;
1996                 wpa_supplicant_mark_disassoc(wpa_s);
1997                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
1998                 l2_packet_deinit(wpa_s->l2);
1999                 wpa_s->l2 = NULL;
2000 #ifdef CONFIG_IBSS_RSN
2001                 ibss_rsn_deinit(wpa_s->ibss_rsn);
2002                 wpa_s->ibss_rsn = NULL;
2003 #endif /* CONFIG_IBSS_RSN */
2004 #ifdef CONFIG_TERMINATE_ONLASTIF
2005                 /* check if last interface */
2006                 if (!any_interfaces(wpa_s->global->ifaces))
2007                         eloop_terminate();
2008 #endif /* CONFIG_TERMINATE_ONLASTIF */
2009                 break;
2010         }
2011 }
2012
2013
2014 #ifdef CONFIG_PEERKEY
2015 static void
2016 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2017                               union wpa_event_data *data)
2018 {
2019         if (data == NULL)
2020                 return;
2021         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2022 }
2023 #endif /* CONFIG_PEERKEY */
2024
2025
2026 #ifdef CONFIG_TDLS
2027 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2028                                       union wpa_event_data *data)
2029 {
2030         if (data == NULL)
2031                 return;
2032         switch (data->tdls.oper) {
2033         case TDLS_REQUEST_SETUP:
2034                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2035                 break;
2036         case TDLS_REQUEST_TEARDOWN:
2037                 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2038                                        data->tdls.reason_code);
2039                 break;
2040         }
2041 }
2042 #endif /* CONFIG_TDLS */
2043
2044
2045 #ifdef CONFIG_IEEE80211V
2046 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2047                                      union wpa_event_data *data)
2048 {
2049         if (data == NULL)
2050                 return;
2051         switch (data->wnm.oper) {
2052         case WNM_OPER_SLEEP:
2053                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2054                            "(action=%d, intval=%d)",
2055                            data->wnm.sleep_action, data->wnm.sleep_intval);
2056                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2057                                              data->wnm.sleep_intval);
2058                 break;
2059         }
2060 }
2061 #endif /* CONFIG_IEEE80211V */
2062
2063
2064 #ifdef CONFIG_IEEE80211R
2065 static void
2066 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2067                                  union wpa_event_data *data)
2068 {
2069         if (data == NULL)
2070                 return;
2071
2072         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2073                                     data->ft_ies.ies_len,
2074                                     data->ft_ies.ft_action,
2075                                     data->ft_ies.target_ap,
2076                                     data->ft_ies.ric_ies,
2077                                     data->ft_ies.ric_ies_len) < 0) {
2078                 /* TODO: prevent MLME/driver from trying to associate? */
2079         }
2080 }
2081 #endif /* CONFIG_IEEE80211R */
2082
2083
2084 #ifdef CONFIG_IBSS_RSN
2085 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2086                                                 union wpa_event_data *data)
2087 {
2088         struct wpa_ssid *ssid;
2089         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2090                 return;
2091         if (data == NULL)
2092                 return;
2093         ssid = wpa_s->current_ssid;
2094         if (ssid == NULL)
2095                 return;
2096         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2097                 return;
2098
2099         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2100 }
2101 #endif /* CONFIG_IBSS_RSN */
2102
2103
2104 #ifdef CONFIG_IEEE80211R
2105 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2106                          size_t len)
2107 {
2108         const u8 *sta_addr, *target_ap_addr;
2109         u16 status;
2110
2111         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2112         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2113                 return; /* only SME case supported for now */
2114         if (len < 1 + 2 * ETH_ALEN + 2)
2115                 return;
2116         if (data[0] != 2)
2117                 return; /* Only FT Action Response is supported for now */
2118         sta_addr = data + 1;
2119         target_ap_addr = data + 1 + ETH_ALEN;
2120         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2121         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2122                 MACSTR " TargetAP " MACSTR " status %u",
2123                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2124
2125         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2126                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2127                         " in FT Action Response", MAC2STR(sta_addr));
2128                 return;
2129         }
2130
2131         if (status) {
2132                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2133                         "failure (status code %d)", status);
2134                 /* TODO: report error to FT code(?) */
2135                 return;
2136         }
2137
2138         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2139                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2140                                     target_ap_addr, NULL, 0) < 0)
2141                 return;
2142
2143 #ifdef CONFIG_SME
2144         {
2145                 struct wpa_bss *bss;
2146                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2147                 if (bss)
2148                         wpa_s->sme.freq = bss->freq;
2149                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2150                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2151                               WLAN_AUTH_FT);
2152         }
2153 #endif /* CONFIG_SME */
2154 }
2155 #endif /* CONFIG_IEEE80211R */
2156
2157
2158 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2159                                                struct unprot_deauth *e)
2160 {
2161 #ifdef CONFIG_IEEE80211W
2162         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2163                    "dropped: " MACSTR " -> " MACSTR
2164                    " (reason code %u)",
2165                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2166         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2167 #endif /* CONFIG_IEEE80211W */
2168 }
2169
2170
2171 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2172                                                  struct unprot_disassoc *e)
2173 {
2174 #ifdef CONFIG_IEEE80211W
2175         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2176                    "dropped: " MACSTR " -> " MACSTR
2177                    " (reason code %u)",
2178                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2179         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2180 #endif /* CONFIG_IEEE80211W */
2181 }
2182
2183
2184 static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2185 {
2186         u8 action, mode;
2187         const u8 *pos, *end;
2188
2189         if (rx->data == NULL || rx->len == 0)
2190                 return;
2191
2192         pos = rx->data;
2193         end = pos + rx->len;
2194         action = *pos++;
2195
2196         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2197                    action, MAC2STR(rx->sa));
2198         switch (action) {
2199         case WNM_BSS_TRANS_MGMT_REQ:
2200                 if (pos + 5 > end)
2201                         break;
2202                 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2203                            "Request: dialog_token=%u request_mode=0x%x "
2204                            "disassoc_timer=%u validity_interval=%u",
2205                            pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2206                 mode = pos[1];
2207                 pos += 5;
2208                 if (mode & 0x08)
2209                         pos += 12; /* BSS Termination Duration */
2210                 if (mode & 0x10) {
2211                         char url[256];
2212                         if (pos + 1 > end || pos + 1 + pos[0] > end) {
2213                                 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2214                                            "Transition Management Request "
2215                                            "(URL)");
2216                                 break;
2217                         }
2218                         os_memcpy(url, pos + 1, pos[0]);
2219                         url[pos[0]] = '\0';
2220                         wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2221                                 "Imminent - session_info_url=%s", url);
2222                 }
2223                 break;
2224         }
2225 }
2226
2227
2228 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2229                           union wpa_event_data *data)
2230 {
2231         struct wpa_supplicant *wpa_s = ctx;
2232         u16 reason_code = 0;
2233         int locally_generated = 0;
2234
2235         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2236             event != EVENT_INTERFACE_ENABLED &&
2237             event != EVENT_INTERFACE_STATUS &&
2238             event != EVENT_SCHED_SCAN_STOPPED) {
2239                 wpa_dbg(wpa_s, MSG_DEBUG,
2240                         "Ignore event %s (%d) while interface is disabled",
2241                         event_to_string(event), event);
2242                 return;
2243         }
2244
2245 #ifndef CONFIG_NO_STDOUT_DEBUG
2246 {
2247         int level = MSG_DEBUG;
2248
2249         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2250                 const struct ieee80211_hdr *hdr;
2251                 u16 fc;
2252                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2253                 fc = le_to_host16(hdr->frame_control);
2254                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2255                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2256                         level = MSG_EXCESSIVE;
2257         }
2258
2259         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2260                 event_to_string(event), event);
2261 }
2262 #endif /* CONFIG_NO_STDOUT_DEBUG */
2263
2264         switch (event) {
2265         case EVENT_AUTH:
2266                 sme_event_auth(wpa_s, data);
2267                 break;
2268         case EVENT_ASSOC:
2269                 wpa_supplicant_event_assoc(wpa_s, data);
2270                 break;
2271         case EVENT_DISASSOC:
2272                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2273                 if (data) {
2274                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2275                                 data->disassoc_info.reason_code,
2276                                 data->disassoc_info.locally_generated ?
2277                                 " (locally generated)" : "");
2278                         if (data->disassoc_info.addr)
2279                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2280                                         MAC2STR(data->disassoc_info.addr));
2281                 }
2282 #ifdef CONFIG_AP
2283                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2284                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2285                                                data->disassoc_info.addr);
2286                         break;
2287                 }
2288                 if (wpa_s->ap_iface) {
2289                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2290                                 "AP mode");
2291                         break;
2292                 }
2293 #endif /* CONFIG_AP */
2294                 if (data) {
2295                         reason_code = data->disassoc_info.reason_code;
2296                         locally_generated =
2297                                 data->disassoc_info.locally_generated;
2298                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2299                                     data->disassoc_info.ie,
2300                                     data->disassoc_info.ie_len);
2301 #ifdef CONFIG_P2P
2302                         wpas_p2p_disassoc_notif(
2303                                 wpa_s, data->disassoc_info.addr, reason_code,
2304                                 data->disassoc_info.ie,
2305                                 data->disassoc_info.ie_len,
2306                                 locally_generated);
2307 #endif /* CONFIG_P2P */
2308                 }
2309                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2310                         sme_event_disassoc(wpa_s, data);
2311                 /* fall through */
2312         case EVENT_DEAUTH:
2313                 if (event == EVENT_DEAUTH) {
2314                         wpa_dbg(wpa_s, MSG_DEBUG,
2315                                 "Deauthentication notification");
2316                         if (data) {
2317                                 reason_code = data->deauth_info.reason_code;
2318                                 locally_generated =
2319                                         data->deauth_info.locally_generated;
2320                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2321                                         data->deauth_info.reason_code,
2322                                         data->deauth_info.locally_generated ?
2323                                         " (locally generated)" : "");
2324                                 if (data->deauth_info.addr) {
2325                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2326                                                 MACSTR,
2327                                                 MAC2STR(data->deauth_info.
2328                                                         addr));
2329                                 }
2330                                 wpa_hexdump(MSG_DEBUG,
2331                                             "Deauthentication frame IE(s)",
2332                                             data->deauth_info.ie,
2333                                             data->deauth_info.ie_len);
2334                         }
2335                 }
2336 #ifdef CONFIG_AP
2337                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2338                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2339                                                data->deauth_info.addr);
2340                         break;
2341                 }
2342                 if (wpa_s->ap_iface) {
2343                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2344                                 "AP mode");
2345                         break;
2346                 }
2347 #endif /* CONFIG_AP */
2348                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2349                                               locally_generated);
2350                 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2351                     ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2352                       (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2353                      eapol_sm_failed(wpa_s->eapol)))
2354                         wpas_auth_failed(wpa_s);
2355 #ifdef CONFIG_P2P
2356                 if (event == EVENT_DEAUTH && data) {
2357                         wpas_p2p_deauth_notif(wpa_s, data->deauth_info.addr,
2358                                               reason_code,
2359                                               data->deauth_info.ie,
2360                                               data->deauth_info.ie_len,
2361                                               locally_generated);
2362                 }
2363 #endif /* CONFIG_P2P */
2364                 break;
2365         case EVENT_MICHAEL_MIC_FAILURE:
2366                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2367                 break;
2368 #ifndef CONFIG_NO_SCAN_PROCESSING
2369         case EVENT_SCAN_RESULTS:
2370                 wpa_supplicant_event_scan_results(wpa_s, data);
2371 #ifdef CONFIG_P2P
2372         if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
2373             wpa_s->global->p2p != NULL &&
2374             wpa_s->wpa_state != WPA_AUTHENTICATING &&
2375             wpa_s->wpa_state != WPA_ASSOCIATING) {
2376                 wpa_s->p2p_cb_on_scan_complete = 0;
2377                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2378                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2379                                 "continued after scan result processing");
2380                 }
2381         }
2382 #endif /* CONFIG_P2P */
2383                 break;
2384 #endif /* CONFIG_NO_SCAN_PROCESSING */
2385         case EVENT_ASSOCINFO:
2386                 wpa_supplicant_event_associnfo(wpa_s, data);
2387                 break;
2388         case EVENT_INTERFACE_STATUS:
2389                 wpa_supplicant_event_interface_status(wpa_s, data);
2390                 break;
2391         case EVENT_PMKID_CANDIDATE:
2392                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2393                 break;
2394 #ifdef CONFIG_PEERKEY
2395         case EVENT_STKSTART:
2396                 wpa_supplicant_event_stkstart(wpa_s, data);
2397                 break;
2398 #endif /* CONFIG_PEERKEY */
2399 #ifdef CONFIG_TDLS
2400         case EVENT_TDLS:
2401                 wpa_supplicant_event_tdls(wpa_s, data);
2402                 break;
2403 #endif /* CONFIG_TDLS */
2404 #ifdef CONFIG_IEEE80211V
2405         case EVENT_WNM:
2406                 wpa_supplicant_event_wnm(wpa_s, data);
2407                 break;
2408 #endif /* CONFIG_IEEE80211V */
2409 #ifdef CONFIG_IEEE80211R
2410         case EVENT_FT_RESPONSE:
2411                 wpa_supplicant_event_ft_response(wpa_s, data);
2412                 break;
2413 #endif /* CONFIG_IEEE80211R */
2414 #ifdef CONFIG_IBSS_RSN
2415         case EVENT_IBSS_RSN_START:
2416                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2417                 break;
2418 #endif /* CONFIG_IBSS_RSN */
2419         case EVENT_ASSOC_REJECT:
2420                 if (data->assoc_reject.bssid)
2421                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2422                                 "bssid=" MACSTR " status_code=%u",
2423                                 MAC2STR(data->assoc_reject.bssid),
2424                                 data->assoc_reject.status_code);
2425                 else
2426                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2427                                 "status_code=%u",
2428                                 data->assoc_reject.status_code);
2429                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2430                         sme_event_assoc_reject(wpa_s, data);
2431                 break;
2432         case EVENT_AUTH_TIMED_OUT:
2433                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2434                         sme_event_auth_timed_out(wpa_s, data);
2435                 break;
2436         case EVENT_ASSOC_TIMED_OUT:
2437                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2438                         sme_event_assoc_timed_out(wpa_s, data);
2439                 break;
2440         case EVENT_TX_STATUS:
2441                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2442                         " type=%d stype=%d",
2443                         MAC2STR(data->tx_status.dst),
2444                         data->tx_status.type, data->tx_status.stype);
2445 #ifdef CONFIG_AP
2446                 if (wpa_s->ap_iface == NULL) {
2447 #ifdef CONFIG_OFFCHANNEL
2448                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2449                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2450                                 offchannel_send_action_tx_status(
2451                                         wpa_s, data->tx_status.dst,
2452                                         data->tx_status.data,
2453                                         data->tx_status.data_len,
2454                                         data->tx_status.ack ?
2455                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2456                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2457 #endif /* CONFIG_OFFCHANNEL */
2458                         break;
2459                 }
2460 #endif /* CONFIG_AP */
2461 #ifdef CONFIG_OFFCHANNEL
2462                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2463                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2464                 /*
2465                  * Catch TX status events for Action frames we sent via group
2466                  * interface in GO mode.
2467                  */
2468                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2469                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2470                     os_memcmp(wpa_s->parent->pending_action_dst,
2471                               data->tx_status.dst, ETH_ALEN) == 0) {
2472                         offchannel_send_action_tx_status(
2473                                 wpa_s->parent, data->tx_status.dst,
2474                                 data->tx_status.data,
2475                                 data->tx_status.data_len,
2476                                 data->tx_status.ack ?
2477                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2478                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2479                         break;
2480                 }
2481 #endif /* CONFIG_OFFCHANNEL */
2482 #ifdef CONFIG_AP
2483                 switch (data->tx_status.type) {
2484                 case WLAN_FC_TYPE_MGMT:
2485                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2486                                       data->tx_status.data_len,
2487                                       data->tx_status.stype,
2488                                       data->tx_status.ack);
2489                         break;
2490                 case WLAN_FC_TYPE_DATA:
2491                         ap_tx_status(wpa_s, data->tx_status.dst,
2492                                      data->tx_status.data,
2493                                      data->tx_status.data_len,
2494                                      data->tx_status.ack);
2495                         break;
2496                 }
2497 #endif /* CONFIG_AP */
2498                 break;
2499 #ifdef CONFIG_AP
2500         case EVENT_EAPOL_TX_STATUS:
2501                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2502                                    data->eapol_tx_status.data,
2503                                    data->eapol_tx_status.data_len,
2504                                    data->eapol_tx_status.ack);
2505                 break;
2506         case EVENT_DRIVER_CLIENT_POLL_OK:
2507                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2508                 break;
2509         case EVENT_RX_FROM_UNKNOWN:
2510                 if (wpa_s->ap_iface == NULL)
2511                         break;
2512                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2513                                        data->rx_from_unknown.wds);
2514                 break;
2515         case EVENT_CH_SWITCH:
2516                 if (!data)
2517                         break;
2518                 if (!wpa_s->ap_iface) {
2519                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2520                                 "event in non-AP mode");
2521                         break;
2522                 }
2523
2524 #ifdef CONFIG_AP
2525                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2526                                   data->ch_switch.ht_enabled,
2527                                   data->ch_switch.ch_offset);
2528 #endif /* CONFIG_AP */
2529                 break;
2530         case EVENT_RX_MGMT: {
2531                 u16 fc, stype;
2532                 const struct ieee80211_mgmt *mgmt;
2533
2534                 mgmt = (const struct ieee80211_mgmt *)
2535                         data->rx_mgmt.frame;
2536                 fc = le_to_host16(mgmt->frame_control);
2537                 stype = WLAN_FC_GET_STYPE(fc);
2538
2539                 if (wpa_s->ap_iface == NULL) {
2540 #ifdef CONFIG_P2P
2541                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2542                             data->rx_mgmt.frame_len > 24) {
2543                                 const u8 *src = mgmt->sa;
2544                                 const u8 *ie = mgmt->u.probe_req.variable;
2545                                 size_t ie_len = data->rx_mgmt.frame_len -
2546                                         (mgmt->u.probe_req.variable -
2547                                          data->rx_mgmt.frame);
2548                                 wpas_p2p_probe_req_rx(
2549                                         wpa_s, src, mgmt->da,
2550                                         mgmt->bssid, ie, ie_len,
2551                                         data->rx_mgmt.ssi_signal);
2552                                 break;
2553                         }
2554 #endif /* CONFIG_P2P */
2555                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2556                                 "management frame in non-AP mode");
2557                         break;
2558                 }
2559
2560                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2561                     data->rx_mgmt.frame_len > 24) {
2562                         const u8 *ie = mgmt->u.probe_req.variable;
2563                         size_t ie_len = data->rx_mgmt.frame_len -
2564                                 (mgmt->u.probe_req.variable -
2565                                  data->rx_mgmt.frame);
2566
2567                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2568                                          mgmt->bssid, ie, ie_len,
2569                                          data->rx_mgmt.ssi_signal);
2570                 }
2571
2572                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2573                 break;
2574                 }
2575 #endif /* CONFIG_AP */
2576         case EVENT_RX_ACTION:
2577                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2578                         " Category=%u DataLen=%d freq=%d MHz",
2579                         MAC2STR(data->rx_action.sa),
2580                         data->rx_action.category, (int) data->rx_action.len,
2581                         data->rx_action.freq);
2582 #ifdef CONFIG_IEEE80211R
2583                 if (data->rx_action.category == WLAN_ACTION_FT) {
2584                         ft_rx_action(wpa_s, data->rx_action.data,
2585                                      data->rx_action.len);
2586                         break;
2587                 }
2588 #endif /* CONFIG_IEEE80211R */
2589 #ifdef CONFIG_IEEE80211W
2590 #ifdef CONFIG_SME
2591                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2592                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2593                                         data->rx_action.data,
2594                                         data->rx_action.len);
2595                         break;
2596                 }
2597 #endif /* CONFIG_SME */
2598 #endif /* CONFIG_IEEE80211W */
2599 #ifdef CONFIG_IEEE80211V
2600                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2601                         ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2602                         break;
2603                 }
2604 #endif /* CONFIG_IEEE80211V */
2605 #ifdef CONFIG_GAS
2606                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2607                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2608                                  data->rx_action.sa, data->rx_action.bssid,
2609                                  data->rx_action.data, data->rx_action.len,
2610                                  data->rx_action.freq) == 0)
2611                         break;
2612 #endif /* CONFIG_GAS */
2613                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2614                         wnm_action_rx(wpa_s, &data->rx_action);
2615                         break;
2616                 }
2617 #ifdef CONFIG_TDLS
2618                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2619                     data->rx_action.len >= 4 &&
2620                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2621                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2622                                 "Response from " MACSTR,
2623                                 MAC2STR(data->rx_action.sa));
2624                         break;
2625                 }
2626 #endif /* CONFIG_TDLS */
2627 #ifdef CONFIG_P2P
2628                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2629                                    data->rx_action.sa,
2630                                    data->rx_action.bssid,
2631                                    data->rx_action.category,
2632                                    data->rx_action.data,
2633                                    data->rx_action.len, data->rx_action.freq);
2634 #endif /* CONFIG_P2P */
2635                 break;
2636         case EVENT_RX_PROBE_REQ:
2637                 if (data->rx_probe_req.sa == NULL ||
2638                     data->rx_probe_req.ie == NULL)
2639                         break;
2640 #ifdef CONFIG_AP
2641                 if (wpa_s->ap_iface) {
2642                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2643                                              data->rx_probe_req.sa,
2644                                              data->rx_probe_req.da,
2645                                              data->rx_probe_req.bssid,
2646                                              data->rx_probe_req.ie,
2647                                              data->rx_probe_req.ie_len,
2648                                              data->rx_probe_req.ssi_signal);
2649                         break;
2650                 }
2651 #endif /* CONFIG_AP */
2652 #ifdef CONFIG_P2P
2653                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2654                                       data->rx_probe_req.da,
2655                                       data->rx_probe_req.bssid,
2656                                       data->rx_probe_req.ie,
2657                                       data->rx_probe_req.ie_len,
2658                                       data->rx_probe_req.ssi_signal);
2659 #endif /* CONFIG_P2P */
2660                 break;
2661         case EVENT_REMAIN_ON_CHANNEL:
2662 #ifdef CONFIG_OFFCHANNEL
2663                 offchannel_remain_on_channel_cb(
2664                         wpa_s, data->remain_on_channel.freq,
2665                         data->remain_on_channel.duration);
2666 #endif /* CONFIG_OFFCHANNEL */
2667 #ifdef CONFIG_P2P
2668                 wpas_p2p_remain_on_channel_cb(
2669                         wpa_s, data->remain_on_channel.freq,
2670                         data->remain_on_channel.duration);
2671 #endif /* CONFIG_P2P */
2672                 break;
2673         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2674 #ifdef CONFIG_OFFCHANNEL
2675                 offchannel_cancel_remain_on_channel_cb(
2676                         wpa_s, data->remain_on_channel.freq);
2677 #endif /* CONFIG_OFFCHANNEL */
2678 #ifdef CONFIG_P2P
2679                 wpas_p2p_cancel_remain_on_channel_cb(
2680                         wpa_s, data->remain_on_channel.freq);
2681 #endif /* CONFIG_P2P */
2682                 break;
2683 #ifdef CONFIG_P2P
2684         case EVENT_P2P_DEV_FOUND: {
2685                 struct p2p_peer_info peer_info;
2686
2687                 os_memset(&peer_info, 0, sizeof(peer_info));
2688                 if (data->p2p_dev_found.dev_addr)
2689                         os_memcpy(peer_info.p2p_device_addr,
2690                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2691                 if (data->p2p_dev_found.pri_dev_type)
2692                         os_memcpy(peer_info.pri_dev_type,
2693                                   data->p2p_dev_found.pri_dev_type,
2694                                   sizeof(peer_info.pri_dev_type));
2695                 if (data->p2p_dev_found.dev_name)
2696                         os_strlcpy(peer_info.device_name,
2697                                    data->p2p_dev_found.dev_name,
2698                                    sizeof(peer_info.device_name));
2699                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2700                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2701                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2702
2703                 /*
2704                  * FIX: new_device=1 is not necessarily correct. We should
2705                  * maintain a P2P peer database in wpa_supplicant and update
2706                  * this information based on whether the peer is truly new.
2707                  */
2708                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2709                 break;
2710                 }
2711         case EVENT_P2P_GO_NEG_REQ_RX:
2712                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2713                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2714                 break;
2715         case EVENT_P2P_GO_NEG_COMPLETED:
2716                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2717                 break;
2718         case EVENT_P2P_PROV_DISC_REQUEST:
2719                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2720                                    data->p2p_prov_disc_req.config_methods,
2721                                    data->p2p_prov_disc_req.dev_addr,
2722                                    data->p2p_prov_disc_req.pri_dev_type,
2723                                    data->p2p_prov_disc_req.dev_name,
2724                                    data->p2p_prov_disc_req.supp_config_methods,
2725                                    data->p2p_prov_disc_req.dev_capab,
2726                                    data->p2p_prov_disc_req.group_capab,
2727                                    NULL, 0);
2728                 break;
2729         case EVENT_P2P_PROV_DISC_RESPONSE:
2730                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2731                                     data->p2p_prov_disc_resp.config_methods);
2732                 break;
2733         case EVENT_P2P_SD_REQUEST:
2734                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2735                                 data->p2p_sd_req.sa,
2736                                 data->p2p_sd_req.dialog_token,
2737                                 data->p2p_sd_req.update_indic,
2738                                 data->p2p_sd_req.tlvs,
2739                                 data->p2p_sd_req.tlvs_len);
2740                 break;
2741         case EVENT_P2P_SD_RESPONSE:
2742                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2743                                  data->p2p_sd_resp.update_indic,
2744                                  data->p2p_sd_resp.tlvs,
2745                                  data->p2p_sd_resp.tlvs_len);
2746                 break;
2747 #endif /* CONFIG_P2P */
2748         case EVENT_EAPOL_RX:
2749                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2750                                         data->eapol_rx.data,
2751                                         data->eapol_rx.data_len);
2752                 break;
2753         case EVENT_SIGNAL_CHANGE:
2754                 bgscan_notify_signal_change(
2755                         wpa_s, data->signal_change.above_threshold,
2756                         data->signal_change.current_signal,
2757                         data->signal_change.current_noise,
2758                         data->signal_change.current_txrate);
2759                 break;
2760         case EVENT_INTERFACE_ENABLED:
2761                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2762                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2763                         wpa_supplicant_update_mac_addr(wpa_s);
2764 #ifdef CONFIG_AP
2765                         if (!wpa_s->ap_iface) {
2766                                 wpa_supplicant_set_state(wpa_s,
2767                                                          WPA_DISCONNECTED);
2768                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2769                         } else
2770                                 wpa_supplicant_set_state(wpa_s,
2771                                                          WPA_COMPLETED);
2772 #else /* CONFIG_AP */
2773                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2774                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2775 #endif /* CONFIG_AP */
2776                 }
2777                 break;
2778         case EVENT_INTERFACE_DISABLED:
2779                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2780                 wpa_supplicant_mark_disassoc(wpa_s);
2781                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2782                 break;
2783         case EVENT_CHANNEL_LIST_CHANGED:
2784                 if (wpa_s->drv_priv == NULL)
2785                         break; /* Ignore event during drv initialization */
2786
2787                 free_hw_features(wpa_s);
2788                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2789                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2790
2791 #ifdef CONFIG_P2P
2792                 wpas_p2p_update_channel_list(wpa_s);
2793 #endif /* CONFIG_P2P */
2794                 break;
2795         case EVENT_INTERFACE_UNAVAILABLE:
2796 #ifdef CONFIG_P2P
2797                 wpas_p2p_interface_unavailable(wpa_s);
2798 #endif /* CONFIG_P2P */
2799                 break;
2800         case EVENT_BEST_CHANNEL:
2801                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2802                         "(%d %d %d)",
2803                         data->best_chan.freq_24, data->best_chan.freq_5,
2804                         data->best_chan.freq_overall);
2805                 wpa_s->best_24_freq = data->best_chan.freq_24;
2806                 wpa_s->best_5_freq = data->best_chan.freq_5;
2807                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2808 #ifdef CONFIG_P2P
2809                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2810                                               data->best_chan.freq_5,
2811                                               data->best_chan.freq_overall);
2812 #endif /* CONFIG_P2P */
2813                 break;
2814         case EVENT_UNPROT_DEAUTH:
2815                 wpa_supplicant_event_unprot_deauth(wpa_s,
2816                                                    &data->unprot_deauth);
2817                 break;
2818         case EVENT_UNPROT_DISASSOC:
2819                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2820                                                      &data->unprot_disassoc);
2821                 break;
2822         case EVENT_STATION_LOW_ACK:
2823 #ifdef CONFIG_AP
2824                 if (wpa_s->ap_iface && data)
2825                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2826                                                   data->low_ack.addr);
2827 #endif /* CONFIG_AP */
2828 #ifdef CONFIG_TDLS
2829                 if (data)
2830                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2831 #endif /* CONFIG_TDLS */
2832                 break;
2833         case EVENT_IBSS_PEER_LOST:
2834 #ifdef CONFIG_IBSS_RSN
2835                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2836 #endif /* CONFIG_IBSS_RSN */
2837                 break;
2838         case EVENT_DRIVER_GTK_REKEY:
2839                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2840                               wpa_s->bssid, ETH_ALEN))
2841                         break;
2842                 if (!wpa_s->wpa)
2843                         break;
2844                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2845                                          data->driver_gtk_rekey.replay_ctr);
2846                 break;
2847         case EVENT_SCHED_SCAN_STOPPED:
2848                 wpa_s->sched_scanning = 0;
2849                 wpa_supplicant_notify_scanning(wpa_s, 0);
2850
2851                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2852                         break;
2853
2854                 /*
2855                  * If we timed out, start a new sched scan to continue
2856                  * searching for more SSIDs.
2857                  */
2858                 if (wpa_s->sched_scan_timed_out)
2859                         wpa_supplicant_req_sched_scan(wpa_s);
2860                 break;
2861         case EVENT_WPS_BUTTON_PUSHED:
2862 #ifdef CONFIG_WPS
2863                 wpas_wps_start_pbc(wpa_s, NULL, 0);
2864 #endif /* CONFIG_WPS */
2865                 break;
2866         default:
2867                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2868                 break;
2869         }
2870 }