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