3 * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.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.
24 #ifndef CONFIG_NATIVE_WINDOWS
25 #include <netinet/in.h>
26 #endif /* CONFIG_NATIVE_WINDOWS */
33 #include "wpa_supplicant.h"
35 #include "l2_packet.h"
36 #include "wpa_supplicant_i.h"
37 #include "ctrl_iface.h"
38 #include "pcsc_funcs.h"
43 const char *wpa_supplicant_version =
44 "wpa_supplicant v" VERSION_STR "\n"
45 "Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.fi> and contributors";
47 const char *wpa_supplicant_license =
48 "This program is free software. You can distribute it and/or modify it\n"
49 "under the terms of the GNU General Public License version 2.\n"
51 "Alternatively, this software may be distributed under the terms of the\n"
52 "BSD license. See README and COPYING for more details.\n"
54 "\nThis product includes software developed by the OpenSSL Project\n"
55 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
56 #endif /* EAP_TLS_FUNCS */
59 #ifndef CONFIG_NO_STDOUT_DEBUG
60 const char *wpa_supplicant_full_license =
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 "You should have received a copy of the GNU General Public License\n"
71 "along with this program; if not, write to the Free Software\n"
72 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
74 "Alternatively, this software may be distributed under the terms of the\n"
77 "Redistribution and use in source and binary forms, with or without\n"
78 "modification, are permitted provided that the following conditions are\n"
81 "1. Redistributions of source code must retain the above copyright\n"
82 " notice, this list of conditions and the following disclaimer.\n"
84 "2. Redistributions in binary form must reproduce the above copyright\n"
85 " notice, this list of conditions and the following disclaimer in the\n"
86 " documentation and/or other materials provided with the distribution.\n"
88 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
89 " names of its contributors may be used to endorse or promote products\n"
90 " derived from this software without specific prior written permission.\n"
92 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
93 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
94 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
95 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
96 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
97 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
98 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
99 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
100 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
101 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
102 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
104 #endif /* CONFIG_NO_STDOUT_DEBUG */
106 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
108 extern int wpa_debug_level;
109 extern int wpa_debug_show_keys;
110 extern int wpa_debug_timestamp;
112 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx);
114 void wpa_msg(struct wpa_supplicant *wpa_s, int level, char *fmt, ...)
118 const int buflen = 2048;
121 buf = malloc(buflen);
123 printf("Failed to allocate message buffer for:\n");
131 len = vsnprintf(buf, buflen, fmt, ap);
133 wpa_printf(level, "%s", buf);
134 wpa_supplicant_ctrl_iface_send(wpa_s, level, buf, len);
139 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
140 const void *data, u16 data_len,
141 size_t *msg_len, void **data_pos)
143 struct ieee802_1x_hdr *hdr;
145 *msg_len = sizeof(*hdr) + data_len;
146 hdr = malloc(*msg_len);
150 hdr->version = wpa_s->conf->eapol_version;
152 hdr->length = htons(data_len);
155 memcpy(hdr + 1, data, data_len);
157 memset(hdr + 1, 0, data_len);
167 * wpa_ether_send - Send Ethernet frame
168 * @wpa_s: pointer to wpa_supplicant data
169 * @dest: Destination MAC address
171 * @buf: Frame payload starting from IEEE 802.1X header
172 * @len: Frame payload length
174 int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest, u16 proto,
175 const u8 *buf, size_t len)
178 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
181 return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
185 #ifdef IEEE8021X_EAPOL
187 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
188 * @ctx: pointer to wpa_supplicant data
189 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
190 * @buf: EAPOL payload (after IEEE 802.1X header)
191 * @len: EAPOL payload length
193 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
194 * to the current Authenticator.
196 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
199 struct wpa_supplicant *wpa_s = ctx;
200 u8 *msg, *dst, bssid[ETH_ALEN];
204 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
207 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
208 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
209 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
210 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
212 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
213 "mode (type=%d len=%lu)", type,
214 (unsigned long) len);
218 if (pmksa_cache_get_current(wpa_s->wpa) &&
219 type == IEEE802_1X_TYPE_EAPOL_START) {
220 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
221 * since they will trigger full EAPOL authentication. */
222 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
227 if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0) {
228 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
230 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
231 memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
233 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
234 " from the driver as the EAPOL destination",
237 dst = wpa_s->last_eapol_src;
238 wpa_printf(MSG_DEBUG, "Using the source address of the"
239 " last received EAPOL frame " MACSTR " as "
240 "the EAPOL destination",
244 /* BSSID was already set (from (Re)Assoc event, so use it as
245 * the EAPOL destination. */
249 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
253 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
254 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
261 * wpa_eapol_set_wep_key - set WEP key for the driver
262 * @ctx: pointer to wpa_supplicant data
263 * @unicast: 1 = individual unicast key, 0 = broadcast key
264 * @keyidx: WEP key index (0..3)
265 * @key: pointer to key data
266 * @keylen: key length in bytes
268 * Returns 0 on success or < 0 on error.
270 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
271 const u8 *key, size_t keylen)
273 struct wpa_supplicant *wpa_s = ctx;
274 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
275 unicast ? wpa_s->bssid :
276 (u8 *) "\xff\xff\xff\xff\xff\xff",
277 keyidx, unicast, (u8 *) "", 0, key, keylen);
279 #endif /* IEEE8021X_EAPOL */
282 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
283 static void wpa_supplicant_set_config_blob(void *ctx,
284 struct wpa_config_blob *blob)
286 struct wpa_supplicant *wpa_s = ctx;
287 wpa_config_set_blob(wpa_s->conf, blob);
291 static const struct wpa_config_blob *
292 wpa_supplicant_get_config_blob(void *ctx, const char *name)
294 struct wpa_supplicant *wpa_s = ctx;
295 return wpa_config_get_blob(wpa_s->conf, name);
297 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
300 /* Configure default/group WEP key for static WEP */
301 static int wpa_set_wep_key(void *ctx, int set_tx, int keyidx, const u8 *key,
304 struct wpa_supplicant *wpa_s = ctx;
305 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
306 (u8 *) "\xff\xff\xff\xff\xff\xff",
307 keyidx, set_tx, (u8 *) "", 0, key, keylen);
311 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
312 struct wpa_ssid *ssid)
319 /* IBSS/WPA-None uses only one key (Group) for both receiving and
320 * sending unicast and multicast packets. */
322 if (ssid->mode != IEEE80211_MODE_IBSS) {
323 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
324 "for WPA-None", ssid->mode);
328 if (!ssid->psk_set) {
329 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
333 switch (wpa_s->group_cipher) {
334 case WPA_CIPHER_CCMP:
335 memcpy(key, ssid->psk, 16);
339 case WPA_CIPHER_TKIP:
340 /* WPA-None uses the same Michael MIC key for both TX and RX */
341 memcpy(key, ssid->psk, 16 + 8);
342 memcpy(key + 16 + 8, ssid->psk + 16, 8);
347 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
348 "WPA-None", wpa_s->group_cipher);
352 /* TODO: should actually remember the previously used seq#, both for TX
353 * and RX from each STA.. */
355 return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
356 0, 1, seq, 6, key, keylen);
360 #ifdef IEEE8021X_EAPOL
361 static void wpa_supplicant_notify_eapol_done(void *ctx)
363 struct wpa_supplicant *wpa_s = ctx;
364 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
365 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
366 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
368 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
369 wpa_supplicant_cancel_auth_timeout(wpa_s);
370 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
373 #endif /* IEEE8021X_EAPOL */
376 struct wpa_blacklist * wpa_blacklist_get(struct wpa_supplicant *wpa_s,
379 struct wpa_blacklist *e;
381 e = wpa_s->blacklist;
383 if (memcmp(e->bssid, bssid, ETH_ALEN) == 0)
392 int wpa_blacklist_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
394 struct wpa_blacklist *e;
396 e = wpa_blacklist_get(wpa_s, bssid);
399 wpa_printf(MSG_DEBUG, "BSSID " MACSTR " blacklist count "
401 MAC2STR(bssid), e->count);
405 e = malloc(sizeof(*e));
408 memset(e, 0, sizeof(*e));
409 memcpy(e->bssid, bssid, ETH_ALEN);
411 e->next = wpa_s->blacklist;
412 wpa_s->blacklist = e;
413 wpa_printf(MSG_DEBUG, "Added BSSID " MACSTR " into blacklist",
420 int wpa_blacklist_del(struct wpa_supplicant *wpa_s, const u8 *bssid)
422 struct wpa_blacklist *e, *prev = NULL;
424 e = wpa_s->blacklist;
426 if (memcmp(e->bssid, bssid, ETH_ALEN) == 0) {
428 wpa_s->blacklist = e->next;
430 prev->next = e->next;
432 wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
433 "blacklist", MAC2STR(bssid));
444 void wpa_blacklist_clear(struct wpa_supplicant *wpa_s)
446 struct wpa_blacklist *e, *prev;
448 e = wpa_s->blacklist;
449 wpa_s->blacklist = NULL;
453 wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
454 "blacklist (clear)", MAC2STR(prev->bssid));
460 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len)
462 static char ssid_txt[MAX_SSID_LEN + 1];
465 if (ssid_len > MAX_SSID_LEN)
466 ssid_len = MAX_SSID_LEN;
467 memcpy(ssid_txt, ssid, ssid_len);
468 ssid_txt[ssid_len] = '\0';
469 for (pos = ssid_txt; *pos != '\0'; pos++) {
470 if ((u8) *pos < 32 || (u8) *pos >= 127)
477 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
479 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
481 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
482 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
486 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
488 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
489 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
493 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
495 struct wpa_supplicant *wpa_s = eloop_ctx;
496 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
497 MAC2STR(wpa_s->bssid));
498 wpa_blacklist_add(wpa_s, wpa_s->bssid);
499 wpa_sm_notify_disassoc(wpa_s->wpa);
500 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
501 wpa_s->reassociate = 1;
502 wpa_supplicant_req_scan(wpa_s, 0, 0);
506 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
509 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
510 wpa_s->driver && strcmp(wpa_s->driver->name, "wired") == 0)
513 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
514 "%d usec", sec, usec);
515 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
516 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
520 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
522 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
523 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
524 wpa_blacklist_del(wpa_s, wpa_s->bssid);
528 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
530 struct eapol_config eapol_conf;
531 struct wpa_ssid *ssid = wpa_s->current_ssid;
533 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
534 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
535 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
537 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
538 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
539 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
541 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
543 memset(&eapol_conf, 0, sizeof(eapol_conf));
544 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
545 eapol_conf.accept_802_1x_keys = 1;
546 eapol_conf.required_keys = 0;
547 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
548 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
550 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
551 eapol_conf.required_keys |=
552 EAPOL_REQUIRE_KEY_BROADCAST;
555 if (wpa_s->conf && wpa_s->driver &&
556 strcmp(wpa_s->driver->name, "wired") == 0) {
557 eapol_conf.required_keys = 0;
560 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
561 eapol_conf.workaround = ssid->eap_workaround;
562 eapol_conf.eap_disabled = wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
563 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA;
564 eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
568 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
569 struct wpa_ssid *ssid)
573 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
574 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
576 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
577 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
578 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
579 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
580 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
581 wpa_s->group_cipher = WPA_CIPHER_NONE;
583 for (i = 0; i < NUM_WEP_KEYS; i++) {
584 if (ssid->wep_key_len[i] > 5) {
585 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
586 wpa_s->group_cipher = WPA_CIPHER_WEP104;
588 } else if (ssid->wep_key_len[i] > 0) {
589 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
590 wpa_s->group_cipher = WPA_CIPHER_WEP40;
595 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
596 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
597 wpa_s->pairwise_cipher);
598 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
600 pmksa_cache_clear_current(wpa_s->wpa);
604 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
606 scard_deinit(wpa_s->scard);
608 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
609 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
610 l2_packet_deinit(wpa_s->l2);
613 wpa_supplicant_ctrl_iface_deinit(wpa_s);
614 if (wpa_s->conf != NULL) {
615 wpa_config_free(wpa_s->conf);
619 free(wpa_s->confname);
620 wpa_s->confname = NULL;
622 wpa_sm_set_eapol(wpa_s->wpa, NULL);
623 eapol_sm_deinit(wpa_s->eapol);
626 rsn_preauth_deinit(wpa_s->wpa);
628 pmksa_candidate_free(wpa_s->wpa);
629 pmksa_cache_free(wpa_s->wpa);
630 wpa_sm_deinit(wpa_s->wpa);
632 wpa_blacklist_clear(wpa_s);
634 free(wpa_s->scan_results);
635 wpa_s->scan_results = NULL;
636 wpa_s->num_scan_results = 0;
638 wpa_supplicant_cancel_scan(wpa_s);
642 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
644 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
646 if (wpa_s->keys_cleared) {
647 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
648 * timing issues with keys being cleared just before new keys
649 * are set or just after association or something similar. This
650 * shows up in group key handshake failing often because of the
651 * client not receiving the first encrypted packets correctly.
652 * Skipping some of the extra key clearing steps seems to help
653 * in completing group key handshake more reliably. */
654 wpa_printf(MSG_DEBUG, "No keys have been configured - "
655 "skip key clearing");
659 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
660 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
661 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
662 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
664 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
667 wpa_s->keys_cleared = 1;
671 const char * wpa_supplicant_state_txt(int state)
674 case WPA_DISCONNECTED:
675 return "DISCONNECTED";
680 case WPA_ASSOCIATING:
681 return "ASSOCIATING";
684 case WPA_4WAY_HANDSHAKE:
685 return "4WAY_HANDSHAKE";
686 case WPA_GROUP_HANDSHAKE:
687 return "GROUP_HANDSHAKE";
696 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
698 wpa_printf(MSG_DEBUG, "State: %s -> %s",
699 wpa_supplicant_state_txt(wpa_s->wpa_state),
700 wpa_supplicant_state_txt(state));
701 if (state == WPA_COMPLETED && wpa_s->new_connection) {
702 wpa_s->new_connection = 0;
703 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
704 MACSTR " completed %s",
705 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
706 "(reauth)" : "(auth)");
707 wpa_s->reassociated_connection = 1;
708 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
709 state == WPA_ASSOCIATED) {
710 wpa_s->new_connection = 1;
712 wpa_s->wpa_state = state;
716 wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
718 return wpa_s->wpa_state;
722 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
725 struct wpa_global *global = eloop_ctx;
726 struct wpa_supplicant *wpa_s;
727 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
728 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
735 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
737 struct wpa_config *conf;
739 if (wpa_s->confname == NULL)
741 conf = wpa_config_read(wpa_s->confname);
743 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
744 "file '%s' - exiting", wpa_s->confname);
748 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
749 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
750 strcmp(conf->ctrl_interface, wpa_s->conf->ctrl_interface)
754 wpa_supplicant_ctrl_iface_deinit(wpa_s);
756 wpa_s->current_ssid = NULL;
758 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
759 * pkcs11_engine_path, pkcs11_module_path.
761 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
762 wpa_sm_set_config(wpa_s->wpa, NULL);
763 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
764 pmksa_cache_notify_reconfig(wpa_s->wpa);
765 rsn_preauth_deinit(wpa_s->wpa);
766 wpa_config_free(wpa_s->conf);
769 wpa_supplicant_ctrl_iface_init(wpa_s);
770 wpa_s->reassociate = 1;
771 wpa_supplicant_req_scan(wpa_s, 0, 0);
772 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
777 #ifndef CONFIG_NATIVE_WINDOWS
778 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
781 struct wpa_global *global = eloop_ctx;
782 struct wpa_supplicant *wpa_s;
783 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
784 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
785 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
790 #endif /* CONFIG_NATIVE_WINDOWS */
793 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
795 struct wpa_ssid *ssid;
796 union wpa_event_data data;
798 ssid = wpa_supplicant_get_ssid(wpa_s);
802 if (wpa_s->current_ssid == NULL)
803 wpa_s->current_ssid = ssid;
804 wpa_supplicant_initiate_eapol(wpa_s);
805 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
806 "generating associated event");
807 memset(&data, 0, sizeof(data));
808 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
812 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
814 struct wpa_supplicant *wpa_s = eloop_ctx;
815 struct wpa_ssid *ssid;
816 int enabled, scan_req = 0;
818 if (wpa_s->disconnected)
822 ssid = wpa_s->conf->ssid;
824 if (!ssid->disabled) {
830 if (!enabled && !wpa_s->scan_req) {
831 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
832 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
835 scan_req = wpa_s->scan_req;
838 if (wpa_s->conf->ap_scan == 0) {
839 wpa_supplicant_gen_assoc_event(wpa_s);
843 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
844 wpa_s->wpa_state == WPA_INACTIVE)
845 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
847 ssid = wpa_s->conf->ssid;
848 if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
850 if (ssid == wpa_s->prev_scan_ssid) {
858 if (!ssid->disabled &&
859 (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
864 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
866 * ap_scan=2 mode - try to associate with each SSID instead of
867 * scanning for each scan_ssid=1 network.
872 /* Continue from the next SSID on the next attempt. */
873 wpa_s->prev_scan_ssid = ssid;
875 /* Start from the beginning of the SSID list. */
876 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
878 wpa_supplicant_associate(wpa_s, NULL, ssid);
882 wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
883 ssid ? "specific": "broadcast");
885 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
886 ssid->ssid, ssid->ssid_len);
887 wpa_s->prev_scan_ssid = ssid;
889 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
891 if (wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
892 ssid ? ssid->ssid_len : 0)) {
893 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
894 wpa_supplicant_req_scan(wpa_s, 10, 0);
899 static wpa_cipher cipher_suite2driver(int cipher)
902 case WPA_CIPHER_NONE:
904 case WPA_CIPHER_WEP40:
906 case WPA_CIPHER_WEP104:
907 return CIPHER_WEP104;
908 case WPA_CIPHER_CCMP:
910 case WPA_CIPHER_TKIP:
917 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
920 case WPA_KEY_MGMT_NONE:
921 return KEY_MGMT_NONE;
922 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
923 return KEY_MGMT_802_1X_NO_WPA;
924 case WPA_KEY_MGMT_IEEE8021X:
925 return KEY_MGMT_802_1X;
926 case WPA_KEY_MGMT_WPA_NONE:
927 return KEY_MGMT_WPA_NONE;
928 case WPA_KEY_MGMT_PSK:
935 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
936 struct wpa_ssid *ssid,
937 struct wpa_ie_data *ie)
939 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
942 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
943 "from association info");
948 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
950 if (!(ie->group_cipher & ssid->group_cipher)) {
951 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
952 "cipher 0x%x (mask 0x%x) - reject",
953 ie->group_cipher, ssid->group_cipher);
956 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
957 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
958 "cipher 0x%x (mask 0x%x) - reject",
959 ie->pairwise_cipher, ssid->pairwise_cipher);
962 if (!(ie->key_mgmt & ssid->key_mgmt)) {
963 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
964 "management 0x%x (mask 0x%x) - reject",
965 ie->key_mgmt, ssid->key_mgmt);
973 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
974 struct wpa_scan_result *bss,
975 struct wpa_ssid *ssid,
976 u8 *wpa_ie, size_t *wpa_ie_len)
978 struct wpa_ie_data ie;
981 if (bss && bss->rsn_ie_len && (ssid->proto & WPA_PROTO_RSN) &&
982 wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie) == 0 &&
983 (ie.group_cipher & ssid->group_cipher) &&
984 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
985 (ie.key_mgmt & ssid->key_mgmt)) {
986 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
987 proto = WPA_PROTO_RSN;
988 } else if (bss && bss->wpa_ie_len && (ssid->proto & WPA_PROTO_WPA) &&
989 wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie) == 0 &&
990 (ie.group_cipher & ssid->group_cipher) &&
991 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
992 (ie.key_mgmt & ssid->key_mgmt)) {
993 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
994 proto = WPA_PROTO_WPA;
996 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
999 if (ssid->proto & WPA_PROTO_RSN)
1000 proto = WPA_PROTO_RSN;
1002 proto = WPA_PROTO_WPA;
1003 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1004 memset(&ie, 0, sizeof(ie));
1005 ie.group_cipher = ssid->group_cipher;
1006 ie.pairwise_cipher = ssid->pairwise_cipher;
1007 ie.key_mgmt = ssid->key_mgmt;
1008 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
1009 "on configuration");
1013 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1014 "pairwise %d key_mgmt %d",
1015 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt);
1017 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1019 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss ? bss->wpa_ie : NULL,
1020 bss ? bss->wpa_ie_len : 0) ||
1021 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss ? bss->rsn_ie : NULL,
1022 bss ? bss->rsn_ie_len : 0))
1025 sel = ie.group_cipher & ssid->group_cipher;
1026 if (sel & WPA_CIPHER_CCMP) {
1027 wpa_s->group_cipher = WPA_CIPHER_CCMP;
1028 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
1029 } else if (sel & WPA_CIPHER_TKIP) {
1030 wpa_s->group_cipher = WPA_CIPHER_TKIP;
1031 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
1032 } else if (sel & WPA_CIPHER_WEP104) {
1033 wpa_s->group_cipher = WPA_CIPHER_WEP104;
1034 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
1035 } else if (sel & WPA_CIPHER_WEP40) {
1036 wpa_s->group_cipher = WPA_CIPHER_WEP40;
1037 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
1039 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
1043 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1044 if (sel & WPA_CIPHER_CCMP) {
1045 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1046 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1047 } else if (sel & WPA_CIPHER_TKIP) {
1048 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1049 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1050 } else if (sel & WPA_CIPHER_NONE) {
1051 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1052 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1054 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
1059 sel = ie.key_mgmt & ssid->key_mgmt;
1060 if (sel & WPA_KEY_MGMT_IEEE8021X) {
1061 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1062 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1063 } else if (sel & WPA_KEY_MGMT_PSK) {
1064 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1065 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1066 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1067 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1068 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1070 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
1071 "key management type.");
1075 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1076 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1077 wpa_s->pairwise_cipher);
1078 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1080 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1081 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
1085 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1086 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1088 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1094 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1095 struct wpa_scan_result *bss,
1096 struct wpa_ssid *ssid)
1101 int algs = AUTH_ALG_OPEN_SYSTEM;
1102 int cipher_pairwise, cipher_group;
1103 struct wpa_driver_associate_params params;
1104 int wep_keys_set = 0;
1105 struct wpa_driver_capa capa;
1106 int assoc_failed = 0;
1108 wpa_s->reassociate = 0;
1110 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1111 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1112 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1113 memset(wpa_s->bssid, 0, ETH_ALEN);
1115 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1116 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1118 wpa_supplicant_cancel_scan(wpa_s);
1120 /* Starting new association, so clear the possibly used WPA IE from the
1121 * previous association. */
1122 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1124 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1126 if (ssid->non_leap == 0)
1127 algs = AUTH_ALG_LEAP;
1129 algs |= AUTH_ALG_LEAP;
1132 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1133 if (ssid->auth_alg) {
1135 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1136 algs |= AUTH_ALG_OPEN_SYSTEM;
1137 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1138 algs |= AUTH_ALG_SHARED_KEY;
1139 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1140 algs |= AUTH_ALG_LEAP;
1141 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1144 wpa_drv_set_auth_alg(wpa_s, algs);
1146 if (bss && (bss->wpa_ie_len || bss->rsn_ie_len) &&
1147 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK))) {
1148 int try_opportunistic;
1149 try_opportunistic = ssid->proactive_key_caching &&
1150 (ssid->proto & WPA_PROTO_RSN);
1151 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1152 wpa_s->current_ssid,
1153 try_opportunistic) == 0)
1154 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1155 wpa_ie_len = sizeof(wpa_ie);
1156 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1157 wpa_ie, &wpa_ie_len)) {
1158 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1159 "management and encryption suites");
1162 } else if (ssid->key_mgmt &
1163 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1164 WPA_KEY_MGMT_WPA_NONE)) {
1165 wpa_ie_len = sizeof(wpa_ie);
1166 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1167 wpa_ie, &wpa_ie_len)) {
1168 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1169 "management and encryption suites (no scan "
1174 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1178 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1180 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1181 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1182 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1183 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1185 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1187 for (i = 0; i < NUM_WEP_KEYS; i++) {
1188 if (ssid->wep_key_len[i]) {
1191 wpa_set_wep_key(wpa_s,
1192 i == ssid->wep_tx_keyidx,
1193 i, ssid->wep_key[i],
1194 ssid->wep_key_len[i]);
1199 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1200 if ((ssid->eapol_flags &
1201 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1202 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1206 /* Assume that dynamic WEP-104 keys will be used and
1207 * set cipher suites in order for drivers to expect
1209 cipher_pairwise = cipher_group = CIPHER_WEP104;
1213 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1214 /* Set the key before (and later after) association */
1215 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1218 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1219 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1220 memset(¶ms, 0, sizeof(params));
1222 params.bssid = bss->bssid;
1223 params.ssid = bss->ssid;
1224 params.ssid_len = bss->ssid_len;
1225 params.freq = bss->freq;
1227 params.ssid = ssid->ssid;
1228 params.ssid_len = ssid->ssid_len;
1230 params.wpa_ie = wpa_ie;
1231 params.wpa_ie_len = wpa_ie_len;
1232 params.pairwise_suite = cipher_pairwise;
1233 params.group_suite = cipher_group;
1234 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1235 params.auth_alg = algs;
1236 params.mode = ssid->mode;
1237 if (wpa_drv_associate(wpa_s, ¶ms) < 0) {
1238 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1240 /* try to continue anyway; new association will be tried again
1245 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1246 /* Set the key after the association just in case association
1247 * cleared the previously configured key. */
1248 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1249 /* No need to timeout authentication since there is no key
1251 wpa_supplicant_cancel_auth_timeout(wpa_s);
1252 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1254 /* Timeout for IEEE 802.11 authentication and association */
1258 else if (wpa_s->conf->ap_scan == 1)
1262 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1265 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1266 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1267 /* Set static WEP keys again */
1269 for (i = 0; i < NUM_WEP_KEYS; i++) {
1270 if (ssid->wep_key_len[i]) {
1271 wpa_set_wep_key(wpa_s,
1272 i == ssid->wep_tx_keyidx,
1273 i, ssid->wep_key[i],
1274 ssid->wep_key_len[i]);
1279 wpa_s->current_ssid = ssid;
1280 wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
1281 wpa_supplicant_initiate_eapol(wpa_s);
1285 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1289 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1290 if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
1291 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1292 addr = wpa_s->bssid;
1294 wpa_clear_keys(wpa_s, addr);
1295 wpa_s->current_ssid = NULL;
1296 wpa_sm_set_config(wpa_s->wpa, NULL);
1297 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1298 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1299 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1303 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1307 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1308 if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
1309 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1310 addr = wpa_s->bssid;
1312 wpa_clear_keys(wpa_s, addr);
1313 wpa_s->current_ssid = NULL;
1314 wpa_sm_set_config(wpa_s->wpa, NULL);
1315 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1316 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1317 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1321 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1323 #define SCAN_AP_LIMIT 128
1324 struct wpa_scan_result *results, *tmp;
1327 results = malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1328 if (results == NULL) {
1329 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1334 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1335 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1337 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1341 if (num > SCAN_AP_LIMIT) {
1342 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1343 num, SCAN_AP_LIMIT);
1344 num = SCAN_AP_LIMIT;
1347 /* Free unneeded memory for unused scan result entries */
1348 tmp = realloc(results, num * sizeof(struct wpa_scan_result));
1349 if (tmp || num == 0) {
1353 free(wpa_s->scan_results);
1354 wpa_s->scan_results = results;
1355 wpa_s->num_scan_results = num;
1361 #ifndef CONFIG_NO_WPA
1362 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
1365 struct wpa_scan_result *results, *curr = NULL;
1367 results = wpa_s->scan_results;
1368 if (results == NULL) {
1372 for (i = 0; i < wpa_s->num_scan_results; i++) {
1373 if (memcmp(results[i].bssid, wpa_s->bssid, ETH_ALEN) == 0) {
1380 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, curr->wpa_ie,
1381 curr->wpa_ie_len) ||
1382 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, curr->rsn_ie,
1393 static int wpa_supplicant_get_beacon_ie(void *ctx)
1395 struct wpa_supplicant *wpa_s = ctx;
1396 if (wpa_get_beacon_ie(wpa_s) == 0) {
1400 /* No WPA/RSN IE found in the cached scan results. Try to get updated
1401 * scan results from the driver. */
1402 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
1406 return wpa_get_beacon_ie(wpa_s);
1408 #endif /* CONFIG_NO_WPA */
1412 * wpa_supplicant_get_ssid - get a pointer to the current network structure
1413 * @wpa_s: pointer to wpa_supplicant data
1415 * Returns: a pointer to the current network structure or %NULL on failure
1417 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1419 struct wpa_ssid *entry;
1420 u8 ssid[MAX_SSID_LEN];
1424 ssid_len = wpa_drv_get_ssid(wpa_s, ssid);
1426 wpa_printf(MSG_WARNING, "Could not read SSID from driver.");
1430 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1431 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1435 entry = wpa_s->conf->ssid;
1437 if (!entry->disabled &&
1438 ssid_len == entry->ssid_len &&
1439 memcmp(ssid, entry->ssid, ssid_len) == 0 &&
1440 (!entry->bssid_set ||
1441 memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1443 entry = entry->next;
1450 #ifndef CONFIG_NO_WPA
1451 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
1452 const void *data, u16 data_len,
1453 size_t *msg_len, void **data_pos)
1455 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
1459 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
1460 const u8 *buf, size_t len)
1462 return wpa_ether_send(wpa_s, dest, proto, buf, len);
1466 static void _wpa_supplicant_req_scan(void *wpa_s, int sec, int usec)
1468 wpa_supplicant_req_scan(wpa_s, sec, usec);
1472 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
1474 wpa_supplicant_cancel_auth_timeout(wpa_s);
1478 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
1480 wpa_supplicant_set_state(wpa_s, state);
1484 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
1486 return wpa_supplicant_get_state(wpa_s);
1490 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
1492 wpa_supplicant_disassociate(wpa_s, reason_code);
1496 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
1498 wpa_supplicant_deauthenticate(wpa_s, reason_code);
1502 static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s)
1504 return wpa_supplicant_get_ssid(wpa_s);
1508 static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
1510 return wpa_drv_get_bssid(wpa_s, bssid);
1514 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
1515 const u8 *addr, int key_idx, int set_tx,
1516 const u8 *seq, size_t seq_len,
1517 const u8 *key, size_t key_len)
1519 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
1524 static int wpa_supplicant_add_pmkid(void *wpa_s,
1525 const u8 *bssid, const u8 *pmkid)
1527 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
1531 static int wpa_supplicant_remove_pmkid(void *wpa_s,
1532 const u8 *bssid, const u8 *pmkid)
1534 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
1536 #endif /* CONFIG_NO_WPA */
1539 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1547 if (wpa_supplicant_drivers[0] == NULL) {
1548 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1554 /* default to first driver in the list */
1555 wpa_s->driver = wpa_supplicant_drivers[0];
1559 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1560 if (strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1561 wpa_s->driver = wpa_supplicant_drivers[i];
1566 printf("Unsupported driver '%s'.\n", name);
1571 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1572 const u8 *buf, size_t len)
1574 struct wpa_supplicant *wpa_s = ctx;
1576 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1577 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1579 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1580 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1581 "no key management is configured");
1585 if (wpa_s->eapol_received == 0) {
1586 /* Timeout for completing IEEE 802.1X and WPA authentication */
1587 wpa_supplicant_req_auth_timeout(
1589 (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1590 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1593 wpa_s->eapol_received++;
1595 if (wpa_s->countermeasures) {
1596 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1601 /* Source address of the incoming EAPOL frame could be compared to the
1602 * current BSSID. However, it is possible that a centralized
1603 * Authenticator could be using another MAC address than the BSSID of
1604 * an AP, so just allow any address to be used for now. The replies are
1605 * still sent to the current BSSID (if available), though. */
1607 memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1608 if (wpa_s->key_mgmt != WPA_KEY_MGMT_PSK &&
1609 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1611 wpa_drv_poll(wpa_s);
1612 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1616 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s,
1617 int wait_for_interface)
1619 static int interface_count = 0;
1622 if (wpa_s->driver->send_eapol) {
1623 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1625 memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1628 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1629 wpa_drv_get_mac_addr(wpa_s),
1631 wpa_supplicant_rx_eapol, wpa_s, 0);
1634 else if (!wait_for_interface)
1636 printf("Waiting for interface..\n");
1640 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1641 fprintf(stderr, "Failed to get own L2 address\n");
1645 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1646 MAC2STR(wpa_s->own_addr));
1648 /* Backwards compatibility call to set_wpa() handler. This is called
1649 * only just after init and just before deinit, so these handler can be
1650 * used to implement same functionality. */
1651 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1652 struct wpa_driver_capa capa;
1653 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1654 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1655 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1656 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1657 /* Continue to allow non-WPA modes to be used. */
1659 fprintf(stderr, "Failed to enable WPA in the "
1665 wpa_clear_keys(wpa_s, NULL);
1667 /* Make sure that TKIP countermeasures are not left enabled (could
1668 * happen if wpa_supplicant is killed during countermeasures. */
1669 wpa_drv_set_countermeasures(wpa_s, 0);
1671 wpa_drv_set_drop_unencrypted(wpa_s, 1);
1673 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1674 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1681 static int wpa_supplicant_daemon(const char *pid_file)
1683 wpa_printf(MSG_DEBUG, "Daemonize..");
1690 FILE *f = fopen(pid_file, "w");
1692 fprintf(f, "%u\n", getpid());
1701 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1703 struct wpa_supplicant *wpa_s;
1705 wpa_s = malloc(sizeof(*wpa_s));
1708 memset(wpa_s, 0, sizeof(*wpa_s));
1709 wpa_s->ctrl_sock = -1;
1710 wpa_s->scan_req = 1;
1716 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1717 struct wpa_interface *iface)
1719 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1720 "'%s' ctrl_interface '%s'", iface->ifname,
1721 iface->confname ? iface->confname : "N/A",
1722 iface->driver ? iface->driver : "default",
1723 iface->ctrl_interface ? iface->ctrl_interface : "N/A");
1725 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
1729 if (iface->confname) {
1730 #ifdef CONFIG_BACKEND_FILE
1731 wpa_s->confname = rel2abs_path(iface->confname);
1732 if (wpa_s->confname == NULL) {
1733 wpa_printf(MSG_ERROR, "Failed to get absolute path "
1734 "for configuration file '%s'.",
1738 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1739 iface->confname, wpa_s->confname);
1740 #else /* CONFIG_BACKEND_FILE */
1741 wpa_s->confname = strdup(iface->confname);
1742 #endif /* CONFIG_BACKEND_FILE */
1743 wpa_s->conf = wpa_config_read(wpa_s->confname);
1744 if (wpa_s->conf == NULL) {
1745 printf("Failed to read read or parse configuration "
1746 "'%s'.\n", wpa_s->confname);
1751 * Override ctrl_interface and driver_param if set on command
1754 if (iface->ctrl_interface) {
1755 free(wpa_s->conf->ctrl_interface);
1756 wpa_s->conf->ctrl_interface =
1757 strdup(iface->ctrl_interface);
1760 if (iface->driver_param) {
1761 free(wpa_s->conf->driver_param);
1762 wpa_s->conf->driver_param =
1763 strdup(iface->driver_param);
1766 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1767 iface->driver_param);
1769 if (wpa_s->conf == NULL) {
1770 printf("\nNo configuration found.\n");
1774 if (iface->ifname == NULL) {
1775 printf("\nInterface name is required.\n");
1778 if (strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1779 printf("Too long interface name '%s'.\n", iface->ifname);
1782 strncpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1788 static int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
1790 #ifdef IEEE8021X_EAPOL
1791 struct eapol_ctx *ctx;
1792 ctx = malloc(sizeof(*ctx));
1794 printf("Failed to allocate EAPOL context.\n");
1798 memset(ctx, 0, sizeof(*ctx));
1800 ctx->msg_ctx = wpa_s;
1801 ctx->eapol_send_ctx = wpa_s;
1803 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
1804 ctx->eapol_send = wpa_supplicant_eapol_send;
1805 ctx->set_wep_key = wpa_eapol_set_wep_key;
1806 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1807 ctx->get_config_blob = wpa_supplicant_get_config_blob;
1808 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
1809 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
1810 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
1811 wpa_s->eapol = eapol_sm_init(ctx);
1812 if (wpa_s->eapol == NULL) {
1814 printf("Failed to initialize EAPOL state machines.\n");
1817 #endif /* IEEE8021X_EAPOL */
1823 static int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
1825 #ifndef CONFIG_NO_WPA
1826 struct wpa_sm_ctx *ctx;
1827 ctx = malloc(sizeof(*ctx));
1829 printf("Failed to allocate WPA context.\n");
1833 memset(ctx, 0, sizeof(*ctx));
1835 ctx->set_state = _wpa_supplicant_set_state;
1836 ctx->get_state = _wpa_supplicant_get_state;
1837 ctx->req_scan = _wpa_supplicant_req_scan;
1838 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
1839 ctx->disassociate = _wpa_supplicant_disassociate;
1840 ctx->set_key = wpa_supplicant_set_key;
1841 ctx->scan = wpa_supplicant_scan;
1842 ctx->get_ssid = _wpa_supplicant_get_ssid;
1843 ctx->get_bssid = wpa_supplicant_get_bssid;
1844 ctx->ether_send = _wpa_ether_send;
1845 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
1846 ctx->alloc_eapol = _wpa_alloc_eapol;
1847 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
1848 ctx->add_pmkid = wpa_supplicant_add_pmkid;
1849 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
1850 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1851 ctx->get_config_blob = wpa_supplicant_get_config_blob;
1853 wpa_s->wpa = wpa_sm_init(ctx);
1854 if (wpa_s->wpa == NULL) {
1855 fprintf(stderr, "Failed to initialize WPA state machine\n");
1858 #endif /* CONFIG_NO_WPA */
1864 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s,
1865 int wait_for_interface)
1869 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
1872 if (wpa_supplicant_init_eapol(wpa_s) < 0)
1875 /* RSNA Supplicant Key Management - INITIALIZE */
1876 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1877 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1879 /* Initialize driver interface and register driver event handler before
1880 * L2 receive handler so that association events are processed before
1881 * EAPOL-Key packets if both become available for the same select()
1883 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1884 if (wpa_s->drv_priv == NULL) {
1885 fprintf(stderr, "Failed to initialize driver interface\n");
1888 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1889 fprintf(stderr, "Driver interface rejected driver_param "
1890 "'%s'\n", wpa_s->conf->driver_param);
1894 ifname = wpa_drv_get_ifname(wpa_s);
1895 if (ifname && strcmp(ifname, wpa_s->ifname) != 0) {
1896 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1897 "name with '%s'", ifname);
1898 strncpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1901 if (wpa_supplicant_init_wpa(wpa_s) < 0)
1904 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname);
1905 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1906 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1908 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1909 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1910 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1911 fprintf(stderr, "Invalid WPA parameter value for "
1912 "dot11RSNAConfigPMKLifetime\n");
1916 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1917 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1918 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1919 fprintf(stderr, "Invalid WPA parameter value for "
1920 "dot11RSNAConfigPMKReauthThreshold\n");
1924 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1925 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1926 wpa_s->conf->dot11RSNAConfigSATimeout)) {
1927 fprintf(stderr, "Invalid WPA parameter value for "
1928 "dot11RSNAConfigSATimeout\n");
1932 if (wpa_supplicant_driver_init(wpa_s, wait_for_interface) < 0) {
1935 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1937 if (wpa_supplicant_ctrl_iface_init(wpa_s)) {
1938 printf("Failed to initialize control interface '%s'.\n"
1939 "You may have another wpa_supplicant process already "
1940 "running or the file was\n"
1941 "left by an unclean termination of wpa_supplicant in "
1942 "which case you will need\n"
1943 "to manually remove this file before starting "
1944 "wpa_supplicant again.\n",
1945 wpa_s->conf->ctrl_interface);
1953 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1955 if (wpa_s->drv_priv) {
1956 wpa_supplicant_deauthenticate(wpa_s, REASON_DEAUTH_LEAVING);
1958 /* Backwards compatibility call to set_wpa() handler. This is
1959 * called only just after init and just before deinit, so these
1960 * handler can be used to implement same functionality. */
1961 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1962 fprintf(stderr, "Failed to disable WPA in the "
1966 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1967 wpa_drv_set_countermeasures(wpa_s, 0);
1968 wpa_clear_keys(wpa_s, NULL);
1970 wpa_drv_deinit(wpa_s);
1972 wpa_supplicant_cleanup(wpa_s);
1977 * wpa_supplicant_add_iface - Add a new network interface
1978 * @global: Pointer to global data from wpa_supplicant_init()
1979 * @iface: Interface configuration options
1980 * Returns: Pointer to the created interface or %NULL on failure
1982 * This function is used to add new network interfaces for %wpa_supplicant.
1983 * This can be called before wpa_supplicant_run() to add interfaces before the
1984 * main event loop has been started. In addition, new interfaces can be added
1985 * dynamically while %wpa_supplicant is already running. This could happen,
1986 * e.g., when a hotplug network adapter is inserted.
1988 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1989 struct wpa_interface *iface)
1991 struct wpa_supplicant *wpa_s;
1993 if (global == NULL || iface == NULL)
1996 wpa_s = wpa_supplicant_alloc();
2000 if (wpa_supplicant_init_iface(wpa_s, iface) ||
2001 wpa_supplicant_init_iface2(wpa_s,
2002 global->params.wait_for_interface)) {
2003 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2005 wpa_supplicant_deinit_iface(wpa_s);
2010 wpa_s->global = global;
2011 wpa_s->next = global->ifaces;
2012 global->ifaces = wpa_s;
2014 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2021 * wpa_supplicant_remove_iface - Remove a network interface
2022 * @global: Pointer to global data from wpa_supplicant_init()
2023 * @wpa_s: Pointer to the network interface to be removed
2024 * Returns: 0 if interface was removed, -1 if interface was not found
2026 * This function can be used to dynamically remove network interfaces from
2027 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2028 * addition, this function is used to remove all remaining interdaces when
2029 * %wpa_supplicant is terminated.
2031 int wpa_supplicant_remove_iface(struct wpa_global *global,
2032 struct wpa_supplicant *wpa_s)
2034 struct wpa_supplicant *prev;
2036 /* Remove interface from the global list of interfaces */
2037 prev = global->ifaces;
2038 if (prev == wpa_s) {
2039 global->ifaces = wpa_s->next;
2041 while (prev && prev->next != wpa_s)
2045 prev->next = wpa_s->next;
2048 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2050 wpa_supplicant_deinit_iface(wpa_s);
2058 * wpa_supplicant_get_iface - Get a new network interface
2059 * @global: Pointer to global data from wpa_supplicant_init()
2060 * @ifname: Interface name
2061 * Returns: Pointer to the interface or %NULL if not found
2063 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2066 struct wpa_supplicant *wpa_s;
2068 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2069 if (strcmp(wpa_s->ifname, ifname) == 0)
2077 * wpa_supplicant_init - Initialize %wpa_supplicant
2078 * @params: Parameters for %wpa_supplicant
2079 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2081 * This function is used to initialize %wpa_supplicant. After successful
2082 * initialization, the returned data pointer can be used to add and remove
2083 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2085 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2087 struct wpa_global *global;
2091 global = malloc(sizeof(*global));
2094 memset(global, 0, sizeof(*global));
2095 global->params.daemonize = params->daemonize;
2096 global->params.wait_for_interface = params->wait_for_interface;
2097 global->params.wait_for_monitor = params->wait_for_monitor;
2098 if (params->pid_file)
2099 global->params.pid_file = strdup(params->pid_file);
2100 if (params->ctrl_interface)
2101 global->params.ctrl_interface = strdup(params->ctrl_interface);
2102 wpa_debug_level = global->params.wpa_debug_level =
2103 params->wpa_debug_level;
2104 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2105 params->wpa_debug_show_keys;
2106 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2107 params->wpa_debug_timestamp;
2111 if (wpa_supplicant_global_ctrl_iface_init(global)) {
2116 if (global->params.wait_for_interface && global->params.daemonize &&
2117 wpa_supplicant_daemon(global->params.pid_file)) {
2127 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2128 * @global: Pointer to global data from wpa_supplicant_init()
2129 * Returns: 0 after successful event loop run, -1 on failure
2131 * This function starts the main event loop and continues running as long as
2132 * there are any remaining events. In most cases, this function is running as
2133 * long as the %wpa_supplicant process in still in use.
2135 int wpa_supplicant_run(struct wpa_global *global)
2137 struct wpa_supplicant *wpa_s;
2139 if (!global->params.wait_for_interface && global->params.daemonize &&
2140 wpa_supplicant_daemon(global->params.pid_file))
2143 if (global->params.wait_for_monitor) {
2144 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2145 wpa_supplicant_ctrl_iface_wait(wpa_s);
2148 eloop_register_signal(SIGINT, wpa_supplicant_terminate, NULL);
2149 eloop_register_signal(SIGTERM, wpa_supplicant_terminate, NULL);
2150 #ifndef CONFIG_NATIVE_WINDOWS
2151 eloop_register_signal(SIGHUP, wpa_supplicant_reconfig, NULL);
2152 #endif /* CONFIG_NATIVE_WINDOWS */
2161 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2162 * @global: Pointer to global data from wpa_supplicant_init()
2164 * This function is called to deinitialize %wpa_supplicant and to free all
2165 * allocated resources. Remaining network interfaces will also be removed.
2167 void wpa_supplicant_deinit(struct wpa_global *global)
2172 while (global->ifaces)
2173 wpa_supplicant_remove_iface(global, global->ifaces);
2175 wpa_supplicant_global_ctrl_iface_deinit(global);
2179 if (global->params.pid_file) {
2180 unlink(global->params.pid_file);
2181 free(global->params.pid_file);
2183 free(global->params.ctrl_interface);