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