~ubuntu-branches/ubuntu/precise/wpasupplicant/precise-proposed

« back to all changes in this revision

Viewing changes to .pc/0004-pmkokc-Flush-PMKSA-cache-entries-and-invalidate-EAP-state-o.patch/wpa_supplicant/wpa_supplicant.c

  • Committer: Package Import Robot
  • Author(s): Mathieu Trudel-Lapierre
  • Date: 2013-06-12 15:57:50 UTC
  • Revision ID: package-import@ubuntu.com-20130612155750-go9lgylcef8n3f2f
Tags: 0.7.3-6ubuntu2.2
* Multiple patches to reduce the number of disconnections for WPA Enterprise
  roaming and Opportunistic Key Caching. (LP: #1187524)
* In debian/patches:
  0001-sme-fix-retry-after-auth-assoc-timeout-failure.patch,
  0002-sme-optimize-recovery-from-common-load-balancing-mechanisms.patch,
  0003-sme-blacklist-bss-on-first-failure-if-only-a-*.patch,
  0004-sme-extend-load-balancing-optimization-in-bss-blacklisting.patch,
  0005-sme-optimize-recovery-from-association-command-failures.patch,
  0006-sme-add-timers-for-authentication-and-association.patch,
  0007-sme-nl80211-set-cipher-suites.patch:
  Cherry-pick patches fixing SME (Session Management Entity) for the nl80211
  driver, which works as a basis for the OKC patches.
* In debian/patches:
  0001-pmkokc-Set-portValid-TRUE-on-association-for-driver-based-4.patch,
  0002-pmkokc-Clear-WPA-and-EAPOL-state-machine-config-pointer-on-.patch,
  0003-pmkokc-Clear-driver-PMKSA-cache-entry-on-PMKSA-cache-expira.patch,
  0004-pmkokc-Flush-PMKSA-cache-entries-and-invalidate-EAP-state-o.patch,
  0005-pmkokc-Fix-proactive_key_caching-configuration-to-WPA-code.patch,
  0006-pmkokc-RSN-Add-a-debug-message-when-considing-addition-of-O.patch,
  0007-pmkokc-Clear-OKC-based-PMKSA-caching-entries-if-PMK-is-chan.patch,
  0008-pmkokc-Move-wpa_sm_remove_pmkid-call-to-PMKSA-cache-entry-f.patch,
  0009-pmkokc-Use-PMKSA-cache-entries-with-only-a-single-network-c.patch,
  0010-pmkokc-PMKSA-Do-not-evict-active-cache-entry-when-adding-ne.patch,
  0011-pmkokc-PMKSA-Set-cur_pmksa-pointer-during-initial-associati.patch,
  0012-pmkokc-PMKSA-make-deauthentication-due-to-cache-entry-remov.patch,
  0013-pmkokc-PMKSA-update-current-cache-entry-due-to-association-.patch:
  Cherry-pick patches to properly do OKC (Opportunistic Key Caching) which
  helps maintaining connectivity on networks secured with WPA Enterprise,
  especially on nl80211-based drivers -- these patches require SME, and add
  or fix key caching and handling of the cache entries.
* debian/patches/force-enable-okc.patch: force Opportunistic Key Caching to
  be enabled.
* debian/patches/less-aggressive-roaming.patch: use less aggressive roaming
  settings to avoid switching to another AP unnecessarily, when the actual
  signal level difference is small.
* debian/patches/wpa_supplicant-dbus-null-error.patch: Don't send NULL to
  dbus_message_new_error().
* debian/patches/0001-nl80211-Fix-UNSPEC-signal-quality-reporting.patch: fix
  marking qual as invalid rather than signal level.
* debian/patches/wpa_supplicant-squelch-driver-disconnect-spam.patch: recover
  cleanly from streams of disconnect messages (like on iwl3945).
* debian/patches/wpa_supplicant-assoc-timeout.patch: increase association
  timeouts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * WPA Supplicant
 
3
 * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License version 2 as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * Alternatively, this software may be distributed under the terms of BSD
 
10
 * license.
 
11
 *
 
12
 * See README and COPYING for more details.
 
13
 *
 
14
 * This file implements functions for registering and unregistering
 
15
 * %wpa_supplicant interfaces. In addition, this file contains number of
 
16
 * functions for managing network connections.
 
17
 */
 
18
 
 
19
#include "includes.h"
 
20
 
 
21
#include "common.h"
 
22
#include "eapol_supp/eapol_supp_sm.h"
 
23
#include "eap_peer/eap.h"
 
24
#include "eap_server/eap_methods.h"
 
25
#include "rsn_supp/wpa.h"
 
26
#include "eloop.h"
 
27
#include "config.h"
 
28
#include "l2_packet/l2_packet.h"
 
29
#include "wpa_supplicant_i.h"
 
30
#include "driver_i.h"
 
31
#include "ctrl_iface.h"
 
32
#include "pcsc_funcs.h"
 
33
#include "common/version.h"
 
34
#include "rsn_supp/preauth.h"
 
35
#include "rsn_supp/pmksa_cache.h"
 
36
#include "common/wpa_ctrl.h"
 
37
#include "mlme.h"
 
38
#include "common/ieee802_11_defs.h"
 
39
#include "blacklist.h"
 
40
#include "wpas_glue.h"
 
41
#include "wps_supplicant.h"
 
42
#include "ibss_rsn.h"
 
43
#include "sme.h"
 
44
#include "ap.h"
 
45
#include "notify.h"
 
46
#include "bgscan.h"
 
47
#include "bss.h"
 
48
#include "scan.h"
 
49
 
 
50
const char *wpa_supplicant_version =
 
51
"wpa_supplicant v" VERSION_STR "\n"
 
52
"Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> and contributors";
 
53
 
 
54
const char *wpa_supplicant_license =
 
55
"This program is free software. You can distribute it and/or modify it\n"
 
56
"under the terms of the GNU General Public License version 2.\n"
 
57
"\n"
 
58
"Alternatively, this software may be distributed under the terms of the\n"
 
59
"BSD license. See README and COPYING for more details.\n"
 
60
#ifdef EAP_TLS_OPENSSL
 
61
"\nThis product includes software developed by the OpenSSL Project\n"
 
62
"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
 
63
#endif /* EAP_TLS_OPENSSL */
 
64
;
 
65
 
 
66
#ifndef CONFIG_NO_STDOUT_DEBUG
 
67
/* Long text divided into parts in order to fit in C89 strings size limits. */
 
68
const char *wpa_supplicant_full_license1 =
 
69
"This program is free software; you can redistribute it and/or modify\n"
 
70
"it under the terms of the GNU General Public License version 2 as\n"
 
71
"published by the Free Software Foundation.\n"
 
72
"\n"
 
73
"This program is distributed in the hope that it will be useful,\n"
 
74
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
 
75
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
 
76
"GNU General Public License for more details.\n"
 
77
"\n";
 
78
const char *wpa_supplicant_full_license2 =
 
79
"You should have received a copy of the GNU General Public License\n"
 
80
"along with this program; if not, write to the Free Software\n"
 
81
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
 
82
"\n"
 
83
"Alternatively, this software may be distributed under the terms of the\n"
 
84
"BSD license.\n"
 
85
"\n"
 
86
"Redistribution and use in source and binary forms, with or without\n"
 
87
"modification, are permitted provided that the following conditions are\n"
 
88
"met:\n"
 
89
"\n";
 
90
const char *wpa_supplicant_full_license3 =
 
91
"1. Redistributions of source code must retain the above copyright\n"
 
92
"   notice, this list of conditions and the following disclaimer.\n"
 
93
"\n"
 
94
"2. Redistributions in binary form must reproduce the above copyright\n"
 
95
"   notice, this list of conditions and the following disclaimer in the\n"
 
96
"   documentation and/or other materials provided with the distribution.\n"
 
97
"\n";
 
98
const char *wpa_supplicant_full_license4 =
 
99
"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
 
100
"   names of its contributors may be used to endorse or promote products\n"
 
101
"   derived from this software without specific prior written permission.\n"
 
102
"\n"
 
103
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
 
104
"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
 
105
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
 
106
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
 
107
const char *wpa_supplicant_full_license5 =
 
108
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
 
109
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
 
110
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
 
111
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
 
112
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
 
113
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
 
114
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
 
115
"\n";
 
116
#endif /* CONFIG_NO_STDOUT_DEBUG */
 
117
 
 
118
extern int wpa_debug_level;
 
119
extern int wpa_debug_show_keys;
 
120
extern int wpa_debug_timestamp;
 
121
extern struct wpa_driver_ops *wpa_drivers[];
 
122
 
 
123
/* Configure default/group WEP keys for static WEP */
 
124
int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
 
125
{
 
126
        int i, set = 0;
 
127
 
 
128
        for (i = 0; i < NUM_WEP_KEYS; i++) {
 
129
                if (ssid->wep_key_len[i] == 0)
 
130
                        continue;
 
131
 
 
132
                set = 1;
 
133
                wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
 
134
                                (u8 *) "\xff\xff\xff\xff\xff\xff",
 
135
                                i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
 
136
                                ssid->wep_key[i], ssid->wep_key_len[i]);
 
137
        }
 
138
 
 
139
        return set;
 
140
}
 
141
 
 
142
 
 
143
static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
 
144
                                           struct wpa_ssid *ssid)
 
145
{
 
146
        u8 key[32];
 
147
        size_t keylen;
 
148
        enum wpa_alg alg;
 
149
        u8 seq[6] = { 0 };
 
150
 
 
151
        /* IBSS/WPA-None uses only one key (Group) for both receiving and
 
152
         * sending unicast and multicast packets. */
 
153
 
 
154
        if (ssid->mode != WPAS_MODE_IBSS) {
 
155
                wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
 
156
                           "for WPA-None", ssid->mode);
 
157
                return -1;
 
158
        }
 
159
 
 
160
        if (!ssid->psk_set) {
 
161
                wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
 
162
                return -1;
 
163
        }
 
164
 
 
165
        switch (wpa_s->group_cipher) {
 
166
        case WPA_CIPHER_CCMP:
 
167
                os_memcpy(key, ssid->psk, 16);
 
168
                keylen = 16;
 
169
                alg = WPA_ALG_CCMP;
 
170
                break;
 
171
        case WPA_CIPHER_TKIP:
 
172
                /* WPA-None uses the same Michael MIC key for both TX and RX */
 
173
                os_memcpy(key, ssid->psk, 16 + 8);
 
174
                os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
 
175
                keylen = 32;
 
176
                alg = WPA_ALG_TKIP;
 
177
                break;
 
178
        default:
 
179
                wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
 
180
                           "WPA-None", wpa_s->group_cipher);
 
181
                return -1;
 
182
        }
 
183
 
 
184
        /* TODO: should actually remember the previously used seq#, both for TX
 
185
         * and RX from each STA.. */
 
186
 
 
187
        return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
 
188
                               0, 1, seq, 6, key, keylen);
 
189
}
 
190
 
 
191
 
 
192
static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
 
193
{
 
194
        struct wpa_supplicant *wpa_s = eloop_ctx;
 
195
        const u8 *bssid = wpa_s->bssid;
 
196
        if (is_zero_ether_addr(bssid))
 
197
                bssid = wpa_s->pending_bssid;
 
198
        wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
 
199
                MAC2STR(bssid));
 
200
        wpa_blacklist_add(wpa_s, bssid);
 
201
        wpa_sm_notify_disassoc(wpa_s->wpa);
 
202
        wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
 
203
        wpa_s->reassociate = 1;
 
204
        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
205
}
 
206
 
 
207
 
 
208
/**
 
209
 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
 
210
 * @wpa_s: Pointer to wpa_supplicant data
 
211
 * @sec: Number of seconds after which to time out authentication
 
212
 * @usec: Number of microseconds after which to time out authentication
 
213
 *
 
214
 * This function is used to schedule a timeout for the current authentication
 
215
 * attempt.
 
216
 */
 
217
void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
 
218
                                     int sec, int usec)
 
219
{
 
220
        if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
 
221
            (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
 
222
                return;
 
223
 
 
224
        wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
 
225
                "%d usec", sec, usec);
 
226
        eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
 
227
        eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
 
228
}
 
229
 
 
230
 
 
231
/**
 
232
 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
 
233
 * @wpa_s: Pointer to wpa_supplicant data
 
234
 *
 
235
 * This function is used to cancel authentication timeout scheduled with
 
236
 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
 
237
 * been completed.
 
238
 */
 
239
void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
 
240
{
 
241
        wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
 
242
        eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
 
243
        wpa_blacklist_del(wpa_s, wpa_s->bssid);
 
244
}
 
245
 
 
246
 
 
247
/**
 
248
 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
 
249
 * @wpa_s: Pointer to wpa_supplicant data
 
250
 *
 
251
 * This function is used to configure EAPOL state machine based on the selected
 
252
 * authentication mode.
 
253
 */
 
254
void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
 
255
{
 
256
#ifdef IEEE8021X_EAPOL
 
257
        struct eapol_config eapol_conf;
 
258
        struct wpa_ssid *ssid = wpa_s->current_ssid;
 
259
 
 
260
#ifdef CONFIG_IBSS_RSN
 
261
        if (ssid->mode == WPAS_MODE_IBSS &&
 
262
            wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
 
263
            wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
 
264
                /*
 
265
                 * RSN IBSS authentication is per-STA and we can disable the
 
266
                 * per-BSSID EAPOL authentication.
 
267
                 */
 
268
                eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
 
269
                eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
 
270
                eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
 
271
                return;
 
272
        }
 
273
#endif /* CONFIG_IBSS_RSN */
 
274
 
 
275
        eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
 
276
        eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
 
277
 
 
278
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
 
279
            wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
 
280
                eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
 
281
        else
 
282
                eapol_sm_notify_portControl(wpa_s->eapol, Auto);
 
283
 
 
284
        os_memset(&eapol_conf, 0, sizeof(eapol_conf));
 
285
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
 
286
                eapol_conf.accept_802_1x_keys = 1;
 
287
                eapol_conf.required_keys = 0;
 
288
                if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
 
289
                        eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
 
290
                }
 
291
                if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
 
292
                        eapol_conf.required_keys |=
 
293
                                EAPOL_REQUIRE_KEY_BROADCAST;
 
294
                }
 
295
 
 
296
                if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
 
297
                        eapol_conf.required_keys = 0;
 
298
        }
 
299
        if (wpa_s->conf)
 
300
                eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
 
301
        eapol_conf.workaround = ssid->eap_workaround;
 
302
        eapol_conf.eap_disabled =
 
303
                !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
 
304
                wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
 
305
                wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
 
306
        eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
 
307
#endif /* IEEE8021X_EAPOL */
 
308
}
 
309
 
 
310
 
 
311
/**
 
312
 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
 
313
 * @wpa_s: Pointer to wpa_supplicant data
 
314
 * @ssid: Configuration data for the network
 
315
 *
 
316
 * This function is used to configure WPA state machine and related parameters
 
317
 * to a mode where WPA is not enabled. This is called as part of the
 
318
 * authentication configuration when the selected network does not use WPA.
 
319
 */
 
320
void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
 
321
                                       struct wpa_ssid *ssid)
 
322
{
 
323
        int i;
 
324
 
 
325
        if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
 
326
                wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
 
327
        else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
 
328
                wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
 
329
        else
 
330
                wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
 
331
        wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
 
332
        wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
 
333
        wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
 
334
        wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
 
335
        wpa_s->group_cipher = WPA_CIPHER_NONE;
 
336
        wpa_s->mgmt_group_cipher = 0;
 
337
 
 
338
        for (i = 0; i < NUM_WEP_KEYS; i++) {
 
339
                if (ssid->wep_key_len[i] > 5) {
 
340
                        wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
 
341
                        wpa_s->group_cipher = WPA_CIPHER_WEP104;
 
342
                        break;
 
343
                } else if (ssid->wep_key_len[i] > 0) {
 
344
                        wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
 
345
                        wpa_s->group_cipher = WPA_CIPHER_WEP40;
 
346
                        break;
 
347
                }
 
348
        }
 
349
 
 
350
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
 
351
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
 
352
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
 
353
                         wpa_s->pairwise_cipher);
 
354
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
 
355
#ifdef CONFIG_IEEE80211W
 
356
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
 
357
                         wpa_s->mgmt_group_cipher);
 
358
#endif /* CONFIG_IEEE80211W */
 
359
 
 
360
        pmksa_cache_clear_current(wpa_s->wpa);
 
361
}
 
362
 
 
363
 
 
364
static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
 
365
{
 
366
        bgscan_deinit(wpa_s);
 
367
        scard_deinit(wpa_s->scard);
 
368
        wpa_s->scard = NULL;
 
369
        wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
 
370
        eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
 
371
        l2_packet_deinit(wpa_s->l2);
 
372
        wpa_s->l2 = NULL;
 
373
        if (wpa_s->l2_br) {
 
374
                l2_packet_deinit(wpa_s->l2_br);
 
375
                wpa_s->l2_br = NULL;
 
376
        }
 
377
 
 
378
        if (wpa_s->ctrl_iface) {
 
379
                wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
 
380
                wpa_s->ctrl_iface = NULL;
 
381
        }
 
382
        if (wpa_s->conf != NULL) {
 
383
                struct wpa_ssid *ssid;
 
384
                for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
 
385
                        wpas_notify_network_removed(wpa_s, ssid);
 
386
                wpa_config_free(wpa_s->conf);
 
387
                wpa_s->conf = NULL;
 
388
        }
 
389
 
 
390
        os_free(wpa_s->confname);
 
391
        wpa_s->confname = NULL;
 
392
 
 
393
        wpa_sm_set_eapol(wpa_s->wpa, NULL);
 
394
        eapol_sm_deinit(wpa_s->eapol);
 
395
        wpa_s->eapol = NULL;
 
396
 
 
397
        rsn_preauth_deinit(wpa_s->wpa);
 
398
 
 
399
        pmksa_candidate_free(wpa_s->wpa);
 
400
        wpa_sm_deinit(wpa_s->wpa);
 
401
        wpa_s->wpa = NULL;
 
402
        wpa_blacklist_clear(wpa_s);
 
403
 
 
404
        wpa_bss_deinit(wpa_s);
 
405
 
 
406
        wpa_supplicant_cancel_scan(wpa_s);
 
407
        wpa_supplicant_cancel_auth_timeout(wpa_s);
 
408
 
 
409
        ieee80211_sta_deinit(wpa_s);
 
410
 
 
411
        wpas_wps_deinit(wpa_s);
 
412
 
 
413
        wpabuf_free(wpa_s->pending_eapol_rx);
 
414
        wpa_s->pending_eapol_rx = NULL;
 
415
 
 
416
#ifdef CONFIG_IBSS_RSN
 
417
        ibss_rsn_deinit(wpa_s->ibss_rsn);
 
418
        wpa_s->ibss_rsn = NULL;
 
419
#endif /* CONFIG_IBSS_RSN */
 
420
 
 
421
        sme_deinit(wpa_s);
 
422
 
 
423
#ifdef CONFIG_AP
 
424
        wpa_supplicant_ap_deinit(wpa_s);
 
425
#endif /* CONFIG_AP */
 
426
 
 
427
        os_free(wpa_s->next_scan_freqs);
 
428
        wpa_s->next_scan_freqs = NULL;
 
429
}
 
430
 
 
431
 
 
432
/**
 
433
 * wpa_clear_keys - Clear keys configured for the driver
 
434
 * @wpa_s: Pointer to wpa_supplicant data
 
435
 * @addr: Previously used BSSID or %NULL if not available
 
436
 *
 
437
 * This function clears the encryption keys that has been previously configured
 
438
 * for the driver.
 
439
 */
 
440
void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
 
441
{
 
442
        u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
 
443
 
 
444
        if (wpa_s->keys_cleared) {
 
445
                /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
 
446
                 * timing issues with keys being cleared just before new keys
 
447
                 * are set or just after association or something similar. This
 
448
                 * shows up in group key handshake failing often because of the
 
449
                 * client not receiving the first encrypted packets correctly.
 
450
                 * Skipping some of the extra key clearing steps seems to help
 
451
                 * in completing group key handshake more reliably. */
 
452
                wpa_printf(MSG_DEBUG, "No keys have been configured - "
 
453
                           "skip key clearing");
 
454
                return;
 
455
        }
 
456
 
 
457
        /* MLME-DELETEKEYS.request */
 
458
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
 
459
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
 
460
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
 
461
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
 
462
#ifdef CONFIG_IEEE80211W
 
463
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
 
464
        wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
 
465
#endif /* CONFIG_IEEE80211W */
 
466
        if (addr) {
 
467
                wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
 
468
                                0);
 
469
                /* MLME-SETPROTECTION.request(None) */
 
470
                wpa_drv_mlme_setprotection(
 
471
                        wpa_s, addr,
 
472
                        MLME_SETPROTECTION_PROTECT_TYPE_NONE,
 
473
                        MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
 
474
        }
 
475
        wpa_s->keys_cleared = 1;
 
476
}
 
477
 
 
478
 
 
479
/**
 
480
 * wpa_supplicant_state_txt - Get the connection state name as a text string
 
481
 * @state: State (wpa_state; WPA_*)
 
482
 * Returns: The state name as a printable text string
 
483
 */
 
484
const char * wpa_supplicant_state_txt(enum wpa_states state)
 
485
{
 
486
        switch (state) {
 
487
        case WPA_DISCONNECTED:
 
488
                return "DISCONNECTED";
 
489
        case WPA_INACTIVE:
 
490
                return "INACTIVE";
 
491
        case WPA_SCANNING:
 
492
                return "SCANNING";
 
493
        case WPA_AUTHENTICATING:
 
494
                return "AUTHENTICATING";
 
495
        case WPA_ASSOCIATING:
 
496
                return "ASSOCIATING";
 
497
        case WPA_ASSOCIATED:
 
498
                return "ASSOCIATED";
 
499
        case WPA_4WAY_HANDSHAKE:
 
500
                return "4WAY_HANDSHAKE";
 
501
        case WPA_GROUP_HANDSHAKE:
 
502
                return "GROUP_HANDSHAKE";
 
503
        case WPA_COMPLETED:
 
504
                return "COMPLETED";
 
505
        default:
 
506
                return "UNKNOWN";
 
507
        }
 
508
}
 
509
 
 
510
 
 
511
/**
 
512
 * wpa_supplicant_set_state - Set current connection state
 
513
 * @wpa_s: Pointer to wpa_supplicant data
 
514
 * @state: The new connection state
 
515
 *
 
516
 * This function is called whenever the connection state changes, e.g.,
 
517
 * association is completed for WPA/WPA2 4-Way Handshake is started.
 
518
 */
 
519
void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
 
520
                              enum wpa_states state)
 
521
{
 
522
        enum wpa_states old_state = wpa_s->wpa_state;
 
523
 
 
524
        wpa_printf(MSG_DEBUG, "State: %s -> %s",
 
525
                   wpa_supplicant_state_txt(wpa_s->wpa_state),
 
526
                   wpa_supplicant_state_txt(state));
 
527
 
 
528
        if (state != WPA_SCANNING)
 
529
                wpa_supplicant_notify_scanning(wpa_s, 0);
 
530
 
 
531
        if (state == WPA_COMPLETED && wpa_s->new_connection) {
 
532
#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
 
533
                struct wpa_ssid *ssid = wpa_s->current_ssid;
 
534
                wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
 
535
                        MACSTR " completed %s [id=%d id_str=%s]",
 
536
                        MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
 
537
                        "(reauth)" : "(auth)",
 
538
                        ssid ? ssid->id : -1,
 
539
                        ssid && ssid->id_str ? ssid->id_str : "");
 
540
#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
 
541
                wpa_s->new_connection = 0;
 
542
                wpa_s->reassociated_connection = 1;
 
543
                wpa_drv_set_operstate(wpa_s, 1);
 
544
                wpa_s->after_wps = 0;
 
545
        } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
 
546
                   state == WPA_ASSOCIATED) {
 
547
                wpa_s->new_connection = 1;
 
548
                wpa_drv_set_operstate(wpa_s, 0);
 
549
        }
 
550
        wpa_s->wpa_state = state;
 
551
 
 
552
        if (wpa_s->wpa_state != old_state)
 
553
                wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
 
554
}
 
555
 
 
556
 
 
557
void wpa_supplicant_terminate_proc(struct wpa_global *global)
 
558
{
 
559
        int pending = 0;
 
560
#ifdef CONFIG_WPS
 
561
        struct wpa_supplicant *wpa_s = global->ifaces;
 
562
        while (wpa_s) {
 
563
                if (wpas_wps_terminate_pending(wpa_s) == 1)
 
564
                        pending = 1;
 
565
                wpa_s = wpa_s->next;
 
566
        }
 
567
#endif /* CONFIG_WPS */
 
568
        if (pending)
 
569
                return;
 
570
        eloop_terminate();
 
571
}
 
572
 
 
573
 
 
574
static void wpa_supplicant_terminate(int sig, void *signal_ctx)
 
575
{
 
576
        struct wpa_global *global = signal_ctx;
 
577
        struct wpa_supplicant *wpa_s;
 
578
        for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
 
579
                wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
 
580
                        "received", sig);
 
581
        }
 
582
        wpa_supplicant_terminate_proc(global);
 
583
}
 
584
 
 
585
 
 
586
static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
 
587
{
 
588
        enum wpa_states old_state = wpa_s->wpa_state;
 
589
 
 
590
        wpa_s->pairwise_cipher = 0;
 
591
        wpa_s->group_cipher = 0;
 
592
        wpa_s->mgmt_group_cipher = 0;
 
593
        wpa_s->key_mgmt = 0;
 
594
        wpa_s->wpa_state = WPA_DISCONNECTED;
 
595
 
 
596
        if (wpa_s->wpa_state != old_state)
 
597
                wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
 
598
}
 
599
 
 
600
 
 
601
/**
 
602
 * wpa_supplicant_reload_configuration - Reload configuration data
 
603
 * @wpa_s: Pointer to wpa_supplicant data
 
604
 * Returns: 0 on success or -1 if configuration parsing failed
 
605
 *
 
606
 * This function can be used to request that the configuration data is reloaded
 
607
 * (e.g., after configuration file change). This function is reloading
 
608
 * configuration only for one interface, so this may need to be called multiple
 
609
 * times if %wpa_supplicant is controlling multiple interfaces and all
 
610
 * interfaces need reconfiguration.
 
611
 */
 
612
int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
 
613
{
 
614
        struct wpa_config *conf;
 
615
        struct wpa_ssid *old_ssid;
 
616
        int reconf_ctrl;
 
617
        int old_ap_scan;
 
618
 
 
619
        if (wpa_s->confname == NULL)
 
620
                return -1;
 
621
        conf = wpa_config_read(wpa_s->confname);
 
622
        if (conf == NULL) {
 
623
                wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
 
624
                        "file '%s' - exiting", wpa_s->confname);
 
625
                return -1;
 
626
        }
 
627
 
 
628
        reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
 
629
                || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
 
630
                    os_strcmp(conf->ctrl_interface,
 
631
                              wpa_s->conf->ctrl_interface) != 0);
 
632
 
 
633
        if (reconf_ctrl && wpa_s->ctrl_iface) {
 
634
                wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
 
635
                wpa_s->ctrl_iface = NULL;
 
636
        }
 
637
 
 
638
        eapol_sm_invalidate_cached_session(wpa_s->eapol);
 
639
        old_ssid = wpa_s->current_ssid;
 
640
        wpa_s->current_ssid = NULL;
 
641
        if (old_ssid != wpa_s->current_ssid)
 
642
                wpas_notify_network_changed(wpa_s);
 
643
 
 
644
        /*
 
645
         * TODO: should notify EAPOL SM about changes in opensc_engine_path,
 
646
         * pkcs11_engine_path, pkcs11_module_path.
 
647
         */
 
648
        if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
 
649
                /*
 
650
                 * Clear forced success to clear EAP state for next
 
651
                 * authentication.
 
652
                 */
 
653
                eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
 
654
        }
 
655
        eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
 
656
        wpa_sm_set_config(wpa_s->wpa, NULL);
 
657
        wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
 
658
        rsn_preauth_deinit(wpa_s->wpa);
 
659
 
 
660
        old_ap_scan = wpa_s->conf->ap_scan;
 
661
        wpa_config_free(wpa_s->conf);
 
662
        wpa_s->conf = conf;
 
663
        if (old_ap_scan != wpa_s->conf->ap_scan)
 
664
                wpas_notify_ap_scan_changed(wpa_s);
 
665
 
 
666
        if (reconf_ctrl)
 
667
                wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
 
668
 
 
669
        wpa_supplicant_clear_status(wpa_s);
 
670
        wpa_s->reassociate = 1;
 
671
        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
672
        wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
 
673
        return 0;
 
674
}
 
675
 
 
676
 
 
677
static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
 
678
{
 
679
        struct wpa_global *global = signal_ctx;
 
680
        struct wpa_supplicant *wpa_s;
 
681
        wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
 
682
        for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
 
683
                if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
 
684
                        wpa_supplicant_terminate_proc(global);
 
685
                }
 
686
        }
 
687
}
 
688
 
 
689
 
 
690
enum wpa_cipher cipher_suite2driver(int cipher)
 
691
{
 
692
        switch (cipher) {
 
693
        case WPA_CIPHER_NONE:
 
694
                return CIPHER_NONE;
 
695
        case WPA_CIPHER_WEP40:
 
696
                return CIPHER_WEP40;
 
697
        case WPA_CIPHER_WEP104:
 
698
                return CIPHER_WEP104;
 
699
        case WPA_CIPHER_CCMP:
 
700
                return CIPHER_CCMP;
 
701
        case WPA_CIPHER_TKIP:
 
702
        default:
 
703
                return CIPHER_TKIP;
 
704
        }
 
705
}
 
706
 
 
707
 
 
708
enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
 
709
{
 
710
        switch (key_mgmt) {
 
711
        case WPA_KEY_MGMT_NONE:
 
712
                return KEY_MGMT_NONE;
 
713
        case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
 
714
                return KEY_MGMT_802_1X_NO_WPA;
 
715
        case WPA_KEY_MGMT_IEEE8021X:
 
716
                return KEY_MGMT_802_1X;
 
717
        case WPA_KEY_MGMT_WPA_NONE:
 
718
                return KEY_MGMT_WPA_NONE;
 
719
        case WPA_KEY_MGMT_FT_IEEE8021X:
 
720
                return KEY_MGMT_FT_802_1X;
 
721
        case WPA_KEY_MGMT_FT_PSK:
 
722
                return KEY_MGMT_FT_PSK;
 
723
        case WPA_KEY_MGMT_IEEE8021X_SHA256:
 
724
                return KEY_MGMT_802_1X_SHA256;
 
725
        case WPA_KEY_MGMT_PSK_SHA256:
 
726
                return KEY_MGMT_PSK_SHA256;
 
727
        case WPA_KEY_MGMT_WPS:
 
728
                return KEY_MGMT_WPS;
 
729
        case WPA_KEY_MGMT_PSK:
 
730
        default:
 
731
                return KEY_MGMT_PSK;
 
732
        }
 
733
}
 
734
 
 
735
 
 
736
static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
 
737
                                         struct wpa_ssid *ssid,
 
738
                                         struct wpa_ie_data *ie)
 
739
{
 
740
        int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
 
741
        if (ret) {
 
742
                if (ret == -2) {
 
743
                        wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
 
744
                                "from association info");
 
745
                }
 
746
                return -1;
 
747
        }
 
748
 
 
749
        wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
 
750
                   "suites");
 
751
        if (!(ie->group_cipher & ssid->group_cipher)) {
 
752
                wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
 
753
                        "cipher 0x%x (mask 0x%x) - reject",
 
754
                        ie->group_cipher, ssid->group_cipher);
 
755
                return -1;
 
756
        }
 
757
        if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
 
758
                wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
 
759
                        "cipher 0x%x (mask 0x%x) - reject",
 
760
                        ie->pairwise_cipher, ssid->pairwise_cipher);
 
761
                return -1;
 
762
        }
 
763
        if (!(ie->key_mgmt & ssid->key_mgmt)) {
 
764
                wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
 
765
                        "management 0x%x (mask 0x%x) - reject",
 
766
                        ie->key_mgmt, ssid->key_mgmt);
 
767
                return -1;
 
768
        }
 
769
 
 
770
#ifdef CONFIG_IEEE80211W
 
771
        if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
 
772
            ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
 
773
                wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
 
774
                        "that does not support management frame protection - "
 
775
                        "reject");
 
776
                return -1;
 
777
        }
 
778
#endif /* CONFIG_IEEE80211W */
 
779
 
 
780
        return 0;
 
781
}
 
782
 
 
783
 
 
784
/**
 
785
 * wpa_supplicant_set_suites - Set authentication and encryption parameters
 
786
 * @wpa_s: Pointer to wpa_supplicant data
 
787
 * @bss: Scan results for the selected BSS, or %NULL if not available
 
788
 * @ssid: Configuration data for the selected network
 
789
 * @wpa_ie: Buffer for the WPA/RSN IE
 
790
 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
 
791
 * used buffer length in case the functions returns success.
 
792
 * Returns: 0 on success or -1 on failure
 
793
 *
 
794
 * This function is used to configure authentication and encryption parameters
 
795
 * based on the network configuration and scan result for the selected BSS (if
 
796
 * available).
 
797
 */
 
798
int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
 
799
                              struct wpa_bss *bss, struct wpa_ssid *ssid,
 
800
                              u8 *wpa_ie, size_t *wpa_ie_len)
 
801
{
 
802
        struct wpa_ie_data ie;
 
803
        int sel, proto;
 
804
        const u8 *bss_wpa, *bss_rsn;
 
805
 
 
806
        if (bss) {
 
807
                bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
 
808
                bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
 
809
        } else
 
810
                bss_wpa = bss_rsn = NULL;
 
811
 
 
812
        if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
 
813
            wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
 
814
            (ie.group_cipher & ssid->group_cipher) &&
 
815
            (ie.pairwise_cipher & ssid->pairwise_cipher) &&
 
816
            (ie.key_mgmt & ssid->key_mgmt)) {
 
817
                wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
 
818
                proto = WPA_PROTO_RSN;
 
819
        } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
 
820
                   wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
 
821
                   (ie.group_cipher & ssid->group_cipher) &&
 
822
                   (ie.pairwise_cipher & ssid->pairwise_cipher) &&
 
823
                   (ie.key_mgmt & ssid->key_mgmt)) {
 
824
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
 
825
                proto = WPA_PROTO_WPA;
 
826
        } else if (bss) {
 
827
                wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
 
828
                return -1;
 
829
        } else {
 
830
                if (ssid->proto & WPA_PROTO_RSN)
 
831
                        proto = WPA_PROTO_RSN;
 
832
                else
 
833
                        proto = WPA_PROTO_WPA;
 
834
                if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
 
835
                        os_memset(&ie, 0, sizeof(ie));
 
836
                        ie.group_cipher = ssid->group_cipher;
 
837
                        ie.pairwise_cipher = ssid->pairwise_cipher;
 
838
                        ie.key_mgmt = ssid->key_mgmt;
 
839
#ifdef CONFIG_IEEE80211W
 
840
                        ie.mgmt_group_cipher =
 
841
                                ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
 
842
                                WPA_CIPHER_AES_128_CMAC : 0;
 
843
#endif /* CONFIG_IEEE80211W */
 
844
                        wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
 
845
                                   "on configuration");
 
846
                } else
 
847
                        proto = ie.proto;
 
848
        }
 
849
 
 
850
        wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
 
851
                   "pairwise %d key_mgmt %d proto %d",
 
852
                   ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
 
853
#ifdef CONFIG_IEEE80211W
 
854
        if (ssid->ieee80211w) {
 
855
                wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
 
856
                           ie.mgmt_group_cipher);
 
857
        }
 
858
#endif /* CONFIG_IEEE80211W */
 
859
 
 
860
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
 
861
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
 
862
                         !!(ssid->proto & WPA_PROTO_RSN));
 
863
 
 
864
        if (bss || !wpa_s->ap_ies_from_associnfo) {
 
865
                if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
 
866
                                         bss_wpa ? 2 + bss_wpa[1] : 0) ||
 
867
                    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
 
868
                                         bss_rsn ? 2 + bss_rsn[1] : 0))
 
869
                        return -1;
 
870
        }
 
871
 
 
872
        sel = ie.group_cipher & ssid->group_cipher;
 
873
        if (sel & WPA_CIPHER_CCMP) {
 
874
                wpa_s->group_cipher = WPA_CIPHER_CCMP;
 
875
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
 
876
        } else if (sel & WPA_CIPHER_TKIP) {
 
877
                wpa_s->group_cipher = WPA_CIPHER_TKIP;
 
878
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
 
879
        } else if (sel & WPA_CIPHER_WEP104) {
 
880
                wpa_s->group_cipher = WPA_CIPHER_WEP104;
 
881
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
 
882
        } else if (sel & WPA_CIPHER_WEP40) {
 
883
                wpa_s->group_cipher = WPA_CIPHER_WEP40;
 
884
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
 
885
        } else {
 
886
                wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
 
887
                return -1;
 
888
        }
 
889
 
 
890
        sel = ie.pairwise_cipher & ssid->pairwise_cipher;
 
891
        if (sel & WPA_CIPHER_CCMP) {
 
892
                wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
 
893
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
 
894
        } else if (sel & WPA_CIPHER_TKIP) {
 
895
                wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
 
896
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
 
897
        } else if (sel & WPA_CIPHER_NONE) {
 
898
                wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
 
899
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
 
900
        } else {
 
901
                wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
 
902
                           "cipher.");
 
903
                return -1;
 
904
        }
 
905
 
 
906
        sel = ie.key_mgmt & ssid->key_mgmt;
 
907
        if (0) {
 
908
#ifdef CONFIG_IEEE80211R
 
909
        } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
 
910
                wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
 
911
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
 
912
        } else if (sel & WPA_KEY_MGMT_FT_PSK) {
 
913
                wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
 
914
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
 
915
#endif /* CONFIG_IEEE80211R */
 
916
#ifdef CONFIG_IEEE80211W
 
917
        } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
 
918
                wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
 
919
                wpa_msg(wpa_s, MSG_DEBUG,
 
920
                        "WPA: using KEY_MGMT 802.1X with SHA256");
 
921
        } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
 
922
                wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
 
923
                wpa_msg(wpa_s, MSG_DEBUG,
 
924
                        "WPA: using KEY_MGMT PSK with SHA256");
 
925
#endif /* CONFIG_IEEE80211W */
 
926
        } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
 
927
                wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
 
928
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
 
929
        } else if (sel & WPA_KEY_MGMT_PSK) {
 
930
                wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
 
931
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
 
932
        } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
 
933
                wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
 
934
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
 
935
        } else {
 
936
                wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
 
937
                           "key management type.");
 
938
                return -1;
 
939
        }
 
940
 
 
941
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
 
942
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
 
943
                         wpa_s->pairwise_cipher);
 
944
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
 
945
 
 
946
#ifdef CONFIG_IEEE80211W
 
947
        sel = ie.mgmt_group_cipher;
 
948
        if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
 
949
            !(ie.capabilities & WPA_CAPABILITY_MFPC))
 
950
                sel = 0;
 
951
        if (sel & WPA_CIPHER_AES_128_CMAC) {
 
952
                wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
 
953
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
 
954
                        "AES-128-CMAC");
 
955
        } else {
 
956
                wpa_s->mgmt_group_cipher = 0;
 
957
                wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
 
958
        }
 
959
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
 
960
                         wpa_s->mgmt_group_cipher);
 
961
        wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
 
962
#endif /* CONFIG_IEEE80211W */
 
963
 
 
964
        if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
 
965
                wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
 
966
                return -1;
 
967
        }
 
968
 
 
969
        if (ssid->key_mgmt &
 
970
            (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
 
971
                wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
 
972
        else
 
973
                wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
 
974
 
 
975
        return 0;
 
976
}
 
977
 
 
978
 
 
979
/**
 
980
 * wpa_supplicant_associate - Request association
 
981
 * @wpa_s: Pointer to wpa_supplicant data
 
982
 * @bss: Scan results for the selected BSS, or %NULL if not available
 
983
 * @ssid: Configuration data for the selected network
 
984
 *
 
985
 * This function is used to request %wpa_supplicant to associate with a BSS.
 
986
 */
 
987
void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
 
988
                              struct wpa_bss *bss, struct wpa_ssid *ssid)
 
989
{
 
990
        u8 wpa_ie[80];
 
991
        size_t wpa_ie_len;
 
992
        int use_crypt, ret, i, bssid_changed;
 
993
        int algs = WPA_AUTH_ALG_OPEN;
 
994
        enum wpa_cipher cipher_pairwise, cipher_group;
 
995
        struct wpa_driver_associate_params params;
 
996
        int wep_keys_set = 0;
 
997
        struct wpa_driver_capa capa;
 
998
        int assoc_failed = 0;
 
999
        struct wpa_ssid *old_ssid;
 
1000
 
 
1001
        if (ssid->mode == WPAS_MODE_AP) {
 
1002
#ifdef CONFIG_AP
 
1003
                if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
 
1004
                        wpa_printf(MSG_INFO, "Driver does not support AP "
 
1005
                                   "mode");
 
1006
                        return;
 
1007
                }
 
1008
                wpa_supplicant_create_ap(wpa_s, ssid);
 
1009
                wpa_s->current_bss = bss;
 
1010
#else /* CONFIG_AP */
 
1011
                wpa_printf(MSG_ERROR, "AP mode support not included in the "
 
1012
                           "build");
 
1013
#endif /* CONFIG_AP */
 
1014
                return;
 
1015
        }
 
1016
 
 
1017
        if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
 
1018
            ssid->mode == IEEE80211_MODE_INFRA) {
 
1019
                sme_authenticate(wpa_s, bss, ssid);
 
1020
                return;
 
1021
        }
 
1022
 
 
1023
        wpa_s->reassociate = 0;
 
1024
        if (bss) {
 
1025
#ifdef CONFIG_IEEE80211R
 
1026
                const u8 *ie, *md = NULL;
 
1027
#endif /* CONFIG_IEEE80211R */
 
1028
                wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
 
1029
                        " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
 
1030
                        wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
 
1031
                bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
 
1032
                os_memset(wpa_s->bssid, 0, ETH_ALEN);
 
1033
                os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
 
1034
                if (bssid_changed)
 
1035
                        wpas_notify_bssid_changed(wpa_s);
 
1036
#ifdef CONFIG_IEEE80211R
 
1037
                ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
 
1038
                if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
 
1039
                        md = ie + 2;
 
1040
                wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
 
1041
                if (md) {
 
1042
                        /* Prepare for the next transition */
 
1043
                        wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
 
1044
                }
 
1045
#endif /* CONFIG_IEEE80211R */
 
1046
#ifdef CONFIG_WPS
 
1047
        } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
 
1048
                   wpa_s->conf->ap_scan == 2 &&
 
1049
                   (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
 
1050
                /* Use ap_scan==1 style network selection to find the network
 
1051
                 */
 
1052
                wpa_s->scan_req = 2;
 
1053
                wpa_s->reassociate = 1;
 
1054
                wpa_supplicant_req_scan(wpa_s, 0, 0);
 
1055
                return;
 
1056
#endif /* CONFIG_WPS */
 
1057
        } else {
 
1058
                wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
 
1059
                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
 
1060
                os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
 
1061
        }
 
1062
        wpa_supplicant_cancel_scan(wpa_s);
 
1063
 
 
1064
        /* Starting new association, so clear the possibly used WPA IE from the
 
1065
         * previous association. */
 
1066
        wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
 
1067
 
 
1068
#ifdef IEEE8021X_EAPOL
 
1069
        if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
 
1070
                if (ssid->leap) {
 
1071
                        if (ssid->non_leap == 0)
 
1072
                                algs = WPA_AUTH_ALG_LEAP;
 
1073
                        else
 
1074
                                algs |= WPA_AUTH_ALG_LEAP;
 
1075
                }
 
1076
        }
 
1077
#endif /* IEEE8021X_EAPOL */
 
1078
        wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
 
1079
        if (ssid->auth_alg) {
 
1080
                algs = ssid->auth_alg;
 
1081
                wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
 
1082
                           algs);
 
1083
        }
 
1084
 
 
1085
        if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
 
1086
                    wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
 
1087
            (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
 
1088
                               WPA_KEY_MGMT_FT_IEEE8021X |
 
1089
                               WPA_KEY_MGMT_FT_PSK |
 
1090
                               WPA_KEY_MGMT_IEEE8021X_SHA256 |
 
1091
                               WPA_KEY_MGMT_PSK_SHA256))) {
 
1092
                int try_opportunistic;
 
1093
                try_opportunistic = ssid->proactive_key_caching &&
 
1094
                        (ssid->proto & WPA_PROTO_RSN);
 
1095
                if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
 
1096
                                            wpa_s->current_ssid,
 
1097
                                            try_opportunistic) == 0)
 
1098
                        eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
 
1099
                wpa_ie_len = sizeof(wpa_ie);
 
1100
                if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
 
1101
                                              wpa_ie, &wpa_ie_len)) {
 
1102
                        wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
 
1103
                                   "management and encryption suites");
 
1104
                        return;
 
1105
                }
 
1106
        } else if (ssid->key_mgmt &
 
1107
                   (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
 
1108
                    WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
 
1109
                    WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
 
1110
                    WPA_KEY_MGMT_IEEE8021X_SHA256)) {
 
1111
                wpa_ie_len = sizeof(wpa_ie);
 
1112
                if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
 
1113
                                              wpa_ie, &wpa_ie_len)) {
 
1114
                        wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
 
1115
                                   "management and encryption suites (no scan "
 
1116
                                   "results)");
 
1117
                        return;
 
1118
                }
 
1119
#ifdef CONFIG_WPS
 
1120
        } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
 
1121
                struct wpabuf *wps_ie;
 
1122
                wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
 
1123
                if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
 
1124
                        wpa_ie_len = wpabuf_len(wps_ie);
 
1125
                        os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
 
1126
                } else
 
1127
                        wpa_ie_len = 0;
 
1128
                wpabuf_free(wps_ie);
 
1129
                wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
 
1130
#endif /* CONFIG_WPS */
 
1131
        } else {
 
1132
                wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
 
1133
                wpa_ie_len = 0;
 
1134
        }
 
1135
 
 
1136
        wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
 
1137
        use_crypt = 1;
 
1138
        cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
 
1139
        cipher_group = cipher_suite2driver(wpa_s->group_cipher);
 
1140
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
 
1141
            wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
 
1142
                if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
 
1143
                        use_crypt = 0;
 
1144
                if (wpa_set_wep_keys(wpa_s, ssid)) {
 
1145
                        use_crypt = 1;
 
1146
                        wep_keys_set = 1;
 
1147
                }
 
1148
        }
 
1149
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
 
1150
                use_crypt = 0;
 
1151
 
 
1152
#ifdef IEEE8021X_EAPOL
 
1153
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
 
1154
                if ((ssid->eapol_flags &
 
1155
                     (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
 
1156
                      EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
 
1157
                    !wep_keys_set) {
 
1158
                        use_crypt = 0;
 
1159
                } else {
 
1160
                        /* Assume that dynamic WEP-104 keys will be used and
 
1161
                         * set cipher suites in order for drivers to expect
 
1162
                         * encryption. */
 
1163
                        cipher_pairwise = cipher_group = CIPHER_WEP104;
 
1164
                }
 
1165
        }
 
1166
#endif /* IEEE8021X_EAPOL */
 
1167
 
 
1168
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
 
1169
                /* Set the key before (and later after) association */
 
1170
                wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
 
1171
        }
 
1172
 
 
1173
        wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
 
1174
        os_memset(&params, 0, sizeof(params));
 
1175
        if (bss) {
 
1176
                params.bssid = bss->bssid;
 
1177
                params.ssid = bss->ssid;
 
1178
                params.ssid_len = bss->ssid_len;
 
1179
                params.freq = bss->freq;
 
1180
        } else {
 
1181
                params.ssid = ssid->ssid;
 
1182
                params.ssid_len = ssid->ssid_len;
 
1183
        }
 
1184
        if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
 
1185
            params.freq == 0)
 
1186
                params.freq = ssid->frequency; /* Initial channel for IBSS */
 
1187
        params.wpa_ie = wpa_ie;
 
1188
        params.wpa_ie_len = wpa_ie_len;
 
1189
        params.pairwise_suite = cipher_pairwise;
 
1190
        params.group_suite = cipher_group;
 
1191
        params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
 
1192
        params.auth_alg = algs;
 
1193
        params.mode = ssid->mode;
 
1194
        for (i = 0; i < NUM_WEP_KEYS; i++) {
 
1195
                if (ssid->wep_key_len[i])
 
1196
                        params.wep_key[i] = ssid->wep_key[i];
 
1197
                params.wep_key_len[i] = ssid->wep_key_len[i];
 
1198
        }
 
1199
        params.wep_tx_keyidx = ssid->wep_tx_keyidx;
 
1200
 
 
1201
        if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
 
1202
            (params.key_mgmt_suite == KEY_MGMT_PSK ||
 
1203
             params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
 
1204
                params.passphrase = ssid->passphrase;
 
1205
                if (ssid->psk_set)
 
1206
                        params.psk = ssid->psk;
 
1207
        }
 
1208
 
 
1209
        params.drop_unencrypted = use_crypt;
 
1210
 
 
1211
#ifdef CONFIG_IEEE80211W
 
1212
        params.mgmt_frame_protection = ssid->ieee80211w;
 
1213
        if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
 
1214
                const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
 
1215
                struct wpa_ie_data ie;
 
1216
                if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
 
1217
                    ie.capabilities &
 
1218
                    (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
 
1219
                        wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
 
1220
                                   "require MFP");
 
1221
                        params.mgmt_frame_protection =
 
1222
                                MGMT_FRAME_PROTECTION_REQUIRED;
 
1223
                }
 
1224
        }
 
1225
#endif /* CONFIG_IEEE80211W */
 
1226
 
 
1227
        if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
 
1228
                ret = ieee80211_sta_associate(wpa_s, &params);
 
1229
        else
 
1230
                ret = wpa_drv_associate(wpa_s, &params);
 
1231
        if (ret < 0) {
 
1232
                wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
 
1233
                        "failed");
 
1234
                /* try to continue anyway; new association will be tried again
 
1235
                 * after timeout */
 
1236
                assoc_failed = 1;
 
1237
        }
 
1238
 
 
1239
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
 
1240
                /* Set the key after the association just in case association
 
1241
                 * cleared the previously configured key. */
 
1242
                wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
 
1243
                /* No need to timeout authentication since there is no key
 
1244
                 * management. */
 
1245
                wpa_supplicant_cancel_auth_timeout(wpa_s);
 
1246
                wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
 
1247
#ifdef CONFIG_IBSS_RSN
 
1248
        } else if (ssid->mode == WPAS_MODE_IBSS &&
 
1249
                   wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
 
1250
                   wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
 
1251
                ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
 
1252
                /*
 
1253
                 * RSN IBSS authentication is per-STA and we can disable the
 
1254
                 * per-BSSID authentication.
 
1255
                 */
 
1256
                wpa_supplicant_cancel_auth_timeout(wpa_s);
 
1257
#endif /* CONFIG_IBSS_RSN */
 
1258
        } else {
 
1259
                /* Timeout for IEEE 802.11 authentication and association */
 
1260
                int timeout = 60;
 
1261
 
 
1262
                if (assoc_failed) {
 
1263
                        /* give IBSS a bit more time */
 
1264
                        timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
 
1265
                } else if (wpa_s->conf->ap_scan == 1) {
 
1266
                        /* give IBSS a bit more time */
 
1267
                        timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
 
1268
                }
 
1269
                wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
 
1270
        }
 
1271
 
 
1272
        if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
 
1273
            capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
 
1274
                /* Set static WEP keys again */
 
1275
                wpa_set_wep_keys(wpa_s, ssid);
 
1276
        }
 
1277
 
 
1278
        if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
 
1279
                /*
 
1280
                 * Do not allow EAP session resumption between different
 
1281
                 * network configurations.
 
1282
                 */
 
1283
                eapol_sm_invalidate_cached_session(wpa_s->eapol);
 
1284
        }
 
1285
        old_ssid = wpa_s->current_ssid;
 
1286
        wpa_s->current_ssid = ssid;
 
1287
        wpa_s->current_bss = bss;
 
1288
        wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
 
1289
        wpa_supplicant_initiate_eapol(wpa_s);
 
1290
        if (old_ssid != wpa_s->current_ssid)
 
1291
                wpas_notify_network_changed(wpa_s);
 
1292
}
 
1293
 
 
1294
 
 
1295
/**
 
1296
 * wpa_supplicant_disassociate - Disassociate the current connection
 
1297
 * @wpa_s: Pointer to wpa_supplicant data
 
1298
 * @reason_code: IEEE 802.11 reason code for the disassociate frame
 
1299
 *
 
1300
 * This function is used to request %wpa_supplicant to disassociate with the
 
1301
 * current AP.
 
1302
 */
 
1303
void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
 
1304
                                 int reason_code)
 
1305
{
 
1306
        struct wpa_ssid *old_ssid;
 
1307
        u8 *addr = NULL;
 
1308
 
 
1309
        if (!is_zero_ether_addr(wpa_s->bssid)) {
 
1310
                if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
 
1311
                        ieee80211_sta_disassociate(wpa_s, reason_code);
 
1312
                else
 
1313
                        wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
 
1314
                addr = wpa_s->bssid;
 
1315
        }
 
1316
        wpa_clear_keys(wpa_s, addr);
 
1317
        wpa_supplicant_mark_disassoc(wpa_s);
 
1318
        old_ssid = wpa_s->current_ssid;
 
1319
        wpa_s->current_ssid = NULL;
 
1320
        wpa_s->current_bss = NULL;
 
1321
        wpa_sm_set_config(wpa_s->wpa, NULL);
 
1322
        eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
 
1323
        if (old_ssid != wpa_s->current_ssid)
 
1324
                wpas_notify_network_changed(wpa_s);
 
1325
        eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
 
1326
}
 
1327
 
 
1328
 
 
1329
/**
 
1330
 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
 
1331
 * @wpa_s: Pointer to wpa_supplicant data
 
1332
 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
 
1333
 *
 
1334
 * This function is used to request %wpa_supplicant to deauthenticate from the
 
1335
 * current AP.
 
1336
 */
 
1337
void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
 
1338
                                   int reason_code)
 
1339
{
 
1340
        struct wpa_ssid *old_ssid;
 
1341
        u8 *addr = NULL;
 
1342
 
 
1343
        if (!is_zero_ether_addr(wpa_s->bssid)) {
 
1344
                if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
 
1345
                        ieee80211_sta_deauthenticate(wpa_s, reason_code);
 
1346
                else
 
1347
                        wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
 
1348
                                               reason_code);
 
1349
                addr = wpa_s->bssid;
 
1350
        }
 
1351
        wpa_clear_keys(wpa_s, addr);
 
1352
        wpa_supplicant_mark_disassoc(wpa_s);
 
1353
        old_ssid = wpa_s->current_ssid;
 
1354
        wpa_s->current_ssid = NULL;
 
1355
        wpa_s->current_bss = NULL;
 
1356
        wpa_sm_set_config(wpa_s->wpa, NULL);
 
1357
        eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
 
1358
        if (old_ssid != wpa_s->current_ssid)
 
1359
                wpas_notify_network_changed(wpa_s);
 
1360
        eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
 
1361
}
 
1362
 
 
1363
 
 
1364
/**
 
1365
 * wpa_supplicant_enable_network - Mark a configured network as enabled
 
1366
 * @wpa_s: wpa_supplicant structure for a network interface
 
1367
 * @ssid: wpa_ssid structure for a configured network or %NULL
 
1368
 *
 
1369
 * Enables the specified network or all networks if no network specified.
 
1370
 */
 
1371
void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
 
1372
                                   struct wpa_ssid *ssid)
 
1373
{
 
1374
        struct wpa_ssid *other_ssid;
 
1375
        int was_disabled;
 
1376
 
 
1377
        if (ssid == NULL) {
 
1378
                other_ssid = wpa_s->conf->ssid;
 
1379
                while (other_ssid) {
 
1380
                        if (other_ssid == wpa_s->current_ssid &&
 
1381
                            other_ssid->disabled)
 
1382
                                wpa_s->reassociate = 1;
 
1383
 
 
1384
                        was_disabled = other_ssid->disabled;
 
1385
 
 
1386
                        other_ssid->disabled = 0;
 
1387
 
 
1388
                        if (was_disabled != other_ssid->disabled)
 
1389
                                wpas_notify_network_enabled_changed(
 
1390
                                        wpa_s, other_ssid);
 
1391
 
 
1392
                        other_ssid = other_ssid->next;
 
1393
                }
 
1394
                if (wpa_s->reassociate)
 
1395
                        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
1396
        } else if (ssid->disabled) {
 
1397
                if (wpa_s->current_ssid == NULL) {
 
1398
                        /*
 
1399
                         * Try to reassociate since there is no current
 
1400
                         * configuration and a new network was made available.
 
1401
                         */
 
1402
                        wpa_s->reassociate = 1;
 
1403
                        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
1404
                }
 
1405
 
 
1406
                was_disabled = ssid->disabled;
 
1407
 
 
1408
                ssid->disabled = 0;
 
1409
 
 
1410
                if (was_disabled != ssid->disabled)
 
1411
                        wpas_notify_network_enabled_changed(wpa_s, ssid);
 
1412
        }
 
1413
}
 
1414
 
 
1415
 
 
1416
/**
 
1417
 * wpa_supplicant_disable_network - Mark a configured network as disabled
 
1418
 * @wpa_s: wpa_supplicant structure for a network interface
 
1419
 * @ssid: wpa_ssid structure for a configured network or %NULL
 
1420
 *
 
1421
 * Disables the specified network or all networks if no network specified.
 
1422
 */
 
1423
void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
 
1424
                                    struct wpa_ssid *ssid)
 
1425
{
 
1426
        struct wpa_ssid *other_ssid;
 
1427
        int was_disabled;
 
1428
 
 
1429
        if (ssid == NULL) {
 
1430
                other_ssid = wpa_s->conf->ssid;
 
1431
                while (other_ssid) {
 
1432
                        was_disabled = other_ssid->disabled;
 
1433
 
 
1434
                        other_ssid->disabled = 1;
 
1435
 
 
1436
                        if (was_disabled != other_ssid->disabled)
 
1437
                                wpas_notify_network_enabled_changed(
 
1438
                                        wpa_s, other_ssid);
 
1439
 
 
1440
                        other_ssid = other_ssid->next;
 
1441
                }
 
1442
                if (wpa_s->current_ssid)
 
1443
                        wpa_supplicant_disassociate(
 
1444
                                wpa_s, WLAN_REASON_DEAUTH_LEAVING);
 
1445
        } else {
 
1446
                if (ssid == wpa_s->current_ssid)
 
1447
                        wpa_supplicant_disassociate(
 
1448
                                wpa_s, WLAN_REASON_DEAUTH_LEAVING);
 
1449
 
 
1450
                was_disabled = ssid->disabled;
 
1451
 
 
1452
                ssid->disabled = 1;
 
1453
 
 
1454
                if (was_disabled != ssid->disabled)
 
1455
                        wpas_notify_network_enabled_changed(wpa_s, ssid);
 
1456
        }
 
1457
}
 
1458
 
 
1459
 
 
1460
/**
 
1461
 * wpa_supplicant_select_network - Attempt association with a network
 
1462
 * @wpa_s: wpa_supplicant structure for a network interface
 
1463
 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
 
1464
 */
 
1465
void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
 
1466
                                   struct wpa_ssid *ssid)
 
1467
{
 
1468
 
 
1469
        struct wpa_ssid *other_ssid;
 
1470
 
 
1471
        if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
 
1472
                wpa_supplicant_disassociate(
 
1473
                        wpa_s, WLAN_REASON_DEAUTH_LEAVING);
 
1474
 
 
1475
        /*
 
1476
         * Mark all other networks disabled or mark all networks enabled if no
 
1477
         * network specified.
 
1478
         */
 
1479
        other_ssid = wpa_s->conf->ssid;
 
1480
        while (other_ssid) {
 
1481
                int was_disabled = other_ssid->disabled;
 
1482
 
 
1483
                other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
 
1484
 
 
1485
                if (was_disabled != other_ssid->disabled)
 
1486
                        wpas_notify_network_enabled_changed(wpa_s, other_ssid);
 
1487
 
 
1488
                other_ssid = other_ssid->next;
 
1489
        }
 
1490
        wpa_s->disconnected = 0;
 
1491
        wpa_s->reassociate = 1;
 
1492
        wpa_supplicant_req_scan(wpa_s, 0, 0);
 
1493
 
 
1494
        if (ssid)
 
1495
                wpas_notify_network_selected(wpa_s, ssid);
 
1496
}
 
1497
 
 
1498
 
 
1499
/**
 
1500
 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
 
1501
 * @wpa_s: wpa_supplicant structure for a network interface
 
1502
 * @ap_scan: AP scan mode
 
1503
 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
 
1504
 *
 
1505
 */
 
1506
int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
 
1507
{
 
1508
 
 
1509
        int old_ap_scan;
 
1510
 
 
1511
        if (ap_scan < 0 || ap_scan > 2)
 
1512
                return -1;
 
1513
 
 
1514
        old_ap_scan = wpa_s->conf->ap_scan;
 
1515
        wpa_s->conf->ap_scan = ap_scan;
 
1516
 
 
1517
        if (old_ap_scan != wpa_s->conf->ap_scan)
 
1518
                wpas_notify_ap_scan_changed(wpa_s);
 
1519
 
 
1520
        return 0;
 
1521
}
 
1522
 
 
1523
 
 
1524
/**
 
1525
 * wpa_supplicant_set_debug_params - Set global debug params
 
1526
 * @global: wpa_global structure
 
1527
 * @debug_level: debug level
 
1528
 * @debug_timestamp: determines if show timestamp in debug data
 
1529
 * @debug_show_keys: determines if show keys in debug data
 
1530
 * Returns: 0 if succeed or -1 if debug_level has wrong value
 
1531
 */
 
1532
int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
 
1533
                                    int debug_timestamp, int debug_show_keys)
 
1534
{
 
1535
 
 
1536
        int old_level, old_timestamp, old_show_keys;
 
1537
 
 
1538
        /* check for allowed debuglevels */
 
1539
        if (debug_level != MSG_MSGDUMP &&
 
1540
            debug_level != MSG_DEBUG &&
 
1541
            debug_level != MSG_INFO &&
 
1542
            debug_level != MSG_WARNING &&
 
1543
            debug_level != MSG_ERROR)
 
1544
                return -1;
 
1545
 
 
1546
        old_level = wpa_debug_level;
 
1547
        old_timestamp = wpa_debug_timestamp;
 
1548
        old_show_keys = wpa_debug_show_keys;
 
1549
 
 
1550
        wpa_debug_level = debug_level;
 
1551
        wpa_debug_timestamp = debug_timestamp ? 1 : 0;
 
1552
        wpa_debug_show_keys = debug_show_keys ? 1 : 0;
 
1553
 
 
1554
        if (wpa_debug_level != old_level)
 
1555
                wpas_notify_debug_level_changed(global);
 
1556
        if (wpa_debug_timestamp != old_timestamp)
 
1557
                wpas_notify_debug_timestamp_changed(global);
 
1558
        if (wpa_debug_show_keys != old_show_keys)
 
1559
                wpas_notify_debug_show_keys_changed(global);
 
1560
 
 
1561
        return 0;
 
1562
}
 
1563
 
 
1564
 
 
1565
/**
 
1566
 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
 
1567
 * @wpa_s: Pointer to wpa_supplicant data
 
1568
 * Returns: A pointer to the current network structure or %NULL on failure
 
1569
 */
 
1570
struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
 
1571
{
 
1572
        struct wpa_ssid *entry;
 
1573
        u8 ssid[MAX_SSID_LEN];
 
1574
        int res;
 
1575
        size_t ssid_len;
 
1576
        u8 bssid[ETH_ALEN];
 
1577
        int wired;
 
1578
 
 
1579
        if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
 
1580
                if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
 
1581
                        wpa_printf(MSG_WARNING, "Could not read SSID from "
 
1582
                                   "MLME.");
 
1583
                        return NULL;
 
1584
                }
 
1585
        } else {
 
1586
                res = wpa_drv_get_ssid(wpa_s, ssid);
 
1587
                if (res < 0) {
 
1588
                        wpa_printf(MSG_WARNING, "Could not read SSID from "
 
1589
                                   "driver.");
 
1590
                        return NULL;
 
1591
                }
 
1592
                ssid_len = res;
 
1593
        }
 
1594
 
 
1595
        if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
 
1596
                os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
 
1597
        else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
 
1598
                wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
 
1599
                return NULL;
 
1600
        }
 
1601
 
 
1602
        wired = wpa_s->conf->ap_scan == 0 &&
 
1603
                (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
 
1604
 
 
1605
        entry = wpa_s->conf->ssid;
 
1606
        while (entry) {
 
1607
                if (!entry->disabled &&
 
1608
                    ((ssid_len == entry->ssid_len &&
 
1609
                      os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
 
1610
                    (!entry->bssid_set ||
 
1611
                     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
 
1612
                        return entry;
 
1613
#ifdef CONFIG_WPS
 
1614
                if (!entry->disabled &&
 
1615
                    (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
 
1616
                    (entry->ssid == NULL || entry->ssid_len == 0) &&
 
1617
                    (!entry->bssid_set ||
 
1618
                     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
 
1619
                        return entry;
 
1620
#endif /* CONFIG_WPS */
 
1621
                entry = entry->next;
 
1622
        }
 
1623
 
 
1624
        return NULL;
 
1625
}
 
1626
 
 
1627
 
 
1628
static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
 
1629
                                     const char *name)
 
1630
{
 
1631
        int i;
 
1632
        size_t len;
 
1633
        const char *pos;
 
1634
 
 
1635
        if (wpa_s == NULL)
 
1636
                return -1;
 
1637
 
 
1638
        if (wpa_drivers[0] == NULL) {
 
1639
                wpa_printf(MSG_ERROR, "No driver interfaces build into "
 
1640
                           "wpa_supplicant.");
 
1641
                return -1;
 
1642
        }
 
1643
 
 
1644
        if (name == NULL) {
 
1645
                /* default to first driver in the list */
 
1646
                wpa_s->driver = wpa_drivers[0];
 
1647
                wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
 
1648
                return 0;
 
1649
        }
 
1650
 
 
1651
        pos = os_strchr(name, ',');
 
1652
        if (pos)
 
1653
                len = pos - name;
 
1654
        else
 
1655
                len = os_strlen(name);
 
1656
        for (i = 0; wpa_drivers[i]; i++) {
 
1657
                if (os_strlen(wpa_drivers[i]->name) == len &&
 
1658
                    os_strncmp(name, wpa_drivers[i]->name, len) ==
 
1659
                    0) {
 
1660
                        wpa_s->driver = wpa_drivers[i];
 
1661
                        wpa_s->global_drv_priv = wpa_s->global->drv_priv[i];
 
1662
                        return 0;
 
1663
                }
 
1664
        }
 
1665
 
 
1666
        wpa_printf(MSG_ERROR, "Unsupported driver '%s'.", name);
 
1667
        return -1;
 
1668
}
 
1669
 
 
1670
 
 
1671
/**
 
1672
 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
 
1673
 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
 
1674
 *      with struct wpa_driver_ops::init()
 
1675
 * @src_addr: Source address of the EAPOL frame
 
1676
 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
 
1677
 * @len: Length of the EAPOL data
 
1678
 *
 
1679
 * This function is called for each received EAPOL frame. Most driver
 
1680
 * interfaces rely on more generic OS mechanism for receiving frames through
 
1681
 * l2_packet, but if such a mechanism is not available, the driver wrapper may
 
1682
 * take care of received EAPOL frames and deliver them to the core supplicant
 
1683
 * code by calling this function.
 
1684
 */
 
1685
void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
 
1686
                             const u8 *buf, size_t len)
 
1687
{
 
1688
        struct wpa_supplicant *wpa_s = ctx;
 
1689
 
 
1690
        wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
 
1691
        wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
 
1692
 
 
1693
        if (wpa_s->wpa_state < WPA_ASSOCIATED) {
 
1694
                /*
 
1695
                 * There is possible race condition between receiving the
 
1696
                 * association event and the EAPOL frame since they are coming
 
1697
                 * through different paths from the driver. In order to avoid
 
1698
                 * issues in trying to process the EAPOL frame before receiving
 
1699
                 * association information, lets queue it for processing until
 
1700
                 * the association event is received.
 
1701
                 */
 
1702
                wpa_printf(MSG_DEBUG, "Not associated - Delay processing of "
 
1703
                           "received EAPOL frame");
 
1704
                wpabuf_free(wpa_s->pending_eapol_rx);
 
1705
                wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
 
1706
                if (wpa_s->pending_eapol_rx) {
 
1707
                        os_get_time(&wpa_s->pending_eapol_rx_time);
 
1708
                        os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
 
1709
                                  ETH_ALEN);
 
1710
                }
 
1711
                return;
 
1712
        }
 
1713
 
 
1714
#ifdef CONFIG_AP
 
1715
        if (wpa_s->ap_iface) {
 
1716
                wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
 
1717
                return;
 
1718
        }
 
1719
#endif /* CONFIG_AP */
 
1720
 
 
1721
        if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
 
1722
                wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
 
1723
                           "no key management is configured");
 
1724
                return;
 
1725
        }
 
1726
 
 
1727
        if (wpa_s->eapol_received == 0 &&
 
1728
            (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
 
1729
             !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
 
1730
             wpa_s->wpa_state != WPA_COMPLETED) &&
 
1731
            (wpa_s->current_ssid == NULL ||
 
1732
             wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
 
1733
                /* Timeout for completing IEEE 802.1X and WPA authentication */
 
1734
                wpa_supplicant_req_auth_timeout(
 
1735
                        wpa_s,
 
1736
                        (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
 
1737
                         wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
 
1738
                         wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
 
1739
                        70 : 10, 0);
 
1740
        }
 
1741
        wpa_s->eapol_received++;
 
1742
 
 
1743
        if (wpa_s->countermeasures) {
 
1744
                wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
 
1745
                           "packet");
 
1746
                return;
 
1747
        }
 
1748
 
 
1749
#ifdef CONFIG_IBSS_RSN
 
1750
        if (wpa_s->current_ssid &&
 
1751
            wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
 
1752
                ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
 
1753
                return;
 
1754
        }
 
1755
#endif /* CONFIG_IBSS_RSN */
 
1756
 
 
1757
        /* Source address of the incoming EAPOL frame could be compared to the
 
1758
         * current BSSID. However, it is possible that a centralized
 
1759
         * Authenticator could be using another MAC address than the BSSID of
 
1760
         * an AP, so just allow any address to be used for now. The replies are
 
1761
         * still sent to the current BSSID (if available), though. */
 
1762
 
 
1763
        os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
 
1764
        if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
 
1765
            eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
 
1766
                return;
 
1767
        wpa_drv_poll(wpa_s);
 
1768
        if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
 
1769
                wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
 
1770
        else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
 
1771
                /*
 
1772
                 * Set portValid = TRUE here since we are going to skip 4-way
 
1773
                 * handshake processing which would normally set portValid. We
 
1774
                 * need this to allow the EAPOL state machines to be completed
 
1775
                 * without going through EAPOL-Key handshake.
 
1776
                 */
 
1777
                eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
 
1778
        }
 
1779
}
 
1780
 
 
1781
 
 
1782
/**
 
1783
 * wpa_supplicant_driver_init - Initialize driver interface parameters
 
1784
 * @wpa_s: Pointer to wpa_supplicant data
 
1785
 * Returns: 0 on success, -1 on failure
 
1786
 *
 
1787
 * This function is called to initialize driver interface parameters.
 
1788
 * wpa_drv_init() must have been called before this function to initialize the
 
1789
 * driver interface.
 
1790
 */
 
1791
int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
 
1792
{
 
1793
        static int interface_count = 0;
 
1794
 
 
1795
        if (wpa_s->driver->send_eapol) {
 
1796
                const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
 
1797
                if (addr)
 
1798
                        os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
 
1799
        } else {
 
1800
                wpa_s->l2 = l2_packet_init(wpa_s->ifname,
 
1801
                                           wpa_drv_get_mac_addr(wpa_s),
 
1802
                                           ETH_P_EAPOL,
 
1803
                                           wpa_supplicant_rx_eapol, wpa_s, 0);
 
1804
                if (wpa_s->l2 == NULL)
 
1805
                        return -1;
 
1806
        }
 
1807
 
 
1808
        if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
 
1809
                wpa_printf(MSG_ERROR, "Failed to get own L2 address");
 
1810
                return -1;
 
1811
        }
 
1812
 
 
1813
        wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
 
1814
                   MAC2STR(wpa_s->own_addr));
 
1815
 
 
1816
        if (wpa_s->bridge_ifname[0]) {
 
1817
                wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
 
1818
                           " '%s'", wpa_s->bridge_ifname);
 
1819
                wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
 
1820
                                              wpa_s->own_addr,
 
1821
                                              ETH_P_EAPOL,
 
1822
                                              wpa_supplicant_rx_eapol, wpa_s,
 
1823
                                              0);
 
1824
                if (wpa_s->l2_br == NULL) {
 
1825
                        wpa_printf(MSG_ERROR, "Failed to open l2_packet "
 
1826
                                   "connection for the bridge interface '%s'",
 
1827
                                   wpa_s->bridge_ifname);
 
1828
                        return -1;
 
1829
                }
 
1830
        }
 
1831
 
 
1832
        wpa_clear_keys(wpa_s, NULL);
 
1833
 
 
1834
        /* Make sure that TKIP countermeasures are not left enabled (could
 
1835
         * happen if wpa_supplicant is killed during countermeasures. */
 
1836
        wpa_drv_set_countermeasures(wpa_s, 0);
 
1837
 
 
1838
        wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
 
1839
        wpa_drv_flush_pmkid(wpa_s);
 
1840
 
 
1841
        wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
 
1842
        if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
 
1843
                wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
 
1844
                interface_count++;
 
1845
        } else
 
1846
                wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
 
1847
 
 
1848
        return 0;
 
1849
}
 
1850
 
 
1851
 
 
1852
static int wpa_supplicant_daemon(const char *pid_file)
 
1853
{
 
1854
        wpa_printf(MSG_DEBUG, "Daemonize..");
 
1855
        return os_daemonize(pid_file);
 
1856
}
 
1857
 
 
1858
 
 
1859
static struct wpa_supplicant * wpa_supplicant_alloc(void)
 
1860
{
 
1861
        struct wpa_supplicant *wpa_s;
 
1862
 
 
1863
        wpa_s = os_zalloc(sizeof(*wpa_s));
 
1864
        if (wpa_s == NULL)
 
1865
                return NULL;
 
1866
        wpa_s->scan_req = 1;
 
1867
        wpa_s->new_connection = 1;
 
1868
 
 
1869
        return wpa_s;
 
1870
}
 
1871
 
 
1872
 
 
1873
static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
 
1874
                                     struct wpa_interface *iface)
 
1875
{
 
1876
        const char *ifname, *driver;
 
1877
        struct wpa_driver_capa capa;
 
1878
 
 
1879
        wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
 
1880
                   "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
 
1881
                   iface->confname ? iface->confname : "N/A",
 
1882
                   iface->driver ? iface->driver : "default",
 
1883
                   iface->ctrl_interface ? iface->ctrl_interface : "N/A",
 
1884
                   iface->bridge_ifname ? iface->bridge_ifname : "N/A");
 
1885
 
 
1886
        if (iface->confname) {
 
1887
#ifdef CONFIG_BACKEND_FILE
 
1888
                wpa_s->confname = os_rel2abs_path(iface->confname);
 
1889
                if (wpa_s->confname == NULL) {
 
1890
                        wpa_printf(MSG_ERROR, "Failed to get absolute path "
 
1891
                                   "for configuration file '%s'.",
 
1892
                                   iface->confname);
 
1893
                        return -1;
 
1894
                }
 
1895
                wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
 
1896
                           iface->confname, wpa_s->confname);
 
1897
#else /* CONFIG_BACKEND_FILE */
 
1898
                wpa_s->confname = os_strdup(iface->confname);
 
1899
#endif /* CONFIG_BACKEND_FILE */
 
1900
                wpa_s->conf = wpa_config_read(wpa_s->confname);
 
1901
                if (wpa_s->conf == NULL) {
 
1902
                        wpa_printf(MSG_ERROR, "Failed to read or parse "
 
1903
                                   "configuration '%s'.", wpa_s->confname);
 
1904
                        return -1;
 
1905
                }
 
1906
 
 
1907
                /*
 
1908
                 * Override ctrl_interface and driver_param if set on command
 
1909
                 * line.
 
1910
                 */
 
1911
                if (iface->ctrl_interface) {
 
1912
                        os_free(wpa_s->conf->ctrl_interface);
 
1913
                        wpa_s->conf->ctrl_interface =
 
1914
                                os_strdup(iface->ctrl_interface);
 
1915
                }
 
1916
 
 
1917
                if (iface->driver_param) {
 
1918
                        os_free(wpa_s->conf->driver_param);
 
1919
                        wpa_s->conf->driver_param =
 
1920
                                os_strdup(iface->driver_param);
 
1921
                }
 
1922
        } else
 
1923
                wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
 
1924
                                                     iface->driver_param);
 
1925
 
 
1926
        if (wpa_s->conf == NULL) {
 
1927
                wpa_printf(MSG_ERROR, "\nNo configuration found.");
 
1928
                return -1;
 
1929
        }
 
1930
 
 
1931
        if (iface->ifname == NULL) {
 
1932
                wpa_printf(MSG_ERROR, "\nInterface name is required.");
 
1933
                return -1;
 
1934
        }
 
1935
        if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
 
1936
                wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
 
1937
                           iface->ifname);
 
1938
                return -1;
 
1939
        }
 
1940
        os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
 
1941
 
 
1942
        if (iface->bridge_ifname) {
 
1943
                if (os_strlen(iface->bridge_ifname) >=
 
1944
                    sizeof(wpa_s->bridge_ifname)) {
 
1945
                        wpa_printf(MSG_ERROR, "\nToo long bridge interface "
 
1946
                                   "name '%s'.", iface->bridge_ifname);
 
1947
                        return -1;
 
1948
                }
 
1949
                os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
 
1950
                           sizeof(wpa_s->bridge_ifname));
 
1951
        }
 
1952
 
 
1953
        /* RSNA Supplicant Key Management - INITIALIZE */
 
1954
        eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
 
1955
        eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
 
1956
 
 
1957
        /* Initialize driver interface and register driver event handler before
 
1958
         * L2 receive handler so that association events are processed before
 
1959
         * EAPOL-Key packets if both become available for the same select()
 
1960
         * call. */
 
1961
        driver = iface->driver;
 
1962
next_driver:
 
1963
        if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
 
1964
                return -1;
 
1965
 
 
1966
        wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
 
1967
        if (wpa_s->drv_priv == NULL) {
 
1968
                const char *pos;
 
1969
                pos = driver ? os_strchr(driver, ',') : NULL;
 
1970
                if (pos) {
 
1971
                        wpa_printf(MSG_DEBUG, "Failed to initialize driver "
 
1972
                                   "interface - try next driver wrapper");
 
1973
                        driver = pos + 1;
 
1974
                        goto next_driver;
 
1975
                }
 
1976
                wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
 
1977
                return -1;
 
1978
        }
 
1979
        if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
 
1980
                wpa_printf(MSG_ERROR, "Driver interface rejected "
 
1981
                           "driver_param '%s'", wpa_s->conf->driver_param);
 
1982
                return -1;
 
1983
        }
 
1984
 
 
1985
        ifname = wpa_drv_get_ifname(wpa_s);
 
1986
        if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
 
1987
                wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
 
1988
                           "name with '%s'", ifname);
 
1989
                os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
 
1990
        }
 
1991
 
 
1992
        if (wpa_supplicant_init_wpa(wpa_s) < 0)
 
1993
                return -1;
 
1994
 
 
1995
        wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
 
1996
                          wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
 
1997
                          NULL);
 
1998
        wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
 
1999
 
 
2000
        if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
 
2001
            wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
 
2002
                             wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
 
2003
                wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
 
2004
                           "dot11RSNAConfigPMKLifetime");
 
2005
                return -1;
 
2006
        }
 
2007
 
 
2008
        if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
 
2009
            wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
 
2010
                             wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
 
2011
                wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
 
2012
                        "dot11RSNAConfigPMKReauthThreshold");
 
2013
                return -1;
 
2014
        }
 
2015
 
 
2016
        if (wpa_s->conf->dot11RSNAConfigSATimeout &&
 
2017
            wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
 
2018
                             wpa_s->conf->dot11RSNAConfigSATimeout)) {
 
2019
                wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
 
2020
                           "dot11RSNAConfigSATimeout");
 
2021
                return -1;
 
2022
        }
 
2023
 
 
2024
        if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
 
2025
                wpa_s->drv_flags = capa.flags;
 
2026
                if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
 
2027
                        if (ieee80211_sta_init(wpa_s))
 
2028
                                return -1;
 
2029
                }
 
2030
                wpa_s->max_scan_ssids = capa.max_scan_ssids;
 
2031
                wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
 
2032
        }
 
2033
        if (wpa_s->max_remain_on_chan == 0)
 
2034
                wpa_s->max_remain_on_chan = 1000;
 
2035
 
 
2036
        if (wpa_supplicant_driver_init(wpa_s) < 0)
 
2037
                return -1;
 
2038
 
 
2039
        if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
 
2040
            wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
 
2041
                wpa_printf(MSG_DEBUG, "Failed to set country");
 
2042
                return -1;
 
2043
        }
 
2044
 
 
2045
        wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
 
2046
 
 
2047
        if (wpas_wps_init(wpa_s))
 
2048
                return -1;
 
2049
 
 
2050
        if (wpa_supplicant_init_eapol(wpa_s) < 0)
 
2051
                return -1;
 
2052
        wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
 
2053
 
 
2054
        wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
 
2055
        if (wpa_s->ctrl_iface == NULL) {
 
2056
                wpa_printf(MSG_ERROR,
 
2057
                           "Failed to initialize control interface '%s'.\n"
 
2058
                           "You may have another wpa_supplicant process "
 
2059
                           "already running or the file was\n"
 
2060
                           "left by an unclean termination of wpa_supplicant "
 
2061
                           "in which case you will need\n"
 
2062
                           "to manually remove this file before starting "
 
2063
                           "wpa_supplicant again.\n",
 
2064
                           wpa_s->conf->ctrl_interface);
 
2065
                return -1;
 
2066
        }
 
2067
 
 
2068
#ifdef CONFIG_IBSS_RSN
 
2069
        wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
 
2070
        if (!wpa_s->ibss_rsn) {
 
2071
                wpa_printf(MSG_DEBUG, "Failed to init IBSS RSN");
 
2072
                return -1;
 
2073
        }
 
2074
#endif /* CONFIG_IBSS_RSN */
 
2075
 
 
2076
        if (wpa_bss_init(wpa_s) < 0)
 
2077
                return -1;
 
2078
 
 
2079
        return 0;
 
2080
}
 
2081
 
 
2082
 
 
2083
static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
 
2084
                                        int notify)
 
2085
{
 
2086
        if (wpa_s->drv_priv) {
 
2087
                wpa_supplicant_deauthenticate(wpa_s,
 
2088
                                              WLAN_REASON_DEAUTH_LEAVING);
 
2089
 
 
2090
                wpa_drv_set_countermeasures(wpa_s, 0);
 
2091
                wpa_clear_keys(wpa_s, NULL);
 
2092
        }
 
2093
 
 
2094
        wpa_supplicant_cleanup(wpa_s);
 
2095
 
 
2096
        if (notify)
 
2097
                wpas_notify_iface_removed(wpa_s);
 
2098
 
 
2099
        if (wpa_s->drv_priv)
 
2100
                wpa_drv_deinit(wpa_s);
 
2101
}
 
2102
 
 
2103
 
 
2104
/**
 
2105
 * wpa_supplicant_add_iface - Add a new network interface
 
2106
 * @global: Pointer to global data from wpa_supplicant_init()
 
2107
 * @iface: Interface configuration options
 
2108
 * Returns: Pointer to the created interface or %NULL on failure
 
2109
 *
 
2110
 * This function is used to add new network interfaces for %wpa_supplicant.
 
2111
 * This can be called before wpa_supplicant_run() to add interfaces before the
 
2112
 * main event loop has been started. In addition, new interfaces can be added
 
2113
 * dynamically while %wpa_supplicant is already running. This could happen,
 
2114
 * e.g., when a hotplug network adapter is inserted.
 
2115
 */
 
2116
struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
 
2117
                                                 struct wpa_interface *iface)
 
2118
{
 
2119
        struct wpa_supplicant *wpa_s;
 
2120
        struct wpa_interface t_iface;
 
2121
        struct wpa_ssid *ssid;
 
2122
 
 
2123
        if (global == NULL || iface == NULL)
 
2124
                return NULL;
 
2125
 
 
2126
        wpa_s = wpa_supplicant_alloc();
 
2127
        if (wpa_s == NULL)
 
2128
                return NULL;
 
2129
 
 
2130
        wpa_s->global = global;
 
2131
 
 
2132
        t_iface = *iface;
 
2133
        if (global->params.override_driver) {
 
2134
                wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
 
2135
                           "('%s' -> '%s')",
 
2136
                           iface->driver, global->params.override_driver);
 
2137
                t_iface.driver = global->params.override_driver;
 
2138
        }
 
2139
        if (global->params.override_ctrl_interface) {
 
2140
                wpa_printf(MSG_DEBUG, "Override interface parameter: "
 
2141
                           "ctrl_interface ('%s' -> '%s')",
 
2142
                           iface->ctrl_interface,
 
2143
                           global->params.override_ctrl_interface);
 
2144
                t_iface.ctrl_interface =
 
2145
                        global->params.override_ctrl_interface;
 
2146
        }
 
2147
        if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
 
2148
                wpa_printf(MSG_DEBUG, "Failed to add interface %s",
 
2149
                           iface->ifname);
 
2150
                wpa_supplicant_deinit_iface(wpa_s, 0);
 
2151
                os_free(wpa_s);
 
2152
                return NULL;
 
2153
        }
 
2154
 
 
2155
        /* Notify the control interfaces about new iface */
 
2156
        if (wpas_notify_iface_added(wpa_s)) {
 
2157
                wpa_supplicant_deinit_iface(wpa_s, 1);
 
2158
                os_free(wpa_s);
 
2159
                return NULL;
 
2160
        }
 
2161
 
 
2162
        for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
 
2163
                wpas_notify_network_added(wpa_s, ssid);
 
2164
 
 
2165
        wpa_s->next = global->ifaces;
 
2166
        global->ifaces = wpa_s;
 
2167
 
 
2168
        wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
 
2169
 
 
2170
        return wpa_s;
 
2171
}
 
2172
 
 
2173
 
 
2174
/**
 
2175
 * wpa_supplicant_remove_iface - Remove a network interface
 
2176
 * @global: Pointer to global data from wpa_supplicant_init()
 
2177
 * @wpa_s: Pointer to the network interface to be removed
 
2178
 * Returns: 0 if interface was removed, -1 if interface was not found
 
2179
 *
 
2180
 * This function can be used to dynamically remove network interfaces from
 
2181
 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
 
2182
 * addition, this function is used to remove all remaining interfaces when
 
2183
 * %wpa_supplicant is terminated.
 
2184
 */
 
2185
int wpa_supplicant_remove_iface(struct wpa_global *global,
 
2186
                                struct wpa_supplicant *wpa_s)
 
2187
{
 
2188
        struct wpa_supplicant *prev;
 
2189
 
 
2190
        /* Remove interface from the global list of interfaces */
 
2191
        prev = global->ifaces;
 
2192
        if (prev == wpa_s) {
 
2193
                global->ifaces = wpa_s->next;
 
2194
        } else {
 
2195
                while (prev && prev->next != wpa_s)
 
2196
                        prev = prev->next;
 
2197
                if (prev == NULL)
 
2198
                        return -1;
 
2199
                prev->next = wpa_s->next;
 
2200
        }
 
2201
 
 
2202
        wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
 
2203
 
 
2204
        wpa_supplicant_deinit_iface(wpa_s, 1);
 
2205
        os_free(wpa_s);
 
2206
 
 
2207
        return 0;
 
2208
}
 
2209
 
 
2210
 
 
2211
/**
 
2212
 * wpa_supplicant_get_iface - Get a new network interface
 
2213
 * @global: Pointer to global data from wpa_supplicant_init()
 
2214
 * @ifname: Interface name
 
2215
 * Returns: Pointer to the interface or %NULL if not found
 
2216
 */
 
2217
struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
 
2218
                                                 const char *ifname)
 
2219
{
 
2220
        struct wpa_supplicant *wpa_s;
 
2221
 
 
2222
        for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
 
2223
                if (os_strcmp(wpa_s->ifname, ifname) == 0)
 
2224
                        return wpa_s;
 
2225
        }
 
2226
        return NULL;
 
2227
}
 
2228
 
 
2229
 
 
2230
/**
 
2231
 * wpa_supplicant_init - Initialize %wpa_supplicant
 
2232
 * @params: Parameters for %wpa_supplicant
 
2233
 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
 
2234
 *
 
2235
 * This function is used to initialize %wpa_supplicant. After successful
 
2236
 * initialization, the returned data pointer can be used to add and remove
 
2237
 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
 
2238
 */
 
2239
struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
 
2240
{
 
2241
        struct wpa_global *global;
 
2242
        int ret, i;
 
2243
 
 
2244
        if (params == NULL)
 
2245
                return NULL;
 
2246
 
 
2247
        wpa_debug_open_file(params->wpa_debug_file_path);
 
2248
        if (params->wpa_debug_syslog)
 
2249
                wpa_debug_open_syslog();
 
2250
 
 
2251
        ret = eap_register_methods();
 
2252
        if (ret) {
 
2253
                wpa_printf(MSG_ERROR, "Failed to register EAP methods");
 
2254
                if (ret == -2)
 
2255
                        wpa_printf(MSG_ERROR, "Two or more EAP methods used "
 
2256
                                   "the same EAP type.");
 
2257
                return NULL;
 
2258
        }
 
2259
 
 
2260
        global = os_zalloc(sizeof(*global));
 
2261
        if (global == NULL)
 
2262
                return NULL;
 
2263
        global->params.daemonize = params->daemonize;
 
2264
        global->params.wait_for_monitor = params->wait_for_monitor;
 
2265
        global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
 
2266
        if (params->pid_file)
 
2267
                global->params.pid_file = os_strdup(params->pid_file);
 
2268
        if (params->ctrl_interface)
 
2269
                global->params.ctrl_interface =
 
2270
                        os_strdup(params->ctrl_interface);
 
2271
        if (params->override_driver)
 
2272
                global->params.override_driver =
 
2273
                        os_strdup(params->override_driver);
 
2274
        if (params->override_ctrl_interface)
 
2275
                global->params.override_ctrl_interface =
 
2276
                        os_strdup(params->override_ctrl_interface);
 
2277
        wpa_debug_level = global->params.wpa_debug_level =
 
2278
                params->wpa_debug_level;
 
2279
        wpa_debug_show_keys = global->params.wpa_debug_show_keys =
 
2280
                params->wpa_debug_show_keys;
 
2281
        wpa_debug_timestamp = global->params.wpa_debug_timestamp =
 
2282
                params->wpa_debug_timestamp;
 
2283
 
 
2284
        if (eloop_init()) {
 
2285
                wpa_printf(MSG_ERROR, "Failed to initialize event loop");
 
2286
                wpa_supplicant_deinit(global);
 
2287
                return NULL;
 
2288
        }
 
2289
 
 
2290
        global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
 
2291
        if (global->ctrl_iface == NULL) {
 
2292
                wpa_supplicant_deinit(global);
 
2293
                return NULL;
 
2294
        }
 
2295
 
 
2296
        if (wpas_notify_supplicant_initialized(global)) {
 
2297
                wpa_supplicant_deinit(global);
 
2298
                return NULL;
 
2299
        }
 
2300
 
 
2301
        for (i = 0; wpa_drivers[i]; i++)
 
2302
                global->drv_count++;
 
2303
        if (global->drv_count == 0) {
 
2304
                wpa_printf(MSG_ERROR, "No drivers enabled");
 
2305
                wpa_supplicant_deinit(global);
 
2306
                return NULL;
 
2307
        }
 
2308
        global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
 
2309
        if (global->drv_priv == NULL) {
 
2310
                wpa_supplicant_deinit(global);
 
2311
                return NULL;
 
2312
        }
 
2313
        for (i = 0; wpa_drivers[i]; i++) {
 
2314
                if (!wpa_drivers[i]->global_init)
 
2315
                        continue;
 
2316
                global->drv_priv[i] = wpa_drivers[i]->global_init();
 
2317
                if (global->drv_priv[i] == NULL) {
 
2318
                        wpa_printf(MSG_ERROR, "Failed to initialize driver "
 
2319
                                   "'%s'", wpa_drivers[i]->name);
 
2320
                        wpa_supplicant_deinit(global);
 
2321
                        return NULL;
 
2322
                }
 
2323
        }
 
2324
 
 
2325
        return global;
 
2326
}
 
2327
 
 
2328
 
 
2329
/**
 
2330
 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
 
2331
 * @global: Pointer to global data from wpa_supplicant_init()
 
2332
 * Returns: 0 after successful event loop run, -1 on failure
 
2333
 *
 
2334
 * This function starts the main event loop and continues running as long as
 
2335
 * there are any remaining events. In most cases, this function is running as
 
2336
 * long as the %wpa_supplicant process in still in use.
 
2337
 */
 
2338
int wpa_supplicant_run(struct wpa_global *global)
 
2339
{
 
2340
        struct wpa_supplicant *wpa_s;
 
2341
 
 
2342
        if (global->params.daemonize &&
 
2343
            wpa_supplicant_daemon(global->params.pid_file))
 
2344
                return -1;
 
2345
 
 
2346
        if (global->params.wait_for_monitor) {
 
2347
                for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
 
2348
                        if (wpa_s->ctrl_iface)
 
2349
                                wpa_supplicant_ctrl_iface_wait(
 
2350
                                        wpa_s->ctrl_iface);
 
2351
        }
 
2352
 
 
2353
        eloop_register_signal_terminate(wpa_supplicant_terminate, global);
 
2354
        eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
 
2355
 
 
2356
        eloop_run();
 
2357
 
 
2358
        return 0;
 
2359
}
 
2360
 
 
2361
 
 
2362
/**
 
2363
 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
 
2364
 * @global: Pointer to global data from wpa_supplicant_init()
 
2365
 *
 
2366
 * This function is called to deinitialize %wpa_supplicant and to free all
 
2367
 * allocated resources. Remaining network interfaces will also be removed.
 
2368
 */
 
2369
void wpa_supplicant_deinit(struct wpa_global *global)
 
2370
{
 
2371
        int i;
 
2372
 
 
2373
        if (global == NULL)
 
2374
                return;
 
2375
 
 
2376
        while (global->ifaces)
 
2377
                wpa_supplicant_remove_iface(global, global->ifaces);
 
2378
 
 
2379
        if (global->ctrl_iface)
 
2380
                wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
 
2381
 
 
2382
        wpas_notify_supplicant_deinitialized(global);
 
2383
 
 
2384
        eap_peer_unregister_methods();
 
2385
#ifdef CONFIG_AP
 
2386
        eap_server_unregister_methods();
 
2387
#endif /* CONFIG_AP */
 
2388
 
 
2389
        for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
 
2390
                if (!global->drv_priv[i])
 
2391
                        continue;
 
2392
                wpa_drivers[i]->global_deinit(global->drv_priv[i]);
 
2393
        }
 
2394
        os_free(global->drv_priv);
 
2395
 
 
2396
        eloop_destroy();
 
2397
 
 
2398
        if (global->params.pid_file) {
 
2399
                os_daemonize_terminate(global->params.pid_file);
 
2400
                os_free(global->params.pid_file);
 
2401
        }
 
2402
        os_free(global->params.ctrl_interface);
 
2403
        os_free(global->params.override_driver);
 
2404
        os_free(global->params.override_ctrl_interface);
 
2405
 
 
2406
        os_free(global);
 
2407
        wpa_debug_close_syslog();
 
2408
        wpa_debug_close_file();
 
2409
}
 
2410
 
 
2411
 
 
2412
static void add_freq(int *freqs, int *num_freqs, int freq)
 
2413
{
 
2414
        int i;
 
2415
 
 
2416
        for (i = 0; i < *num_freqs; i++) {
 
2417
                if (freqs[i] == freq)
 
2418
                        return;
 
2419
        }
 
2420
 
 
2421
        freqs[*num_freqs] = freq;
 
2422
        (*num_freqs)++;
 
2423
}
 
2424
 
 
2425
 
 
2426
static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
 
2427
{
 
2428
        struct wpa_bss *bss, *cbss;
 
2429
        const int max_freqs = 10;
 
2430
        int *freqs;
 
2431
        int num_freqs = 0;
 
2432
 
 
2433
        freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
 
2434
        if (freqs == NULL)
 
2435
                return NULL;
 
2436
 
 
2437
        cbss = wpa_s->current_bss;
 
2438
 
 
2439
        dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
 
2440
                if (bss == cbss)
 
2441
                        continue;
 
2442
                if (bss->ssid_len == cbss->ssid_len &&
 
2443
                    os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
 
2444
                    wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
 
2445
                        add_freq(freqs, &num_freqs, bss->freq);
 
2446
                        if (num_freqs == max_freqs)
 
2447
                                break;
 
2448
                }
 
2449
        }
 
2450
 
 
2451
        if (num_freqs == 0) {
 
2452
                os_free(freqs);
 
2453
                freqs = NULL;
 
2454
        }
 
2455
 
 
2456
        return freqs;
 
2457
}
 
2458
 
 
2459
 
 
2460
void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
 
2461
{
 
2462
        int timeout;
 
2463
        int count;
 
2464
        int *freqs = NULL;
 
2465
 
 
2466
        /*
 
2467
         * Add the failed BSSID into the blacklist and speed up next scan
 
2468
         * attempt if there could be other APs that could accept association.
 
2469
         * The current blacklist count indicates how many times we have tried
 
2470
         * connecting to this AP and multiple attempts mean that other APs are
 
2471
         * either not available or has already been tried, so that we can start
 
2472
         * increasing the delay here to avoid constant scanning.
 
2473
         */
 
2474
        count = wpa_blacklist_add(wpa_s, bssid);
 
2475
        if (count == 1 && wpa_s->current_bss) {
 
2476
                /*
 
2477
                 * This BSS was not in the blacklist before. If there is
 
2478
                 * another BSS available for the same ESS, we should try that
 
2479
                 * next. Otherwise, we may as well try this one once more
 
2480
                 * before allowing other, likely worse, ESSes to be considered.
 
2481
                 */
 
2482
                freqs = get_bss_freqs_in_ess(wpa_s);
 
2483
                if (freqs) {
 
2484
                        wpa_printf(MSG_DEBUG, "Another BSS in this ESS has "
 
2485
                                   "been seen; try it next");
 
2486
                        wpa_blacklist_add(wpa_s, bssid);
 
2487
                        /*
 
2488
                         * On the next scan, go through only the known channels
 
2489
                         * used in this ESS based on previous scans to speed up
 
2490
                         * common load balancing use case.
 
2491
                         */
 
2492
                        os_free(wpa_s->next_scan_freqs);
 
2493
                        wpa_s->next_scan_freqs = freqs;
 
2494
                }
 
2495
        }
 
2496
 
 
2497
        switch (count) {
 
2498
        case 1:
 
2499
                timeout = 100;
 
2500
                break;
 
2501
        case 2:
 
2502
                timeout = 500;
 
2503
                break;
 
2504
        case 3:
 
2505
                timeout = 1000;
 
2506
                break;
 
2507
        default:
 
2508
                timeout = 5000;
 
2509
        }
 
2510
 
 
2511
        /*
 
2512
         * TODO: if more than one possible AP is available in scan results,
 
2513
         * could try the other ones before requesting a new scan.
 
2514
         */
 
2515
        wpa_supplicant_req_scan(wpa_s, timeout / 1000,
 
2516
                                1000 * (timeout % 1000));
 
2517
}