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