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