source: rtems-libbsd/freebsd/contrib/wpa/wpa_supplicant/events.c @ 9c9d11b

55-freebsd-126-freebsd-12
Last change on this file since 9c9d11b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

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