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