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