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