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