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