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

55-freebsd-126-freebsd-12
Last change on this file since bc4bb2e was bc4bb2e, checked in by gordon <gordon@…>, on 10/17/17 at 17:22:36

Update wpa_supplicant/hostapd for 2017-01 vulnerability release.

hostapd: Avoid key reinstallation in FT handshake
Prevent reinstallation of an already in-use group key
Extend protection of GTK/IGTK reinstallation of WNM-Sleep Mode cases
Fix TK configuration to the driver in EAPOL-Key 3/4 retry case
Prevent installation of an all-zero TK
Fix PTK rekeying to generate a new ANonce
TDLS: Reject TPK-TK reconfiguration
WNM: Ignore Key Data in WNM Sleep Mode Response frame if no PMF in use
WNM: Ignore WNM-Sleep Mode Response if WNM-Sleep Mode has not been used
WNM: Ignore WNM-Sleep Mode Response without pending request
FT: Do not allow multiple Reassociation Response frames
TDLS: Ignore incoming TDLS Setup Response retries

Submitted by: jhb
Obtained from: https://w1.fi/security/2017-01/ (against later version)
Security: FreeBSD-SA-17:07
Security: CERT VU#228519
Security: CVE-2017-13077
Security: CVE-2017-13078
Security: CVE-2017-13079
Security: CVE-2017-13080
Security: CVE-2017-13081
Security: CVE-2017-13082
Security: CVE-2017-13086
Security: CVE-2017-13087
Security: CVE-2017-13088
Differential Revision: https://reviews.freebsd.org/D12693

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