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