1aa9c2109c007271e53c0185d77f2c510e45eb4f
[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         min_diff = 2;
1029         if (current_bss->level < 0) {
1030                 if (current_bss->level < -85)
1031                         min_diff = 1;
1032                 else if (current_bss->level < -80)
1033                         min_diff = 2;
1034                 else if (current_bss->level < -75)
1035                         min_diff = 3;
1036                 else if (current_bss->level < -70)
1037                         min_diff = 4;
1038                 else
1039                         min_diff = 5;
1040         }
1041         if (abs(current_bss->level - selected->level) < min_diff) {
1042                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1043                         "in signal level");
1044                 return 0;
1045         }
1046
1047         return 1;
1048 #else /* CONFIG_NO_ROAMING */
1049         return 0;
1050 #endif /* CONFIG_NO_ROAMING */
1051 }
1052
1053
1054 /* Return != 0 if no scan results could be fetched or if scan results should not
1055  * be shared with other virtual interfaces. */
1056 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1057                                               union wpa_event_data *data)
1058 {
1059         struct wpa_scan_results *scan_res;
1060         int ap = 0;
1061 #ifndef CONFIG_NO_RANDOM_POOL
1062         size_t i, num;
1063 #endif /* CONFIG_NO_RANDOM_POOL */
1064
1065 #ifdef CONFIG_AP
1066         if (wpa_s->ap_iface)
1067                 ap = 1;
1068 #endif /* CONFIG_AP */
1069
1070         wpa_supplicant_notify_scanning(wpa_s, 0);
1071
1072 #ifdef CONFIG_P2P
1073         if (wpa_s->global->p2p_cb_on_scan_complete &&
1074             !wpa_s->global->p2p_disabled &&
1075             wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending &&
1076             !wpa_s->scan_res_handler) {
1077                 wpa_s->global->p2p_cb_on_scan_complete = 0;
1078                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
1079                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
1080                                 "stopped scan processing");
1081                         wpa_s->sta_scan_pending = 1;
1082                         wpa_supplicant_req_scan(wpa_s, 5, 0);
1083                         return -1;
1084                 }
1085         }
1086         wpa_s->sta_scan_pending = 0;
1087 #endif /* CONFIG_P2P */
1088
1089         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1090                                                    data ? &data->scan_info :
1091                                                    NULL, 1);
1092         if (scan_res == NULL) {
1093                 if (wpa_s->conf->ap_scan == 2 || ap)
1094                         return -1;
1095                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1096                         "scanning again");
1097                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1098                 return -1;
1099         }
1100
1101 #ifndef CONFIG_NO_RANDOM_POOL
1102         num = scan_res->num;
1103         if (num > 10)
1104                 num = 10;
1105         for (i = 0; i < num; i++) {
1106                 u8 buf[5];
1107                 struct wpa_scan_res *res = scan_res->res[i];
1108                 buf[0] = res->bssid[5];
1109                 buf[1] = res->qual & 0xff;
1110                 buf[2] = res->noise & 0xff;
1111                 buf[3] = res->level & 0xff;
1112                 buf[4] = res->tsf & 0xff;
1113                 random_add_randomness(buf, sizeof(buf));
1114         }
1115 #endif /* CONFIG_NO_RANDOM_POOL */
1116
1117         if (wpa_s->scan_res_handler) {
1118                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1119                                          struct wpa_scan_results *scan_res);
1120
1121                 scan_res_handler = wpa_s->scan_res_handler;
1122                 wpa_s->scan_res_handler = NULL;
1123                 scan_res_handler(wpa_s, scan_res);
1124
1125                 wpa_scan_results_free(scan_res);
1126                 return -2;
1127         }
1128
1129         if (ap) {
1130                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1131 #ifdef CONFIG_AP
1132                 if (wpa_s->ap_iface->scan_cb)
1133                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1134 #endif /* CONFIG_AP */
1135                 wpa_scan_results_free(scan_res);
1136                 return 0;
1137         }
1138
1139         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available");
1140         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1141         wpas_notify_scan_results(wpa_s);
1142
1143         wpas_notify_scan_done(wpa_s, 1);
1144
1145         if (sme_proc_obss_scan(wpa_s) > 0) {
1146                 wpa_scan_results_free(scan_res);
1147                 return 0;
1148         }
1149
1150         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s))) {
1151                 wpa_scan_results_free(scan_res);
1152                 return 0;
1153         }
1154
1155         if (autoscan_notify_scan(wpa_s, scan_res)) {
1156                 wpa_scan_results_free(scan_res);
1157                 return 0;
1158         }
1159
1160         if (wpa_s->disconnected) {
1161                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1162                 wpa_scan_results_free(scan_res);
1163                 return 0;
1164         }
1165
1166         if (!wpas_driver_bss_selection(wpa_s) &&
1167             bgscan_notify_scan(wpa_s, scan_res) == 1) {
1168                 wpa_scan_results_free(scan_res);
1169                 return 0;
1170         }
1171
1172         wpas_wps_update_ap_info(wpa_s, scan_res);
1173
1174         wpa_scan_results_free(scan_res);
1175
1176         return wpas_select_network_from_last_scan(wpa_s);
1177 }
1178
1179
1180 int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
1181 {
1182         struct wpa_bss *selected;
1183         struct wpa_ssid *ssid = NULL;
1184
1185         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1186
1187         if (selected) {
1188                 int skip;
1189                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1190                 if (skip) {
1191                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1192                         return 0;
1193                 }
1194
1195                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1196                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1197                         return -1;
1198                 }
1199                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1200                 /*
1201                  * Do not notify other virtual radios of scan results since we do not
1202                  * want them to start other associations at the same time.
1203                  */
1204                 return 1;
1205         } else {
1206                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1207                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1208                 if (ssid) {
1209                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1210                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1211                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1212                 } else {
1213                         int timeout_sec = wpa_s->scan_interval;
1214                         int timeout_usec = 0;
1215 #ifdef CONFIG_P2P
1216                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1217                                 return 0;
1218
1219                         if (wpa_s->p2p_in_provisioning) {
1220                                 /*
1221                                  * Use shorter wait during P2P Provisioning
1222                                  * state to speed up group formation.
1223                                  */
1224                                 timeout_sec = 0;
1225                                 timeout_usec = 250000;
1226                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1227                                                             timeout_usec);
1228                                 return 0;
1229                         }
1230 #endif /* CONFIG_P2P */
1231 #ifdef CONFIG_INTERWORKING
1232                         if (wpa_s->conf->auto_interworking &&
1233                             wpa_s->conf->interworking &&
1234                             wpa_s->conf->cred) {
1235                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1236                                         "start ANQP fetch since no matching "
1237                                         "networks found");
1238                                 wpa_s->network_select = 1;
1239                                 wpa_s->auto_network_select = 1;
1240                                 interworking_start_fetch_anqp(wpa_s);
1241                                 return 1;
1242                         }
1243 #endif /* CONFIG_INTERWORKING */
1244                         if (wpa_supplicant_req_sched_scan(wpa_s))
1245                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1246                                                             timeout_usec);
1247                 }
1248         }
1249         return 0;
1250 }
1251
1252
1253 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1254                                               union wpa_event_data *data)
1255 {
1256         const char *rn, *rn2;
1257         struct wpa_supplicant *ifs;
1258
1259         if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
1260                 /*
1261                  * If no scan results could be fetched, then no need to
1262                  * notify those interfaces that did not actually request
1263                  * this scan. Similarly, if scan results started a new operation on this
1264                  * interface, do not notify other interfaces to avoid concurrent
1265                  * operations during a connection attempt.
1266                  */
1267                 return;
1268         }
1269
1270         /*
1271          * Check other interfaces to see if they have the same radio-name. If
1272          * so, they get updated with this same scan info.
1273          */
1274         if (!wpa_s->driver->get_radio_name)
1275                 return;
1276
1277         rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
1278         if (rn == NULL || rn[0] == '\0')
1279                 return;
1280
1281         wpa_dbg(wpa_s, MSG_DEBUG, "Checking for other virtual interfaces "
1282                 "sharing same radio (%s) in event_scan_results", rn);
1283
1284         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
1285                 if (ifs == wpa_s || !ifs->driver->get_radio_name)
1286                         continue;
1287
1288                 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
1289                 if (rn2 && os_strcmp(rn, rn2) == 0) {
1290                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1291                                    "sibling", ifs->ifname);
1292                         _wpa_supplicant_event_scan_results(ifs, data);
1293                 }
1294         }
1295 }
1296
1297 #endif /* CONFIG_NO_SCAN_PROCESSING */
1298
1299
1300 #ifdef CONFIG_WNM
1301
1302 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1303 {
1304         struct wpa_supplicant *wpa_s = eloop_ctx;
1305
1306         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1307                 return;
1308
1309         wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1310                    MAC2STR(wpa_s->bssid));
1311         /* TODO: could skip this if normal data traffic has been sent */
1312         /* TODO: Consider using some more appropriate data frame for this */
1313         if (wpa_s->l2)
1314                 l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800, (u8 *) "", 0);
1315
1316 #ifdef CONFIG_SME
1317         if (wpa_s->sme.bss_max_idle_period) {
1318                 unsigned int msec;
1319                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1320                 if (msec > 100)
1321                         msec -= 100;
1322                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1323                                        wnm_bss_keep_alive, wpa_s, NULL);
1324         }
1325 #endif /* CONFIG_SME */
1326 }
1327
1328
1329 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1330                                    const u8 *ies, size_t ies_len)
1331 {
1332         struct ieee802_11_elems elems;
1333
1334         if (ies == NULL)
1335                 return;
1336
1337         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1338                 return;
1339
1340 #ifdef CONFIG_SME
1341         if (elems.bss_max_idle_period) {
1342                 unsigned int msec;
1343                 wpa_s->sme.bss_max_idle_period =
1344                         WPA_GET_LE16(elems.bss_max_idle_period);
1345                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1346                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1347                            (elems.bss_max_idle_period[2] & 0x01) ?
1348                            " (protected keep-live required)" : "");
1349                 if (wpa_s->sme.bss_max_idle_period == 0)
1350                         wpa_s->sme.bss_max_idle_period = 1;
1351                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1352                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1353                          /* msec times 1000 */
1354                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1355                         if (msec > 100)
1356                                 msec -= 100;
1357                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1358                                                wnm_bss_keep_alive, wpa_s,
1359                                                NULL);
1360                 }
1361         }
1362 #endif /* CONFIG_SME */
1363 }
1364
1365 #endif /* CONFIG_WNM */
1366
1367
1368 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1369 {
1370 #ifdef CONFIG_WNM
1371         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1372 #endif /* CONFIG_WNM */
1373 }
1374
1375
1376 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1377                                           union wpa_event_data *data)
1378 {
1379         int l, len, found = 0, wpa_found, rsn_found;
1380         const u8 *p;
1381
1382         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1383         if (data->assoc_info.req_ies)
1384                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1385                             data->assoc_info.req_ies_len);
1386         if (data->assoc_info.resp_ies) {
1387                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1388                             data->assoc_info.resp_ies_len);
1389 #ifdef CONFIG_TDLS
1390                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1391                                         data->assoc_info.resp_ies_len);
1392 #endif /* CONFIG_TDLS */
1393 #ifdef CONFIG_WNM
1394                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1395                                        data->assoc_info.resp_ies_len);
1396 #endif /* CONFIG_WNM */
1397         }
1398         if (data->assoc_info.beacon_ies)
1399                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1400                             data->assoc_info.beacon_ies,
1401                             data->assoc_info.beacon_ies_len);
1402         if (data->assoc_info.freq)
1403                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1404                         data->assoc_info.freq);
1405
1406         p = data->assoc_info.req_ies;
1407         l = data->assoc_info.req_ies_len;
1408
1409         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1410         while (p && l >= 2) {
1411                 len = p[1] + 2;
1412                 if (len > l) {
1413                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1414                                     p, l);
1415                         break;
1416                 }
1417                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1418                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1419                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1420                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1421                                 break;
1422                         found = 1;
1423                         wpa_find_assoc_pmkid(wpa_s);
1424                         break;
1425                 }
1426                 l -= len;
1427                 p += len;
1428         }
1429         if (!found && data->assoc_info.req_ies)
1430                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1431
1432 #ifdef CONFIG_IEEE80211R
1433 #ifdef CONFIG_SME
1434         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1435                 u8 bssid[ETH_ALEN];
1436                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1437                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1438                                                  data->assoc_info.resp_ies,
1439                                                  data->assoc_info.resp_ies_len,
1440                                                  bssid) < 0) {
1441                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1442                                 "Reassociation Response failed");
1443                         wpa_supplicant_deauthenticate(
1444                                 wpa_s, WLAN_REASON_INVALID_IE);
1445                         return -1;
1446                 }
1447         }
1448
1449         p = data->assoc_info.resp_ies;
1450         l = data->assoc_info.resp_ies_len;
1451
1452 #ifdef CONFIG_WPS_STRICT
1453         if (p && wpa_s->current_ssid &&
1454             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1455                 struct wpabuf *wps;
1456                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1457                 if (wps == NULL) {
1458                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1459                                 "include WPS IE in (Re)Association Response");
1460                         return -1;
1461                 }
1462
1463                 if (wps_validate_assoc_resp(wps) < 0) {
1464                         wpabuf_free(wps);
1465                         wpa_supplicant_deauthenticate(
1466                                 wpa_s, WLAN_REASON_INVALID_IE);
1467                         return -1;
1468                 }
1469                 wpabuf_free(wps);
1470         }
1471 #endif /* CONFIG_WPS_STRICT */
1472
1473         /* Go through the IEs and make a copy of the MDIE, if present. */
1474         while (p && l >= 2) {
1475                 len = p[1] + 2;
1476                 if (len > l) {
1477                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1478                                     p, l);
1479                         break;
1480                 }
1481                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1482                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1483                         wpa_s->sme.ft_used = 1;
1484                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1485                                   MOBILITY_DOMAIN_ID_LEN);
1486                         break;
1487                 }
1488                 l -= len;
1489                 p += len;
1490         }
1491 #endif /* CONFIG_SME */
1492
1493         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1494                              data->assoc_info.resp_ies_len);
1495 #endif /* CONFIG_IEEE80211R */
1496
1497         /* WPA/RSN IE from Beacon/ProbeResp */
1498         p = data->assoc_info.beacon_ies;
1499         l = data->assoc_info.beacon_ies_len;
1500
1501         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1502          */
1503         wpa_found = rsn_found = 0;
1504         while (p && l >= 2) {
1505                 len = p[1] + 2;
1506                 if (len > l) {
1507                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1508                                     p, l);
1509                         break;
1510                 }
1511                 if (!wpa_found &&
1512                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1513                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1514                         wpa_found = 1;
1515                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1516                 }
1517
1518                 if (!rsn_found &&
1519                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1520                         rsn_found = 1;
1521                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1522                 }
1523
1524                 l -= len;
1525                 p += len;
1526         }
1527
1528         if (!wpa_found && data->assoc_info.beacon_ies)
1529                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1530         if (!rsn_found && data->assoc_info.beacon_ies)
1531                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1532         if (wpa_found || rsn_found)
1533                 wpa_s->ap_ies_from_associnfo = 1;
1534
1535         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1536             wpa_s->assoc_freq != data->assoc_info.freq) {
1537                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1538                            "%u to %u MHz",
1539                            wpa_s->assoc_freq, data->assoc_info.freq);
1540                 wpa_supplicant_update_scan_results(wpa_s);
1541         }
1542
1543         wpa_s->assoc_freq = data->assoc_info.freq;
1544
1545         return 0;
1546 }
1547
1548
1549 static struct wpa_bss * wpa_supplicant_get_new_bss(
1550         struct wpa_supplicant *wpa_s, const u8 *bssid)
1551 {
1552         struct wpa_bss *bss = NULL;
1553         struct wpa_ssid *ssid = wpa_s->current_ssid;
1554
1555         if (ssid->ssid_len > 0)
1556                 bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
1557         if (!bss)
1558                 bss = wpa_bss_get_bssid(wpa_s, bssid);
1559
1560         return bss;
1561 }
1562
1563
1564 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1565 {
1566         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1567
1568         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1569                 return -1;
1570
1571         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1572                 return 0;
1573
1574         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1575                                         WPA_IE_VENDOR_TYPE);
1576         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1577
1578         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1579                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1580             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1581                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1582                 return -1;
1583
1584         return 0;
1585 }
1586
1587
1588 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1589                                        union wpa_event_data *data)
1590 {
1591         u8 bssid[ETH_ALEN];
1592         int ft_completed;
1593         struct wpa_driver_capa capa;
1594
1595 #ifdef CONFIG_AP
1596         if (wpa_s->ap_iface) {
1597                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1598                                     data->assoc_info.addr,
1599                                     data->assoc_info.req_ies,
1600                                     data->assoc_info.req_ies_len,
1601                                     data->assoc_info.reassoc);
1602                 return;
1603         }
1604 #endif /* CONFIG_AP */
1605
1606         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1607         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1608                 return;
1609
1610         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1611                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1612                 wpa_supplicant_disassociate(
1613                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1614                 return;
1615         }
1616
1617         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1618         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1619                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1620                         MACSTR, MAC2STR(bssid));
1621                 random_add_randomness(bssid, ETH_ALEN);
1622                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1623                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1624                 wpas_notify_bssid_changed(wpa_s);
1625
1626                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1627                         wpa_clear_keys(wpa_s, bssid);
1628                 }
1629                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1630                         wpa_supplicant_disassociate(
1631                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1632                         return;
1633                 }
1634                 if (wpa_s->current_ssid) {
1635                         struct wpa_bss *bss = NULL;
1636
1637                         bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1638                         if (!bss) {
1639                                 wpa_supplicant_update_scan_results(wpa_s);
1640
1641                                 /* Get the BSS from the new scan results */
1642                                 bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
1643                         }
1644
1645                         if (bss)
1646                                 wpa_s->current_bss = bss;
1647                 }
1648
1649                 if (wpa_s->conf->ap_scan == 1 &&
1650                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1651                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1652                                 wpa_msg(wpa_s, MSG_WARNING,
1653                                         "WPA/RSN IEs not updated");
1654                 }
1655         }
1656
1657 #ifdef CONFIG_SME
1658         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1659         wpa_s->sme.prev_bssid_set = 1;
1660 #endif /* CONFIG_SME */
1661
1662         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1663         if (wpa_s->current_ssid) {
1664                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1665                  * initialized before association, but for other modes,
1666                  * initialize PC/SC here, if the current configuration needs
1667                  * smartcard or SIM/USIM. */
1668                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1669         }
1670         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1671         if (wpa_s->l2)
1672                 l2_packet_notify_auth_start(wpa_s->l2);
1673
1674         /*
1675          * Set portEnabled first to FALSE in order to get EAP state machine out
1676          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1677          * state machine may transit to AUTHENTICATING state based on obsolete
1678          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1679          * AUTHENTICATED without ever giving chance to EAP state machine to
1680          * reset the state.
1681          */
1682         if (!ft_completed) {
1683                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1684                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1685         }
1686         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1687                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1688         /* 802.1X::portControl = Auto */
1689         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1690         wpa_s->eapol_received = 0;
1691         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1692             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1693             (wpa_s->current_ssid &&
1694              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1695                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1696                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1697         } else if (!ft_completed) {
1698                 /* Timeout for receiving the first EAPOL packet */
1699                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1700         }
1701         wpa_supplicant_cancel_scan(wpa_s);
1702
1703         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1704             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1705                 /*
1706                  * We are done; the driver will take care of RSN 4-way
1707                  * handshake.
1708                  */
1709                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1710                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1711                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1712                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1713         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1714                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1715                 /*
1716                  * The driver will take care of RSN 4-way handshake, so we need
1717                  * to allow EAPOL supplicant to complete its work without
1718                  * waiting for WPA supplicant.
1719                  */
1720                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1721         } else if (ft_completed) {
1722                 /*
1723                  * FT protocol completed - make sure EAPOL state machine ends
1724                  * up in authenticated.
1725                  */
1726                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1727                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1728                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1729                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1730         }
1731
1732         if (wpa_s->pending_eapol_rx) {
1733                 struct os_time now, age;
1734                 os_get_time(&now);
1735                 os_time_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1736                 if (age.sec == 0 && age.usec < 100000 &&
1737                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1738                     0) {
1739                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1740                                 "frame that was received just before "
1741                                 "association notification");
1742                         wpa_supplicant_rx_eapol(
1743                                 wpa_s, wpa_s->pending_eapol_rx_src,
1744                                 wpabuf_head(wpa_s->pending_eapol_rx),
1745                                 wpabuf_len(wpa_s->pending_eapol_rx));
1746                 }
1747                 wpabuf_free(wpa_s->pending_eapol_rx);
1748                 wpa_s->pending_eapol_rx = NULL;
1749         }
1750
1751         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1752              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
1753             wpa_s->current_ssid && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1754             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE) {
1755                 /* Set static WEP keys again */
1756                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
1757         }
1758
1759 #ifdef CONFIG_IBSS_RSN
1760         if (wpa_s->current_ssid &&
1761             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
1762             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1763             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
1764             wpa_s->ibss_rsn == NULL) {
1765                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
1766                 if (!wpa_s->ibss_rsn) {
1767                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
1768                         wpa_supplicant_deauthenticate(
1769                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1770                         return;
1771                 }
1772
1773                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
1774         }
1775 #endif /* CONFIG_IBSS_RSN */
1776
1777         wpas_wps_notify_assoc(wpa_s, bssid);
1778 }
1779
1780
1781 static int disconnect_reason_recoverable(u16 reason_code)
1782 {
1783         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
1784                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
1785                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
1786 }
1787
1788
1789 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
1790                                           u16 reason_code,
1791                                           int locally_generated)
1792 {
1793         const u8 *bssid;
1794
1795         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1796                 /*
1797                  * At least Host AP driver and a Prism3 card seemed to be
1798                  * generating streams of disconnected events when configuring
1799                  * IBSS for WPA-None. Ignore them for now.
1800                  */
1801                 return;
1802         }
1803
1804         bssid = wpa_s->bssid;
1805         if (is_zero_ether_addr(bssid))
1806                 bssid = wpa_s->pending_bssid;
1807
1808         if (!is_zero_ether_addr(bssid) ||
1809             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
1810                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
1811                         " reason=%d%s",
1812                         MAC2STR(bssid), reason_code,
1813                         locally_generated ? " locally_generated=1" : "");
1814         }
1815 }
1816
1817
1818 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
1819                                  int locally_generated)
1820 {
1821         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
1822             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
1823                 return 0; /* Not in 4-way handshake with PSK */
1824
1825         /*
1826          * It looks like connection was lost while trying to go through PSK
1827          * 4-way handshake. Filter out known disconnection cases that are caused
1828          * by something else than PSK mismatch to avoid confusing reports.
1829          */
1830
1831         if (locally_generated) {
1832                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
1833                         return 0;
1834         }
1835
1836         return 1;
1837 }
1838
1839
1840 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
1841                                                  u16 reason_code,
1842                                                  int locally_generated)
1843 {
1844         const u8 *bssid;
1845         int authenticating;
1846         u8 prev_pending_bssid[ETH_ALEN];
1847         struct wpa_bss *fast_reconnect = NULL;
1848         struct wpa_ssid *fast_reconnect_ssid = NULL;
1849         struct wpa_ssid *last_ssid;
1850
1851         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
1852         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
1853
1854         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1855                 /*
1856                  * At least Host AP driver and a Prism3 card seemed to be
1857                  * generating streams of disconnected events when configuring
1858                  * IBSS for WPA-None. Ignore them for now.
1859                  */
1860                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
1861                         "IBSS/WPA-None mode");
1862                 return;
1863         }
1864
1865         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
1866                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
1867                         "pre-shared key may be incorrect");
1868                 wpas_auth_failed(wpa_s);
1869         }
1870         if (!wpa_s->auto_reconnect_disabled ||
1871             wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
1872                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
1873                         "reconnect (wps=%d wpa_state=%d)",
1874                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
1875                         wpa_s->wpa_state);
1876                 if (wpa_s->wpa_state == WPA_COMPLETED &&
1877                     wpa_s->current_ssid &&
1878                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
1879                     !locally_generated &&
1880                     disconnect_reason_recoverable(reason_code)) {
1881                         /*
1882                          * It looks like the AP has dropped association with
1883                          * us, but could allow us to get back in. Try to
1884                          * reconnect to the same BSS without full scan to save
1885                          * time for some common cases.
1886                          */
1887                         fast_reconnect = wpa_s->current_bss;
1888                         fast_reconnect_ssid = wpa_s->current_ssid;
1889                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
1890                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1891                 else
1892                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
1893                                 "immediate scan");
1894         } else {
1895                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
1896                         "try to re-connect");
1897                 wpa_s->reassociate = 0;
1898                 wpa_s->disconnected = 1;
1899                 wpa_supplicant_cancel_sched_scan(wpa_s);
1900         }
1901         bssid = wpa_s->bssid;
1902         if (is_zero_ether_addr(bssid))
1903                 bssid = wpa_s->pending_bssid;
1904         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
1905                 wpas_connection_failed(wpa_s, bssid);
1906         wpa_sm_notify_disassoc(wpa_s->wpa);
1907         if (locally_generated)
1908                 wpa_s->disconnect_reason = -reason_code;
1909         else
1910                 wpa_s->disconnect_reason = reason_code;
1911         wpas_notify_disconnect_reason(wpa_s);
1912         if (wpa_supplicant_dynamic_keys(wpa_s)) {
1913                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
1914                 wpa_s->keys_cleared = 0;
1915                 wpa_clear_keys(wpa_s, wpa_s->bssid);
1916         }
1917         last_ssid = wpa_s->current_ssid;
1918         wpa_supplicant_mark_disassoc(wpa_s);
1919
1920         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
1921                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
1922                 wpa_s->current_ssid = last_ssid;
1923         }
1924
1925         if (fast_reconnect) {
1926 #ifndef CONFIG_NO_SCAN_PROCESSING
1927                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
1928                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
1929                                            fast_reconnect_ssid) < 0) {
1930                         /* Recover through full scan */
1931                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
1932                 }
1933 #endif /* CONFIG_NO_SCAN_PROCESSING */
1934         }
1935 }
1936
1937
1938 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
1939 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
1940 {
1941         struct wpa_supplicant *wpa_s = eloop_ctx;
1942
1943         if (!wpa_s->pending_mic_error_report)
1944                 return;
1945
1946         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
1947         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
1948         wpa_s->pending_mic_error_report = 0;
1949 }
1950 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
1951
1952
1953 static void
1954 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
1955                                          union wpa_event_data *data)
1956 {
1957         int pairwise;
1958         struct os_time t;
1959
1960         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
1961         pairwise = (data && data->michael_mic_failure.unicast);
1962         os_get_time(&t);
1963         if ((wpa_s->last_michael_mic_error &&
1964              t.sec - wpa_s->last_michael_mic_error <= 60) ||
1965             wpa_s->pending_mic_error_report) {
1966                 if (wpa_s->pending_mic_error_report) {
1967                         /*
1968                          * Send the pending MIC error report immediately since
1969                          * we are going to start countermeasures and AP better
1970                          * do the same.
1971                          */
1972                         wpa_sm_key_request(wpa_s->wpa, 1,
1973                                            wpa_s->pending_mic_error_pairwise);
1974                 }
1975
1976                 /* Send the new MIC error report immediately since we are going
1977                  * to start countermeasures and AP better do the same.
1978                  */
1979                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
1980
1981                 /* initialize countermeasures */
1982                 wpa_s->countermeasures = 1;
1983
1984                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
1985
1986                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
1987
1988                 /*
1989                  * Need to wait for completion of request frame. We do not get
1990                  * any callback for the message completion, so just wait a
1991                  * short while and hope for the best. */
1992                 os_sleep(0, 10000);
1993
1994                 wpa_drv_set_countermeasures(wpa_s, 1);
1995                 wpa_supplicant_deauthenticate(wpa_s,
1996                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
1997                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
1998                                      wpa_s, NULL);
1999                 eloop_register_timeout(60, 0,
2000                                        wpa_supplicant_stop_countermeasures,
2001                                        wpa_s, NULL);
2002                 /* TODO: mark the AP rejected for 60 second. STA is
2003                  * allowed to associate with another AP.. */
2004         } else {
2005 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2006                 if (wpa_s->mic_errors_seen) {
2007                         /*
2008                          * Reduce the effectiveness of Michael MIC error
2009                          * reports as a means for attacking against TKIP if
2010                          * more than one MIC failure is noticed with the same
2011                          * PTK. We delay the transmission of the reports by a
2012                          * random time between 0 and 60 seconds in order to
2013                          * force the attacker wait 60 seconds before getting
2014                          * the information on whether a frame resulted in a MIC
2015                          * failure.
2016                          */
2017                         u8 rval[4];
2018                         int sec;
2019
2020                         if (os_get_random(rval, sizeof(rval)) < 0)
2021                                 sec = os_random() % 60;
2022                         else
2023                                 sec = WPA_GET_BE32(rval) % 60;
2024                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2025                                 "report %d seconds", sec);
2026                         wpa_s->pending_mic_error_report = 1;
2027                         wpa_s->pending_mic_error_pairwise = pairwise;
2028                         eloop_cancel_timeout(
2029                                 wpa_supplicant_delayed_mic_error_report,
2030                                 wpa_s, NULL);
2031                         eloop_register_timeout(
2032                                 sec, os_random() % 1000000,
2033                                 wpa_supplicant_delayed_mic_error_report,
2034                                 wpa_s, NULL);
2035                 } else {
2036                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2037                 }
2038 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2039                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2040 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2041         }
2042         wpa_s->last_michael_mic_error = t.sec;
2043         wpa_s->mic_errors_seen++;
2044 }
2045
2046
2047 #ifdef CONFIG_TERMINATE_ONLASTIF
2048 static int any_interfaces(struct wpa_supplicant *head)
2049 {
2050         struct wpa_supplicant *wpa_s;
2051
2052         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2053                 if (!wpa_s->interface_removed)
2054                         return 1;
2055         return 0;
2056 }
2057 #endif /* CONFIG_TERMINATE_ONLASTIF */
2058
2059
2060 static void
2061 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2062                                       union wpa_event_data *data)
2063 {
2064         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2065                 return;
2066
2067         switch (data->interface_status.ievent) {
2068         case EVENT_INTERFACE_ADDED:
2069                 if (!wpa_s->interface_removed)
2070                         break;
2071                 wpa_s->interface_removed = 0;
2072                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2073                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2074                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2075                                 "driver after interface was added");
2076                 }
2077                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2078                 break;
2079         case EVENT_INTERFACE_REMOVED:
2080                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2081                 wpa_s->interface_removed = 1;
2082                 wpa_supplicant_mark_disassoc(wpa_s);
2083                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2084                 l2_packet_deinit(wpa_s->l2);
2085                 wpa_s->l2 = NULL;
2086 #ifdef CONFIG_IBSS_RSN
2087                 ibss_rsn_deinit(wpa_s->ibss_rsn);
2088                 wpa_s->ibss_rsn = NULL;
2089 #endif /* CONFIG_IBSS_RSN */
2090 #ifdef CONFIG_TERMINATE_ONLASTIF
2091                 /* check if last interface */
2092                 if (!any_interfaces(wpa_s->global->ifaces))
2093                         eloop_terminate();
2094 #endif /* CONFIG_TERMINATE_ONLASTIF */
2095                 break;
2096         }
2097 }
2098
2099
2100 #ifdef CONFIG_PEERKEY
2101 static void
2102 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2103                               union wpa_event_data *data)
2104 {
2105         if (data == NULL)
2106                 return;
2107         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2108 }
2109 #endif /* CONFIG_PEERKEY */
2110
2111
2112 #ifdef CONFIG_TDLS
2113 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2114                                       union wpa_event_data *data)
2115 {
2116         if (data == NULL)
2117                 return;
2118         switch (data->tdls.oper) {
2119         case TDLS_REQUEST_SETUP:
2120                 wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2121                 break;
2122         case TDLS_REQUEST_TEARDOWN:
2123                 wpa_tdls_send_teardown(wpa_s->wpa, data->tdls.peer,
2124                                        data->tdls.reason_code);
2125                 break;
2126         }
2127 }
2128 #endif /* CONFIG_TDLS */
2129
2130
2131 #ifdef CONFIG_IEEE80211V
2132 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2133                                      union wpa_event_data *data)
2134 {
2135         if (data == NULL)
2136                 return;
2137         switch (data->wnm.oper) {
2138         case WNM_OPER_SLEEP:
2139                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2140                            "(action=%d, intval=%d)",
2141                            data->wnm.sleep_action, data->wnm.sleep_intval);
2142                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2143                                              data->wnm.sleep_intval);
2144                 break;
2145         }
2146 }
2147 #endif /* CONFIG_IEEE80211V */
2148
2149
2150 #ifdef CONFIG_IEEE80211R
2151 static void
2152 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2153                                  union wpa_event_data *data)
2154 {
2155         if (data == NULL)
2156                 return;
2157
2158         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2159                                     data->ft_ies.ies_len,
2160                                     data->ft_ies.ft_action,
2161                                     data->ft_ies.target_ap,
2162                                     data->ft_ies.ric_ies,
2163                                     data->ft_ies.ric_ies_len) < 0) {
2164                 /* TODO: prevent MLME/driver from trying to associate? */
2165         }
2166 }
2167 #endif /* CONFIG_IEEE80211R */
2168
2169
2170 #ifdef CONFIG_IBSS_RSN
2171 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2172                                                 union wpa_event_data *data)
2173 {
2174         struct wpa_ssid *ssid;
2175         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2176                 return;
2177         if (data == NULL)
2178                 return;
2179         ssid = wpa_s->current_ssid;
2180         if (ssid == NULL)
2181                 return;
2182         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2183                 return;
2184
2185         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2186 }
2187 #endif /* CONFIG_IBSS_RSN */
2188
2189
2190 #ifdef CONFIG_IEEE80211R
2191 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2192                          size_t len)
2193 {
2194         const u8 *sta_addr, *target_ap_addr;
2195         u16 status;
2196
2197         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2198         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2199                 return; /* only SME case supported for now */
2200         if (len < 1 + 2 * ETH_ALEN + 2)
2201                 return;
2202         if (data[0] != 2)
2203                 return; /* Only FT Action Response is supported for now */
2204         sta_addr = data + 1;
2205         target_ap_addr = data + 1 + ETH_ALEN;
2206         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2207         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2208                 MACSTR " TargetAP " MACSTR " status %u",
2209                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2210
2211         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2212                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2213                         " in FT Action Response", MAC2STR(sta_addr));
2214                 return;
2215         }
2216
2217         if (status) {
2218                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2219                         "failure (status code %d)", status);
2220                 /* TODO: report error to FT code(?) */
2221                 return;
2222         }
2223
2224         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2225                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2226                                     target_ap_addr, NULL, 0) < 0)
2227                 return;
2228
2229 #ifdef CONFIG_SME
2230         {
2231                 struct wpa_bss *bss;
2232                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2233                 if (bss)
2234                         wpa_s->sme.freq = bss->freq;
2235                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2236                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2237                               WLAN_AUTH_FT);
2238         }
2239 #endif /* CONFIG_SME */
2240 }
2241 #endif /* CONFIG_IEEE80211R */
2242
2243
2244 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2245                                                struct unprot_deauth *e)
2246 {
2247 #ifdef CONFIG_IEEE80211W
2248         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2249                    "dropped: " MACSTR " -> " MACSTR
2250                    " (reason code %u)",
2251                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2252         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2253 #endif /* CONFIG_IEEE80211W */
2254 }
2255
2256
2257 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2258                                                  struct unprot_disassoc *e)
2259 {
2260 #ifdef CONFIG_IEEE80211W
2261         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2262                    "dropped: " MACSTR " -> " MACSTR
2263                    " (reason code %u)",
2264                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2265         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2266 #endif /* CONFIG_IEEE80211W */
2267 }
2268
2269
2270 static void wnm_action_rx(struct wpa_supplicant *wpa_s, struct rx_action *rx)
2271 {
2272         u8 action, mode;
2273         const u8 *pos, *end;
2274
2275         if (rx->data == NULL || rx->len == 0)
2276                 return;
2277
2278         pos = rx->data;
2279         end = pos + rx->len;
2280         action = *pos++;
2281
2282         wpa_printf(MSG_DEBUG, "WNM: RX action %u from " MACSTR,
2283                    action, MAC2STR(rx->sa));
2284         switch (action) {
2285         case WNM_BSS_TRANS_MGMT_REQ:
2286                 if (pos + 5 > end)
2287                         break;
2288                 wpa_printf(MSG_DEBUG, "WNM: BSS Transition Management "
2289                            "Request: dialog_token=%u request_mode=0x%x "
2290                            "disassoc_timer=%u validity_interval=%u",
2291                            pos[0], pos[1], WPA_GET_LE16(pos + 2), pos[4]);
2292                 mode = pos[1];
2293                 pos += 5;
2294                 if (mode & 0x08)
2295                         pos += 12; /* BSS Termination Duration */
2296                 if (mode & 0x10) {
2297                         char url[256];
2298                         if (pos + 1 > end || pos + 1 + pos[0] > end) {
2299                                 wpa_printf(MSG_DEBUG, "WNM: Invalid BSS "
2300                                            "Transition Management Request "
2301                                            "(URL)");
2302                                 break;
2303                         }
2304                         os_memcpy(url, pos + 1, pos[0]);
2305                         url[pos[0]] = '\0';
2306                         wpa_msg(wpa_s, MSG_INFO, "WNM: ESS Disassociation "
2307                                 "Imminent - session_info_url=%s", url);
2308                 }
2309                 break;
2310         }
2311 }
2312
2313
2314 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2315                           union wpa_event_data *data)
2316 {
2317         struct wpa_supplicant *wpa_s = ctx;
2318         u16 reason_code = 0;
2319         int locally_generated = 0;
2320
2321         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2322             event != EVENT_INTERFACE_ENABLED &&
2323             event != EVENT_INTERFACE_STATUS &&
2324             event != EVENT_SCHED_SCAN_STOPPED) {
2325                 wpa_dbg(wpa_s, MSG_DEBUG,
2326                         "Ignore event %s (%d) while interface is disabled",
2327                         event_to_string(event), event);
2328                 return;
2329         }
2330
2331 #ifndef CONFIG_NO_STDOUT_DEBUG
2332 {
2333         int level = MSG_DEBUG;
2334
2335         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2336                 const struct ieee80211_hdr *hdr;
2337                 u16 fc;
2338                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2339                 fc = le_to_host16(hdr->frame_control);
2340                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2341                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2342                         level = MSG_EXCESSIVE;
2343         }
2344
2345         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2346                 event_to_string(event), event);
2347 }
2348 #endif /* CONFIG_NO_STDOUT_DEBUG */
2349
2350         switch (event) {
2351         case EVENT_AUTH:
2352                 sme_event_auth(wpa_s, data);
2353                 break;
2354         case EVENT_ASSOC:
2355                 wpa_supplicant_event_assoc(wpa_s, data);
2356                 break;
2357         case EVENT_DISASSOC:
2358                 wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2359                 if (data) {
2360                         wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2361                                 data->disassoc_info.reason_code,
2362                                 data->disassoc_info.locally_generated ?
2363                                 " (locally generated)" : "");
2364                         if (data->disassoc_info.addr)
2365                                 wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2366                                         MAC2STR(data->disassoc_info.addr));
2367                 }
2368 #ifdef CONFIG_AP
2369                 if (wpa_s->ap_iface && data && data->disassoc_info.addr) {
2370                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2371                                                data->disassoc_info.addr);
2372                         break;
2373                 }
2374                 if (wpa_s->ap_iface) {
2375                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in "
2376                                 "AP mode");
2377                         break;
2378                 }
2379 #endif /* CONFIG_AP */
2380                 if (data) {
2381                         reason_code = data->disassoc_info.reason_code;
2382                         locally_generated =
2383                                 data->disassoc_info.locally_generated;
2384                         wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2385                                     data->disassoc_info.ie,
2386                                     data->disassoc_info.ie_len);
2387 #ifdef CONFIG_P2P
2388                         wpas_p2p_disassoc_notif(
2389                                 wpa_s, data->disassoc_info.addr, reason_code,
2390                                 data->disassoc_info.ie,
2391                                 data->disassoc_info.ie_len,
2392                                 locally_generated);
2393 #endif /* CONFIG_P2P */
2394                 }
2395                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2396                         sme_event_disassoc(wpa_s, data);
2397                 /* fall through */
2398         case EVENT_DEAUTH:
2399                 if (event == EVENT_DEAUTH) {
2400                         wpa_dbg(wpa_s, MSG_DEBUG,
2401                                 "Deauthentication notification");
2402                         if (data) {
2403                                 reason_code = data->deauth_info.reason_code;
2404                                 locally_generated =
2405                                         data->deauth_info.locally_generated;
2406                                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2407                                         data->deauth_info.reason_code,
2408                                         data->deauth_info.locally_generated ?
2409                                         " (locally generated)" : "");
2410                                 if (data->deauth_info.addr) {
2411                                         wpa_dbg(wpa_s, MSG_DEBUG, " * address "
2412                                                 MACSTR,
2413                                                 MAC2STR(data->deauth_info.
2414                                                         addr));
2415                                 }
2416                                 wpa_hexdump(MSG_DEBUG,
2417                                             "Deauthentication frame IE(s)",
2418                                             data->deauth_info.ie,
2419                                             data->deauth_info.ie_len);
2420                         }
2421                 }
2422 #ifdef CONFIG_AP
2423                 if (wpa_s->ap_iface && data && data->deauth_info.addr) {
2424                         hostapd_notif_disassoc(wpa_s->ap_iface->bss[0],
2425                                                data->deauth_info.addr);
2426                         break;
2427                 }
2428                 if (wpa_s->ap_iface) {
2429                         wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in "
2430                                 "AP mode");
2431                         break;
2432                 }
2433 #endif /* CONFIG_AP */
2434                 wpa_supplicant_event_disassoc(wpa_s, reason_code,
2435                                               locally_generated);
2436                 if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2437                     ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2438                       (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2439                      eapol_sm_failed(wpa_s->eapol)))
2440                         wpas_auth_failed(wpa_s);
2441 #ifdef CONFIG_P2P
2442                 if (event == EVENT_DEAUTH && data) {
2443                         if (wpas_p2p_deauth_notif(wpa_s,
2444                                                   data->deauth_info.addr,
2445                                                   reason_code,
2446                                                   data->deauth_info.ie,
2447                                                   data->deauth_info.ie_len,
2448                                                   locally_generated) > 0) {
2449                                 /*
2450                                  * The interface was removed, so cannot
2451                                  * continue processing any additional
2452                                  * operations after this.
2453                                  */
2454                                 break;
2455                         }
2456                 }
2457 #endif /* CONFIG_P2P */
2458                 wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2459                                                      locally_generated);
2460                 break;
2461         case EVENT_MICHAEL_MIC_FAILURE:
2462                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2463                 break;
2464 #ifndef CONFIG_NO_SCAN_PROCESSING
2465         case EVENT_SCAN_RESULTS:
2466                 wpa_supplicant_event_scan_results(wpa_s, data);
2467 #ifdef CONFIG_P2P
2468         if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
2469             wpa_s->global->p2p != NULL &&
2470             wpa_s->wpa_state != WPA_AUTHENTICATING &&
2471             wpa_s->wpa_state != WPA_ASSOCIATING) {
2472                 wpa_s->global->p2p_cb_on_scan_complete = 0;
2473                 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
2474                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
2475                                 "continued after scan result processing");
2476                 }
2477         }
2478 #endif /* CONFIG_P2P */
2479                 break;
2480 #endif /* CONFIG_NO_SCAN_PROCESSING */
2481         case EVENT_ASSOCINFO:
2482                 wpa_supplicant_event_associnfo(wpa_s, data);
2483                 break;
2484         case EVENT_INTERFACE_STATUS:
2485                 wpa_supplicant_event_interface_status(wpa_s, data);
2486                 break;
2487         case EVENT_PMKID_CANDIDATE:
2488                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
2489                 break;
2490 #ifdef CONFIG_PEERKEY
2491         case EVENT_STKSTART:
2492                 wpa_supplicant_event_stkstart(wpa_s, data);
2493                 break;
2494 #endif /* CONFIG_PEERKEY */
2495 #ifdef CONFIG_TDLS
2496         case EVENT_TDLS:
2497                 wpa_supplicant_event_tdls(wpa_s, data);
2498                 break;
2499 #endif /* CONFIG_TDLS */
2500 #ifdef CONFIG_IEEE80211V
2501         case EVENT_WNM:
2502                 wpa_supplicant_event_wnm(wpa_s, data);
2503                 break;
2504 #endif /* CONFIG_IEEE80211V */
2505 #ifdef CONFIG_IEEE80211R
2506         case EVENT_FT_RESPONSE:
2507                 wpa_supplicant_event_ft_response(wpa_s, data);
2508                 break;
2509 #endif /* CONFIG_IEEE80211R */
2510 #ifdef CONFIG_IBSS_RSN
2511         case EVENT_IBSS_RSN_START:
2512                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
2513                 break;
2514 #endif /* CONFIG_IBSS_RSN */
2515         case EVENT_ASSOC_REJECT:
2516                 if (data->assoc_reject.bssid)
2517                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2518                                 "bssid=" MACSTR " status_code=%u",
2519                                 MAC2STR(data->assoc_reject.bssid),
2520                                 data->assoc_reject.status_code);
2521                 else
2522                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
2523                                 "status_code=%u",
2524                                 data->assoc_reject.status_code);
2525                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2526                         sme_event_assoc_reject(wpa_s, data);
2527                 else {
2528                         const u8 *bssid = data->assoc_reject.bssid;
2529                         if (bssid == NULL || is_zero_ether_addr(bssid))
2530                                 bssid = wpa_s->pending_bssid;
2531                         wpas_connection_failed(wpa_s, bssid);
2532                         wpa_supplicant_mark_disassoc(wpa_s);
2533                 }
2534                 break;
2535         case EVENT_AUTH_TIMED_OUT:
2536                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2537                         sme_event_auth_timed_out(wpa_s, data);
2538                 break;
2539         case EVENT_ASSOC_TIMED_OUT:
2540                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2541                         sme_event_assoc_timed_out(wpa_s, data);
2542                 break;
2543         case EVENT_TX_STATUS:
2544                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
2545                         " type=%d stype=%d",
2546                         MAC2STR(data->tx_status.dst),
2547                         data->tx_status.type, data->tx_status.stype);
2548 #ifdef CONFIG_AP
2549                 if (wpa_s->ap_iface == NULL) {
2550 #ifdef CONFIG_OFFCHANNEL
2551                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2552                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
2553                                 offchannel_send_action_tx_status(
2554                                         wpa_s, data->tx_status.dst,
2555                                         data->tx_status.data,
2556                                         data->tx_status.data_len,
2557                                         data->tx_status.ack ?
2558                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
2559                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
2560 #endif /* CONFIG_OFFCHANNEL */
2561                         break;
2562                 }
2563 #endif /* CONFIG_AP */
2564 #ifdef CONFIG_OFFCHANNEL
2565                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
2566                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
2567                 /*
2568                  * Catch TX status events for Action frames we sent via group
2569                  * interface in GO mode.
2570                  */
2571                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
2572                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
2573                     os_memcmp(wpa_s->parent->pending_action_dst,
2574                               data->tx_status.dst, ETH_ALEN) == 0) {
2575                         offchannel_send_action_tx_status(
2576                                 wpa_s->parent, data->tx_status.dst,
2577                                 data->tx_status.data,
2578                                 data->tx_status.data_len,
2579                                 data->tx_status.ack ?
2580                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
2581                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
2582                         break;
2583                 }
2584 #endif /* CONFIG_OFFCHANNEL */
2585 #ifdef CONFIG_AP
2586                 switch (data->tx_status.type) {
2587                 case WLAN_FC_TYPE_MGMT:
2588                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
2589                                       data->tx_status.data_len,
2590                                       data->tx_status.stype,
2591                                       data->tx_status.ack);
2592                         break;
2593                 case WLAN_FC_TYPE_DATA:
2594                         ap_tx_status(wpa_s, data->tx_status.dst,
2595                                      data->tx_status.data,
2596                                      data->tx_status.data_len,
2597                                      data->tx_status.ack);
2598                         break;
2599                 }
2600 #endif /* CONFIG_AP */
2601                 break;
2602 #ifdef CONFIG_AP
2603         case EVENT_EAPOL_TX_STATUS:
2604                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
2605                                    data->eapol_tx_status.data,
2606                                    data->eapol_tx_status.data_len,
2607                                    data->eapol_tx_status.ack);
2608                 break;
2609         case EVENT_DRIVER_CLIENT_POLL_OK:
2610                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
2611                 break;
2612         case EVENT_RX_FROM_UNKNOWN:
2613                 if (wpa_s->ap_iface == NULL)
2614                         break;
2615                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
2616                                        data->rx_from_unknown.wds);
2617                 break;
2618         case EVENT_CH_SWITCH:
2619                 if (!data)
2620                         break;
2621                 if (!wpa_s->ap_iface) {
2622                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
2623                                 "event in non-AP mode");
2624                         break;
2625                 }
2626
2627 #ifdef CONFIG_AP
2628                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
2629                                   data->ch_switch.ht_enabled,
2630                                   data->ch_switch.ch_offset);
2631 #endif /* CONFIG_AP */
2632                 break;
2633         case EVENT_RX_MGMT: {
2634                 u16 fc, stype;
2635                 const struct ieee80211_mgmt *mgmt;
2636
2637                 mgmt = (const struct ieee80211_mgmt *)
2638                         data->rx_mgmt.frame;
2639                 fc = le_to_host16(mgmt->frame_control);
2640                 stype = WLAN_FC_GET_STYPE(fc);
2641
2642                 if (wpa_s->ap_iface == NULL) {
2643 #ifdef CONFIG_P2P
2644                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2645                             data->rx_mgmt.frame_len > 24) {
2646                                 const u8 *src = mgmt->sa;
2647                                 const u8 *ie = mgmt->u.probe_req.variable;
2648                                 size_t ie_len = data->rx_mgmt.frame_len -
2649                                         (mgmt->u.probe_req.variable -
2650                                          data->rx_mgmt.frame);
2651                                 wpas_p2p_probe_req_rx(
2652                                         wpa_s, src, mgmt->da,
2653                                         mgmt->bssid, ie, ie_len,
2654                                         data->rx_mgmt.ssi_signal);
2655                                 break;
2656                         }
2657 #endif /* CONFIG_P2P */
2658                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
2659                                 "management frame in non-AP mode");
2660                         break;
2661                 }
2662
2663                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
2664                     data->rx_mgmt.frame_len > 24) {
2665                         const u8 *ie = mgmt->u.probe_req.variable;
2666                         size_t ie_len = data->rx_mgmt.frame_len -
2667                                 (mgmt->u.probe_req.variable -
2668                                  data->rx_mgmt.frame);
2669
2670                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
2671                                          mgmt->bssid, ie, ie_len,
2672                                          data->rx_mgmt.ssi_signal);
2673                 }
2674
2675                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
2676                 break;
2677                 }
2678 #endif /* CONFIG_AP */
2679         case EVENT_RX_ACTION:
2680                 wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2681                         " Category=%u DataLen=%d freq=%d MHz",
2682                         MAC2STR(data->rx_action.sa),
2683                         data->rx_action.category, (int) data->rx_action.len,
2684                         data->rx_action.freq);
2685 #ifdef CONFIG_IEEE80211R
2686                 if (data->rx_action.category == WLAN_ACTION_FT) {
2687                         ft_rx_action(wpa_s, data->rx_action.data,
2688                                      data->rx_action.len);
2689                         break;
2690                 }
2691 #endif /* CONFIG_IEEE80211R */
2692 #ifdef CONFIG_IEEE80211W
2693 #ifdef CONFIG_SME
2694                 if (data->rx_action.category == WLAN_ACTION_SA_QUERY) {
2695                         sme_sa_query_rx(wpa_s, data->rx_action.sa,
2696                                         data->rx_action.data,
2697                                         data->rx_action.len);
2698                         break;
2699                 }
2700 #endif /* CONFIG_SME */
2701 #endif /* CONFIG_IEEE80211W */
2702 #ifdef CONFIG_IEEE80211V
2703                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2704                         ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
2705                         break;
2706                 }
2707 #endif /* CONFIG_IEEE80211V */
2708 #ifdef CONFIG_GAS
2709                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2710                     gas_query_rx(wpa_s->gas, data->rx_action.da,
2711                                  data->rx_action.sa, data->rx_action.bssid,
2712                                  data->rx_action.data, data->rx_action.len,
2713                                  data->rx_action.freq) == 0)
2714                         break;
2715 #endif /* CONFIG_GAS */
2716                 if (data->rx_action.category == WLAN_ACTION_WNM) {
2717                         wnm_action_rx(wpa_s, &data->rx_action);
2718                         break;
2719                 }
2720 #ifdef CONFIG_TDLS
2721                 if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
2722                     data->rx_action.len >= 4 &&
2723                     data->rx_action.data[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2724                         wpa_dbg(wpa_s, MSG_DEBUG, "TDLS: Received Discovery "
2725                                 "Response from " MACSTR,
2726                                 MAC2STR(data->rx_action.sa));
2727                         break;
2728                 }
2729 #endif /* CONFIG_TDLS */
2730 #ifdef CONFIG_P2P
2731                 wpas_p2p_rx_action(wpa_s, data->rx_action.da,
2732                                    data->rx_action.sa,
2733                                    data->rx_action.bssid,
2734                                    data->rx_action.category,
2735                                    data->rx_action.data,
2736                                    data->rx_action.len, data->rx_action.freq);
2737 #endif /* CONFIG_P2P */
2738                 break;
2739         case EVENT_RX_PROBE_REQ:
2740                 if (data->rx_probe_req.sa == NULL ||
2741                     data->rx_probe_req.ie == NULL)
2742                         break;
2743 #ifdef CONFIG_AP
2744                 if (wpa_s->ap_iface) {
2745                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
2746                                              data->rx_probe_req.sa,
2747                                              data->rx_probe_req.da,
2748                                              data->rx_probe_req.bssid,
2749                                              data->rx_probe_req.ie,
2750                                              data->rx_probe_req.ie_len,
2751                                              data->rx_probe_req.ssi_signal);
2752                         break;
2753                 }
2754 #endif /* CONFIG_AP */
2755 #ifdef CONFIG_P2P
2756                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
2757                                       data->rx_probe_req.da,
2758                                       data->rx_probe_req.bssid,
2759                                       data->rx_probe_req.ie,
2760                                       data->rx_probe_req.ie_len,
2761                                       data->rx_probe_req.ssi_signal);
2762 #endif /* CONFIG_P2P */
2763                 break;
2764         case EVENT_REMAIN_ON_CHANNEL:
2765 #ifdef CONFIG_OFFCHANNEL
2766                 offchannel_remain_on_channel_cb(
2767                         wpa_s, data->remain_on_channel.freq,
2768                         data->remain_on_channel.duration);
2769 #endif /* CONFIG_OFFCHANNEL */
2770 #ifdef CONFIG_P2P
2771                 wpas_p2p_remain_on_channel_cb(
2772                         wpa_s, data->remain_on_channel.freq,
2773                         data->remain_on_channel.duration);
2774 #endif /* CONFIG_P2P */
2775                 break;
2776         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
2777 #ifdef CONFIG_OFFCHANNEL
2778                 offchannel_cancel_remain_on_channel_cb(
2779                         wpa_s, data->remain_on_channel.freq);
2780 #endif /* CONFIG_OFFCHANNEL */
2781 #ifdef CONFIG_P2P
2782                 wpas_p2p_cancel_remain_on_channel_cb(
2783                         wpa_s, data->remain_on_channel.freq);
2784 #endif /* CONFIG_P2P */
2785                 break;
2786 #ifdef CONFIG_P2P
2787         case EVENT_P2P_DEV_FOUND: {
2788                 struct p2p_peer_info peer_info;
2789
2790                 os_memset(&peer_info, 0, sizeof(peer_info));
2791                 if (data->p2p_dev_found.dev_addr)
2792                         os_memcpy(peer_info.p2p_device_addr,
2793                                   data->p2p_dev_found.dev_addr, ETH_ALEN);
2794                 if (data->p2p_dev_found.pri_dev_type)
2795                         os_memcpy(peer_info.pri_dev_type,
2796                                   data->p2p_dev_found.pri_dev_type,
2797                                   sizeof(peer_info.pri_dev_type));
2798                 if (data->p2p_dev_found.dev_name)
2799                         os_strlcpy(peer_info.device_name,
2800                                    data->p2p_dev_found.dev_name,
2801                                    sizeof(peer_info.device_name));
2802                 peer_info.config_methods = data->p2p_dev_found.config_methods;
2803                 peer_info.dev_capab = data->p2p_dev_found.dev_capab;
2804                 peer_info.group_capab = data->p2p_dev_found.group_capab;
2805
2806                 /*
2807                  * FIX: new_device=1 is not necessarily correct. We should
2808                  * maintain a P2P peer database in wpa_supplicant and update
2809                  * this information based on whether the peer is truly new.
2810                  */
2811                 wpas_dev_found(wpa_s, data->p2p_dev_found.addr, &peer_info, 1);
2812                 break;
2813                 }
2814         case EVENT_P2P_GO_NEG_REQ_RX:
2815                 wpas_go_neg_req_rx(wpa_s, data->p2p_go_neg_req_rx.src,
2816                                    data->p2p_go_neg_req_rx.dev_passwd_id);
2817                 break;
2818         case EVENT_P2P_GO_NEG_COMPLETED:
2819                 wpas_go_neg_completed(wpa_s, data->p2p_go_neg_completed.res);
2820                 break;
2821         case EVENT_P2P_PROV_DISC_REQUEST:
2822                 wpas_prov_disc_req(wpa_s, data->p2p_prov_disc_req.peer,
2823                                    data->p2p_prov_disc_req.config_methods,
2824                                    data->p2p_prov_disc_req.dev_addr,
2825                                    data->p2p_prov_disc_req.pri_dev_type,
2826                                    data->p2p_prov_disc_req.dev_name,
2827                                    data->p2p_prov_disc_req.supp_config_methods,
2828                                    data->p2p_prov_disc_req.dev_capab,
2829                                    data->p2p_prov_disc_req.group_capab,
2830                                    NULL, 0);
2831                 break;
2832         case EVENT_P2P_PROV_DISC_RESPONSE:
2833                 wpas_prov_disc_resp(wpa_s, data->p2p_prov_disc_resp.peer,
2834                                     data->p2p_prov_disc_resp.config_methods);
2835                 break;
2836         case EVENT_P2P_SD_REQUEST:
2837                 wpas_sd_request(wpa_s, data->p2p_sd_req.freq,
2838                                 data->p2p_sd_req.sa,
2839                                 data->p2p_sd_req.dialog_token,
2840                                 data->p2p_sd_req.update_indic,
2841                                 data->p2p_sd_req.tlvs,
2842                                 data->p2p_sd_req.tlvs_len);
2843                 break;
2844         case EVENT_P2P_SD_RESPONSE:
2845                 wpas_sd_response(wpa_s, data->p2p_sd_resp.sa,
2846                                  data->p2p_sd_resp.update_indic,
2847                                  data->p2p_sd_resp.tlvs,
2848                                  data->p2p_sd_resp.tlvs_len);
2849                 break;
2850 #endif /* CONFIG_P2P */
2851         case EVENT_EAPOL_RX:
2852                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
2853                                         data->eapol_rx.data,
2854                                         data->eapol_rx.data_len);
2855                 break;
2856         case EVENT_SIGNAL_CHANGE:
2857                 bgscan_notify_signal_change(
2858                         wpa_s, data->signal_change.above_threshold,
2859                         data->signal_change.current_signal,
2860                         data->signal_change.current_noise,
2861                         data->signal_change.current_txrate);
2862                 break;
2863         case EVENT_INTERFACE_ENABLED:
2864                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
2865                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2866                         wpa_supplicant_update_mac_addr(wpa_s);
2867 #ifdef CONFIG_AP
2868                         if (!wpa_s->ap_iface) {
2869                                 wpa_supplicant_set_state(wpa_s,
2870                                                          WPA_DISCONNECTED);
2871                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
2872                         } else
2873                                 wpa_supplicant_set_state(wpa_s,
2874                                                          WPA_COMPLETED);
2875 #else /* CONFIG_AP */
2876                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2877                         wpa_supplicant_req_scan(wpa_s, 0, 0);
2878 #endif /* CONFIG_AP */
2879                 }
2880                 break;
2881         case EVENT_INTERFACE_DISABLED:
2882                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
2883                 wpa_supplicant_mark_disassoc(wpa_s);
2884                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2885                 break;
2886         case EVENT_CHANNEL_LIST_CHANGED:
2887                 if (wpa_s->drv_priv == NULL)
2888                         break; /* Ignore event during drv initialization */
2889
2890                 free_hw_features(wpa_s);
2891                 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2892                         wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2893
2894 #ifdef CONFIG_P2P
2895                 wpas_p2p_update_channel_list(wpa_s);
2896 #endif /* CONFIG_P2P */
2897                 break;
2898         case EVENT_INTERFACE_UNAVAILABLE:
2899 #ifdef CONFIG_P2P
2900                 wpas_p2p_interface_unavailable(wpa_s);
2901 #endif /* CONFIG_P2P */
2902                 break;
2903         case EVENT_BEST_CHANNEL:
2904                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
2905                         "(%d %d %d)",
2906                         data->best_chan.freq_24, data->best_chan.freq_5,
2907                         data->best_chan.freq_overall);
2908                 wpa_s->best_24_freq = data->best_chan.freq_24;
2909                 wpa_s->best_5_freq = data->best_chan.freq_5;
2910                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
2911 #ifdef CONFIG_P2P
2912                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
2913                                               data->best_chan.freq_5,
2914                                               data->best_chan.freq_overall);
2915 #endif /* CONFIG_P2P */
2916                 break;
2917         case EVENT_UNPROT_DEAUTH:
2918                 wpa_supplicant_event_unprot_deauth(wpa_s,
2919                                                    &data->unprot_deauth);
2920                 break;
2921         case EVENT_UNPROT_DISASSOC:
2922                 wpa_supplicant_event_unprot_disassoc(wpa_s,
2923                                                      &data->unprot_disassoc);
2924                 break;
2925         case EVENT_STATION_LOW_ACK:
2926 #ifdef CONFIG_AP
2927                 if (wpa_s->ap_iface && data)
2928                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
2929                                                   data->low_ack.addr);
2930 #endif /* CONFIG_AP */
2931 #ifdef CONFIG_TDLS
2932                 if (data)
2933                         wpa_tdls_disable_link(wpa_s->wpa, data->low_ack.addr);
2934 #endif /* CONFIG_TDLS */
2935                 break;
2936         case EVENT_IBSS_PEER_LOST:
2937 #ifdef CONFIG_IBSS_RSN
2938                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
2939 #endif /* CONFIG_IBSS_RSN */
2940                 break;
2941         case EVENT_DRIVER_GTK_REKEY:
2942                 if (os_memcmp(data->driver_gtk_rekey.bssid,
2943                               wpa_s->bssid, ETH_ALEN))
2944                         break;
2945                 if (!wpa_s->wpa)
2946                         break;
2947                 wpa_sm_update_replay_ctr(wpa_s->wpa,
2948                                          data->driver_gtk_rekey.replay_ctr);
2949                 break;
2950         case EVENT_SCHED_SCAN_STOPPED:
2951                 wpa_s->sched_scanning = 0;
2952                 wpa_supplicant_notify_scanning(wpa_s, 0);
2953
2954                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
2955                         break;
2956
2957                 /*
2958                  * If we timed out, start a new sched scan to continue
2959                  * searching for more SSIDs.
2960                  */
2961                 if (wpa_s->sched_scan_timed_out)
2962                         wpa_supplicant_req_sched_scan(wpa_s);
2963                 break;
2964         case EVENT_WPS_BUTTON_PUSHED:
2965 #ifdef CONFIG_WPS
2966                 wpas_wps_start_pbc(wpa_s, NULL, 0);
2967 #endif /* CONFIG_WPS */
2968                 break;
2969         default:
2970                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
2971                 break;
2972         }
2973 }