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