mesh: Add mesh peering manager
[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                                 return ssid;
1107                 }
1108         }
1109         return NULL;
1110 }
1111
1112
1113 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
1114  * on BSS added and BSS changed events */
1115 static void wpa_supplicant_rsn_preauth_scan_results(
1116         struct wpa_supplicant *wpa_s)
1117 {
1118         struct wpa_bss *bss;
1119
1120         if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
1121                 return;
1122
1123         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1124                 const u8 *ssid, *rsn;
1125
1126                 ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
1127                 if (ssid == NULL)
1128                         continue;
1129
1130                 rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1131                 if (rsn == NULL)
1132                         continue;
1133
1134                 rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
1135         }
1136
1137 }
1138
1139
1140 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
1141                                        struct wpa_bss *selected,
1142                                        struct wpa_ssid *ssid)
1143 {
1144         struct wpa_bss *current_bss = NULL;
1145         int min_diff;
1146
1147         if (wpa_s->reassociate)
1148                 return 1; /* explicit request to reassociate */
1149         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1150                 return 1; /* we are not associated; continue */
1151         if (wpa_s->current_ssid == NULL)
1152                 return 1; /* unknown current SSID */
1153         if (wpa_s->current_ssid != ssid)
1154                 return 1; /* different network block */
1155
1156         if (wpas_driver_bss_selection(wpa_s))
1157                 return 0; /* Driver-based roaming */
1158
1159         if (wpa_s->current_ssid->ssid)
1160                 current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
1161                                           wpa_s->current_ssid->ssid,
1162                                           wpa_s->current_ssid->ssid_len);
1163         if (!current_bss)
1164                 current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
1165
1166         if (!current_bss)
1167                 return 1; /* current BSS not seen in scan results */
1168
1169         if (current_bss == selected)
1170                 return 0;
1171
1172         if (selected->last_update_idx > current_bss->last_update_idx)
1173                 return 1; /* current BSS not seen in the last scan */
1174
1175 #ifndef CONFIG_NO_ROAMING
1176         wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
1177         wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
1178                 MAC2STR(current_bss->bssid), current_bss->level);
1179         wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR " level=%d",
1180                 MAC2STR(selected->bssid), selected->level);
1181
1182         if (wpa_s->current_ssid->bssid_set &&
1183             os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
1184             0) {
1185                 wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
1186                         "has preferred BSSID");
1187                 return 1;
1188         }
1189
1190         if (current_bss->level < 0 && current_bss->level > selected->level) {
1191                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
1192                         "signal level");
1193                 return 0;
1194         }
1195
1196         min_diff = 2;
1197         if (current_bss->level < 0) {
1198                 if (current_bss->level < -85)
1199                         min_diff = 1;
1200                 else if (current_bss->level < -80)
1201                         min_diff = 2;
1202                 else if (current_bss->level < -75)
1203                         min_diff = 3;
1204                 else if (current_bss->level < -70)
1205                         min_diff = 4;
1206                 else
1207                         min_diff = 5;
1208         }
1209         if (abs(current_bss->level - selected->level) < min_diff) {
1210                 wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - too small difference "
1211                         "in signal level");
1212                 return 0;
1213         }
1214
1215         return 1;
1216 #else /* CONFIG_NO_ROAMING */
1217         return 0;
1218 #endif /* CONFIG_NO_ROAMING */
1219 }
1220
1221
1222 /* Return != 0 if no scan results could be fetched or if scan results should not
1223  * be shared with other virtual interfaces. */
1224 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1225                                               union wpa_event_data *data,
1226                                               int own_request)
1227 {
1228         struct wpa_scan_results *scan_res = NULL;
1229         int ret = 0;
1230         int ap = 0;
1231 #ifndef CONFIG_NO_RANDOM_POOL
1232         size_t i, num;
1233 #endif /* CONFIG_NO_RANDOM_POOL */
1234
1235 #ifdef CONFIG_AP
1236         if (wpa_s->ap_iface)
1237                 ap = 1;
1238 #endif /* CONFIG_AP */
1239
1240         wpa_supplicant_notify_scanning(wpa_s, 0);
1241
1242         scan_res = wpa_supplicant_get_scan_results(wpa_s,
1243                                                    data ? &data->scan_info :
1244                                                    NULL, 1);
1245         if (scan_res == NULL) {
1246                 if (wpa_s->conf->ap_scan == 2 || ap ||
1247                     wpa_s->scan_res_handler == scan_only_handler)
1248                         return -1;
1249                 if (!own_request)
1250                         return -1;
1251                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
1252                         "scanning again");
1253                 wpa_supplicant_req_new_scan(wpa_s, 1, 0);
1254                 ret = -1;
1255                 goto scan_work_done;
1256         }
1257
1258 #ifndef CONFIG_NO_RANDOM_POOL
1259         num = scan_res->num;
1260         if (num > 10)
1261                 num = 10;
1262         for (i = 0; i < num; i++) {
1263                 u8 buf[5];
1264                 struct wpa_scan_res *res = scan_res->res[i];
1265                 buf[0] = res->bssid[5];
1266                 buf[1] = res->qual & 0xff;
1267                 buf[2] = res->noise & 0xff;
1268                 buf[3] = res->level & 0xff;
1269                 buf[4] = res->tsf & 0xff;
1270                 random_add_randomness(buf, sizeof(buf));
1271         }
1272 #endif /* CONFIG_NO_RANDOM_POOL */
1273
1274         if (own_request && wpa_s->scan_res_handler &&
1275             (wpa_s->own_scan_running || !wpa_s->external_scan_running)) {
1276                 void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
1277                                          struct wpa_scan_results *scan_res);
1278
1279                 scan_res_handler = wpa_s->scan_res_handler;
1280                 wpa_s->scan_res_handler = NULL;
1281                 scan_res_handler(wpa_s, scan_res);
1282                 ret = -2;
1283                 goto scan_work_done;
1284         }
1285
1286         if (ap) {
1287                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
1288 #ifdef CONFIG_AP
1289                 if (wpa_s->ap_iface->scan_cb)
1290                         wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
1291 #endif /* CONFIG_AP */
1292                 goto scan_work_done;
1293         }
1294
1295         wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
1296                 wpa_s->own_scan_running, wpa_s->external_scan_running);
1297         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1298             wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1299                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1300                              wpa_s->manual_scan_id);
1301                 wpa_s->manual_scan_use_id = 0;
1302         } else {
1303                 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1304         }
1305         wpas_notify_scan_results(wpa_s);
1306
1307         wpas_notify_scan_done(wpa_s, 1);
1308
1309         if (!wpa_s->own_scan_running && wpa_s->external_scan_running) {
1310                 wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
1311                 wpa_scan_results_free(scan_res);
1312                 return 0;
1313         }
1314
1315         if (sme_proc_obss_scan(wpa_s) > 0)
1316                 goto scan_work_done;
1317
1318         if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
1319                 goto scan_work_done;
1320
1321         if (autoscan_notify_scan(wpa_s, scan_res))
1322                 goto scan_work_done;
1323
1324         if (wpa_s->disconnected) {
1325                 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1326                 goto scan_work_done;
1327         }
1328
1329         if (!wpas_driver_bss_selection(wpa_s) &&
1330             bgscan_notify_scan(wpa_s, scan_res) == 1)
1331                 goto scan_work_done;
1332
1333         wpas_wps_update_ap_info(wpa_s, scan_res);
1334
1335         wpa_scan_results_free(scan_res);
1336
1337         if (wpa_s->scan_work) {
1338                 struct wpa_radio_work *work = wpa_s->scan_work;
1339                 wpa_s->scan_work = NULL;
1340                 radio_work_done(work);
1341         }
1342
1343         return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
1344
1345 scan_work_done:
1346         wpa_scan_results_free(scan_res);
1347         if (wpa_s->scan_work) {
1348                 struct wpa_radio_work *work = wpa_s->scan_work;
1349                 wpa_s->scan_work = NULL;
1350                 radio_work_done(work);
1351         }
1352         return ret;
1353 }
1354
1355
1356 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
1357                                               int new_scan, int own_request)
1358 {
1359         struct wpa_bss *selected;
1360         struct wpa_ssid *ssid = NULL;
1361
1362         selected = wpa_supplicant_pick_network(wpa_s, &ssid);
1363
1364         if (selected) {
1365                 int skip;
1366                 skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
1367                 if (skip) {
1368                         if (new_scan)
1369                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1370                         return 0;
1371                 }
1372
1373                 if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
1374                         wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
1375                         return -1;
1376                 }
1377                 if (new_scan)
1378                         wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1379                 /*
1380                  * Do not notify other virtual radios of scan results since we do not
1381                  * want them to start other associations at the same time.
1382                  */
1383                 return 1;
1384         } else {
1385                 wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
1386                 ssid = wpa_supplicant_pick_new_network(wpa_s);
1387                 if (ssid) {
1388                         wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
1389                         wpa_supplicant_associate(wpa_s, NULL, ssid);
1390                         if (new_scan)
1391                                 wpa_supplicant_rsn_preauth_scan_results(wpa_s);
1392                 } else if (own_request) {
1393                         /*
1394                          * No SSID found. If SCAN results are as a result of
1395                          * own scan request and not due to a scan request on
1396                          * another shared interface, try another scan.
1397                          */
1398                         int timeout_sec = wpa_s->scan_interval;
1399                         int timeout_usec = 0;
1400 #ifdef CONFIG_P2P
1401                         if (wpas_p2p_scan_no_go_seen(wpa_s) == 1)
1402                                 return 0;
1403
1404                         if (wpa_s->p2p_in_provisioning ||
1405                             wpa_s->show_group_started ||
1406                             wpa_s->p2p_in_invitation) {
1407                                 /*
1408                                  * Use shorter wait during P2P Provisioning
1409                                  * state and during P2P join-a-group operation
1410                                  * to speed up group formation.
1411                                  */
1412                                 timeout_sec = 0;
1413                                 timeout_usec = 250000;
1414                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1415                                                             timeout_usec);
1416                                 return 0;
1417                         }
1418 #endif /* CONFIG_P2P */
1419 #ifdef CONFIG_INTERWORKING
1420                         if (wpa_s->conf->auto_interworking &&
1421                             wpa_s->conf->interworking &&
1422                             wpa_s->conf->cred) {
1423                                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
1424                                         "start ANQP fetch since no matching "
1425                                         "networks found");
1426                                 wpa_s->network_select = 1;
1427                                 wpa_s->auto_network_select = 1;
1428                                 interworking_start_fetch_anqp(wpa_s);
1429                                 return 1;
1430                         }
1431 #endif /* CONFIG_INTERWORKING */
1432 #ifdef CONFIG_WPS
1433                         if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
1434                                 wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
1435                                 timeout_sec = 0;
1436                                 timeout_usec = 500000;
1437                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1438                                                             timeout_usec);
1439                                 return 0;
1440                         }
1441 #endif /* CONFIG_WPS */
1442                         if (wpa_supplicant_req_sched_scan(wpa_s))
1443                                 wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
1444                                                             timeout_usec);
1445                 }
1446         }
1447         return 0;
1448 }
1449
1450
1451 static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
1452                                               union wpa_event_data *data)
1453 {
1454         struct wpa_supplicant *ifs;
1455
1456         if (_wpa_supplicant_event_scan_results(wpa_s, data, 1) != 0) {
1457                 /*
1458                  * If no scan results could be fetched, then no need to
1459                  * notify those interfaces that did not actually request
1460                  * this scan. Similarly, if scan results started a new operation on this
1461                  * interface, do not notify other interfaces to avoid concurrent
1462                  * operations during a connection attempt.
1463                  */
1464                 return;
1465         }
1466
1467         /*
1468          * Check other interfaces to see if they share the same radio. If
1469          * so, they get updated with this same scan info.
1470          */
1471         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
1472                          radio_list) {
1473                 if (ifs != wpa_s) {
1474                         wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
1475                                    "sibling", ifs->ifname);
1476                         _wpa_supplicant_event_scan_results(ifs, data, 0);
1477                 }
1478         }
1479 }
1480
1481 #endif /* CONFIG_NO_SCAN_PROCESSING */
1482
1483
1484 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
1485 {
1486 #ifdef CONFIG_NO_SCAN_PROCESSING
1487         return -1;
1488 #else /* CONFIG_NO_SCAN_PROCESSING */
1489         struct os_reltime now;
1490
1491         if (wpa_s->last_scan_res_used <= 0)
1492                 return -1;
1493
1494         os_get_reltime(&now);
1495         if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
1496                 wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
1497                 return -1;
1498         }
1499
1500         return wpas_select_network_from_last_scan(wpa_s, 0, 1);
1501 #endif /* CONFIG_NO_SCAN_PROCESSING */
1502 }
1503
1504 #ifdef CONFIG_WNM
1505
1506 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
1507 {
1508         struct wpa_supplicant *wpa_s = eloop_ctx;
1509
1510         if (wpa_s->wpa_state < WPA_ASSOCIATED)
1511                 return;
1512
1513         if (!wpa_s->no_keep_alive) {
1514                 wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
1515                            MAC2STR(wpa_s->bssid));
1516                 /* TODO: could skip this if normal data traffic has been sent */
1517                 /* TODO: Consider using some more appropriate data frame for
1518                  * this */
1519                 if (wpa_s->l2)
1520                         l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
1521                                        (u8 *) "", 0);
1522         }
1523
1524 #ifdef CONFIG_SME
1525         if (wpa_s->sme.bss_max_idle_period) {
1526                 unsigned int msec;
1527                 msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
1528                 if (msec > 100)
1529                         msec -= 100;
1530                 eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1531                                        wnm_bss_keep_alive, wpa_s, NULL);
1532         }
1533 #endif /* CONFIG_SME */
1534 }
1535
1536
1537 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
1538                                    const u8 *ies, size_t ies_len)
1539 {
1540         struct ieee802_11_elems elems;
1541
1542         if (ies == NULL)
1543                 return;
1544
1545         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1546                 return;
1547
1548 #ifdef CONFIG_SME
1549         if (elems.bss_max_idle_period) {
1550                 unsigned int msec;
1551                 wpa_s->sme.bss_max_idle_period =
1552                         WPA_GET_LE16(elems.bss_max_idle_period);
1553                 wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
1554                            "TU)%s", wpa_s->sme.bss_max_idle_period,
1555                            (elems.bss_max_idle_period[2] & 0x01) ?
1556                            " (protected keep-live required)" : "");
1557                 if (wpa_s->sme.bss_max_idle_period == 0)
1558                         wpa_s->sme.bss_max_idle_period = 1;
1559                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
1560                         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1561                          /* msec times 1000 */
1562                         msec = wpa_s->sme.bss_max_idle_period * 1024;
1563                         if (msec > 100)
1564                                 msec -= 100;
1565                         eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
1566                                                wnm_bss_keep_alive, wpa_s,
1567                                                NULL);
1568                 }
1569         }
1570 #endif /* CONFIG_SME */
1571 }
1572
1573 #endif /* CONFIG_WNM */
1574
1575
1576 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
1577 {
1578 #ifdef CONFIG_WNM
1579         eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
1580 #endif /* CONFIG_WNM */
1581 }
1582
1583
1584 #ifdef CONFIG_INTERWORKING
1585
1586 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
1587                             size_t len)
1588 {
1589         int res;
1590
1591         wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
1592         res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
1593         if (res) {
1594                 wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
1595         }
1596
1597         return res;
1598 }
1599
1600
1601 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
1602                                             const u8 *ies, size_t ies_len)
1603 {
1604         struct ieee802_11_elems elems;
1605
1606         if (ies == NULL)
1607                 return;
1608
1609         if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
1610                 return;
1611
1612         if (elems.qos_map_set) {
1613                 wpas_qos_map_set(wpa_s, elems.qos_map_set,
1614                                  elems.qos_map_set_len);
1615         }
1616 }
1617
1618 #endif /* CONFIG_INTERWORKING */
1619
1620
1621 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
1622                                           union wpa_event_data *data)
1623 {
1624         int l, len, found = 0, wpa_found, rsn_found;
1625         const u8 *p;
1626 #ifdef CONFIG_IEEE80211R
1627         u8 bssid[ETH_ALEN];
1628 #endif /* CONFIG_IEEE80211R */
1629
1630         wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
1631         if (data->assoc_info.req_ies)
1632                 wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
1633                             data->assoc_info.req_ies_len);
1634         if (data->assoc_info.resp_ies) {
1635                 wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
1636                             data->assoc_info.resp_ies_len);
1637 #ifdef CONFIG_TDLS
1638                 wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
1639                                         data->assoc_info.resp_ies_len);
1640 #endif /* CONFIG_TDLS */
1641 #ifdef CONFIG_WNM
1642                 wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1643                                        data->assoc_info.resp_ies_len);
1644 #endif /* CONFIG_WNM */
1645 #ifdef CONFIG_INTERWORKING
1646                 interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
1647                                                 data->assoc_info.resp_ies_len);
1648 #endif /* CONFIG_INTERWORKING */
1649         }
1650         if (data->assoc_info.beacon_ies)
1651                 wpa_hexdump(MSG_DEBUG, "beacon_ies",
1652                             data->assoc_info.beacon_ies,
1653                             data->assoc_info.beacon_ies_len);
1654         if (data->assoc_info.freq)
1655                 wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
1656                         data->assoc_info.freq);
1657
1658         p = data->assoc_info.req_ies;
1659         l = data->assoc_info.req_ies_len;
1660
1661         /* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
1662         while (p && l >= 2) {
1663                 len = p[1] + 2;
1664                 if (len > l) {
1665                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1666                                     p, l);
1667                         break;
1668                 }
1669                 if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1670                      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
1671                     (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
1672                         if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
1673                                 break;
1674                         found = 1;
1675                         wpa_find_assoc_pmkid(wpa_s);
1676                         break;
1677                 }
1678                 l -= len;
1679                 p += len;
1680         }
1681         if (!found && data->assoc_info.req_ies)
1682                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1683
1684 #ifdef CONFIG_IEEE80211R
1685 #ifdef CONFIG_SME
1686         if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
1687                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1688                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1689                                                  data->assoc_info.resp_ies,
1690                                                  data->assoc_info.resp_ies_len,
1691                                                  bssid) < 0) {
1692                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1693                                 "Reassociation Response failed");
1694                         wpa_supplicant_deauthenticate(
1695                                 wpa_s, WLAN_REASON_INVALID_IE);
1696                         return -1;
1697                 }
1698         }
1699
1700         p = data->assoc_info.resp_ies;
1701         l = data->assoc_info.resp_ies_len;
1702
1703 #ifdef CONFIG_WPS_STRICT
1704         if (p && wpa_s->current_ssid &&
1705             wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1706                 struct wpabuf *wps;
1707                 wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
1708                 if (wps == NULL) {
1709                         wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
1710                                 "include WPS IE in (Re)Association Response");
1711                         return -1;
1712                 }
1713
1714                 if (wps_validate_assoc_resp(wps) < 0) {
1715                         wpabuf_free(wps);
1716                         wpa_supplicant_deauthenticate(
1717                                 wpa_s, WLAN_REASON_INVALID_IE);
1718                         return -1;
1719                 }
1720                 wpabuf_free(wps);
1721         }
1722 #endif /* CONFIG_WPS_STRICT */
1723
1724         /* Go through the IEs and make a copy of the MDIE, if present. */
1725         while (p && l >= 2) {
1726                 len = p[1] + 2;
1727                 if (len > l) {
1728                         wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
1729                                     p, l);
1730                         break;
1731                 }
1732                 if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
1733                     p[1] >= MOBILITY_DOMAIN_ID_LEN) {
1734                         wpa_s->sme.ft_used = 1;
1735                         os_memcpy(wpa_s->sme.mobility_domain, p + 2,
1736                                   MOBILITY_DOMAIN_ID_LEN);
1737                         break;
1738                 }
1739                 l -= len;
1740                 p += len;
1741         }
1742 #endif /* CONFIG_SME */
1743
1744         /* Process FT when SME is in the driver */
1745         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1746             wpa_ft_is_completed(wpa_s->wpa)) {
1747                 if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
1748                     wpa_ft_validate_reassoc_resp(wpa_s->wpa,
1749                                                  data->assoc_info.resp_ies,
1750                                                  data->assoc_info.resp_ies_len,
1751                                                  bssid) < 0) {
1752                         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
1753                                 "Reassociation Response failed");
1754                         wpa_supplicant_deauthenticate(
1755                                 wpa_s, WLAN_REASON_INVALID_IE);
1756                         return -1;
1757                 }
1758                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
1759         }
1760
1761         wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
1762                              data->assoc_info.resp_ies_len);
1763 #endif /* CONFIG_IEEE80211R */
1764
1765         /* WPA/RSN IE from Beacon/ProbeResp */
1766         p = data->assoc_info.beacon_ies;
1767         l = data->assoc_info.beacon_ies_len;
1768
1769         /* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
1770          */
1771         wpa_found = rsn_found = 0;
1772         while (p && l >= 2) {
1773                 len = p[1] + 2;
1774                 if (len > l) {
1775                         wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
1776                                     p, l);
1777                         break;
1778                 }
1779                 if (!wpa_found &&
1780                     p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
1781                     os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
1782                         wpa_found = 1;
1783                         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
1784                 }
1785
1786                 if (!rsn_found &&
1787                     p[0] == WLAN_EID_RSN && p[1] >= 2) {
1788                         rsn_found = 1;
1789                         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
1790                 }
1791
1792                 l -= len;
1793                 p += len;
1794         }
1795
1796         if (!wpa_found && data->assoc_info.beacon_ies)
1797                 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
1798         if (!rsn_found && data->assoc_info.beacon_ies)
1799                 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
1800         if (wpa_found || rsn_found)
1801                 wpa_s->ap_ies_from_associnfo = 1;
1802
1803         if (wpa_s->assoc_freq && data->assoc_info.freq &&
1804             wpa_s->assoc_freq != data->assoc_info.freq) {
1805                 wpa_printf(MSG_DEBUG, "Operating frequency changed from "
1806                            "%u to %u MHz",
1807                            wpa_s->assoc_freq, data->assoc_info.freq);
1808                 wpa_supplicant_update_scan_results(wpa_s);
1809         }
1810
1811         wpa_s->assoc_freq = data->assoc_info.freq;
1812
1813         return 0;
1814 }
1815
1816
1817 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
1818 {
1819         const u8 *bss_wpa = NULL, *bss_rsn = NULL;
1820
1821         if (!wpa_s->current_bss || !wpa_s->current_ssid)
1822                 return -1;
1823
1824         if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
1825                 return 0;
1826
1827         bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
1828                                         WPA_IE_VENDOR_TYPE);
1829         bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
1830
1831         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1832                                  bss_wpa ? 2 + bss_wpa[1] : 0) ||
1833             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1834                                  bss_rsn ? 2 + bss_rsn[1] : 0))
1835                 return -1;
1836
1837         return 0;
1838 }
1839
1840
1841 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
1842                                        union wpa_event_data *data)
1843 {
1844         u8 bssid[ETH_ALEN];
1845         int ft_completed;
1846
1847 #ifdef CONFIG_AP
1848         if (wpa_s->ap_iface) {
1849                 if (!data)
1850                         return;
1851                 hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
1852                                     data->assoc_info.addr,
1853                                     data->assoc_info.req_ies,
1854                                     data->assoc_info.req_ies_len,
1855                                     data->assoc_info.reassoc);
1856                 return;
1857         }
1858 #endif /* CONFIG_AP */
1859
1860         ft_completed = wpa_ft_is_completed(wpa_s->wpa);
1861         if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
1862                 return;
1863
1864         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1865                 wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
1866                 wpa_supplicant_deauthenticate(
1867                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1868                 return;
1869         }
1870
1871         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
1872         if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
1873                 wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
1874                         MACSTR, MAC2STR(bssid));
1875                 random_add_randomness(bssid, ETH_ALEN);
1876                 os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
1877                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1878                 wpas_notify_bssid_changed(wpa_s);
1879
1880                 if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
1881                         wpa_clear_keys(wpa_s, bssid);
1882                 }
1883                 if (wpa_supplicant_select_config(wpa_s) < 0) {
1884                         wpa_supplicant_deauthenticate(
1885                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1886                         return;
1887                 }
1888
1889                 if (wpa_s->conf->ap_scan == 1 &&
1890                     wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
1891                         if (wpa_supplicant_assoc_update_ie(wpa_s) < 0)
1892                                 wpa_msg(wpa_s, MSG_WARNING,
1893                                         "WPA/RSN IEs not updated");
1894                 }
1895         }
1896
1897 #ifdef CONFIG_SME
1898         os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
1899         wpa_s->sme.prev_bssid_set = 1;
1900         wpa_s->sme.last_unprot_disconnect.sec = 0;
1901 #endif /* CONFIG_SME */
1902
1903         wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
1904         if (wpa_s->current_ssid) {
1905                 /* When using scanning (ap_scan=1), SIM PC/SC interface can be
1906                  * initialized before association, but for other modes,
1907                  * initialize PC/SC here, if the current configuration needs
1908                  * smartcard or SIM/USIM. */
1909                 wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
1910         }
1911         wpa_sm_notify_assoc(wpa_s->wpa, bssid);
1912         if (wpa_s->l2)
1913                 l2_packet_notify_auth_start(wpa_s->l2);
1914
1915         /*
1916          * Set portEnabled first to FALSE in order to get EAP state machine out
1917          * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
1918          * state machine may transit to AUTHENTICATING state based on obsolete
1919          * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
1920          * AUTHENTICATED without ever giving chance to EAP state machine to
1921          * reset the state.
1922          */
1923         if (!ft_completed) {
1924                 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1925                 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1926         }
1927         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) || ft_completed)
1928                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1929         /* 802.1X::portControl = Auto */
1930         eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
1931         wpa_s->eapol_received = 0;
1932         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1933             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
1934             (wpa_s->current_ssid &&
1935              wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
1936                 if (wpa_s->current_ssid &&
1937                     wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
1938                     (wpa_s->drv_flags &
1939                      WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
1940                         /*
1941                          * Set the key after having received joined-IBSS event
1942                          * from the driver.
1943                          */
1944                         wpa_supplicant_set_wpa_none_key(wpa_s,
1945                                                         wpa_s->current_ssid);
1946                 }
1947                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1948                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1949         } else if (!ft_completed) {
1950                 /* Timeout for receiving the first EAPOL packet */
1951                 wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
1952         }
1953         wpa_supplicant_cancel_scan(wpa_s);
1954
1955         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1956             wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1957                 /*
1958                  * We are done; the driver will take care of RSN 4-way
1959                  * handshake.
1960                  */
1961                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1962                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1963                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1964                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1965         } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1966                    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1967                 /*
1968                  * The driver will take care of RSN 4-way handshake, so we need
1969                  * to allow EAPOL supplicant to complete its work without
1970                  * waiting for WPA supplicant.
1971                  */
1972                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1973         } else if (ft_completed) {
1974                 /*
1975                  * FT protocol completed - make sure EAPOL state machine ends
1976                  * up in authenticated.
1977                  */
1978                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1979                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1980                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1981                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
1982         }
1983
1984         wpa_s->last_eapol_matches_bssid = 0;
1985
1986         if (wpa_s->pending_eapol_rx) {
1987                 struct os_reltime now, age;
1988                 os_get_reltime(&now);
1989                 os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
1990                 if (age.sec == 0 && age.usec < 100000 &&
1991                     os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
1992                     0) {
1993                         wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
1994                                 "frame that was received just before "
1995                                 "association notification");
1996                         wpa_supplicant_rx_eapol(
1997                                 wpa_s, wpa_s->pending_eapol_rx_src,
1998                                 wpabuf_head(wpa_s->pending_eapol_rx),
1999                                 wpabuf_len(wpa_s->pending_eapol_rx));
2000                 }
2001                 wpabuf_free(wpa_s->pending_eapol_rx);
2002                 wpa_s->pending_eapol_rx = NULL;
2003         }
2004
2005         if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2006              wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
2007             wpa_s->current_ssid &&
2008             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
2009                 /* Set static WEP keys again */
2010                 wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
2011         }
2012
2013 #ifdef CONFIG_IBSS_RSN
2014         if (wpa_s->current_ssid &&
2015             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
2016             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2017             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
2018             wpa_s->ibss_rsn == NULL) {
2019                 wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
2020                 if (!wpa_s->ibss_rsn) {
2021                         wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
2022                         wpa_supplicant_deauthenticate(
2023                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2024                         return;
2025                 }
2026
2027                 ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
2028         }
2029 #endif /* CONFIG_IBSS_RSN */
2030
2031         wpas_wps_notify_assoc(wpa_s, bssid);
2032 }
2033
2034
2035 static int disconnect_reason_recoverable(u16 reason_code)
2036 {
2037         return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
2038                 reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
2039                 reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
2040 }
2041
2042
2043 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
2044                                           u16 reason_code,
2045                                           int locally_generated)
2046 {
2047         const u8 *bssid;
2048
2049         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2050                 /*
2051                  * At least Host AP driver and a Prism3 card seemed to be
2052                  * generating streams of disconnected events when configuring
2053                  * IBSS for WPA-None. Ignore them for now.
2054                  */
2055                 return;
2056         }
2057
2058         bssid = wpa_s->bssid;
2059         if (is_zero_ether_addr(bssid))
2060                 bssid = wpa_s->pending_bssid;
2061
2062         if (!is_zero_ether_addr(bssid) ||
2063             wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2064                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
2065                         " reason=%d%s",
2066                         MAC2STR(bssid), reason_code,
2067                         locally_generated ? " locally_generated=1" : "");
2068         }
2069 }
2070
2071
2072 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
2073                                  int locally_generated)
2074 {
2075         if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
2076             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
2077                 return 0; /* Not in 4-way handshake with PSK */
2078
2079         /*
2080          * It looks like connection was lost while trying to go through PSK
2081          * 4-way handshake. Filter out known disconnection cases that are caused
2082          * by something else than PSK mismatch to avoid confusing reports.
2083          */
2084
2085         if (locally_generated) {
2086                 if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
2087                         return 0;
2088         }
2089
2090         return 1;
2091 }
2092
2093
2094 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
2095                                                  u16 reason_code,
2096                                                  int locally_generated)
2097 {
2098         const u8 *bssid;
2099         int authenticating;
2100         u8 prev_pending_bssid[ETH_ALEN];
2101         struct wpa_bss *fast_reconnect = NULL;
2102         struct wpa_ssid *fast_reconnect_ssid = NULL;
2103         struct wpa_ssid *last_ssid;
2104
2105         authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
2106         os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
2107
2108         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2109                 /*
2110                  * At least Host AP driver and a Prism3 card seemed to be
2111                  * generating streams of disconnected events when configuring
2112                  * IBSS for WPA-None. Ignore them for now.
2113                  */
2114                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
2115                         "IBSS/WPA-None mode");
2116                 return;
2117         }
2118
2119         if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
2120                 wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
2121                         "pre-shared key may be incorrect");
2122                 if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
2123                         return; /* P2P group removed */
2124                 wpas_auth_failed(wpa_s, "WRONG_KEY");
2125         }
2126         if (!wpa_s->disconnected &&
2127             (!wpa_s->auto_reconnect_disabled ||
2128              wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
2129              wpas_wps_searching(wpa_s))) {
2130                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
2131                         "reconnect (wps=%d/%d wpa_state=%d)",
2132                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
2133                         wpas_wps_searching(wpa_s),
2134                         wpa_s->wpa_state);
2135                 if (wpa_s->wpa_state == WPA_COMPLETED &&
2136                     wpa_s->current_ssid &&
2137                     wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
2138                     !locally_generated &&
2139                     disconnect_reason_recoverable(reason_code)) {
2140                         /*
2141                          * It looks like the AP has dropped association with
2142                          * us, but could allow us to get back in. Try to
2143                          * reconnect to the same BSS without full scan to save
2144                          * time for some common cases.
2145                          */
2146                         fast_reconnect = wpa_s->current_bss;
2147                         fast_reconnect_ssid = wpa_s->current_ssid;
2148                 } else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
2149                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2150                 else
2151                         wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
2152                                 "immediate scan");
2153         } else {
2154                 wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
2155                         "try to re-connect");
2156                 wpa_s->reassociate = 0;
2157                 wpa_s->disconnected = 1;
2158                 wpa_supplicant_cancel_sched_scan(wpa_s);
2159         }
2160         bssid = wpa_s->bssid;
2161         if (is_zero_ether_addr(bssid))
2162                 bssid = wpa_s->pending_bssid;
2163         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2164                 wpas_connection_failed(wpa_s, bssid);
2165         wpa_sm_notify_disassoc(wpa_s->wpa);
2166         if (locally_generated)
2167                 wpa_s->disconnect_reason = -reason_code;
2168         else
2169                 wpa_s->disconnect_reason = reason_code;
2170         wpas_notify_disconnect_reason(wpa_s);
2171         if (wpa_supplicant_dynamic_keys(wpa_s)) {
2172                 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
2173                 wpa_clear_keys(wpa_s, wpa_s->bssid);
2174         }
2175         last_ssid = wpa_s->current_ssid;
2176         wpa_supplicant_mark_disassoc(wpa_s);
2177
2178         if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
2179                 sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
2180                 wpa_s->current_ssid = last_ssid;
2181         }
2182
2183         if (fast_reconnect &&
2184             !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
2185             !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
2186             !disallowed_ssid(wpa_s, fast_reconnect->ssid,
2187                              fast_reconnect->ssid_len) &&
2188             !wpas_temp_disabled(wpa_s, fast_reconnect_ssid)) {
2189 #ifndef CONFIG_NO_SCAN_PROCESSING
2190                 wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
2191                 if (wpa_supplicant_connect(wpa_s, fast_reconnect,
2192                                            fast_reconnect_ssid) < 0) {
2193                         /* Recover through full scan */
2194                         wpa_supplicant_req_scan(wpa_s, 0, 100000);
2195                 }
2196 #endif /* CONFIG_NO_SCAN_PROCESSING */
2197         } else if (fast_reconnect) {
2198                 /*
2199                  * Could not reconnect to the same BSS due to network being
2200                  * disabled. Use a new scan to match the alternative behavior
2201                  * above, i.e., to continue automatic reconnection attempt in a
2202                  * way that enforces disabled network rules.
2203                  */
2204                 wpa_supplicant_req_scan(wpa_s, 0, 100000);
2205         }
2206 }
2207
2208
2209 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2210 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
2211 {
2212         struct wpa_supplicant *wpa_s = eloop_ctx;
2213
2214         if (!wpa_s->pending_mic_error_report)
2215                 return;
2216
2217         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
2218         wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
2219         wpa_s->pending_mic_error_report = 0;
2220 }
2221 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2222
2223
2224 static void
2225 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
2226                                          union wpa_event_data *data)
2227 {
2228         int pairwise;
2229         struct os_reltime t;
2230
2231         wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
2232         pairwise = (data && data->michael_mic_failure.unicast);
2233         os_get_reltime(&t);
2234         if ((wpa_s->last_michael_mic_error.sec &&
2235              !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
2236             wpa_s->pending_mic_error_report) {
2237                 if (wpa_s->pending_mic_error_report) {
2238                         /*
2239                          * Send the pending MIC error report immediately since
2240                          * we are going to start countermeasures and AP better
2241                          * do the same.
2242                          */
2243                         wpa_sm_key_request(wpa_s->wpa, 1,
2244                                            wpa_s->pending_mic_error_pairwise);
2245                 }
2246
2247                 /* Send the new MIC error report immediately since we are going
2248                  * to start countermeasures and AP better do the same.
2249                  */
2250                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2251
2252                 /* initialize countermeasures */
2253                 wpa_s->countermeasures = 1;
2254
2255                 wpa_blacklist_add(wpa_s, wpa_s->bssid);
2256
2257                 wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
2258
2259                 /*
2260                  * Need to wait for completion of request frame. We do not get
2261                  * any callback for the message completion, so just wait a
2262                  * short while and hope for the best. */
2263                 os_sleep(0, 10000);
2264
2265                 wpa_drv_set_countermeasures(wpa_s, 1);
2266                 wpa_supplicant_deauthenticate(wpa_s,
2267                                               WLAN_REASON_MICHAEL_MIC_FAILURE);
2268                 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
2269                                      wpa_s, NULL);
2270                 eloop_register_timeout(60, 0,
2271                                        wpa_supplicant_stop_countermeasures,
2272                                        wpa_s, NULL);
2273                 /* TODO: mark the AP rejected for 60 second. STA is
2274                  * allowed to associate with another AP.. */
2275         } else {
2276 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
2277                 if (wpa_s->mic_errors_seen) {
2278                         /*
2279                          * Reduce the effectiveness of Michael MIC error
2280                          * reports as a means for attacking against TKIP if
2281                          * more than one MIC failure is noticed with the same
2282                          * PTK. We delay the transmission of the reports by a
2283                          * random time between 0 and 60 seconds in order to
2284                          * force the attacker wait 60 seconds before getting
2285                          * the information on whether a frame resulted in a MIC
2286                          * failure.
2287                          */
2288                         u8 rval[4];
2289                         int sec;
2290
2291                         if (os_get_random(rval, sizeof(rval)) < 0)
2292                                 sec = os_random() % 60;
2293                         else
2294                                 sec = WPA_GET_BE32(rval) % 60;
2295                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
2296                                 "report %d seconds", sec);
2297                         wpa_s->pending_mic_error_report = 1;
2298                         wpa_s->pending_mic_error_pairwise = pairwise;
2299                         eloop_cancel_timeout(
2300                                 wpa_supplicant_delayed_mic_error_report,
2301                                 wpa_s, NULL);
2302                         eloop_register_timeout(
2303                                 sec, os_random() % 1000000,
2304                                 wpa_supplicant_delayed_mic_error_report,
2305                                 wpa_s, NULL);
2306                 } else {
2307                         wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2308                 }
2309 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2310                 wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
2311 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
2312         }
2313         wpa_s->last_michael_mic_error = t;
2314         wpa_s->mic_errors_seen++;
2315 }
2316
2317
2318 #ifdef CONFIG_TERMINATE_ONLASTIF
2319 static int any_interfaces(struct wpa_supplicant *head)
2320 {
2321         struct wpa_supplicant *wpa_s;
2322
2323         for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
2324                 if (!wpa_s->interface_removed)
2325                         return 1;
2326         return 0;
2327 }
2328 #endif /* CONFIG_TERMINATE_ONLASTIF */
2329
2330
2331 static void
2332 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
2333                                       union wpa_event_data *data)
2334 {
2335         if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
2336                 return;
2337
2338         switch (data->interface_status.ievent) {
2339         case EVENT_INTERFACE_ADDED:
2340                 if (!wpa_s->interface_removed)
2341                         break;
2342                 wpa_s->interface_removed = 0;
2343                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
2344                 if (wpa_supplicant_driver_init(wpa_s) < 0) {
2345                         wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
2346                                 "driver after interface was added");
2347                 }
2348                 break;
2349         case EVENT_INTERFACE_REMOVED:
2350                 wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
2351                 wpa_s->interface_removed = 1;
2352                 wpa_supplicant_mark_disassoc(wpa_s);
2353                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
2354                 l2_packet_deinit(wpa_s->l2);
2355                 wpa_s->l2 = NULL;
2356 #ifdef CONFIG_TERMINATE_ONLASTIF
2357                 /* check if last interface */
2358                 if (!any_interfaces(wpa_s->global->ifaces))
2359                         eloop_terminate();
2360 #endif /* CONFIG_TERMINATE_ONLASTIF */
2361                 break;
2362         }
2363 }
2364
2365
2366 #ifdef CONFIG_PEERKEY
2367 static void
2368 wpa_supplicant_event_stkstart(struct wpa_supplicant *wpa_s,
2369                               union wpa_event_data *data)
2370 {
2371         if (data == NULL)
2372                 return;
2373         wpa_sm_stkstart(wpa_s->wpa, data->stkstart.peer);
2374 }
2375 #endif /* CONFIG_PEERKEY */
2376
2377
2378 #ifdef CONFIG_TDLS
2379 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
2380                                       union wpa_event_data *data)
2381 {
2382         if (data == NULL)
2383                 return;
2384         switch (data->tdls.oper) {
2385         case TDLS_REQUEST_SETUP:
2386                 wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
2387                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2388                         wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
2389                 else
2390                         wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
2391                 break;
2392         case TDLS_REQUEST_TEARDOWN:
2393                 if (wpa_tdls_is_external_setup(wpa_s->wpa))
2394                         wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
2395                                                data->tdls.reason_code);
2396                 else
2397                         wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
2398                                           data->tdls.peer);
2399                 break;
2400         }
2401 }
2402 #endif /* CONFIG_TDLS */
2403
2404
2405 #ifdef CONFIG_WNM
2406 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
2407                                      union wpa_event_data *data)
2408 {
2409         if (data == NULL)
2410                 return;
2411         switch (data->wnm.oper) {
2412         case WNM_OPER_SLEEP:
2413                 wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
2414                            "(action=%d, intval=%d)",
2415                            data->wnm.sleep_action, data->wnm.sleep_intval);
2416                 ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
2417                                              data->wnm.sleep_intval, NULL);
2418                 break;
2419         }
2420 }
2421 #endif /* CONFIG_WNM */
2422
2423
2424 #ifdef CONFIG_IEEE80211R
2425 static void
2426 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
2427                                  union wpa_event_data *data)
2428 {
2429         if (data == NULL)
2430                 return;
2431
2432         if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
2433                                     data->ft_ies.ies_len,
2434                                     data->ft_ies.ft_action,
2435                                     data->ft_ies.target_ap,
2436                                     data->ft_ies.ric_ies,
2437                                     data->ft_ies.ric_ies_len) < 0) {
2438                 /* TODO: prevent MLME/driver from trying to associate? */
2439         }
2440 }
2441 #endif /* CONFIG_IEEE80211R */
2442
2443
2444 #ifdef CONFIG_IBSS_RSN
2445 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
2446                                                 union wpa_event_data *data)
2447 {
2448         struct wpa_ssid *ssid;
2449         if (wpa_s->wpa_state < WPA_ASSOCIATED)
2450                 return;
2451         if (data == NULL)
2452                 return;
2453         ssid = wpa_s->current_ssid;
2454         if (ssid == NULL)
2455                 return;
2456         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2457                 return;
2458
2459         ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
2460 }
2461
2462
2463 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
2464                                            union wpa_event_data *data)
2465 {
2466         struct wpa_ssid *ssid = wpa_s->current_ssid;
2467
2468         if (ssid == NULL)
2469                 return;
2470
2471         /* check if the ssid is correctly configured as IBSS/RSN */
2472         if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
2473                 return;
2474
2475         ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
2476                              data->rx_mgmt.frame_len);
2477 }
2478 #endif /* CONFIG_IBSS_RSN */
2479
2480
2481 #ifdef CONFIG_IEEE80211R
2482 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
2483                          size_t len)
2484 {
2485         const u8 *sta_addr, *target_ap_addr;
2486         u16 status;
2487
2488         wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
2489         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
2490                 return; /* only SME case supported for now */
2491         if (len < 1 + 2 * ETH_ALEN + 2)
2492                 return;
2493         if (data[0] != 2)
2494                 return; /* Only FT Action Response is supported for now */
2495         sta_addr = data + 1;
2496         target_ap_addr = data + 1 + ETH_ALEN;
2497         status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
2498         wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
2499                 MACSTR " TargetAP " MACSTR " status %u",
2500                 MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
2501
2502         if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
2503                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
2504                         " in FT Action Response", MAC2STR(sta_addr));
2505                 return;
2506         }
2507
2508         if (status) {
2509                 wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
2510                         "failure (status code %d)", status);
2511                 /* TODO: report error to FT code(?) */
2512                 return;
2513         }
2514
2515         if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
2516                                     len - (1 + 2 * ETH_ALEN + 2), 1,
2517                                     target_ap_addr, NULL, 0) < 0)
2518                 return;
2519
2520 #ifdef CONFIG_SME
2521         {
2522                 struct wpa_bss *bss;
2523                 bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
2524                 if (bss)
2525                         wpa_s->sme.freq = bss->freq;
2526                 wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
2527                 sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
2528                               WLAN_AUTH_FT);
2529         }
2530 #endif /* CONFIG_SME */
2531 }
2532 #endif /* CONFIG_IEEE80211R */
2533
2534
2535 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
2536                                                struct unprot_deauth *e)
2537 {
2538 #ifdef CONFIG_IEEE80211W
2539         wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
2540                    "dropped: " MACSTR " -> " MACSTR
2541                    " (reason code %u)",
2542                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2543         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2544 #endif /* CONFIG_IEEE80211W */
2545 }
2546
2547
2548 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
2549                                                  struct unprot_disassoc *e)
2550 {
2551 #ifdef CONFIG_IEEE80211W
2552         wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
2553                    "dropped: " MACSTR " -> " MACSTR
2554                    " (reason code %u)",
2555                    MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
2556         sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
2557 #endif /* CONFIG_IEEE80211W */
2558 }
2559
2560
2561 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
2562                                   u16 reason_code, int locally_generated,
2563                                   const u8 *ie, size_t ie_len, int deauth)
2564 {
2565 #ifdef CONFIG_AP
2566         if (wpa_s->ap_iface && addr) {
2567                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
2568                 return;
2569         }
2570
2571         if (wpa_s->ap_iface) {
2572                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
2573                 return;
2574         }
2575 #endif /* CONFIG_AP */
2576
2577         wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
2578
2579         if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
2580               ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2581                 (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
2582                eapol_sm_failed(wpa_s->eapol))) &&
2583              !wpa_s->eap_expected_failure))
2584                 wpas_auth_failed(wpa_s, "AUTH_FAILED");
2585
2586 #ifdef CONFIG_P2P
2587         if (deauth && reason_code > 0) {
2588                 if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
2589                                           locally_generated) > 0) {
2590                         /*
2591                          * The interface was removed, so cannot continue
2592                          * processing any additional operations after this.
2593                          */
2594                         return;
2595                 }
2596         }
2597 #endif /* CONFIG_P2P */
2598
2599         wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
2600                                              locally_generated);
2601 }
2602
2603
2604 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
2605                                 struct disassoc_info *info)
2606 {
2607         u16 reason_code = 0;
2608         int locally_generated = 0;
2609         const u8 *addr = NULL;
2610         const u8 *ie = NULL;
2611         size_t ie_len = 0;
2612
2613         wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
2614
2615         if (info) {
2616                 addr = info->addr;
2617                 ie = info->ie;
2618                 ie_len = info->ie_len;
2619                 reason_code = info->reason_code;
2620                 locally_generated = info->locally_generated;
2621                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
2622                         locally_generated ? " (locally generated)" : "");
2623                 if (addr)
2624                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2625                                 MAC2STR(addr));
2626                 wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
2627                             ie, ie_len);
2628         }
2629
2630 #ifdef CONFIG_AP
2631         if (wpa_s->ap_iface && info && info->addr) {
2632                 hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
2633                 return;
2634         }
2635
2636         if (wpa_s->ap_iface) {
2637                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
2638                 return;
2639         }
2640 #endif /* CONFIG_AP */
2641
2642 #ifdef CONFIG_P2P
2643         if (info) {
2644                 wpas_p2p_disassoc_notif(
2645                         wpa_s, info->addr, reason_code, info->ie, info->ie_len,
2646                         locally_generated);
2647         }
2648 #endif /* CONFIG_P2P */
2649
2650         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
2651                 sme_event_disassoc(wpa_s, info);
2652
2653         wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
2654                               ie, ie_len, 0);
2655 }
2656
2657
2658 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
2659                               struct deauth_info *info)
2660 {
2661         u16 reason_code = 0;
2662         int locally_generated = 0;
2663         const u8 *addr = NULL;
2664         const u8 *ie = NULL;
2665         size_t ie_len = 0;
2666
2667         wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
2668
2669         if (info) {
2670                 addr = info->addr;
2671                 ie = info->ie;
2672                 ie_len = info->ie_len;
2673                 reason_code = info->reason_code;
2674                 locally_generated = info->locally_generated;
2675                 wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
2676                         reason_code,
2677                         locally_generated ? " (locally generated)" : "");
2678                 if (addr) {
2679                         wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
2680                                 MAC2STR(addr));
2681                 }
2682                 wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
2683                             ie, ie_len);
2684         }
2685
2686         wpa_reset_ft_completed(wpa_s->wpa);
2687
2688         wpas_event_disconnect(wpa_s, addr, reason_code,
2689                               locally_generated, ie, ie_len, 1);
2690 }
2691
2692
2693 static const char * reg_init_str(enum reg_change_initiator init)
2694 {
2695         switch (init) {
2696         case REGDOM_SET_BY_CORE:
2697                 return "CORE";
2698         case REGDOM_SET_BY_USER:
2699                 return "USER";
2700         case REGDOM_SET_BY_DRIVER:
2701                 return "DRIVER";
2702         case REGDOM_SET_BY_COUNTRY_IE:
2703                 return "COUNTRY_IE";
2704         case REGDOM_BEACON_HINT:
2705                 return "BEACON_HINT";
2706         }
2707         return "?";
2708 }
2709
2710
2711 static const char * reg_type_str(enum reg_type type)
2712 {
2713         switch (type) {
2714         case REGDOM_TYPE_UNKNOWN:
2715                 return "UNKNOWN";
2716         case REGDOM_TYPE_COUNTRY:
2717                 return "COUNTRY";
2718         case REGDOM_TYPE_WORLD:
2719                 return "WORLD";
2720         case REGDOM_TYPE_CUSTOM_WORLD:
2721                 return "CUSTOM_WORLD";
2722         case REGDOM_TYPE_INTERSECTION:
2723                 return "INTERSECTION";
2724         }
2725         return "?";
2726 }
2727
2728
2729 static void wpa_supplicant_update_channel_list(
2730         struct wpa_supplicant *wpa_s, struct channel_list_changed *info)
2731 {
2732         struct wpa_supplicant *ifs;
2733
2734         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
2735                 reg_init_str(info->initiator), reg_type_str(info->type),
2736                 info->alpha2[0] ? " alpha2=" : "",
2737                 info->alpha2[0] ? info->alpha2 : "");
2738
2739         if (wpa_s->drv_priv == NULL)
2740                 return; /* Ignore event during drv initialization */
2741
2742         free_hw_features(wpa_s);
2743         wpa_s->hw.modes = wpa_drv_get_hw_feature_data(
2744                 wpa_s, &wpa_s->hw.num_modes, &wpa_s->hw.flags);
2745
2746         wpas_p2p_update_channel_list(wpa_s);
2747
2748         /*
2749          * Check other interfaces to see if they share the same radio. If
2750          * so, they get updated with this same hw mode info.
2751          */
2752         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
2753                          radio_list) {
2754                 if (ifs != wpa_s) {
2755                         wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
2756                                    ifs->ifname);
2757                         free_hw_features(ifs);
2758                         ifs->hw.modes = wpa_drv_get_hw_feature_data(
2759                                 ifs, &ifs->hw.num_modes, &ifs->hw.flags);
2760                 }
2761         }
2762 }
2763
2764
2765 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
2766                                       const u8 *frame, size_t len, int freq)
2767 {
2768         const struct ieee80211_mgmt *mgmt;
2769         const u8 *payload;
2770         size_t plen;
2771         u8 category;
2772
2773         if (len < IEEE80211_HDRLEN + 2)
2774                 return;
2775
2776         mgmt = (const struct ieee80211_mgmt *) frame;
2777         payload = frame + IEEE80211_HDRLEN;
2778         category = *payload++;
2779         plen = len - IEEE80211_HDRLEN - 1;
2780
2781         wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
2782                 " Category=%u DataLen=%d freq=%d MHz",
2783                 MAC2STR(mgmt->sa), category, (int) plen, freq);
2784
2785 #ifdef CONFIG_IEEE80211R
2786         if (category == WLAN_ACTION_FT) {
2787                 ft_rx_action(wpa_s, payload, plen);
2788                 return;
2789         }
2790 #endif /* CONFIG_IEEE80211R */
2791
2792 #ifdef CONFIG_IEEE80211W
2793 #ifdef CONFIG_SME
2794         if (category == WLAN_ACTION_SA_QUERY) {
2795                 sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
2796                 return;
2797         }
2798 #endif /* CONFIG_SME */
2799 #endif /* CONFIG_IEEE80211W */
2800
2801 #ifdef CONFIG_WNM
2802         if (mgmt->u.action.category == WLAN_ACTION_WNM) {
2803                 ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
2804                 return;
2805         }
2806 #endif /* CONFIG_WNM */
2807
2808 #ifdef CONFIG_GAS
2809         if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
2810              mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
2811             gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
2812                          mgmt->u.action.category,
2813                          payload, plen, freq) == 0)
2814                 return;
2815 #endif /* CONFIG_GAS */
2816
2817 #ifdef CONFIG_TDLS
2818         if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
2819             payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
2820                 wpa_dbg(wpa_s, MSG_DEBUG,
2821                         "TDLS: Received Discovery Response from " MACSTR,
2822                         MAC2STR(mgmt->sa));
2823                 return;
2824         }
2825 #endif /* CONFIG_TDLS */
2826
2827 #ifdef CONFIG_INTERWORKING
2828         if (category == WLAN_ACTION_QOS && plen >= 1 &&
2829             payload[0] == QOS_QOS_MAP_CONFIG) {
2830                 const u8 *pos = payload + 1;
2831                 size_t qlen = plen - 1;
2832                 wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
2833                         MACSTR, MAC2STR(mgmt->sa));
2834                 if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
2835                     qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
2836                     pos[1] <= qlen - 2 && pos[1] >= 16)
2837                         wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
2838                 return;
2839         }
2840 #endif /* CONFIG_INTERWORKING */
2841
2842         wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
2843                            category, payload, plen, freq);
2844         if (wpa_s->ifmsh)
2845                 mesh_mpm_action_rx(wpa_s, mgmt, len);
2846 }
2847
2848
2849 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
2850                                              union wpa_event_data *event)
2851 {
2852 #ifdef CONFIG_P2P
2853         struct wpa_supplicant *ifs;
2854 #endif /* CONFIG_P2P */
2855         struct wpa_freq_range_list *list;
2856         char *str = NULL;
2857
2858         list = &event->freq_range;
2859
2860         if (list->num)
2861                 str = freq_range_list_str(list);
2862         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
2863                 str ? str : "");
2864
2865 #ifdef CONFIG_P2P
2866         if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
2867                 wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
2868                         __func__);
2869         } else {
2870                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
2871                 wpas_p2p_update_channel_list(wpa_s);
2872         }
2873
2874         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
2875                 int freq;
2876                 if (!ifs->current_ssid ||
2877                     !ifs->current_ssid->p2p_group ||
2878                     (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
2879                      ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
2880                         continue;
2881
2882                 freq = ifs->current_ssid->frequency;
2883                 if (!freq_range_list_includes(list, freq)) {
2884                         wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating frequency %d MHz in safe range",
2885                                 freq);
2886                         continue;
2887                 }
2888
2889                 wpa_dbg(ifs, MSG_DEBUG, "P2P GO operating in unsafe frequency %d MHz",
2890                         freq);
2891                 /* TODO: Consider using CSA or removing the group within
2892                  * wpa_supplicant */
2893                 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
2894         }
2895 #endif /* CONFIG_P2P */
2896
2897         os_free(str);
2898 }
2899
2900
2901 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
2902                                             union wpa_event_data *data)
2903 {
2904         wpa_dbg(wpa_s, MSG_DEBUG,
2905                 "Connection authorized by device, previous state %d",
2906                 wpa_s->wpa_state);
2907         if (wpa_s->wpa_state == WPA_ASSOCIATED) {
2908                 wpa_supplicant_cancel_auth_timeout(wpa_s);
2909                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2910                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2911                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
2912         }
2913         wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
2914         wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
2915                                data->assoc_info.ptk_kek);
2916 }
2917
2918
2919 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
2920                           union wpa_event_data *data)
2921 {
2922         struct wpa_supplicant *wpa_s = ctx;
2923
2924         if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
2925             event != EVENT_INTERFACE_ENABLED &&
2926             event != EVENT_INTERFACE_STATUS &&
2927             event != EVENT_SCHED_SCAN_STOPPED) {
2928                 wpa_dbg(wpa_s, MSG_DEBUG,
2929                         "Ignore event %s (%d) while interface is disabled",
2930                         event_to_string(event), event);
2931                 return;
2932         }
2933
2934 #ifndef CONFIG_NO_STDOUT_DEBUG
2935 {
2936         int level = MSG_DEBUG;
2937
2938         if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
2939                 const struct ieee80211_hdr *hdr;
2940                 u16 fc;
2941                 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
2942                 fc = le_to_host16(hdr->frame_control);
2943                 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2944                     WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
2945                         level = MSG_EXCESSIVE;
2946         }
2947
2948         wpa_dbg(wpa_s, level, "Event %s (%d) received",
2949                 event_to_string(event), event);
2950 }
2951 #endif /* CONFIG_NO_STDOUT_DEBUG */
2952
2953         switch (event) {
2954         case EVENT_AUTH:
2955                 sme_event_auth(wpa_s, data);
2956                 break;
2957         case EVENT_ASSOC:
2958                 wpa_supplicant_event_assoc(wpa_s, data);
2959                 if (data && data->assoc_info.authorized)
2960                         wpa_supplicant_event_assoc_auth(wpa_s, data);
2961                 break;
2962         case EVENT_DISASSOC:
2963                 wpas_event_disassoc(wpa_s,
2964                                     data ? &data->disassoc_info : NULL);
2965                 break;
2966         case EVENT_DEAUTH:
2967                 wpas_event_deauth(wpa_s,
2968                                   data ? &data->deauth_info : NULL);
2969                 break;
2970         case EVENT_MICHAEL_MIC_FAILURE:
2971                 wpa_supplicant_event_michael_mic_failure(wpa_s, data);
2972                 break;
2973 #ifndef CONFIG_NO_SCAN_PROCESSING
2974         case EVENT_SCAN_STARTED:
2975                 os_get_reltime(&wpa_s->scan_start_time);
2976                 if (wpa_s->own_scan_requested) {
2977                         struct os_reltime diff;
2978
2979                         os_reltime_sub(&wpa_s->scan_start_time,
2980                                        &wpa_s->scan_trigger_time, &diff);
2981                         wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
2982                                 diff.sec, diff.usec);
2983                         wpa_s->own_scan_requested = 0;
2984                         wpa_s->own_scan_running = 1;
2985                         if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
2986                             wpa_s->manual_scan_use_id) {
2987                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
2988                                              WPA_EVENT_SCAN_STARTED "id=%u",
2989                                              wpa_s->manual_scan_id);
2990                         } else {
2991                                 wpa_msg_ctrl(wpa_s, MSG_INFO,
2992                                              WPA_EVENT_SCAN_STARTED);
2993                         }
2994                 } else {
2995                         wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
2996                         wpa_s->external_scan_running = 1;
2997                         wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
2998                 }
2999                 break;
3000         case EVENT_SCAN_RESULTS:
3001                 if (os_reltime_initialized(&wpa_s->scan_start_time)) {
3002                         struct os_reltime now, diff;
3003                         os_get_reltime(&now);
3004                         os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
3005                         wpa_s->scan_start_time.sec = 0;
3006                         wpa_s->scan_start_time.usec = 0;
3007                         wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
3008                                 diff.sec, diff.usec);
3009                 }
3010                 wpa_supplicant_event_scan_results(wpa_s, data);
3011                 wpa_s->own_scan_running = 0;
3012                 wpa_s->external_scan_running = 0;
3013                 radio_work_check_next(wpa_s);
3014                 break;
3015 #endif /* CONFIG_NO_SCAN_PROCESSING */
3016         case EVENT_ASSOCINFO:
3017                 wpa_supplicant_event_associnfo(wpa_s, data);
3018                 break;
3019         case EVENT_INTERFACE_STATUS:
3020                 wpa_supplicant_event_interface_status(wpa_s, data);
3021                 break;
3022         case EVENT_PMKID_CANDIDATE:
3023                 wpa_supplicant_event_pmkid_candidate(wpa_s, data);
3024                 break;
3025 #ifdef CONFIG_PEERKEY
3026         case EVENT_STKSTART:
3027                 wpa_supplicant_event_stkstart(wpa_s, data);
3028                 break;
3029 #endif /* CONFIG_PEERKEY */
3030 #ifdef CONFIG_TDLS
3031         case EVENT_TDLS:
3032                 wpa_supplicant_event_tdls(wpa_s, data);
3033                 break;
3034 #endif /* CONFIG_TDLS */
3035 #ifdef CONFIG_WNM
3036         case EVENT_WNM:
3037                 wpa_supplicant_event_wnm(wpa_s, data);
3038                 break;
3039 #endif /* CONFIG_WNM */
3040 #ifdef CONFIG_IEEE80211R
3041         case EVENT_FT_RESPONSE:
3042                 wpa_supplicant_event_ft_response(wpa_s, data);
3043                 break;
3044 #endif /* CONFIG_IEEE80211R */
3045 #ifdef CONFIG_IBSS_RSN
3046         case EVENT_IBSS_RSN_START:
3047                 wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
3048                 break;
3049 #endif /* CONFIG_IBSS_RSN */
3050         case EVENT_ASSOC_REJECT:
3051                 if (data->assoc_reject.bssid)
3052                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3053                                 "bssid=" MACSTR " status_code=%u",
3054                                 MAC2STR(data->assoc_reject.bssid),
3055                                 data->assoc_reject.status_code);
3056                 else
3057                         wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
3058                                 "status_code=%u",
3059                                 data->assoc_reject.status_code);
3060                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3061                         sme_event_assoc_reject(wpa_s, data);
3062                 else {
3063                         const u8 *bssid = data->assoc_reject.bssid;
3064                         if (bssid == NULL || is_zero_ether_addr(bssid))
3065                                 bssid = wpa_s->pending_bssid;
3066                         wpas_connection_failed(wpa_s, bssid);
3067                         wpa_supplicant_mark_disassoc(wpa_s);
3068                 }
3069                 break;
3070         case EVENT_AUTH_TIMED_OUT:
3071                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3072                         sme_event_auth_timed_out(wpa_s, data);
3073                 break;
3074         case EVENT_ASSOC_TIMED_OUT:
3075                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
3076                         sme_event_assoc_timed_out(wpa_s, data);
3077                 break;
3078         case EVENT_TX_STATUS:
3079                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
3080                         " type=%d stype=%d",
3081                         MAC2STR(data->tx_status.dst),
3082                         data->tx_status.type, data->tx_status.stype);
3083 #ifdef CONFIG_AP
3084                 if (wpa_s->ap_iface == NULL) {
3085 #ifdef CONFIG_OFFCHANNEL
3086                         if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3087                             data->tx_status.stype == WLAN_FC_STYPE_ACTION)
3088                                 offchannel_send_action_tx_status(
3089                                         wpa_s, data->tx_status.dst,
3090                                         data->tx_status.data,
3091                                         data->tx_status.data_len,
3092                                         data->tx_status.ack ?
3093                                         OFFCHANNEL_SEND_ACTION_SUCCESS :
3094                                         OFFCHANNEL_SEND_ACTION_NO_ACK);
3095 #endif /* CONFIG_OFFCHANNEL */
3096                         break;
3097                 }
3098 #endif /* CONFIG_AP */
3099 #ifdef CONFIG_OFFCHANNEL
3100                 wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
3101                         MACSTR, MAC2STR(wpa_s->parent->pending_action_dst));
3102                 /*
3103                  * Catch TX status events for Action frames we sent via group
3104                  * interface in GO mode.
3105                  */
3106                 if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
3107                     data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
3108                     os_memcmp(wpa_s->parent->pending_action_dst,
3109                               data->tx_status.dst, ETH_ALEN) == 0) {
3110                         offchannel_send_action_tx_status(
3111                                 wpa_s->parent, data->tx_status.dst,
3112                                 data->tx_status.data,
3113                                 data->tx_status.data_len,
3114                                 data->tx_status.ack ?
3115                                 OFFCHANNEL_SEND_ACTION_SUCCESS :
3116                                 OFFCHANNEL_SEND_ACTION_NO_ACK);
3117                         break;
3118                 }
3119 #endif /* CONFIG_OFFCHANNEL */
3120 #ifdef CONFIG_AP
3121                 switch (data->tx_status.type) {
3122                 case WLAN_FC_TYPE_MGMT:
3123                         ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
3124                                       data->tx_status.data_len,
3125                                       data->tx_status.stype,
3126                                       data->tx_status.ack);
3127                         break;
3128                 case WLAN_FC_TYPE_DATA:
3129                         ap_tx_status(wpa_s, data->tx_status.dst,
3130                                      data->tx_status.data,
3131                                      data->tx_status.data_len,
3132                                      data->tx_status.ack);
3133                         break;
3134                 }
3135 #endif /* CONFIG_AP */
3136                 break;
3137 #ifdef CONFIG_AP
3138         case EVENT_EAPOL_TX_STATUS:
3139                 ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
3140                                    data->eapol_tx_status.data,
3141                                    data->eapol_tx_status.data_len,
3142                                    data->eapol_tx_status.ack);
3143                 break;
3144         case EVENT_DRIVER_CLIENT_POLL_OK:
3145                 ap_client_poll_ok(wpa_s, data->client_poll.addr);
3146                 break;
3147         case EVENT_RX_FROM_UNKNOWN:
3148                 if (wpa_s->ap_iface == NULL)
3149                         break;
3150                 ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
3151                                        data->rx_from_unknown.wds);
3152                 break;
3153         case EVENT_CH_SWITCH:
3154                 if (!data)
3155                         break;
3156                 if (!wpa_s->ap_iface) {
3157                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
3158                                 "event in non-AP mode");
3159                         break;
3160                 }
3161
3162                 wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
3163                                   data->ch_switch.ht_enabled,
3164                                   data->ch_switch.ch_offset,
3165                                   data->ch_switch.ch_width,
3166                                   data->ch_switch.cf1,
3167                                   data->ch_switch.cf2);
3168                 break;
3169 #endif /* CONFIG_AP */
3170         case EVENT_RX_MGMT: {
3171                 u16 fc, stype;
3172                 const struct ieee80211_mgmt *mgmt;
3173
3174 #ifdef CONFIG_TESTING_OPTIONS
3175                 if (wpa_s->ext_mgmt_frame_handling) {
3176                         struct rx_mgmt *rx = &data->rx_mgmt;
3177                         size_t hex_len = 2 * rx->frame_len + 1;
3178                         char *hex = os_malloc(hex_len);
3179                         if (hex) {
3180                                 wpa_snprintf_hex(hex, hex_len,
3181                                                  rx->frame, rx->frame_len);
3182                                 wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
3183                                         rx->freq, rx->datarate, rx->ssi_signal,
3184                                         hex);
3185                                 os_free(hex);
3186                         }
3187                         break;
3188                 }
3189 #endif /* CONFIG_TESTING_OPTIONS */
3190
3191                 mgmt = (const struct ieee80211_mgmt *)
3192                         data->rx_mgmt.frame;
3193                 fc = le_to_host16(mgmt->frame_control);
3194                 stype = WLAN_FC_GET_STYPE(fc);
3195
3196 #ifdef CONFIG_AP
3197                 if (wpa_s->ap_iface == NULL) {
3198 #endif /* CONFIG_AP */
3199 #ifdef CONFIG_P2P
3200                         if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3201                             data->rx_mgmt.frame_len > 24) {
3202                                 const u8 *src = mgmt->sa;
3203                                 const u8 *ie = mgmt->u.probe_req.variable;
3204                                 size_t ie_len = data->rx_mgmt.frame_len -
3205                                         (mgmt->u.probe_req.variable -
3206                                          data->rx_mgmt.frame);
3207                                 wpas_p2p_probe_req_rx(
3208                                         wpa_s, src, mgmt->da,
3209                                         mgmt->bssid, ie, ie_len,
3210                                         data->rx_mgmt.ssi_signal);
3211                                 break;
3212                         }
3213 #endif /* CONFIG_P2P */
3214 #ifdef CONFIG_IBSS_RSN
3215                         if (wpa_s->current_ssid &&
3216                             wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
3217                             stype == WLAN_FC_STYPE_AUTH &&
3218                             data->rx_mgmt.frame_len >= 30) {
3219                                 wpa_supplicant_event_ibss_auth(wpa_s, data);
3220                                 break;
3221                         }
3222 #endif /* CONFIG_IBSS_RSN */
3223
3224                         if (stype == WLAN_FC_STYPE_ACTION) {
3225                                 wpas_event_rx_mgmt_action(
3226                                         wpa_s, data->rx_mgmt.frame,
3227                                         data->rx_mgmt.frame_len,
3228                                         data->rx_mgmt.freq);
3229                                 break;
3230                         }
3231
3232                         if (wpa_s->ifmsh) {
3233                                 mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
3234                                 break;
3235                         }
3236
3237                         wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
3238                                 "management frame in non-AP mode");
3239                         break;
3240 #ifdef CONFIG_AP
3241                 }
3242
3243                 if (stype == WLAN_FC_STYPE_PROBE_REQ &&
3244                     data->rx_mgmt.frame_len > 24) {
3245                         const u8 *ie = mgmt->u.probe_req.variable;
3246                         size_t ie_len = data->rx_mgmt.frame_len -
3247                                 (mgmt->u.probe_req.variable -
3248                                  data->rx_mgmt.frame);
3249
3250                         wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
3251                                          mgmt->bssid, ie, ie_len,
3252                                          data->rx_mgmt.ssi_signal);
3253                 }
3254
3255                 ap_mgmt_rx(wpa_s, &data->rx_mgmt);
3256 #endif /* CONFIG_AP */
3257                 break;
3258                 }
3259         case EVENT_RX_PROBE_REQ:
3260                 if (data->rx_probe_req.sa == NULL ||
3261                     data->rx_probe_req.ie == NULL)
3262                         break;
3263 #ifdef CONFIG_AP
3264                 if (wpa_s->ap_iface) {
3265                         hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
3266                                              data->rx_probe_req.sa,
3267                                              data->rx_probe_req.da,
3268                                              data->rx_probe_req.bssid,
3269                                              data->rx_probe_req.ie,
3270                                              data->rx_probe_req.ie_len,
3271                                              data->rx_probe_req.ssi_signal);
3272                         break;
3273                 }
3274 #endif /* CONFIG_AP */
3275                 wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
3276                                       data->rx_probe_req.da,
3277                                       data->rx_probe_req.bssid,
3278                                       data->rx_probe_req.ie,
3279                                       data->rx_probe_req.ie_len,
3280                                       data->rx_probe_req.ssi_signal);
3281                 break;
3282         case EVENT_REMAIN_ON_CHANNEL:
3283 #ifdef CONFIG_OFFCHANNEL
3284                 offchannel_remain_on_channel_cb(
3285                         wpa_s, data->remain_on_channel.freq,
3286                         data->remain_on_channel.duration);
3287 #endif /* CONFIG_OFFCHANNEL */
3288                 wpas_p2p_remain_on_channel_cb(
3289                         wpa_s, data->remain_on_channel.freq,
3290                         data->remain_on_channel.duration);
3291                 break;
3292         case EVENT_CANCEL_REMAIN_ON_CHANNEL:
3293 #ifdef CONFIG_OFFCHANNEL
3294                 offchannel_cancel_remain_on_channel_cb(
3295                         wpa_s, data->remain_on_channel.freq);
3296 #endif /* CONFIG_OFFCHANNEL */
3297                 wpas_p2p_cancel_remain_on_channel_cb(
3298                         wpa_s, data->remain_on_channel.freq);
3299                 break;
3300         case EVENT_EAPOL_RX:
3301                 wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
3302                                         data->eapol_rx.data,
3303                                         data->eapol_rx.data_len);
3304                 break;
3305         case EVENT_SIGNAL_CHANGE:
3306                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
3307                         "above=%d signal=%d noise=%d txrate=%d",
3308                         data->signal_change.above_threshold,
3309                         data->signal_change.current_signal,
3310                         data->signal_change.current_noise,
3311                         data->signal_change.current_txrate);
3312                 bgscan_notify_signal_change(
3313                         wpa_s, data->signal_change.above_threshold,
3314                         data->signal_change.current_signal,
3315                         data->signal_change.current_noise,
3316                         data->signal_change.current_txrate);
3317                 break;
3318         case EVENT_INTERFACE_ENABLED:
3319                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
3320                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3321                         wpa_supplicant_update_mac_addr(wpa_s);
3322                         if (wpa_s->p2p_mgmt) {
3323                                 wpa_supplicant_set_state(wpa_s,
3324                                                          WPA_DISCONNECTED);
3325                                 break;
3326                         }
3327
3328 #ifdef CONFIG_AP
3329                         if (!wpa_s->ap_iface) {
3330                                 wpa_supplicant_set_state(wpa_s,
3331                                                          WPA_DISCONNECTED);
3332                                 wpa_supplicant_req_scan(wpa_s, 0, 0);
3333                         } else
3334                                 wpa_supplicant_set_state(wpa_s,
3335                                                          WPA_COMPLETED);
3336 #else /* CONFIG_AP */
3337                         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3338                         wpa_supplicant_req_scan(wpa_s, 0, 0);
3339 #endif /* CONFIG_AP */
3340                 }
3341                 break;
3342         case EVENT_INTERFACE_DISABLED:
3343                 wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
3344 #ifdef CONFIG_P2P
3345                 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
3346                     (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
3347                      wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
3348                         /*
3349                          * The interface was externally disabled. Remove
3350                          * it assuming an external entity will start a
3351                          * new session if needed.
3352                          */
3353                         wpas_p2p_disconnect(wpa_s);
3354                         break;
3355                 }
3356                 if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
3357                     p2p_in_progress(wpa_s->global->p2p) > 1) {
3358                         /* This radio work will be cancelled, so clear P2P
3359                          * state as well.
3360                          */
3361                         p2p_stop_find(wpa_s->global->p2p);
3362                 }
3363 #endif /* CONFIG_P2P */
3364
3365                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3366                         /*
3367                          * Indicate disconnection to keep ctrl_iface events
3368                          * consistent.
3369                          */
3370                         wpa_supplicant_event_disassoc(
3371                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
3372                 }
3373                 wpa_supplicant_mark_disassoc(wpa_s);
3374                 radio_remove_works(wpa_s, NULL, 0);
3375
3376                 wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
3377                 break;
3378         case EVENT_CHANNEL_LIST_CHANGED:
3379                 wpa_supplicant_update_channel_list(
3380                         wpa_s, &data->channel_list_changed);
3381                 break;
3382         case EVENT_INTERFACE_UNAVAILABLE:
3383                 wpas_p2p_interface_unavailable(wpa_s);
3384                 break;
3385         case EVENT_BEST_CHANNEL:
3386                 wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
3387                         "(%d %d %d)",
3388                         data->best_chan.freq_24, data->best_chan.freq_5,
3389                         data->best_chan.freq_overall);
3390                 wpa_s->best_24_freq = data->best_chan.freq_24;
3391                 wpa_s->best_5_freq = data->best_chan.freq_5;
3392                 wpa_s->best_overall_freq = data->best_chan.freq_overall;
3393                 wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
3394                                               data->best_chan.freq_5,
3395                                               data->best_chan.freq_overall);
3396                 break;
3397         case EVENT_UNPROT_DEAUTH:
3398                 wpa_supplicant_event_unprot_deauth(wpa_s,
3399                                                    &data->unprot_deauth);
3400                 break;
3401         case EVENT_UNPROT_DISASSOC:
3402                 wpa_supplicant_event_unprot_disassoc(wpa_s,
3403                                                      &data->unprot_disassoc);
3404                 break;
3405         case EVENT_STATION_LOW_ACK:
3406 #ifdef CONFIG_AP
3407                 if (wpa_s->ap_iface && data)
3408                         hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
3409                                                   data->low_ack.addr);
3410 #endif /* CONFIG_AP */
3411 #ifdef CONFIG_TDLS
3412                 if (data)
3413                         wpa_tdls_disable_unreachable_link(wpa_s->wpa,
3414                                                           data->low_ack.addr);
3415 #endif /* CONFIG_TDLS */
3416                 break;
3417         case EVENT_IBSS_PEER_LOST:
3418 #ifdef CONFIG_IBSS_RSN
3419                 ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
3420 #endif /* CONFIG_IBSS_RSN */
3421                 break;
3422         case EVENT_DRIVER_GTK_REKEY:
3423                 if (os_memcmp(data->driver_gtk_rekey.bssid,
3424                               wpa_s->bssid, ETH_ALEN))
3425                         break;
3426                 if (!wpa_s->wpa)
3427                         break;
3428                 wpa_sm_update_replay_ctr(wpa_s->wpa,
3429                                          data->driver_gtk_rekey.replay_ctr);
3430                 break;
3431         case EVENT_SCHED_SCAN_STOPPED:
3432                 wpa_s->pno = 0;
3433                 wpa_s->sched_scanning = 0;
3434                 wpa_supplicant_notify_scanning(wpa_s, 0);
3435
3436                 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
3437                         break;
3438
3439                 /*
3440                  * Start a new sched scan to continue searching for more SSIDs
3441                  * either if timed out or PNO schedule scan is pending.
3442                  */
3443                 if (wpa_s->sched_scan_timed_out) {
3444                         wpa_supplicant_req_sched_scan(wpa_s);
3445                 } else if (wpa_s->pno_sched_pending) {
3446                         wpa_s->pno_sched_pending = 0;
3447                         wpas_start_pno(wpa_s);
3448                 }
3449
3450                 break;
3451         case EVENT_WPS_BUTTON_PUSHED:
3452 #ifdef CONFIG_WPS
3453                 wpas_wps_start_pbc(wpa_s, NULL, 0);
3454 #endif /* CONFIG_WPS */
3455                 break;
3456         case EVENT_AVOID_FREQUENCIES:
3457                 wpa_supplicant_notify_avoid_freq(wpa_s, data);
3458                 break;
3459         case EVENT_CONNECT_FAILED_REASON:
3460 #ifdef CONFIG_AP
3461                 if (!wpa_s->ap_iface || !data)
3462                         break;
3463                 hostapd_event_connect_failed_reason(
3464                         wpa_s->ap_iface->bss[0],
3465                         data->connect_failed_reason.addr,
3466                         data->connect_failed_reason.code);
3467 #endif /* CONFIG_AP */
3468                 break;
3469         case EVENT_NEW_PEER_CANDIDATE:
3470                 wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
3471                                      data->mesh_peer.ies,
3472                                      data->mesh_peer.ie_len);
3473                 break;
3474         default:
3475                 wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
3476                 break;
3477         }
3478 }