OSDN Git Service

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