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 const char *wpa_supplicant_version =
43 "wpa_supplicant v" VERSION_STR "\n"
44 "Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi> and contributors";
46 const char *wpa_supplicant_license =
47 "This program is free software. You can distribute it and/or modify it\n"
48 "under the terms of the GNU General Public License version 2.\n"
50 "Alternatively, this software may be distributed under the terms of the\n"
51 "BSD license. See README and COPYING for more details.\n"
52 #ifdef EAP_TLS_OPENSSL
53 "\nThis product includes software developed by the OpenSSL Project\n"
54 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
55 #endif /* EAP_TLS_OPENSSL */
58 #ifndef CONFIG_NO_STDOUT_DEBUG
59 /* Long text divided into parts in order to fit in C89 strings size limits. */
60 const char *wpa_supplicant_full_license1 =
61 "This program is free software; you can redistribute it and/or modify\n"
62 "it under the terms of the GNU General Public License version 2 as\n"
63 "published by the Free Software Foundation.\n"
65 "This program is distributed in the hope that it will be useful,\n"
66 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
67 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
68 "GNU General Public License for more details.\n"
70 const char *wpa_supplicant_full_license2 =
71 "You should have received a copy of the GNU General Public License\n"
72 "along with this program; if not, write to the Free Software\n"
73 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
75 "Alternatively, this software may be distributed under the terms of the\n"
78 "Redistribution and use in source and binary forms, with or without\n"
79 "modification, are permitted provided that the following conditions are\n"
82 const char *wpa_supplicant_full_license3 =
83 "1. Redistributions of source code must retain the above copyright\n"
84 " notice, this list of conditions and the following disclaimer.\n"
86 "2. Redistributions in binary form must reproduce the above copyright\n"
87 " notice, this list of conditions and the following disclaimer in the\n"
88 " documentation and/or other materials provided with the distribution.\n"
90 const char *wpa_supplicant_full_license4 =
91 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
92 " names of its contributors may be used to endorse or promote products\n"
93 " derived from this software without specific prior written permission.\n"
95 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
96 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
97 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
98 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
99 const char *wpa_supplicant_full_license5 =
100 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
101 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
102 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
103 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
104 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
105 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
106 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
108 #endif /* CONFIG_NO_STDOUT_DEBUG */
110 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
112 extern int wpa_debug_level;
113 extern int wpa_debug_show_keys;
114 extern int wpa_debug_timestamp;
116 /* Configure default/group WEP keys for static WEP */
117 static int wpa_set_wep_keys(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
122 for (i = 0; i < NUM_WEP_KEYS; i++) {
123 if (ssid->wep_key_len[i] == 0)
127 wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
128 (u8 *) "\xff\xff\xff\xff\xff\xff",
129 i, i == ssid->wep_tx_keyidx, (u8 *) "", 0,
130 ssid->wep_key[i], ssid->wep_key_len[i]);
137 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
138 struct wpa_ssid *ssid)
145 /* IBSS/WPA-None uses only one key (Group) for both receiving and
146 * sending unicast and multicast packets. */
148 if (ssid->mode != IEEE80211_MODE_IBSS) {
149 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
150 "for WPA-None", ssid->mode);
154 if (!ssid->psk_set) {
155 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
159 switch (wpa_s->group_cipher) {
160 case WPA_CIPHER_CCMP:
161 os_memcpy(key, ssid->psk, 16);
165 case WPA_CIPHER_TKIP:
166 /* WPA-None uses the same Michael MIC key for both TX and RX */
167 os_memcpy(key, ssid->psk, 16 + 8);
168 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
173 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
174 "WPA-None", wpa_s->group_cipher);
178 /* TODO: should actually remember the previously used seq#, both for TX
179 * and RX from each STA.. */
181 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
182 0, 1, seq, 6, key, keylen);
186 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
188 struct wpa_supplicant *wpa_s = eloop_ctx;
189 const u8 *bssid = wpa_s->bssid;
190 if (is_zero_ether_addr(bssid))
191 bssid = wpa_s->pending_bssid;
192 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
194 wpa_blacklist_add(wpa_s, bssid);
195 wpa_sm_notify_disassoc(wpa_s->wpa);
196 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
197 wpa_s->reassociate = 1;
198 wpa_supplicant_req_scan(wpa_s, 0, 0);
203 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
204 * @wpa_s: Pointer to wpa_supplicant data
205 * @sec: Number of seconds after which to time out authentication
206 * @usec: Number of microseconds after which to time out authentication
208 * This function is used to schedule a timeout for the current authentication
211 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
214 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
215 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0)
218 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
219 "%d usec", sec, usec);
220 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
221 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
226 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
227 * @wpa_s: Pointer to wpa_supplicant data
229 * This function is used to cancel authentication timeout scheduled with
230 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
233 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
235 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
236 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
237 wpa_blacklist_del(wpa_s, wpa_s->bssid);
242 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
243 * @wpa_s: Pointer to wpa_supplicant data
245 * This function is used to configure EAPOL state machine based on the selected
246 * authentication mode.
248 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
250 #ifdef IEEE8021X_EAPOL
251 struct eapol_config eapol_conf;
252 struct wpa_ssid *ssid = wpa_s->current_ssid;
254 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
255 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
256 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
258 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
259 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
260 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
262 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
264 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
265 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
266 eapol_conf.accept_802_1x_keys = 1;
267 eapol_conf.required_keys = 0;
268 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
269 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
271 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
272 eapol_conf.required_keys |=
273 EAPOL_REQUIRE_KEY_BROADCAST;
276 if (wpa_s->conf && wpa_s->driver &&
277 os_strcmp(wpa_s->driver->name, "wired") == 0) {
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 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
288 #endif /* IEEE8021X_EAPOL */
293 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
294 * @wpa_s: Pointer to wpa_supplicant data
295 * @ssid: Configuration data for the network
297 * This function is used to configure WPA state machine and related parameters
298 * to a mode where WPA is not enabled. This is called as part of the
299 * authentication configuration when the selected network does not use WPA.
301 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
302 struct wpa_ssid *ssid)
306 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
307 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
309 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
310 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
311 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
312 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
313 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
314 wpa_s->group_cipher = WPA_CIPHER_NONE;
315 wpa_s->mgmt_group_cipher = 0;
317 for (i = 0; i < NUM_WEP_KEYS; i++) {
318 if (ssid->wep_key_len[i] > 5) {
319 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
320 wpa_s->group_cipher = WPA_CIPHER_WEP104;
322 } else if (ssid->wep_key_len[i] > 0) {
323 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
324 wpa_s->group_cipher = WPA_CIPHER_WEP40;
329 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
330 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
331 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
332 wpa_s->pairwise_cipher);
333 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
334 #ifdef CONFIG_IEEE80211W
335 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
336 wpa_s->mgmt_group_cipher);
337 #endif /* CONFIG_IEEE80211W */
339 pmksa_cache_clear_current(wpa_s->wpa);
343 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
345 scard_deinit(wpa_s->scard);
347 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
348 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
349 l2_packet_deinit(wpa_s->l2);
352 l2_packet_deinit(wpa_s->l2_br);
356 if (wpa_s->ctrl_iface) {
357 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
358 wpa_s->ctrl_iface = NULL;
360 if (wpa_s->conf != NULL) {
361 wpa_config_free(wpa_s->conf);
365 os_free(wpa_s->confname);
366 wpa_s->confname = NULL;
368 wpa_sm_set_eapol(wpa_s->wpa, NULL);
369 eapol_sm_deinit(wpa_s->eapol);
372 rsn_preauth_deinit(wpa_s->wpa);
374 pmksa_candidate_free(wpa_s->wpa);
375 wpa_sm_deinit(wpa_s->wpa);
377 wpa_blacklist_clear(wpa_s);
379 wpa_scan_results_free(wpa_s->scan_res);
380 wpa_s->scan_res = NULL;
382 wpa_supplicant_cancel_scan(wpa_s);
383 wpa_supplicant_cancel_auth_timeout(wpa_s);
385 ieee80211_sta_deinit(wpa_s);
390 * wpa_clear_keys - Clear keys configured for the driver
391 * @wpa_s: Pointer to wpa_supplicant data
392 * @addr: Previously used BSSID or %NULL if not available
394 * This function clears the encryption keys that has been previously configured
397 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
399 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
401 if (wpa_s->keys_cleared) {
402 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
403 * timing issues with keys being cleared just before new keys
404 * are set or just after association or something similar. This
405 * shows up in group key handshake failing often because of the
406 * client not receiving the first encrypted packets correctly.
407 * Skipping some of the extra key clearing steps seems to help
408 * in completing group key handshake more reliably. */
409 wpa_printf(MSG_DEBUG, "No keys have been configured - "
410 "skip key clearing");
414 /* MLME-DELETEKEYS.request */
415 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
416 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
417 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
418 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
420 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
422 /* MLME-SETPROTECTION.request(None) */
423 wpa_drv_mlme_setprotection(
425 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
426 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
428 wpa_s->keys_cleared = 1;
433 * wpa_supplicant_state_txt - Get the connection state name as a text string
434 * @state: State (wpa_state; WPA_*)
435 * Returns: The state name as a printable text string
437 const char * wpa_supplicant_state_txt(int state)
440 case WPA_DISCONNECTED:
441 return "DISCONNECTED";
446 case WPA_ASSOCIATING:
447 return "ASSOCIATING";
450 case WPA_4WAY_HANDSHAKE:
451 return "4WAY_HANDSHAKE";
452 case WPA_GROUP_HANDSHAKE:
453 return "GROUP_HANDSHAKE";
463 * wpa_supplicant_set_state - Set current connection state
464 * @wpa_s: Pointer to wpa_supplicant data
465 * @state: The new connection state
467 * This function is called whenever the connection state changes, e.g.,
468 * association is completed for WPA/WPA2 4-Way Handshake is started.
470 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
472 wpa_printf(MSG_DEBUG, "State: %s -> %s",
473 wpa_supplicant_state_txt(wpa_s->wpa_state),
474 wpa_supplicant_state_txt(state));
476 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
479 if (state == WPA_COMPLETED && wpa_s->new_connection) {
480 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
481 struct wpa_ssid *ssid = wpa_s->current_ssid;
482 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
483 MACSTR " completed %s [id=%d id_str=%s]",
484 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
485 "(reauth)" : "(auth)",
486 ssid ? ssid->id : -1,
487 ssid && ssid->id_str ? ssid->id_str : "");
488 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
489 wpa_s->new_connection = 0;
490 wpa_s->reassociated_connection = 1;
491 wpa_drv_set_operstate(wpa_s, 1);
492 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
493 state == WPA_ASSOCIATED) {
494 wpa_s->new_connection = 1;
495 wpa_drv_set_operstate(wpa_s, 0);
497 wpa_s->wpa_state = state;
501 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
504 struct wpa_global *global = eloop_ctx;
505 struct wpa_supplicant *wpa_s;
506 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
507 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
514 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
516 wpa_s->pairwise_cipher = 0;
517 wpa_s->group_cipher = 0;
518 wpa_s->mgmt_group_cipher = 0;
520 wpa_s->wpa_state = WPA_DISCONNECTED;
525 * wpa_supplicant_reload_configuration - Reload configuration data
526 * @wpa_s: Pointer to wpa_supplicant data
527 * Returns: 0 on success or -1 if configuration parsing failed
529 * This function can be used to request that the configuration data is reloaded
530 * (e.g., after configuration file change). This function is reloading
531 * configuration only for one interface, so this may need to be called multiple
532 * times if %wpa_supplicant is controlling multiple interfaces and all
533 * interfaces need reconfiguration.
535 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
537 struct wpa_config *conf;
539 if (wpa_s->confname == NULL)
541 conf = wpa_config_read(wpa_s->confname);
543 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
544 "file '%s' - exiting", wpa_s->confname);
548 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
549 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
550 os_strcmp(conf->ctrl_interface,
551 wpa_s->conf->ctrl_interface) != 0);
553 if (reconf_ctrl && wpa_s->ctrl_iface) {
554 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
555 wpa_s->ctrl_iface = NULL;
558 eapol_sm_invalidate_cached_session(wpa_s->eapol);
559 wpa_s->current_ssid = NULL;
561 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
562 * pkcs11_engine_path, pkcs11_module_path.
564 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
566 * Clear forced success to clear EAP state for next
569 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
571 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
572 wpa_sm_set_config(wpa_s->wpa, NULL);
573 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
574 rsn_preauth_deinit(wpa_s->wpa);
575 wpa_config_free(wpa_s->conf);
578 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
580 wpa_supplicant_clear_status(wpa_s);
581 wpa_s->reassociate = 1;
582 wpa_supplicant_req_scan(wpa_s, 0, 0);
583 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
588 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
591 struct wpa_global *global = eloop_ctx;
592 struct wpa_supplicant *wpa_s;
593 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
594 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
595 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
602 static wpa_cipher cipher_suite2driver(int cipher)
605 case WPA_CIPHER_NONE:
607 case WPA_CIPHER_WEP40:
609 case WPA_CIPHER_WEP104:
610 return CIPHER_WEP104;
611 case WPA_CIPHER_CCMP:
613 case WPA_CIPHER_TKIP:
620 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
623 case WPA_KEY_MGMT_NONE:
624 return KEY_MGMT_NONE;
625 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
626 return KEY_MGMT_802_1X_NO_WPA;
627 case WPA_KEY_MGMT_IEEE8021X:
628 return KEY_MGMT_802_1X;
629 case WPA_KEY_MGMT_WPA_NONE:
630 return KEY_MGMT_WPA_NONE;
631 case WPA_KEY_MGMT_FT_IEEE8021X:
632 return KEY_MGMT_FT_802_1X;
633 case WPA_KEY_MGMT_FT_PSK:
634 return KEY_MGMT_FT_PSK;
635 case WPA_KEY_MGMT_IEEE8021X_SHA256:
636 return KEY_MGMT_802_1X_SHA256;
637 case WPA_KEY_MGMT_PSK_SHA256:
638 return KEY_MGMT_PSK_SHA256;
639 case WPA_KEY_MGMT_PSK:
646 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
647 struct wpa_ssid *ssid,
648 struct wpa_ie_data *ie)
650 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
653 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
654 "from association info");
659 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
661 if (!(ie->group_cipher & ssid->group_cipher)) {
662 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
663 "cipher 0x%x (mask 0x%x) - reject",
664 ie->group_cipher, ssid->group_cipher);
667 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
668 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
669 "cipher 0x%x (mask 0x%x) - reject",
670 ie->pairwise_cipher, ssid->pairwise_cipher);
673 if (!(ie->key_mgmt & ssid->key_mgmt)) {
674 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
675 "management 0x%x (mask 0x%x) - reject",
676 ie->key_mgmt, ssid->key_mgmt);
680 #ifdef CONFIG_IEEE80211W
681 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
682 ssid->ieee80211w == IEEE80211W_REQUIRED) {
683 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
684 "that does not support management frame protection - "
688 #endif /* CONFIG_IEEE80211W */
695 * wpa_supplicant_set_suites - Set authentication and encryption parameters
696 * @wpa_s: Pointer to wpa_supplicant data
697 * @bss: Scan results for the selected BSS, or %NULL if not available
698 * @ssid: Configuration data for the selected network
699 * @wpa_ie: Buffer for the WPA/RSN IE
700 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
701 * used buffer length in case the functions returns success.
702 * Returns: 0 on success or -1 on failure
704 * This function is used to configure authentication and encryption parameters
705 * based on the network configuration and scan result for the selected BSS (if
708 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
709 struct wpa_scan_res *bss,
710 struct wpa_ssid *ssid,
711 u8 *wpa_ie, size_t *wpa_ie_len)
713 struct wpa_ie_data ie;
715 const u8 *bss_wpa, *bss_rsn;
718 bss_wpa = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
719 bss_rsn = wpa_scan_get_ie(bss, WLAN_EID_RSN);
721 bss_wpa = bss_rsn = NULL;
723 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
724 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
725 (ie.group_cipher & ssid->group_cipher) &&
726 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
727 (ie.key_mgmt & ssid->key_mgmt)) {
728 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
729 proto = WPA_PROTO_RSN;
730 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
731 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[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, "WPA: using IEEE 802.11i/D3.0");
736 proto = WPA_PROTO_WPA;
738 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
741 if (ssid->proto & WPA_PROTO_RSN)
742 proto = WPA_PROTO_RSN;
744 proto = WPA_PROTO_WPA;
745 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
746 os_memset(&ie, 0, sizeof(ie));
747 ie.group_cipher = ssid->group_cipher;
748 ie.pairwise_cipher = ssid->pairwise_cipher;
749 ie.key_mgmt = ssid->key_mgmt;
750 #ifdef CONFIG_IEEE80211W
751 ie.mgmt_group_cipher =
752 ssid->ieee80211w != NO_IEEE80211W ?
753 WPA_CIPHER_AES_128_CMAC : 0;
754 #endif /* CONFIG_IEEE80211W */
755 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
761 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
762 "pairwise %d key_mgmt %d proto %d",
763 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
764 #ifdef CONFIG_IEEE80211W
765 if (ssid->ieee80211w) {
766 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
767 ie.mgmt_group_cipher);
769 #endif /* CONFIG_IEEE80211W */
771 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
772 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
773 !!(ssid->proto & WPA_PROTO_RSN));
775 if (bss || !wpa_s->ap_ies_from_associnfo) {
776 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
777 bss_wpa ? 2 + bss_wpa[1] : 0) ||
778 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
779 bss_rsn ? 2 + bss_rsn[1] : 0))
783 sel = ie.group_cipher & ssid->group_cipher;
784 if (sel & WPA_CIPHER_CCMP) {
785 wpa_s->group_cipher = WPA_CIPHER_CCMP;
786 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
787 } else if (sel & WPA_CIPHER_TKIP) {
788 wpa_s->group_cipher = WPA_CIPHER_TKIP;
789 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
790 } else if (sel & WPA_CIPHER_WEP104) {
791 wpa_s->group_cipher = WPA_CIPHER_WEP104;
792 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
793 } else if (sel & WPA_CIPHER_WEP40) {
794 wpa_s->group_cipher = WPA_CIPHER_WEP40;
795 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
797 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
801 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
802 if (sel & WPA_CIPHER_CCMP) {
803 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
804 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
805 } else if (sel & WPA_CIPHER_TKIP) {
806 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
807 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
808 } else if (sel & WPA_CIPHER_NONE) {
809 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
810 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
812 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
817 sel = ie.key_mgmt & ssid->key_mgmt;
819 #ifdef CONFIG_IEEE80211R
820 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
821 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
822 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
823 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
824 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
825 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
826 #endif /* CONFIG_IEEE80211R */
827 #ifdef CONFIG_IEEE80211W
828 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
829 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
830 wpa_msg(wpa_s, MSG_DEBUG,
831 "WPA: using KEY_MGMT 802.1X with SHA256");
832 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
833 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
834 wpa_msg(wpa_s, MSG_DEBUG,
835 "WPA: using KEY_MGMT PSK with SHA256");
836 #endif /* CONFIG_IEEE80211W */
837 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
838 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
839 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
840 } else if (sel & WPA_KEY_MGMT_PSK) {
841 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
842 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
843 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
844 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
845 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
847 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
848 "key management type.");
852 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
853 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
854 wpa_s->pairwise_cipher);
855 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
857 #ifdef CONFIG_IEEE80211W
858 sel = ie.mgmt_group_cipher;
859 if (ssid->ieee80211w == NO_IEEE80211W ||
860 !(ie.capabilities & WPA_CAPABILITY_MFPC))
862 if (sel & WPA_CIPHER_AES_128_CMAC) {
863 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
864 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
867 wpa_s->mgmt_group_cipher = 0;
868 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
870 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
871 wpa_s->mgmt_group_cipher);
872 #endif /* CONFIG_IEEE80211W */
874 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
875 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
880 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
881 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
883 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
890 * wpa_supplicant_associate - Request association
891 * @wpa_s: Pointer to wpa_supplicant data
892 * @bss: Scan results for the selected BSS, or %NULL if not available
893 * @ssid: Configuration data for the selected network
895 * This function is used to request %wpa_supplicant to associate with a BSS.
897 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
898 struct wpa_scan_res *bss, struct wpa_ssid *ssid)
902 int use_crypt, ret, i;
903 int algs = AUTH_ALG_OPEN_SYSTEM;
904 wpa_cipher cipher_pairwise, cipher_group;
905 struct wpa_driver_associate_params params;
906 int wep_keys_set = 0;
907 struct wpa_driver_capa capa;
908 int assoc_failed = 0;
910 wpa_s->reassociate = 0;
912 #ifdef CONFIG_IEEE80211R
914 #endif /* CONFIG_IEEE80211R */
915 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
916 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
917 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
918 ie ? wpa_ssid_txt(ie + 2, ie[1]) : "", bss->freq);
919 os_memset(wpa_s->bssid, 0, ETH_ALEN);
920 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
921 #ifdef CONFIG_IEEE80211R
922 ie = wpa_scan_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
923 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
925 wpa_sm_set_ft_params(wpa_s->wpa, md, NULL, 0, NULL);
926 /* Prepare for the next transition */
927 wpa_ft_prepare_auth_request(wpa_s->wpa);
928 #endif /* CONFIG_IEEE80211R */
930 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
931 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
932 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
934 wpa_supplicant_cancel_scan(wpa_s);
936 /* Starting new association, so clear the possibly used WPA IE from the
937 * previous association. */
938 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
940 if (wpa_drv_set_mode(wpa_s, ssid->mode)) {
941 wpa_printf(MSG_WARNING, "Failed to set operating mode");
945 #ifdef IEEE8021X_EAPOL
946 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
948 if (ssid->non_leap == 0)
949 algs = AUTH_ALG_LEAP;
951 algs |= AUTH_ALG_LEAP;
954 #endif /* IEEE8021X_EAPOL */
955 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
956 if (ssid->auth_alg) {
958 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
959 algs |= AUTH_ALG_OPEN_SYSTEM;
960 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
961 algs |= AUTH_ALG_SHARED_KEY;
962 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
963 algs |= AUTH_ALG_LEAP;
964 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
967 wpa_drv_set_auth_alg(wpa_s, algs);
969 if (bss && (wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
970 wpa_scan_get_ie(bss, WLAN_EID_RSN)) &&
971 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
972 WPA_KEY_MGMT_FT_IEEE8021X |
973 WPA_KEY_MGMT_FT_PSK |
974 WPA_KEY_MGMT_IEEE8021X_SHA256 |
975 WPA_KEY_MGMT_PSK_SHA256))) {
976 int try_opportunistic;
977 try_opportunistic = ssid->proactive_key_caching &&
978 (ssid->proto & WPA_PROTO_RSN);
979 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
981 try_opportunistic) == 0)
982 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
983 wpa_ie_len = sizeof(wpa_ie);
984 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
985 wpa_ie, &wpa_ie_len)) {
986 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
987 "management and encryption suites");
990 } else if (ssid->key_mgmt &
991 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
992 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
993 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
994 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
995 wpa_ie_len = sizeof(wpa_ie);
996 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
997 wpa_ie, &wpa_ie_len)) {
998 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
999 "management and encryption suites (no scan "
1004 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1008 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1010 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1011 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1012 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1013 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1014 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1016 if (wpa_set_wep_keys(wpa_s, ssid)) {
1022 #ifdef IEEE8021X_EAPOL
1023 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1024 if ((ssid->eapol_flags &
1025 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1026 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1030 /* Assume that dynamic WEP-104 keys will be used and
1031 * set cipher suites in order for drivers to expect
1033 cipher_pairwise = cipher_group = CIPHER_WEP104;
1036 #endif /* IEEE8021X_EAPOL */
1038 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1039 /* Set the key before (and later after) association */
1040 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1043 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1044 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1045 os_memset(¶ms, 0, sizeof(params));
1047 const u8 *ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
1048 params.bssid = bss->bssid;
1049 params.ssid = ie ? ie + 2 : (u8 *) "";
1050 params.ssid_len = ie ? ie[1] : 0;
1051 params.freq = bss->freq;
1053 params.ssid = ssid->ssid;
1054 params.ssid_len = ssid->ssid_len;
1056 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1057 params.freq = ssid->frequency; /* Initial channel for IBSS */
1058 params.wpa_ie = wpa_ie;
1059 params.wpa_ie_len = wpa_ie_len;
1060 params.pairwise_suite = cipher_pairwise;
1061 params.group_suite = cipher_group;
1062 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1063 params.auth_alg = algs;
1064 params.mode = ssid->mode;
1065 for (i = 0; i < NUM_WEP_KEYS; i++) {
1066 if (ssid->wep_key_len[i])
1067 params.wep_key[i] = ssid->wep_key[i];
1068 params.wep_key_len[i] = ssid->wep_key_len[i];
1070 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1072 if (wpa_s->driver_4way_handshake &&
1073 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1074 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1075 params.passphrase = ssid->passphrase;
1077 params.psk = ssid->psk;
1080 #ifdef CONFIG_IEEE80211W
1081 switch (ssid->ieee80211w) {
1083 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1085 case IEEE80211W_OPTIONAL:
1086 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1088 case IEEE80211W_REQUIRED:
1089 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1092 #endif /* CONFIG_IEEE80211W */
1094 if (wpa_s->use_client_mlme)
1095 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1097 ret = wpa_drv_associate(wpa_s, ¶ms);
1099 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1101 /* try to continue anyway; new association will be tried again
1106 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1107 /* Set the key after the association just in case association
1108 * cleared the previously configured key. */
1109 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1110 /* No need to timeout authentication since there is no key
1112 wpa_supplicant_cancel_auth_timeout(wpa_s);
1113 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1115 /* Timeout for IEEE 802.11 authentication and association */
1119 /* give IBSS a bit more time */
1120 timeout = ssid->mode ? 10 : 5;
1121 } else if (wpa_s->conf->ap_scan == 1) {
1122 /* give IBSS a bit more time */
1123 timeout = ssid->mode ? 20 : 10;
1125 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1128 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1129 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1130 /* Set static WEP keys again */
1131 wpa_set_wep_keys(wpa_s, ssid);
1134 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1136 * Do not allow EAP session resumption between different
1137 * network configurations.
1139 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1141 wpa_s->current_ssid = ssid;
1142 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1143 wpa_supplicant_initiate_eapol(wpa_s);
1148 * wpa_supplicant_disassociate - Disassociate the current connection
1149 * @wpa_s: Pointer to wpa_supplicant data
1150 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1152 * This function is used to request %wpa_supplicant to disassociate with the
1155 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1159 if (!is_zero_ether_addr(wpa_s->bssid)) {
1160 if (wpa_s->use_client_mlme)
1161 ieee80211_sta_disassociate(wpa_s, reason_code);
1163 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1164 addr = wpa_s->bssid;
1166 wpa_clear_keys(wpa_s, addr);
1167 wpa_supplicant_mark_disassoc(wpa_s);
1168 wpa_s->current_ssid = NULL;
1169 wpa_sm_set_config(wpa_s->wpa, NULL);
1170 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1175 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1176 * @wpa_s: Pointer to wpa_supplicant data
1177 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1179 * This function is used to request %wpa_supplicant to disassociate with the
1182 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1186 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1187 if (!is_zero_ether_addr(wpa_s->bssid)) {
1188 if (wpa_s->use_client_mlme)
1189 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1191 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1193 addr = wpa_s->bssid;
1195 wpa_clear_keys(wpa_s, addr);
1196 wpa_s->current_ssid = NULL;
1197 wpa_sm_set_config(wpa_s->wpa, NULL);
1198 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1199 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1200 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1204 static int wpa_supplicant_get_scan_results_old(struct wpa_supplicant *wpa_s)
1206 #define SCAN_AP_LIMIT 128
1207 struct wpa_scan_result *results;
1209 struct wpa_scan_results *res;
1211 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1212 if (results == NULL) {
1213 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1218 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1219 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1221 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1225 if (num > SCAN_AP_LIMIT) {
1226 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1227 num, SCAN_AP_LIMIT);
1228 num = SCAN_AP_LIMIT;
1231 wpa_scan_results_free(wpa_s->scan_res);
1232 wpa_s->scan_res = NULL;
1234 /* Convert old scan result data structure to the new one */
1235 res = os_zalloc(sizeof(*res));
1240 res->res = os_zalloc(num * sizeof(struct wpa_scan_res *));
1241 if (res->res == NULL) {
1247 for (i = 0; i < num; i++) {
1248 struct wpa_scan_result *bss = &results[i];
1249 struct wpa_scan_res *r;
1253 ie_len = 2 + bss->ssid_len + bss->rsn_ie_len + bss->wpa_ie_len;
1256 if (bss->mdie_present)
1259 r = os_zalloc(sizeof(*r) + ie_len);
1263 os_memcpy(r->bssid, bss->bssid, ETH_ALEN);
1264 r->freq = bss->freq;
1265 r->caps = bss->caps;
1266 r->qual = bss->qual;
1267 r->noise = bss->noise;
1268 r->level = bss->level;
1272 pos = (u8 *) (r + 1);
1275 *pos++ = WLAN_EID_SSID;
1276 *pos++ = bss->ssid_len;
1277 os_memcpy(pos, bss->ssid, bss->ssid_len);
1278 pos += bss->ssid_len;
1281 /* Fake Supported Rate IE to include max rate */
1282 *pos++ = WLAN_EID_SUPP_RATES;
1284 *pos++ = bss->maxrate;
1287 if (bss->rsn_ie_len) {
1288 os_memcpy(pos, bss->rsn_ie, bss->rsn_ie_len);
1289 pos += bss->rsn_ie_len;
1292 if (bss->mdie_present) {
1293 os_memcpy(pos, bss->mdie, 5);
1297 if (bss->wpa_ie_len) {
1298 os_memcpy(pos, bss->wpa_ie, bss->wpa_ie_len);
1299 pos += bss->wpa_ie_len;
1302 res->res[res->num++] = r;
1306 wpa_s->scan_res = res;
1313 * wpa_supplicant_get_scan_results - Get scan results
1314 * @wpa_s: Pointer to wpa_supplicant data
1315 * Returns: 0 on success, -1 on failure
1317 * This function is request the current scan results from the driver and stores
1318 * a local copy of the results in wpa_s->scan_res.
1320 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1324 if (wpa_s->use_client_mlme) {
1325 wpa_scan_results_free(wpa_s->scan_res);
1326 wpa_s->scan_res = ieee80211_sta_get_scan_results(wpa_s);
1327 if (wpa_s->scan_res == NULL) {
1328 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1332 } else if (wpa_s->driver->get_scan_results2 == NULL)
1333 ret = wpa_supplicant_get_scan_results_old(wpa_s);
1335 wpa_scan_results_free(wpa_s->scan_res);
1336 wpa_s->scan_res = wpa_drv_get_scan_results2(wpa_s);
1337 if (wpa_s->scan_res == NULL) {
1338 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1344 if (wpa_s->scan_res)
1345 wpa_scan_sort_results(wpa_s->scan_res);
1352 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1353 * @wpa_s: Pointer to wpa_supplicant data
1354 * Returns: A pointer to the current network structure or %NULL on failure
1356 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1358 struct wpa_ssid *entry;
1359 u8 ssid[MAX_SSID_LEN];
1365 if (wpa_s->use_client_mlme) {
1366 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1367 wpa_printf(MSG_WARNING, "Could not read SSID from "
1372 res = wpa_drv_get_ssid(wpa_s, ssid);
1374 wpa_printf(MSG_WARNING, "Could not read SSID from "
1381 if (wpa_s->use_client_mlme)
1382 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1383 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1384 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1388 wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
1389 os_strcmp(wpa_s->driver->name, "wired") == 0;
1391 entry = wpa_s->conf->ssid;
1393 if (!entry->disabled &&
1394 ((ssid_len == entry->ssid_len &&
1395 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1396 (!entry->bssid_set ||
1397 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1399 entry = entry->next;
1406 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1414 if (wpa_supplicant_drivers[0] == NULL) {
1415 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1421 /* default to first driver in the list */
1422 wpa_s->driver = wpa_supplicant_drivers[0];
1426 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1427 if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1428 wpa_s->driver = wpa_supplicant_drivers[i];
1433 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
1438 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1439 const u8 *buf, size_t len)
1441 struct wpa_supplicant *wpa_s = ctx;
1443 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1444 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1446 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1447 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1448 "no key management is configured");
1452 if (wpa_s->eapol_received == 0 &&
1453 (!wpa_s->driver_4way_handshake ||
1454 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1455 wpa_s->wpa_state != WPA_COMPLETED)) {
1456 /* Timeout for completing IEEE 802.1X and WPA authentication */
1457 wpa_supplicant_req_auth_timeout(
1459 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1460 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1463 wpa_s->eapol_received++;
1465 if (wpa_s->countermeasures) {
1466 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1471 /* Source address of the incoming EAPOL frame could be compared to the
1472 * current BSSID. However, it is possible that a centralized
1473 * Authenticator could be using another MAC address than the BSSID of
1474 * an AP, so just allow any address to be used for now. The replies are
1475 * still sent to the current BSSID (if available), though. */
1477 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1478 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1479 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1481 wpa_drv_poll(wpa_s);
1482 if (!wpa_s->driver_4way_handshake)
1483 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1484 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1486 * Set portValid = TRUE here since we are going to skip 4-way
1487 * handshake processing which would normally set portValid. We
1488 * need this to allow the EAPOL state machines to be completed
1489 * without going through EAPOL-Key handshake.
1491 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1496 void wpa_supplicant_sta_free_hw_features(struct wpa_hw_modes *hw_features,
1497 size_t num_hw_features)
1499 ieee80211_sta_free_hw_features(hw_features, num_hw_features);
1503 void wpa_supplicant_sta_rx(void *ctx, const u8 *buf, size_t len,
1504 struct ieee80211_rx_status *rx_status)
1506 struct wpa_supplicant *wpa_s = ctx;
1507 ieee80211_sta_rx(wpa_s, buf, len, rx_status);
1512 * wpa_supplicant_driver_init - Initialize driver interface parameters
1513 * @wpa_s: Pointer to wpa_supplicant data
1514 * Returns: 0 on success, -1 on failure
1516 * This function is called to initialize driver interface parameters.
1517 * wpa_drv_init() must have been called before this function to initialize the
1520 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
1522 static int interface_count = 0;
1524 if (wpa_s->driver->send_eapol) {
1525 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1527 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1529 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1530 wpa_drv_get_mac_addr(wpa_s),
1532 wpa_supplicant_rx_eapol, wpa_s, 0);
1533 if (wpa_s->l2 == NULL)
1537 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1538 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
1542 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1543 MAC2STR(wpa_s->own_addr));
1545 if (wpa_s->bridge_ifname[0]) {
1546 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
1547 " '%s'", wpa_s->bridge_ifname);
1548 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
1551 wpa_supplicant_rx_eapol, wpa_s,
1553 if (wpa_s->l2_br == NULL) {
1554 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1555 "connection for the bridge interface '%s'",
1556 wpa_s->bridge_ifname);
1561 /* Backwards compatibility call to set_wpa() handler. This is called
1562 * only just after init and just before deinit, so these handler can be
1563 * used to implement same functionality. */
1564 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1565 struct wpa_driver_capa capa;
1566 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1567 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1568 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1569 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1570 /* Continue to allow non-WPA modes to be used. */
1572 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
1578 wpa_clear_keys(wpa_s, NULL);
1580 /* Make sure that TKIP countermeasures are not left enabled (could
1581 * happen if wpa_supplicant is killed during countermeasures. */
1582 wpa_drv_set_countermeasures(wpa_s, 0);
1584 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1586 wpa_printf(MSG_DEBUG, "RSN: flushing PMKID list in the driver");
1587 wpa_drv_flush_pmkid(wpa_s);
1589 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1590 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1597 static int wpa_supplicant_daemon(const char *pid_file)
1599 wpa_printf(MSG_DEBUG, "Daemonize..");
1600 return os_daemonize(pid_file);
1604 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1606 struct wpa_supplicant *wpa_s;
1608 wpa_s = os_zalloc(sizeof(*wpa_s));
1611 wpa_s->scan_req = 1;
1617 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1618 struct wpa_interface *iface)
1620 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1621 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
1622 iface->confname ? iface->confname : "N/A",
1623 iface->driver ? iface->driver : "default",
1624 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
1625 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
1627 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
1631 if (iface->confname) {
1632 #ifdef CONFIG_BACKEND_FILE
1633 wpa_s->confname = os_rel2abs_path(iface->confname);
1634 if (wpa_s->confname == NULL) {
1635 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1636 "for configuration file '%s'.",
1640 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1641 iface->confname, wpa_s->confname);
1642 #else /* CONFIG_BACKEND_FILE */
1643 wpa_s->confname = os_strdup(iface->confname);
1644 #endif /* CONFIG_BACKEND_FILE */
1645 wpa_s->conf = wpa_config_read(wpa_s->confname);
1646 if (wpa_s->conf == NULL) {
1647 wpa_printf(MSG_ERROR, "Failed to read or parse "
1648 "configuration '%s'.", wpa_s->confname);
1653 * Override ctrl_interface and driver_param if set on command
1656 if (iface->ctrl_interface) {
1657 os_free(wpa_s->conf->ctrl_interface);
1658 wpa_s->conf->ctrl_interface =
1659 os_strdup(iface->ctrl_interface);
1662 if (iface->driver_param) {
1663 os_free(wpa_s->conf->driver_param);
1664 wpa_s->conf->driver_param =
1665 os_strdup(iface->driver_param);
1668 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1669 iface->driver_param);
1671 if (wpa_s->conf == NULL) {
1672 wpa_printf(MSG_ERROR, "\nNo configuration found.");
1676 if (iface->ifname == NULL) {
1677 wpa_printf(MSG_ERROR, "\nInterface name is required.");
1680 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1681 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
1685 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1687 if (iface->bridge_ifname) {
1688 if (os_strlen(iface->bridge_ifname) >=
1689 sizeof(wpa_s->bridge_ifname)) {
1690 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
1691 "name '%s'.", iface->bridge_ifname);
1694 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
1695 sizeof(wpa_s->bridge_ifname));
1702 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s)
1705 struct wpa_driver_capa capa;
1707 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
1710 /* RSNA Supplicant Key Management - INITIALIZE */
1711 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1712 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1714 /* Initialize driver interface and register driver event handler before
1715 * L2 receive handler so that association events are processed before
1716 * EAPOL-Key packets if both become available for the same select()
1718 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1719 if (wpa_s->drv_priv == NULL) {
1720 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
1723 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1724 wpa_printf(MSG_ERROR, "Driver interface rejected "
1725 "driver_param '%s'", wpa_s->conf->driver_param);
1729 ifname = wpa_drv_get_ifname(wpa_s);
1730 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
1731 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1732 "name with '%s'", ifname);
1733 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1736 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1739 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
1740 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
1742 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1744 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1745 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1746 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1747 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1748 "dot11RSNAConfigPMKLifetime");
1752 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1753 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1754 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1755 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1756 "dot11RSNAConfigPMKReauthThreshold");
1760 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1761 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1762 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1763 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
1764 "dot11RSNAConfigSATimeout");
1768 if (wpa_supplicant_driver_init(wpa_s) < 0)
1771 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1773 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1775 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1777 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1778 if (wpa_s->ctrl_iface == NULL) {
1779 wpa_printf(MSG_ERROR,
1780 "Failed to initialize control interface '%s'.\n"
1781 "You may have another wpa_supplicant process "
1782 "already running or the file was\n"
1783 "left by an unclean termination of wpa_supplicant "
1784 "in which case you will need\n"
1785 "to manually remove this file before starting "
1786 "wpa_supplicant again.\n",
1787 wpa_s->conf->ctrl_interface);
1791 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
1792 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1793 wpa_s->use_client_mlme = 1;
1794 if (ieee80211_sta_init(wpa_s))
1797 if (capa.flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE)
1798 wpa_s->driver_4way_handshake = 1;
1805 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1807 if (wpa_s->drv_priv) {
1808 wpa_supplicant_deauthenticate(wpa_s,
1809 WLAN_REASON_DEAUTH_LEAVING);
1811 /* Backwards compatibility call to set_wpa() handler. This is
1812 * called only just after init and just before deinit, so these
1813 * handler can be used to implement same functionality. */
1814 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1815 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
1819 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1820 wpa_drv_set_countermeasures(wpa_s, 0);
1821 wpa_clear_keys(wpa_s, NULL);
1824 wpas_dbus_unregister_iface(wpa_s);
1826 wpa_supplicant_cleanup(wpa_s);
1828 if (wpa_s->drv_priv)
1829 wpa_drv_deinit(wpa_s);
1834 * wpa_supplicant_add_iface - Add a new network interface
1835 * @global: Pointer to global data from wpa_supplicant_init()
1836 * @iface: Interface configuration options
1837 * Returns: Pointer to the created interface or %NULL on failure
1839 * This function is used to add new network interfaces for %wpa_supplicant.
1840 * This can be called before wpa_supplicant_run() to add interfaces before the
1841 * main event loop has been started. In addition, new interfaces can be added
1842 * dynamically while %wpa_supplicant is already running. This could happen,
1843 * e.g., when a hotplug network adapter is inserted.
1845 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1846 struct wpa_interface *iface)
1848 struct wpa_supplicant *wpa_s;
1850 if (global == NULL || iface == NULL)
1853 wpa_s = wpa_supplicant_alloc();
1857 if (wpa_supplicant_init_iface(wpa_s, iface) ||
1858 wpa_supplicant_init_iface2(wpa_s)) {
1859 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
1861 wpa_supplicant_deinit_iface(wpa_s);
1866 wpa_s->global = global;
1868 /* Register the interface with the dbus control interface */
1869 if (wpas_dbus_register_iface(wpa_s)) {
1870 wpa_supplicant_deinit_iface(wpa_s);
1875 wpa_s->next = global->ifaces;
1876 global->ifaces = wpa_s;
1878 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
1885 * wpa_supplicant_remove_iface - Remove a network interface
1886 * @global: Pointer to global data from wpa_supplicant_init()
1887 * @wpa_s: Pointer to the network interface to be removed
1888 * Returns: 0 if interface was removed, -1 if interface was not found
1890 * This function can be used to dynamically remove network interfaces from
1891 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
1892 * addition, this function is used to remove all remaining interfaces when
1893 * %wpa_supplicant is terminated.
1895 int wpa_supplicant_remove_iface(struct wpa_global *global,
1896 struct wpa_supplicant *wpa_s)
1898 struct wpa_supplicant *prev;
1900 /* Remove interface from the global list of interfaces */
1901 prev = global->ifaces;
1902 if (prev == wpa_s) {
1903 global->ifaces = wpa_s->next;
1905 while (prev && prev->next != wpa_s)
1909 prev->next = wpa_s->next;
1912 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
1914 wpa_supplicant_deinit_iface(wpa_s);
1922 * wpa_supplicant_get_iface - Get a new network interface
1923 * @global: Pointer to global data from wpa_supplicant_init()
1924 * @ifname: Interface name
1925 * Returns: Pointer to the interface or %NULL if not found
1927 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
1930 struct wpa_supplicant *wpa_s;
1932 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1933 if (os_strcmp(wpa_s->ifname, ifname) == 0)
1941 * wpa_supplicant_init - Initialize %wpa_supplicant
1942 * @params: Parameters for %wpa_supplicant
1943 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
1945 * This function is used to initialize %wpa_supplicant. After successful
1946 * initialization, the returned data pointer can be used to add and remove
1947 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
1949 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
1951 struct wpa_global *global;
1957 wpa_debug_open_file(params->wpa_debug_file_path);
1959 ret = eap_peer_register_methods();
1961 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1963 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
1964 "the same EAP type.");
1968 global = os_zalloc(sizeof(*global));
1971 global->params.daemonize = params->daemonize;
1972 global->params.wait_for_monitor = params->wait_for_monitor;
1973 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
1974 if (params->pid_file)
1975 global->params.pid_file = os_strdup(params->pid_file);
1976 if (params->ctrl_interface)
1977 global->params.ctrl_interface =
1978 os_strdup(params->ctrl_interface);
1979 wpa_debug_level = global->params.wpa_debug_level =
1980 params->wpa_debug_level;
1981 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
1982 params->wpa_debug_show_keys;
1983 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
1984 params->wpa_debug_timestamp;
1986 if (eloop_init(global)) {
1987 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1988 wpa_supplicant_deinit(global);
1992 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
1993 if (global->ctrl_iface == NULL) {
1994 wpa_supplicant_deinit(global);
1998 if (global->params.dbus_ctrl_interface) {
1999 global->dbus_ctrl_iface =
2000 wpa_supplicant_dbus_ctrl_iface_init(global);
2001 if (global->dbus_ctrl_iface == NULL) {
2002 wpa_supplicant_deinit(global);
2012 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2013 * @global: Pointer to global data from wpa_supplicant_init()
2014 * Returns: 0 after successful event loop run, -1 on failure
2016 * This function starts the main event loop and continues running as long as
2017 * there are any remaining events. In most cases, this function is running as
2018 * long as the %wpa_supplicant process in still in use.
2020 int wpa_supplicant_run(struct wpa_global *global)
2022 struct wpa_supplicant *wpa_s;
2024 if (global->params.daemonize &&
2025 wpa_supplicant_daemon(global->params.pid_file))
2028 if (global->params.wait_for_monitor) {
2029 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2030 if (wpa_s->ctrl_iface)
2031 wpa_supplicant_ctrl_iface_wait(
2035 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2036 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2045 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2046 * @global: Pointer to global data from wpa_supplicant_init()
2048 * This function is called to deinitialize %wpa_supplicant and to free all
2049 * allocated resources. Remaining network interfaces will also be removed.
2051 void wpa_supplicant_deinit(struct wpa_global *global)
2056 while (global->ifaces)
2057 wpa_supplicant_remove_iface(global, global->ifaces);
2059 if (global->ctrl_iface)
2060 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2061 if (global->dbus_ctrl_iface)
2062 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2064 eap_peer_unregister_methods();
2068 if (global->params.pid_file) {
2069 os_daemonize_terminate(global->params.pid_file);
2070 os_free(global->params.pid_file);
2072 os_free(global->params.ctrl_interface);
2075 wpa_debug_close_file();