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