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