3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
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.
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "eap_peer/eap.h"
26 #include "drivers/driver.h"
28 #include "l2_packet/l2_packet.h"
29 #include "wpa_supplicant_i.h"
30 #include "ctrl_iface.h"
31 #include "ctrl_iface_dbus.h"
32 #include "pcsc_funcs.h"
35 #include "pmksa_cache.h"
38 #include "ieee802_11_defs.h"
39 #include "blacklist.h"
40 #include "wpas_glue.h"
42 #include "wps_supplicant.h"
44 const char *wpa_supplicant_version =
45 "wpa_supplicant v" VERSION_STR "\n"
46 "Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> and contributors";
48 const char *wpa_supplicant_license =
49 "This program is free software. You can distribute it and/or modify it\n"
50 "under the terms of the GNU General Public License version 2.\n"
52 "Alternatively, this software may be distributed under the terms of the\n"
53 "BSD license. See README and COPYING for more details.\n"
54 #ifdef EAP_TLS_OPENSSL
55 "\nThis product includes software developed by the OpenSSL Project\n"
56 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
57 #endif /* EAP_TLS_OPENSSL */
60 #ifndef CONFIG_NO_STDOUT_DEBUG
61 /* Long text divided into parts in order to fit in C89 strings size limits. */
62 const char *wpa_supplicant_full_license1 =
63 "This program is free software; you can redistribute it and/or modify\n"
64 "it under the terms of the GNU General Public License version 2 as\n"
65 "published by the Free Software Foundation.\n"
67 "This program is distributed in the hope that it will be useful,\n"
68 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
69 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
70 "GNU General Public License for more details.\n"
72 const char *wpa_supplicant_full_license2 =
73 "You should have received a copy of the GNU General Public License\n"
74 "along with this program; if not, write to the Free Software\n"
75 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
77 "Alternatively, this software may be distributed under the terms of the\n"
80 "Redistribution and use in source and binary forms, with or without\n"
81 "modification, are permitted provided that the following conditions are\n"
84 const char *wpa_supplicant_full_license3 =
85 "1. Redistributions of source code must retain the above copyright\n"
86 " notice, this list of conditions and the following disclaimer.\n"
88 "2. Redistributions in binary form must reproduce the above copyright\n"
89 " notice, this list of conditions and the following disclaimer in the\n"
90 " documentation and/or other materials provided with the distribution.\n"
92 const char *wpa_supplicant_full_license4 =
93 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
94 " names of its contributors may be used to endorse or promote products\n"
95 " derived from this software without specific prior written permission.\n"
97 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
98 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
99 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
100 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
101 const char *wpa_supplicant_full_license5 =
102 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
103 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
104 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
105 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
106 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
107 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
108 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
110 #endif /* CONFIG_NO_STDOUT_DEBUG */
112 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
114 extern int wpa_debug_level;
115 extern int wpa_debug_show_keys;
116 extern int wpa_debug_timestamp;
118 /* Configure default/group WEP keys for static WEP */
119 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
120 struct wpa_ssid *ssid)
124 for (i = 0; i < NUM_WEP_KEYS; i++) {
125 if (ssid->wep_key_len[i] == 0)
129 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
130 (u8 *) "\xff\xff\xff\xff\xff\xff",
131 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
132 ssid->wep_key[i], ssid->wep_key_len[i]);
139 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
140 struct wpa_ssid *ssid)
147 /* IBSS/WPA-None uses only one key (Group) for both receiving and
148 * sending unicast and multicast packets. */
150 if (ssid->mode != IEEE80211_MODE_IBSS) {
151 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
152 "for WPA-None", ssid->mode);
156 if (!ssid->psk_set) {
157 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
161 switch (wpa_s->group_cipher) {
162 case WPA_CIPHER_CCMP:
163 os_memcpy(key, ssid->psk, 16);
167 case WPA_CIPHER_TKIP:
168 /* WPA-None uses the same Michael MIC key for both TX and RX */
169 os_memcpy(key, ssid->psk, 16 + 8);
170 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
175 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
176 "WPA-None", wpa_s->group_cipher);
180 /* TODO: should actually remember the previously used seq#, both for TX
181 * and RX from each STA.. */
183 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
184 0, 1, seq, 6, key, keylen);
188 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
190 struct wpa_supplicant *wpa_s = eloop_ctx;
191 const u8 *bssid = wpa_s->bssid;
192 if (is_zero_ether_addr(bssid))
193 bssid = wpa_s->pending_bssid;
194 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
196 wpa_blacklist_add(wpa_s, bssid);
197 wpa_sm_notify_disassoc(wpa_s->wpa);
198 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
199 wpa_s->reassociate = 1;
200 wpa_supplicant_req_scan(wpa_s, 0, 0);
205 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
206 * @wpa_s: Pointer to wpa_supplicant data
207 * @sec: Number of seconds after which to time out authentication
208 * @usec: Number of microseconds after which to time out authentication
210 * This function is used to schedule a timeout for the current authentication
213 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
216 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
217 wpa_s->driver && IS_WIRED(wpa_s->driver))
220 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
221 "%d usec", sec, usec);
222 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
223 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
228 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
229 * @wpa_s: Pointer to wpa_supplicant data
231 * This function is used to cancel authentication timeout scheduled with
232 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
235 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
237 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
238 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
239 wpa_blacklist_del(wpa_s, wpa_s->bssid);
244 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
245 * @wpa_s: Pointer to wpa_supplicant data
247 * This function is used to configure EAPOL state machine based on the selected
248 * authentication mode.
250 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
252 #ifdef IEEE8021X_EAPOL
253 struct eapol_config eapol_conf;
254 struct wpa_ssid *ssid = wpa_s->current_ssid;
256 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
257 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
259 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
260 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
261 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
263 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
265 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
266 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
267 eapol_conf.accept_802_1x_keys = 1;
268 eapol_conf.required_keys = 0;
269 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
270 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
272 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
273 eapol_conf.required_keys |=
274 EAPOL_REQUIRE_KEY_BROADCAST;
277 if (wpa_s->conf && wpa_s->driver && IS_WIRED(wpa_s->driver)) {
278 eapol_conf.required_keys = 0;
282 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
283 eapol_conf.workaround = ssid->eap_workaround;
284 eapol_conf.eap_disabled =
285 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
286 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
287 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
288 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
289 #endif /* IEEE8021X_EAPOL */
294 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
295 * @wpa_s: Pointer to wpa_supplicant data
296 * @ssid: Configuration data for the network
298 * This function is used to configure WPA state machine and related parameters
299 * to a mode where WPA is not enabled. This is called as part of the
300 * authentication configuration when the selected network does not use WPA.
302 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
303 struct wpa_ssid *ssid)
307 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
308 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
309 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
310 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
312 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
313 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
314 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
315 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
316 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
317 wpa_s->group_cipher = WPA_CIPHER_NONE;
318 wpa_s->mgmt_group_cipher = 0;
320 for (i = 0; i < NUM_WEP_KEYS; i++) {
321 if (ssid->wep_key_len[i] > 5) {
322 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
323 wpa_s->group_cipher = WPA_CIPHER_WEP104;
325 } else if (ssid->wep_key_len[i] > 0) {
326 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
327 wpa_s->group_cipher = WPA_CIPHER_WEP40;
332 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
333 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
334 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
335 wpa_s->pairwise_cipher);
336 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
337 #ifdef CONFIG_IEEE80211W
338 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
339 wpa_s->mgmt_group_cipher);
340 #endif /* CONFIG_IEEE80211W */
342 pmksa_cache_clear_current(wpa_s->wpa);
346 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
348 scard_deinit(wpa_s->scard);
350 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
351 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
352 l2_packet_deinit(wpa_s->l2);
355 l2_packet_deinit(wpa_s->l2_br);
359 if (wpa_s->ctrl_iface) {
360 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
361 wpa_s->ctrl_iface = NULL;
363 if (wpa_s->conf != NULL) {
364 wpa_config_free(wpa_s->conf);
368 os_free(wpa_s->confname);
369 wpa_s->confname = NULL;
371 wpa_sm_set_eapol(wpa_s->wpa, NULL);
372 eapol_sm_deinit(wpa_s->eapol);
375 rsn_preauth_deinit(wpa_s->wpa);
377 pmksa_candidate_free(wpa_s->wpa);
378 wpa_sm_deinit(wpa_s->wpa);
380 wpa_blacklist_clear(wpa_s);
382 wpa_scan_results_free(wpa_s->scan_res);
383 wpa_s->scan_res = NULL;
385 wpa_supplicant_cancel_scan(wpa_s);
386 wpa_supplicant_cancel_auth_timeout(wpa_s);
388 ieee80211_sta_deinit(wpa_s);
390 wpas_wps_deinit(wpa_s);
395 * wpa_clear_keys - Clear keys configured for the driver
396 * @wpa_s: Pointer to wpa_supplicant data
397 * @addr: Previously used BSSID or %NULL if not available
399 * This function clears the encryption keys that has been previously configured
402 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
404 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
406 if (wpa_s->keys_cleared) {
407 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
408 * timing issues with keys being cleared just before new keys
409 * are set or just after association or something similar. This
410 * shows up in group key handshake failing often because of the
411 * client not receiving the first encrypted packets correctly.
412 * Skipping some of the extra key clearing steps seems to help
413 * in completing group key handshake more reliably. */
414 wpa_printf(MSG_DEBUG, "No keys have been configured - "
415 "skip key clearing");
419 /* MLME-DELETEKEYS.request */
420 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
421 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
422 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
423 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
425 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
427 /* MLME-SETPROTECTION.request(None) */
428 wpa_drv_mlme_setprotection(
430 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
431 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
433 wpa_s->keys_cleared = 1;
438 * wpa_supplicant_state_txt - Get the connection state name as a text string
439 * @state: State (wpa_state; WPA_*)
440 * Returns: The state name as a printable text string
442 const char * wpa_supplicant_state_txt(int state)
445 case WPA_DISCONNECTED:
446 return "DISCONNECTED";
451 case WPA_ASSOCIATING:
452 return "ASSOCIATING";
455 case WPA_4WAY_HANDSHAKE:
456 return "4WAY_HANDSHAKE";
457 case WPA_GROUP_HANDSHAKE:
458 return "GROUP_HANDSHAKE";
468 * wpa_supplicant_set_state - Set current connection state
469 * @wpa_s: Pointer to wpa_supplicant data
470 * @state: The new connection state
472 * This function is called whenever the connection state changes, e.g.,
473 * association is completed for WPA/WPA2 4-Way Handshake is started.
475 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
477 wpa_printf(MSG_DEBUG, "State: %s -> %s",
478 wpa_supplicant_state_txt(wpa_s->wpa_state),
479 wpa_supplicant_state_txt(state));
481 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
484 if (state == WPA_COMPLETED && wpa_s->new_connection) {
485 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
486 struct wpa_ssid *ssid = wpa_s->current_ssid;
487 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
488 MACSTR " completed %s [id=%d id_str=%s]",
489 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
490 "(reauth)" : "(auth)",
491 ssid ? ssid->id : -1,
492 ssid && ssid->id_str ? ssid->id_str : "");
493 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
494 wpa_s->new_connection = 0;
495 wpa_s->reassociated_connection = 1;
496 wpa_drv_set_operstate(wpa_s, 1);
497 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
498 state == WPA_ASSOCIATED) {
499 wpa_s->new_connection = 1;
500 wpa_drv_set_operstate(wpa_s, 0);
502 wpa_s->wpa_state = state;
506 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
509 struct wpa_global *global = eloop_ctx;
510 struct wpa_supplicant *wpa_s;
511 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
512 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
519 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
521 wpa_s->pairwise_cipher = 0;
522 wpa_s->group_cipher = 0;
523 wpa_s->mgmt_group_cipher = 0;
525 wpa_s->wpa_state = WPA_DISCONNECTED;
530 * wpa_supplicant_reload_configuration - Reload configuration data
531 * @wpa_s: Pointer to wpa_supplicant data
532 * Returns: 0 on success or -1 if configuration parsing failed
534 * This function can be used to request that the configuration data is reloaded
535 * (e.g., after configuration file change). This function is reloading
536 * configuration only for one interface, so this may need to be called multiple
537 * times if %wpa_supplicant is controlling multiple interfaces and all
538 * interfaces need reconfiguration.
540 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
542 struct wpa_config *conf;
544 if (wpa_s->confname == NULL)
546 conf = wpa_config_read(wpa_s->confname);
548 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
549 "file '%s' - exiting", wpa_s->confname);
553 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
554 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
555 os_strcmp(conf->ctrl_interface,
556 wpa_s->conf->ctrl_interface) != 0);
558 if (reconf_ctrl && wpa_s->ctrl_iface) {
559 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
560 wpa_s->ctrl_iface = NULL;
563 eapol_sm_invalidate_cached_session(wpa_s->eapol);
564 wpa_s->current_ssid = NULL;
566 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
567 * pkcs11_engine_path, pkcs11_module_path.
569 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
571 * Clear forced success to clear EAP state for next
574 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
576 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
577 wpa_sm_set_config(wpa_s->wpa, NULL);
578 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
579 rsn_preauth_deinit(wpa_s->wpa);
580 wpa_config_free(wpa_s->conf);
583 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
585 wpa_supplicant_clear_status(wpa_s);
586 wpa_s->reassociate = 1;
587 wpa_supplicant_req_scan(wpa_s, 0, 0);
588 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
593 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
596 struct wpa_global *global = eloop_ctx;
597 struct wpa_supplicant *wpa_s;
598 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
599 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
600 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
607 static wpa_cipher cipher_suite2driver(int cipher)
610 case WPA_CIPHER_NONE:
612 case WPA_CIPHER_WEP40:
614 case WPA_CIPHER_WEP104:
615 return CIPHER_WEP104;
616 case WPA_CIPHER_CCMP:
618 case WPA_CIPHER_TKIP:
625 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
628 case WPA_KEY_MGMT_NONE:
629 return KEY_MGMT_NONE;
630 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
631 return KEY_MGMT_802_1X_NO_WPA;
632 case WPA_KEY_MGMT_IEEE8021X:
633 return KEY_MGMT_802_1X;
634 case WPA_KEY_MGMT_WPA_NONE:
635 return KEY_MGMT_WPA_NONE;
636 case WPA_KEY_MGMT_FT_IEEE8021X:
637 return KEY_MGMT_FT_802_1X;
638 case WPA_KEY_MGMT_FT_PSK:
639 return KEY_MGMT_FT_PSK;
640 case WPA_KEY_MGMT_IEEE8021X_SHA256:
641 return KEY_MGMT_802_1X_SHA256;
642 case WPA_KEY_MGMT_PSK_SHA256:
643 return KEY_MGMT_PSK_SHA256;
644 case WPA_KEY_MGMT_WPS:
646 case WPA_KEY_MGMT_PSK:
653 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
654 struct wpa_ssid *ssid,
655 struct wpa_ie_data *ie)
657 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
660 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
661 "from association info");
666 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
668 if (!(ie->group_cipher & ssid->group_cipher)) {
669 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
670 "cipher 0x%x (mask 0x%x) - reject",
671 ie->group_cipher, ssid->group_cipher);
674 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
675 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
676 "cipher 0x%x (mask 0x%x) - reject",
677 ie->pairwise_cipher, ssid->pairwise_cipher);
680 if (!(ie->key_mgmt & ssid->key_mgmt)) {
681 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
682 "management 0x%x (mask 0x%x) - reject",
683 ie->key_mgmt, ssid->key_mgmt);
687 #ifdef CONFIG_IEEE80211W
688 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
689 ssid->ieee80211w == IEEE80211W_REQUIRED) {
690 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
691 "that does not support management frame protection - "
695 #endif /* CONFIG_IEEE80211W */
702 * wpa_supplicant_set_suites - Set authentication and encryption parameters
703 * @wpa_s: Pointer to wpa_supplicant data
704 * @bss: Scan results for the selected BSS, or %NULL if not available
705 * @ssid: Configuration data for the selected network
706 * @wpa_ie: Buffer for the WPA/RSN IE
707 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
708 * used buffer length in case the functions returns success.
709 * Returns: 0 on success or -1 on failure
711 * This function is used to configure authentication and encryption parameters
712 * based on the network configuration and scan result for the selected BSS (if
715 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
716 struct wpa_scan_res *bss,
717 struct wpa_ssid *ssid,
718 u8 *wpa_ie, size_t *wpa_ie_len)
720 struct wpa_ie_data ie;
722 const u8 *bss_wpa, *bss_rsn;
725 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
726 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
728 bss_wpa = bss_rsn = NULL;
730 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
731 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
732 (ie.group_cipher & ssid->group_cipher) &&
733 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
734 (ie.key_mgmt & ssid->key_mgmt)) {
735 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
736 proto = WPA_PROTO_RSN;
737 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
738 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
739 (ie.group_cipher & ssid->group_cipher) &&
740 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
741 (ie.key_mgmt & ssid->key_mgmt)) {
742 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
743 proto = WPA_PROTO_WPA;
745 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
748 if (ssid->proto & WPA_PROTO_RSN)
749 proto = WPA_PROTO_RSN;
751 proto = WPA_PROTO_WPA;
752 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
753 os_memset(&ie, 0, sizeof(ie));
754 ie.group_cipher = ssid->group_cipher;
755 ie.pairwise_cipher = ssid->pairwise_cipher;
756 ie.key_mgmt = ssid->key_mgmt;
757 #ifdef CONFIG_IEEE80211W
758 ie.mgmt_group_cipher =
759 ssid->ieee80211w != NO_IEEE80211W ?
760 WPA_CIPHER_AES_128_CMAC : 0;
761 #endif /* CONFIG_IEEE80211W */
762 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
768 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
769 "pairwise %d key_mgmt %d proto %d",
770 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
771 #ifdef CONFIG_IEEE80211W
772 if (ssid->ieee80211w) {
773 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
774 ie.mgmt_group_cipher);
776 #endif /* CONFIG_IEEE80211W */
778 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
779 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
780 !!(ssid->proto & WPA_PROTO_RSN));
782 if (bss || !wpa_s->ap_ies_from_associnfo) {
783 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
784 bss_wpa ? 2 + bss_wpa[1] : 0) ||
785 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
786 bss_rsn ? 2 + bss_rsn[1] : 0))
790 sel = ie.group_cipher & ssid->group_cipher;
791 if (sel & WPA_CIPHER_CCMP) {
792 wpa_s->group_cipher = WPA_CIPHER_CCMP;
793 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
794 } else if (sel & WPA_CIPHER_TKIP) {
795 wpa_s->group_cipher = WPA_CIPHER_TKIP;
796 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
797 } else if (sel & WPA_CIPHER_WEP104) {
798 wpa_s->group_cipher = WPA_CIPHER_WEP104;
799 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
800 } else if (sel & WPA_CIPHER_WEP40) {
801 wpa_s->group_cipher = WPA_CIPHER_WEP40;
802 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
804 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
808 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
809 if (sel & WPA_CIPHER_CCMP) {
810 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
811 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
812 } else if (sel & WPA_CIPHER_TKIP) {
813 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
814 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
815 } else if (sel & WPA_CIPHER_NONE) {
816 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
817 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
819 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
824 sel = ie.key_mgmt & ssid->key_mgmt;
826 #ifdef CONFIG_IEEE80211R
827 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
828 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
829 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
830 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
831 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
832 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
833 #endif /* CONFIG_IEEE80211R */
834 #ifdef CONFIG_IEEE80211W
835 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
836 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
837 wpa_msg(wpa_s, MSG_DEBUG,
838 "WPA: using KEY_MGMT 802.1X with SHA256");
839 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
840 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
841 wpa_msg(wpa_s, MSG_DEBUG,
842 "WPA: using KEY_MGMT PSK with SHA256");
843 #endif /* CONFIG_IEEE80211W */
844 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
845 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
846 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
847 } else if (sel & WPA_KEY_MGMT_PSK) {
848 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
849 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
850 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
851 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
852 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
854 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
855 "key management type.");
859 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
860 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
861 wpa_s->pairwise_cipher);
862 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
864 #ifdef CONFIG_IEEE80211W
865 sel = ie.mgmt_group_cipher;
866 if (ssid->ieee80211w == NO_IEEE80211W ||
867 !(ie.capabilities & WPA_CAPABILITY_MFPC))
869 if (sel & WPA_CIPHER_AES_128_CMAC) {
870 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
871 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
874 wpa_s->mgmt_group_cipher = 0;
875 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
877 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
878 wpa_s->mgmt_group_cipher);
879 #endif /* CONFIG_IEEE80211W */
881 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
882 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
887 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
888 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
890 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
897 * wpa_supplicant_associate - Request association
898 * @wpa_s: Pointer to wpa_supplicant data
899 * @bss: Scan results for the selected BSS, or %NULL if not available
900 * @ssid: Configuration data for the selected network
902 * This function is used to request %wpa_supplicant to associate with a BSS.
904 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
905 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
909 int use_crypt, ret, i;
910 int algs = AUTH_ALG_OPEN_SYSTEM;
911 wpa_cipher cipher_pairwise, cipher_group;
912 struct wpa_driver_associate_params params;
913 int wep_keys_set = 0;
914 struct wpa_driver_capa capa;
915 int assoc_failed = 0;
917 wpa_s->reassociate = 0;
919 #ifdef CONFIG_IEEE80211R
921 #endif /* CONFIG_IEEE80211R */
922 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
923 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
924 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
925 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
926 os_memset(wpa_s->bssid, 0, ETH_ALEN);
927 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
928 #ifdef CONFIG_IEEE80211R
929 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
930 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
932 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
934 /* Prepare for the next transition */
935 wpa_ft_prepare_auth_request(wpa_s->wpa);
937 #endif /* CONFIG_IEEE80211R */
939 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
940 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
941 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
943 wpa_supplicant_cancel_scan(wpa_s);
945 /* Starting new association, so clear the possibly used WPA IE from the
946 * previous association. */
947 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
949 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
950 wpa_printf(MSG_WARNING, "Failed to set operating mode");
954 #ifdef IEEE8021X_EAPOL
955 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
957 if (ssid->non_leap == 0)
958 algs = AUTH_ALG_LEAP;
960 algs |= AUTH_ALG_LEAP;
963 #endif /* IEEE8021X_EAPOL */
964 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
965 if (ssid->auth_alg) {
967 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
968 algs |= AUTH_ALG_OPEN_SYSTEM;
969 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
970 algs |= AUTH_ALG_SHARED_KEY;
971 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
972 algs |= AUTH_ALG_LEAP;
973 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
976 wpa_drv_set_auth_alg(wpa_s, algs);
978 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
979 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
980 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
981 WPA_KEY_MGMT_FT_IEEE8021X |
982 WPA_KEY_MGMT_FT_PSK |
983 WPA_KEY_MGMT_IEEE8021X_SHA256 |
984 WPA_KEY_MGMT_PSK_SHA256))) {
985 int try_opportunistic;
986 try_opportunistic = ssid->proactive_key_caching &&
987 (ssid->proto & WPA_PROTO_RSN);
988 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
990 try_opportunistic) == 0)
991 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
992 wpa_ie_len = sizeof(wpa_ie);
993 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
994 wpa_ie, &wpa_ie_len)) {
995 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
996 "management and encryption suites");
999 } else if (ssid->key_mgmt &
1000 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1001 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1002 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1003 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1004 wpa_ie_len = sizeof(wpa_ie);
1005 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1006 wpa_ie, &wpa_ie_len)) {
1007 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1008 "management and encryption suites (no scan "
1013 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1014 struct wpabuf *wps_ie;
1015 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1016 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1017 wpa_ie_len = wpabuf_len(wps_ie);
1018 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1020 wpabuf_free(wps_ie);
1021 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1022 #endif /* CONFIG_WPS */
1024 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1028 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1030 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1031 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1032 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1033 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1034 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1036 if (wpa_set_wep_keys(wpa_s, ssid)) {
1041 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1044 #ifdef IEEE8021X_EAPOL
1045 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1046 if ((ssid->eapol_flags &
1047 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1048 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1052 /* Assume that dynamic WEP-104 keys will be used and
1053 * set cipher suites in order for drivers to expect
1055 cipher_pairwise = cipher_group = CIPHER_WEP104;
1058 #endif /* IEEE8021X_EAPOL */
1060 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1061 /* Set the key before (and later after) association */
1062 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1065 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1066 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1067 os_memset(¶ms, 0, sizeof(params));
1069 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1070 params.bssid = bss->bssid;
1071 params.ssid = ie ? ie + 2 : (u8 *) "";
1072 params.ssid_len = ie ? ie[1] : 0;
1073 params.freq = bss->freq;
1075 params.ssid = ssid->ssid;
1076 params.ssid_len = ssid->ssid_len;
1078 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1079 params.freq = ssid->frequency; /* Initial channel for IBSS */
1080 params.wpa_ie = wpa_ie;
1081 params.wpa_ie_len = wpa_ie_len;
1082 params.pairwise_suite = cipher_pairwise;
1083 params.group_suite = cipher_group;
1084 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1085 params.auth_alg = algs;
1086 params.mode = ssid->mode;
1087 for (i = 0; i < NUM_WEP_KEYS; i++) {
1088 if (ssid->wep_key_len[i])
1089 params.wep_key[i] = ssid->wep_key[i];
1090 params.wep_key_len[i] = ssid->wep_key_len[i];
1092 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1094 if (wpa_s->driver_4way_handshake &&
1095 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1096 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1097 params.passphrase = ssid->passphrase;
1099 params.psk = ssid->psk;
1102 #ifdef CONFIG_IEEE80211W
1103 switch (ssid->ieee80211w) {
1105 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1107 case IEEE80211W_OPTIONAL:
1108 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1110 case IEEE80211W_REQUIRED:
1111 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1114 #endif /* CONFIG_IEEE80211W */
1116 if (wpa_s->use_client_mlme)
1117 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1119 ret = wpa_drv_associate(wpa_s, ¶ms);
1121 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1123 /* try to continue anyway; new association will be tried again
1128 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1129 /* Set the key after the association just in case association
1130 * cleared the previously configured key. */
1131 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1132 /* No need to timeout authentication since there is no key
1134 wpa_supplicant_cancel_auth_timeout(wpa_s);
1135 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1137 /* Timeout for IEEE 802.11 authentication and association */
1141 /* give IBSS a bit more time */
1142 timeout = ssid->mode ? 10 : 5;
1143 } else if (wpa_s->conf->ap_scan == 1) {
1144 /* give IBSS a bit more time */
1145 timeout = ssid->mode ? 20 : 10;
1147 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1150 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1151 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1152 /* Set static WEP keys again */
1153 wpa_set_wep_keys(wpa_s, ssid);
1156 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1158 * Do not allow EAP session resumption between different
1159 * network configurations.
1161 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1163 wpa_s->current_ssid = ssid;
1164 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1165 wpa_supplicant_initiate_eapol(wpa_s);
1170 * wpa_supplicant_disassociate - Disassociate the current connection
1171 * @wpa_s: Pointer to wpa_supplicant data
1172 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1174 * This function is used to request %wpa_supplicant to disassociate with the
1177 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1181 if (!is_zero_ether_addr(wpa_s->bssid)) {
1182 if (wpa_s->use_client_mlme)
1183 ieee80211_sta_disassociate(wpa_s, reason_code);
1185 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1186 addr = wpa_s->bssid;
1188 wpa_clear_keys(wpa_s, addr);
1189 wpa_supplicant_mark_disassoc(wpa_s);
1190 wpa_s->current_ssid = NULL;
1191 wpa_sm_set_config(wpa_s->wpa, NULL);
1192 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1197 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1198 * @wpa_s: Pointer to wpa_supplicant data
1199 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1201 * This function is used to request %wpa_supplicant to disassociate with the
1204 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1208 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1209 if (!is_zero_ether_addr(wpa_s->bssid)) {
1210 if (wpa_s->use_client_mlme)
1211 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1213 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1215 addr = wpa_s->bssid;
1217 wpa_clear_keys(wpa_s, addr);
1218 wpa_s->current_ssid = NULL;
1219 wpa_sm_set_config(wpa_s->wpa, NULL);
1220 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1221 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1222 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1226 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1228 #define SCAN_AP_LIMIT 128
1229 struct wpa_scan_result *results;
1231 struct wpa_scan_results *res;
1233 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1234 if (results == NULL) {
1235 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1240 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1241 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1243 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1247 if (num > SCAN_AP_LIMIT) {
1248 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1249 num, SCAN_AP_LIMIT);
1250 num = SCAN_AP_LIMIT;
1253 wpa_scan_results_free(wpa_s->scan_res);
1254 wpa_s->scan_res = NULL;
1256 /* Convert old scan result data structure to the new one */
1257 res = os_zalloc(sizeof(*res));
1262 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1263 if (res->res == NULL) {
1269 for (i = 0; i < num; i++) {
1270 struct wpa_scan_result *bss = &results[i];
1271 struct wpa_scan_res *r;
1275 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1278 if (bss->mdie_present)
1281 r = os_zalloc(sizeof(*r) + ie_len);
1285 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1286 r->freq = bss->freq;
1287 r->caps = bss->caps;
1288 r->qual = bss->qual;
1289 r->noise = bss->noise;
1290 r->level = bss->level;
1294 pos = (u8 *) (r + 1);
1297 *pos++ = WLAN_EID_SSID;
1298 *pos++ = bss->ssid_len;
1299 os_memcpy(pos, bss->ssid, bss->ssid_len);
1300 pos += bss->ssid_len;
1303 /* Fake Supported Rate IE to include max rate */
1304 *pos++ = WLAN_EID_SUPP_RATES;
1306 *pos++ = bss->maxrate;
1309 if (bss->rsn_ie_len) {
1310 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1311 pos += bss->rsn_ie_len;
1314 if (bss->mdie_present) {
1315 os_memcpy(pos, bss->mdie, 5);
1319 if (bss->wpa_ie_len) {
1320 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1321 pos += bss->wpa_ie_len;
1324 res->res[res->num++] = r;
1328 wpa_s->scan_res = res;
1335 * wpa_supplicant_get_scan_results - Get scan results
1336 * @wpa_s: Pointer to wpa_supplicant data
1337 * Returns: 0 on success, -1 on failure
1339 * This function is request the current scan results from the driver and stores
1340 * a local copy of the results in wpa_s->scan_res.
1342 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1346 if (wpa_s->use_client_mlme) {
1347 wpa_scan_results_free(wpa_s->scan_res);
1348 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1349 if (wpa_s->scan_res == NULL) {
1350 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1354 } else if (wpa_s->driver->get_scan_results2 == NULL)
1355 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1357 wpa_scan_results_free(wpa_s->scan_res);
1358 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1359 if (wpa_s->scan_res == NULL) {
1360 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1366 if (wpa_s->scan_res)
1367 wpa_scan_sort_results(wpa_s->scan_res);
1374 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1375 * @wpa_s: Pointer to wpa_supplicant data
1376 * Returns: A pointer to the current network structure or %NULL on failure
1378 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1380 struct wpa_ssid *entry;
1381 u8 ssid[MAX_SSID_LEN];
1387 if (wpa_s->use_client_mlme) {
1388 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1389 wpa_printf(MSG_WARNING, "Could not read SSID from "
1394 res = wpa_drv_get_ssid(wpa_s, ssid);
1396 wpa_printf(MSG_WARNING, "Could not read SSID from "
1403 if (wpa_s->use_client_mlme)
1404 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1405 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1406 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1410 wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
1411 IS_WIRED(wpa_s->driver);
1413 entry = wpa_s->conf->ssid;
1415 if (!entry->disabled &&
1416 ((ssid_len == entry->ssid_len &&
1417 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1418 (!entry->bssid_set ||
1419 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1421 entry = entry->next;
1428 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1436 if (wpa_supplicant_drivers[0] == NULL) {
1437 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1443 /* default to first driver in the list */
1444 wpa_s->driver = wpa_supplicant_drivers[0];
1448 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1449 if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1450 wpa_s->driver = wpa_supplicant_drivers[i];
1455 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
1460 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1461 const u8 *buf, size_t len)
1463 struct wpa_supplicant *wpa_s = ctx;
1465 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1466 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1468 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1469 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1470 "no key management is configured");
1474 if (wpa_s->eapol_received == 0 &&
1475 (!wpa_s->driver_4way_handshake ||
1476 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1477 wpa_s->wpa_state != WPA_COMPLETED)) {
1478 /* Timeout for completing IEEE 802.1X and WPA authentication */
1479 wpa_supplicant_req_auth_timeout(
1481 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1482 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1485 wpa_s->eapol_received++;
1487 if (wpa_s->countermeasures) {
1488 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1493 /* Source address of the incoming EAPOL frame could be compared to the
1494 * current BSSID. However, it is possible that a centralized
1495 * Authenticator could be using another MAC address than the BSSID of
1496 * an AP, so just allow any address to be used for now. The replies are
1497 * still sent to the current BSSID (if available), though. */
1499 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1500 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1501 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1503 wpa_drv_poll(wpa_s);
1504 if (!wpa_s->driver_4way_handshake)
1505 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1506 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1508 * Set portValid = TRUE here since we are going to skip 4-way
1509 * handshake processing which would normally set portValid. We
1510 * need this to allow the EAPOL state machines to be completed
1511 * without going through EAPOL-Key handshake.
1513 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1518 void wpa_supplicant_sta_free_hw_features(struct wpa_hw_modes *hw_features,
1519 size_t num_hw_features)
1521 ieee80211_sta_free_hw_features(hw_features, num_hw_features);
1525 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1526 struct ieee80211_rx_status *rx_status)
1528 struct wpa_supplicant *wpa_s = ctx;
1529 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1534 * wpa_supplicant_driver_init - Initialize driver interface parameters
1535 * @wpa_s: Pointer to wpa_supplicant data
1536 * Returns: 0 on success, -1 on failure
1538 * This function is called to initialize driver interface parameters.
1539 * wpa_drv_init() must have been called before this function to initialize the
1542 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1544 static int interface_count = 0;
1546 if (wpa_s->driver->send_eapol) {
1547 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1549 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1551 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1552 wpa_drv_get_mac_addr(wpa_s),
1554 wpa_supplicant_rx_eapol, wpa_s, 0);
1555 if (wpa_s->l2 == NULL)
1559 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1560 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1564 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1565 MAC2STR(wpa_s->own_addr));
1567 if (wpa_s->bridge_ifname[0]) {
1568 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1569 " '%s'", wpa_s->bridge_ifname);
1570 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1573 wpa_supplicant_rx_eapol, wpa_s,
1575 if (wpa_s->l2_br == NULL) {
1576 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1577 "connection for the bridge interface '%s'",
1578 wpa_s->bridge_ifname);
1583 /* Backwards compatibility call to set_wpa() handler. This is called
1584 * only just after init and just before deinit, so these handler can be
1585 * used to implement same functionality. */
1586 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1587 struct wpa_driver_capa capa;
1588 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1589 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1590 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1591 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1592 /* Continue to allow non-WPA modes to be used. */
1594 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1600 wpa_clear_keys(wpa_s, NULL);
1602 /* Make sure that TKIP countermeasures are not left enabled (could
1603 * happen if wpa_supplicant is killed during countermeasures. */
1604 wpa_drv_set_countermeasures(wpa_s, 0);
1606 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1608 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1609 wpa_drv_flush_pmkid(wpa_s);
1611 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1612 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1619 static int wpa_supplicant_daemon(const char *pid_file)
1621 wpa_printf(MSG_DEBUG, "Daemonize..");
1622 return os_daemonize(pid_file);
1626 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1628 struct wpa_supplicant *wpa_s;
1630 wpa_s = os_zalloc(sizeof(*wpa_s));
1633 wpa_s->scan_req = 1;
1639 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1640 struct wpa_interface *iface)
1642 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1643 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1644 iface->confname ? iface->confname : "N/A",
1645 iface->driver ? iface->driver : "default",
1646 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1647 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1649 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
1653 if (iface->confname) {
1654 #ifdef CONFIG_BACKEND_FILE
1655 wpa_s->confname = os_rel2abs_path(iface->confname);
1656 if (wpa_s->confname == NULL) {
1657 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1658 "for configuration file '%s'.",
1662 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1663 iface->confname, wpa_s->confname);
1664 #else /* CONFIG_BACKEND_FILE */
1665 wpa_s->confname = os_strdup(iface->confname);
1666 #endif /* CONFIG_BACKEND_FILE */
1667 wpa_s->conf = wpa_config_read(wpa_s->confname);
1668 if (wpa_s->conf == NULL) {
1669 wpa_printf(MSG_ERROR, "Failed to read or parse "
1670 "configuration '%s'.", wpa_s->confname);
1675 * Override ctrl_interface and driver_param if set on command
1678 if (iface->ctrl_interface) {
1679 os_free(wpa_s->conf->ctrl_interface);
1680 wpa_s->conf->ctrl_interface =
1681 os_strdup(iface->ctrl_interface);
1684 if (iface->driver_param) {
1685 os_free(wpa_s->conf->driver_param);
1686 wpa_s->conf->driver_param =
1687 os_strdup(iface->driver_param);
1690 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1691 iface->driver_param);
1693 if (wpa_s->conf == NULL) {
1694 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1698 if (iface->ifname == NULL) {
1699 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1702 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1703 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1707 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1709 if (iface->bridge_ifname) {
1710 if (os_strlen(iface->bridge_ifname) >=
1711 sizeof(wpa_s->bridge_ifname)) {
1712 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1713 "name '%s'.", iface->bridge_ifname);
1716 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1717 sizeof(wpa_s->bridge_ifname));
1724 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s)
1727 struct wpa_driver_capa capa;
1729 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
1732 /* RSNA Supplicant Key Management - INITIALIZE */
1733 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1734 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1736 /* Initialize driver interface and register driver event handler before
1737 * L2 receive handler so that association events are processed before
1738 * EAPOL-Key packets if both become available for the same select()
1740 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1741 if (wpa_s->drv_priv == NULL) {
1742 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1745 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1746 wpa_printf(MSG_ERROR, "Driver interface rejected "
1747 "driver_param '%s'", wpa_s->conf->driver_param);
1751 ifname = wpa_drv_get_ifname(wpa_s);
1752 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1753 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1754 "name with '%s'", ifname);
1755 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1758 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1761 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1762 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1764 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1766 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1767 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1768 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1769 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1770 "dot11RSNAConfigPMKLifetime");
1774 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1775 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1776 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1777 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1778 "dot11RSNAConfigPMKReauthThreshold");
1782 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1783 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1784 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1785 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1786 "dot11RSNAConfigSATimeout");
1790 if (wpa_supplicant_driver_init(wpa_s) < 0)
1793 if (wpa_s->conf->alpha2[0] && wpa_s->conf->alpha2[1] &&
1794 wpa_drv_set_country(wpa_s, wpa_s->conf->alpha2)) {
1795 wpa_printf(MSG_DEBUG, "Failed to set country");
1799 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1801 if (wpas_wps_init(wpa_s))
1804 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1806 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1808 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1809 if (wpa_s->ctrl_iface == NULL) {
1810 wpa_printf(MSG_ERROR,
1811 "Failed to initialize control interface '%s'.\n"
1812 "You may have another wpa_supplicant process "
1813 "already running or the file was\n"
1814 "left by an unclean termination of wpa_supplicant "
1815 "in which case you will need\n"
1816 "to manually remove this file before starting "
1817 "wpa_supplicant again.\n",
1818 wpa_s->conf->ctrl_interface);
1822 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1823 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1824 wpa_s->use_client_mlme = 1;
1825 if (ieee80211_sta_init(wpa_s))
1828 if (capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)
1829 wpa_s->driver_4way_handshake = 1;
1836 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1838 if (wpa_s->drv_priv) {
1839 wpa_supplicant_deauthenticate(wpa_s,
1840 WLAN_REASON_DEAUTH_LEAVING);
1842 /* Backwards compatibility call to set_wpa() handler. This is
1843 * called only just after init and just before deinit, so these
1844 * handler can be used to implement same functionality. */
1845 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1846 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1850 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1851 wpa_drv_set_countermeasures(wpa_s, 0);
1852 wpa_clear_keys(wpa_s, NULL);
1855 wpas_dbus_unregister_iface(wpa_s);
1857 wpa_supplicant_cleanup(wpa_s);
1859 if (wpa_s->drv_priv)
1860 wpa_drv_deinit(wpa_s);
1865 * wpa_supplicant_add_iface - Add a new network interface
1866 * @global: Pointer to global data from wpa_supplicant_init()
1867 * @iface: Interface configuration options
1868 * Returns: Pointer to the created interface or %NULL on failure
1870 * This function is used to add new network interfaces for %wpa_supplicant.
1871 * This can be called before wpa_supplicant_run() to add interfaces before the
1872 * main event loop has been started. In addition, new interfaces can be added
1873 * dynamically while %wpa_supplicant is already running. This could happen,
1874 * e.g., when a hotplug network adapter is inserted.
1876 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1877 struct wpa_interface *iface)
1879 struct wpa_supplicant *wpa_s;
1881 if (global == NULL || iface == NULL)
1884 wpa_s = wpa_supplicant_alloc();
1888 if (wpa_supplicant_init_iface(wpa_s, iface) ||
1889 wpa_supplicant_init_iface2(wpa_s)) {
1890 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
1892 wpa_supplicant_deinit_iface(wpa_s);
1897 wpa_s->global = global;
1899 /* Register the interface with the dbus control interface */
1900 if (wpas_dbus_register_iface(wpa_s)) {
1901 wpa_supplicant_deinit_iface(wpa_s);
1906 wpa_s->next = global->ifaces;
1907 global->ifaces = wpa_s;
1909 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
1916 * wpa_supplicant_remove_iface - Remove a network interface
1917 * @global: Pointer to global data from wpa_supplicant_init()
1918 * @wpa_s: Pointer to the network interface to be removed
1919 * Returns: 0 if interface was removed, -1 if interface was not found
1921 * This function can be used to dynamically remove network interfaces from
1922 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
1923 * addition, this function is used to remove all remaining interfaces when
1924 * %wpa_supplicant is terminated.
1926 int wpa_supplicant_remove_iface(struct wpa_global *global,
1927 struct wpa_supplicant *wpa_s)
1929 struct wpa_supplicant *prev;
1931 /* Remove interface from the global list of interfaces */
1932 prev = global->ifaces;
1933 if (prev == wpa_s) {
1934 global->ifaces = wpa_s->next;
1936 while (prev && prev->next != wpa_s)
1940 prev->next = wpa_s->next;
1943 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
1945 wpa_supplicant_deinit_iface(wpa_s);
1953 * wpa_supplicant_get_iface - Get a new network interface
1954 * @global: Pointer to global data from wpa_supplicant_init()
1955 * @ifname: Interface name
1956 * Returns: Pointer to the interface or %NULL if not found
1958 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
1961 struct wpa_supplicant *wpa_s;
1963 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1964 if (os_strcmp(wpa_s->ifname, ifname) == 0)
1972 * wpa_supplicant_init - Initialize %wpa_supplicant
1973 * @params: Parameters for %wpa_supplicant
1974 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
1976 * This function is used to initialize %wpa_supplicant. After successful
1977 * initialization, the returned data pointer can be used to add and remove
1978 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
1980 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
1982 struct wpa_global *global;
1988 wpa_debug_open_file(params->wpa_debug_file_path);
1990 ret = eap_peer_register_methods();
1992 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1994 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
1995 "the same EAP type.");
1999 global = os_zalloc(sizeof(*global));
2002 global->params.daemonize = params->daemonize;
2003 global->params.wait_for_monitor = params->wait_for_monitor;
2004 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2005 if (params->pid_file)
2006 global->params.pid_file = os_strdup(params->pid_file);
2007 if (params->ctrl_interface)
2008 global->params.ctrl_interface =
2009 os_strdup(params->ctrl_interface);
2010 wpa_debug_level = global->params.wpa_debug_level =
2011 params->wpa_debug_level;
2012 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2013 params->wpa_debug_show_keys;
2014 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2015 params->wpa_debug_timestamp;
2017 if (eloop_init(global)) {
2018 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2019 wpa_supplicant_deinit(global);
2023 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2024 if (global->ctrl_iface == NULL) {
2025 wpa_supplicant_deinit(global);
2029 if (global->params.dbus_ctrl_interface) {
2030 global->dbus_ctrl_iface =
2031 wpa_supplicant_dbus_ctrl_iface_init(global);
2032 if (global->dbus_ctrl_iface == NULL) {
2033 wpa_supplicant_deinit(global);
2043 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2044 * @global: Pointer to global data from wpa_supplicant_init()
2045 * Returns: 0 after successful event loop run, -1 on failure
2047 * This function starts the main event loop and continues running as long as
2048 * there are any remaining events. In most cases, this function is running as
2049 * long as the %wpa_supplicant process in still in use.
2051 int wpa_supplicant_run(struct wpa_global *global)
2053 struct wpa_supplicant *wpa_s;
2055 if (global->params.daemonize &&
2056 wpa_supplicant_daemon(global->params.pid_file))
2059 if (global->params.wait_for_monitor) {
2060 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2061 if (wpa_s->ctrl_iface)
2062 wpa_supplicant_ctrl_iface_wait(
2066 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2067 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2076 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2077 * @global: Pointer to global data from wpa_supplicant_init()
2079 * This function is called to deinitialize %wpa_supplicant and to free all
2080 * allocated resources. Remaining network interfaces will also be removed.
2082 void wpa_supplicant_deinit(struct wpa_global *global)
2087 while (global->ifaces)
2088 wpa_supplicant_remove_iface(global, global->ifaces);
2090 if (global->ctrl_iface)
2091 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2092 if (global->dbus_ctrl_iface)
2093 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2095 eap_peer_unregister_methods();
2099 if (global->params.pid_file) {
2100 os_daemonize_terminate(global->params.pid_file);
2101 os_free(global->params.pid_file);
2103 os_free(global->params.ctrl_interface);
2106 wpa_debug_close_file();