]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - build-utilities/hostap.git/blob - wpa_supplicant/wpa_supplicant.c
ndis: Work around lack of C99 designated initializers in MSVC
[build-utilities/hostap.git] / wpa_supplicant / wpa_supplicant.c
1 /*
2  * WPA Supplicant
3  * Copyright (c) 2003-2011, 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 "crypto/random.h"
23 #include "crypto/sha1.h"
24 #include "eapol_supp/eapol_supp_sm.h"
25 #include "eap_peer/eap.h"
26 #include "eap_server/eap_methods.h"
27 #include "rsn_supp/wpa.h"
28 #include "eloop.h"
29 #include "config.h"
30 #include "l2_packet/l2_packet.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "ctrl_iface.h"
34 #include "pcsc_funcs.h"
35 #include "common/version.h"
36 #include "rsn_supp/preauth.h"
37 #include "rsn_supp/pmksa_cache.h"
38 #include "common/wpa_ctrl.h"
39 #include "common/ieee802_11_defs.h"
40 #include "p2p/p2p.h"
41 #include "blacklist.h"
42 #include "wpas_glue.h"
43 #include "wps_supplicant.h"
44 #include "ibss_rsn.h"
45 #include "sme.h"
46 #include "gas_query.h"
47 #include "ap.h"
48 #include "p2p_supplicant.h"
49 #include "notify.h"
50 #include "bgscan.h"
51 #include "bss.h"
52 #include "scan.h"
53 #include "offchannel.h"
55 const char *wpa_supplicant_version =
56 "wpa_supplicant v" VERSION_STR "\n"
57 "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
59 const char *wpa_supplicant_license =
60 "This program is free software. You can distribute it and/or modify it\n"
61 "under the terms of the GNU General Public License version 2.\n"
62 "\n"
63 "Alternatively, this software may be distributed under the terms of the\n"
64 "BSD license. See README and COPYING for more details.\n"
65 #ifdef EAP_TLS_OPENSSL
66 "\nThis product includes software developed by the OpenSSL Project\n"
67 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
68 #endif /* EAP_TLS_OPENSSL */
69 ;
71 #ifndef CONFIG_NO_STDOUT_DEBUG
72 /* Long text divided into parts in order to fit in C89 strings size limits. */
73 const char *wpa_supplicant_full_license1 =
74 "This program is free software; you can redistribute it and/or modify\n"
75 "it under the terms of the GNU General Public License version 2 as\n"
76 "published by the Free Software Foundation.\n"
77 "\n"
78 "This program is distributed in the hope that it will be useful,\n"
79 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
80 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
81 "GNU General Public License for more details.\n"
82 "\n";
83 const char *wpa_supplicant_full_license2 =
84 "You should have received a copy of the GNU General Public License\n"
85 "along with this program; if not, write to the Free Software\n"
86 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
87 "\n"
88 "Alternatively, this software may be distributed under the terms of the\n"
89 "BSD license.\n"
90 "\n"
91 "Redistribution and use in source and binary forms, with or without\n"
92 "modification, are permitted provided that the following conditions are\n"
93 "met:\n"
94 "\n";
95 const char *wpa_supplicant_full_license3 =
96 "1. Redistributions of source code must retain the above copyright\n"
97 "   notice, this list of conditions and the following disclaimer.\n"
98 "\n"
99 "2. Redistributions in binary form must reproduce the above copyright\n"
100 "   notice, this list of conditions and the following disclaimer in the\n"
101 "   documentation and/or other materials provided with the distribution.\n"
102 "\n";
103 const char *wpa_supplicant_full_license4 =
104 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
105 "   names of its contributors may be used to endorse or promote products\n"
106 "   derived from this software without specific prior written permission.\n"
107 "\n"
108 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
109 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
110 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
111 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
112 const char *wpa_supplicant_full_license5 =
113 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
114 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
115 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
116 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
117 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
118 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
119 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
120 "\n";
121 #endif /* CONFIG_NO_STDOUT_DEBUG */
123 extern int wpa_debug_level;
124 extern int wpa_debug_show_keys;
125 extern int wpa_debug_timestamp;
126 extern struct wpa_driver_ops *wpa_drivers[];
128 /* Configure default/group WEP keys for static WEP */
129 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
131         int i, set = 0;
133         for (i = 0; i < NUM_WEP_KEYS; i++) {
134                 if (ssid->wep_key_len[i] == 0)
135                         continue;
137                 set = 1;
138                 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
139                                 i, i == ssid->wep_tx_keyidx, NULL, 0,
140                                 ssid->wep_key[i], ssid->wep_key_len[i]);
141         }
143         return set;
147 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
148                                            struct wpa_ssid *ssid)
150         u8 key[32];
151         size_t keylen;
152         enum wpa_alg alg;
153         u8 seq[6] = { 0 };
155         /* IBSS/WPA-None uses only one key (Group) for both receiving and
156          * sending unicast and multicast packets. */
158         if (ssid->mode != WPAS_MODE_IBSS) {
159                 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
160                         "IBSS/ad-hoc) for WPA-None", ssid->mode);
161                 return -1;
162         }
164         if (!ssid->psk_set) {
165                 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
166                         "WPA-None");
167                 return -1;
168         }
170         switch (wpa_s->group_cipher) {
171         case WPA_CIPHER_CCMP:
172                 os_memcpy(key, ssid->psk, 16);
173                 keylen = 16;
174                 alg = WPA_ALG_CCMP;
175                 break;
176         case WPA_CIPHER_TKIP:
177                 /* WPA-None uses the same Michael MIC key for both TX and RX */
178                 os_memcpy(key, ssid->psk, 16 + 8);
179                 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
180                 keylen = 32;
181                 alg = WPA_ALG_TKIP;
182                 break;
183         default:
184                 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
185                         "WPA-None", wpa_s->group_cipher);
186                 return -1;
187         }
189         /* TODO: should actually remember the previously used seq#, both for TX
190          * and RX from each STA.. */
192         return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
196 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
198         struct wpa_supplicant *wpa_s = eloop_ctx;
199         const u8 *bssid = wpa_s->bssid;
200         if (is_zero_ether_addr(bssid))
201                 bssid = wpa_s->pending_bssid;
202         wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
203                 MAC2STR(bssid));
204         wpa_blacklist_add(wpa_s, bssid);
205         wpa_sm_notify_disassoc(wpa_s->wpa);
206         wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
207         wpa_s->reassociate = 1;
209         /*
210          * If we timed out, the AP or the local radio may be busy.
211          * So, wait a second until scanning again.
212          */
213         wpa_supplicant_req_scan(wpa_s, 1, 0);
217 /**
218  * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
219  * @wpa_s: Pointer to wpa_supplicant data
220  * @sec: Number of seconds after which to time out authentication
221  * @usec: Number of microseconds after which to time out authentication
222  *
223  * This function is used to schedule a timeout for the current authentication
224  * attempt.
225  */
226 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
227                                      int sec, int usec)
229         if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
230             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
231                 return;
233         wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
234                 "%d usec", sec, usec);
235         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
236         eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
240 /**
241  * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
242  * @wpa_s: Pointer to wpa_supplicant data
243  *
244  * This function is used to cancel authentication timeout scheduled with
245  * wpa_supplicant_req_auth_timeout() and it is called when authentication has
246  * been completed.
247  */
248 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
250         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
251         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
252         wpa_blacklist_del(wpa_s, wpa_s->bssid);
256 /**
257  * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
258  * @wpa_s: Pointer to wpa_supplicant data
259  *
260  * This function is used to configure EAPOL state machine based on the selected
261  * authentication mode.
262  */
263 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
265 #ifdef IEEE8021X_EAPOL
266         struct eapol_config eapol_conf;
267         struct wpa_ssid *ssid = wpa_s->current_ssid;
269 #ifdef CONFIG_IBSS_RSN
270         if (ssid->mode == WPAS_MODE_IBSS &&
271             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
272             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
273                 /*
274                  * RSN IBSS authentication is per-STA and we can disable the
275                  * per-BSSID EAPOL authentication.
276                  */
277                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
278                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
279                 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
280                 return;
281         }
282 #endif /* CONFIG_IBSS_RSN */
284         eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
285         eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
287         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
288             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
289                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
290         else
291                 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
293         os_memset(&eapol_conf, 0, sizeof(eapol_conf));
294         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
295                 eapol_conf.accept_802_1x_keys = 1;
296                 eapol_conf.required_keys = 0;
297                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
298                         eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
299                 }
300                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
301                         eapol_conf.required_keys |=
302                                 EAPOL_REQUIRE_KEY_BROADCAST;
303                 }
305                 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
306                         eapol_conf.required_keys = 0;
307         }
308         if (wpa_s->conf)
309                 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
310         eapol_conf.workaround = ssid->eap_workaround;
311         eapol_conf.eap_disabled =
312                 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
313                 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
314                 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
315         eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
316 #endif /* IEEE8021X_EAPOL */
320 /**
321  * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
322  * @wpa_s: Pointer to wpa_supplicant data
323  * @ssid: Configuration data for the network
324  *
325  * This function is used to configure WPA state machine and related parameters
326  * to a mode where WPA is not enabled. This is called as part of the
327  * authentication configuration when the selected network does not use WPA.
328  */
329 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
330                                        struct wpa_ssid *ssid)
332         int i;
334         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
335                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
336         else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
337                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
338         else
339                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
340         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
341         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
342         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
343         wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
344         wpa_s->group_cipher = WPA_CIPHER_NONE;
345         wpa_s->mgmt_group_cipher = 0;
347         for (i = 0; i < NUM_WEP_KEYS; i++) {
348                 if (ssid->wep_key_len[i] > 5) {
349                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
350                         wpa_s->group_cipher = WPA_CIPHER_WEP104;
351                         break;
352                 } else if (ssid->wep_key_len[i] > 0) {
353                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
354                         wpa_s->group_cipher = WPA_CIPHER_WEP40;
355                         break;
356                 }
357         }
359         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
360         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
361         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
362                          wpa_s->pairwise_cipher);
363         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
364 #ifdef CONFIG_IEEE80211W
365         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
366                          wpa_s->mgmt_group_cipher);
367 #endif /* CONFIG_IEEE80211W */
369         pmksa_cache_clear_current(wpa_s->wpa);
373 static void free_hw_features(struct wpa_supplicant *wpa_s)
375         int i;
376         if (wpa_s->hw.modes == NULL)
377                 return;
379         for (i = 0; i < wpa_s->hw.num_modes; i++) {
380                 os_free(wpa_s->hw.modes[i].channels);
381                 os_free(wpa_s->hw.modes[i].rates);
382         }
384         os_free(wpa_s->hw.modes);
385         wpa_s->hw.modes = NULL;
389 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
391         bgscan_deinit(wpa_s);
392         scard_deinit(wpa_s->scard);
393         wpa_s->scard = NULL;
394         wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
395         eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
396         l2_packet_deinit(wpa_s->l2);
397         wpa_s->l2 = NULL;
398         if (wpa_s->l2_br) {
399                 l2_packet_deinit(wpa_s->l2_br);
400                 wpa_s->l2_br = NULL;
401         }
403         if (wpa_s->ctrl_iface) {
404                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
405                 wpa_s->ctrl_iface = NULL;
406         }
407         if (wpa_s->conf != NULL) {
408                 struct wpa_ssid *ssid;
409                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
410                         wpas_notify_network_removed(wpa_s, ssid);
411                 wpa_config_free(wpa_s->conf);
412                 wpa_s->conf = NULL;
413         }
415         os_free(wpa_s->confname);
416         wpa_s->confname = NULL;
418         wpa_sm_set_eapol(wpa_s->wpa, NULL);
419         eapol_sm_deinit(wpa_s->eapol);
420         wpa_s->eapol = NULL;
422         rsn_preauth_deinit(wpa_s->wpa);
424 #ifdef CONFIG_TDLS
425         wpa_tdls_deinit(wpa_s->wpa);
426 #endif /* CONFIG_TDLS */
428         pmksa_candidate_free(wpa_s->wpa);
429         wpa_sm_deinit(wpa_s->wpa);
430         wpa_s->wpa = NULL;
431         wpa_blacklist_clear(wpa_s);
433         wpa_bss_deinit(wpa_s);
435         wpa_supplicant_cancel_scan(wpa_s);
436         wpa_supplicant_cancel_auth_timeout(wpa_s);
437         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
438 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
439         eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
440                              wpa_s, NULL);
441 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
443         wpas_wps_deinit(wpa_s);
445         wpabuf_free(wpa_s->pending_eapol_rx);
446         wpa_s->pending_eapol_rx = NULL;
448 #ifdef CONFIG_IBSS_RSN
449         ibss_rsn_deinit(wpa_s->ibss_rsn);
450         wpa_s->ibss_rsn = NULL;
451 #endif /* CONFIG_IBSS_RSN */
453         sme_deinit(wpa_s);
455 #ifdef CONFIG_AP
456         wpa_supplicant_ap_deinit(wpa_s);
457 #endif /* CONFIG_AP */
459 #ifdef CONFIG_P2P
460         wpas_p2p_deinit(wpa_s);
461 #endif /* CONFIG_P2P */
463 #ifdef CONFIG_OFFCHANNEL
464         offchannel_deinit(wpa_s);
465 #endif /* CONFIG_OFFCHANNEL */
467         wpa_supplicant_cancel_sched_scan(wpa_s);
469         os_free(wpa_s->next_scan_freqs);
470         wpa_s->next_scan_freqs = NULL;
472         gas_query_deinit(wpa_s->gas);
473         wpa_s->gas = NULL;
475         free_hw_features(wpa_s);
479 /**
480  * wpa_clear_keys - Clear keys configured for the driver
481  * @wpa_s: Pointer to wpa_supplicant data
482  * @addr: Previously used BSSID or %NULL if not available
483  *
484  * This function clears the encryption keys that has been previously configured
485  * for the driver.
486  */
487 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
489         if (wpa_s->keys_cleared) {
490                 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
491                  * timing issues with keys being cleared just before new keys
492                  * are set or just after association or something similar. This
493                  * shows up in group key handshake failing often because of the
494                  * client not receiving the first encrypted packets correctly.
495                  * Skipping some of the extra key clearing steps seems to help
496                  * in completing group key handshake more reliably. */
497                 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
498                         "skip key clearing");
499                 return;
500         }
502         /* MLME-DELETEKEYS.request */
503         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
504         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
505         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
506         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
507 #ifdef CONFIG_IEEE80211W
508         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
509         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
510 #endif /* CONFIG_IEEE80211W */
511         if (addr) {
512                 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
513                                 0);
514                 /* MLME-SETPROTECTION.request(None) */
515                 wpa_drv_mlme_setprotection(
516                         wpa_s, addr,
517                         MLME_SETPROTECTION_PROTECT_TYPE_NONE,
518                         MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
519         }
520         wpa_s->keys_cleared = 1;
524 /**
525  * wpa_supplicant_state_txt - Get the connection state name as a text string
526  * @state: State (wpa_state; WPA_*)
527  * Returns: The state name as a printable text string
528  */
529 const char * wpa_supplicant_state_txt(enum wpa_states state)
531         switch (state) {
532         case WPA_DISCONNECTED:
533                 return "DISCONNECTED";
534         case WPA_INACTIVE:
535                 return "INACTIVE";
536         case WPA_INTERFACE_DISABLED:
537                 return "INTERFACE_DISABLED";
538         case WPA_SCANNING:
539                 return "SCANNING";
540         case WPA_AUTHENTICATING:
541                 return "AUTHENTICATING";
542         case WPA_ASSOCIATING:
543                 return "ASSOCIATING";
544         case WPA_ASSOCIATED:
545                 return "ASSOCIATED";
546         case WPA_4WAY_HANDSHAKE:
547                 return "4WAY_HANDSHAKE";
548         case WPA_GROUP_HANDSHAKE:
549                 return "GROUP_HANDSHAKE";
550         case WPA_COMPLETED:
551                 return "COMPLETED";
552         default:
553                 return "UNKNOWN";
554         }
558 #ifdef CONFIG_BGSCAN
560 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
562         if (wpas_driver_bss_selection(wpa_s))
563                 return;
564         if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
565                 return;
567         bgscan_deinit(wpa_s);
568         if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
569                 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
570                         wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
571                                 "bgscan");
572                         /*
573                          * Live without bgscan; it is only used as a roaming
574                          * optimization, so the initial connection is not
575                          * affected.
576                          */
577                 } else
578                         wpa_s->bgscan_ssid = wpa_s->current_ssid;
579         } else
580                 wpa_s->bgscan_ssid = NULL;
584 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
586         if (wpa_s->bgscan_ssid != NULL) {
587                 bgscan_deinit(wpa_s);
588                 wpa_s->bgscan_ssid = NULL;
589         }
592 #endif /* CONFIG_BGSCAN */
595 /**
596  * wpa_supplicant_set_state - Set current connection state
597  * @wpa_s: Pointer to wpa_supplicant data
598  * @state: The new connection state
599  *
600  * This function is called whenever the connection state changes, e.g.,
601  * association is completed for WPA/WPA2 4-Way Handshake is started.
602  */
603 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
604                               enum wpa_states state)
606         enum wpa_states old_state = wpa_s->wpa_state;
608         wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
609                 wpa_supplicant_state_txt(wpa_s->wpa_state),
610                 wpa_supplicant_state_txt(state));
612         if (state != WPA_SCANNING)
613                 wpa_supplicant_notify_scanning(wpa_s, 0);
615         if (state == WPA_COMPLETED && wpa_s->new_connection) {
616 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
617                 struct wpa_ssid *ssid = wpa_s->current_ssid;
618                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
619                         MACSTR " completed %s [id=%d id_str=%s]",
620                         MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
621                         "(reauth)" : "(auth)",
622                         ssid ? ssid->id : -1,
623                         ssid && ssid->id_str ? ssid->id_str : "");
624 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
625                 wpa_s->new_connection = 0;
626                 wpa_s->reassociated_connection = 1;
627                 wpa_drv_set_operstate(wpa_s, 1);
628 #ifndef IEEE8021X_EAPOL
629                 wpa_drv_set_supp_port(wpa_s, 1);
630 #endif /* IEEE8021X_EAPOL */
631                 wpa_s->after_wps = 0;
632 #ifdef CONFIG_P2P
633                 wpas_p2p_completed(wpa_s);
634 #endif /* CONFIG_P2P */
635         } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
636                    state == WPA_ASSOCIATED) {
637                 wpa_s->new_connection = 1;
638                 wpa_drv_set_operstate(wpa_s, 0);
639 #ifndef IEEE8021X_EAPOL
640                 wpa_drv_set_supp_port(wpa_s, 0);
641 #endif /* IEEE8021X_EAPOL */
642         }
643         wpa_s->wpa_state = state;
645 #ifdef CONFIG_BGSCAN
646         if (state == WPA_COMPLETED)
647                 wpa_supplicant_start_bgscan(wpa_s);
648         else
649                 wpa_supplicant_stop_bgscan(wpa_s);
650 #endif /* CONFIG_BGSCAN */
652         if (wpa_s->wpa_state != old_state) {
653                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
655                 if (wpa_s->wpa_state == WPA_COMPLETED ||
656                     old_state == WPA_COMPLETED)
657                         wpas_notify_auth_changed(wpa_s);
658         }
662 void wpa_supplicant_terminate_proc(struct wpa_global *global)
664         int pending = 0;
665 #ifdef CONFIG_WPS
666         struct wpa_supplicant *wpa_s = global->ifaces;
667         while (wpa_s) {
668                 if (wpas_wps_terminate_pending(wpa_s) == 1)
669                         pending = 1;
670                 wpa_s = wpa_s->next;
671         }
672 #endif /* CONFIG_WPS */
673         if (pending)
674                 return;
675         eloop_terminate();
679 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
681         struct wpa_global *global = signal_ctx;
682         struct wpa_supplicant *wpa_s;
683         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
684                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
685                         "received", sig);
686         }
687         wpa_supplicant_terminate_proc(global);
691 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
693         enum wpa_states old_state = wpa_s->wpa_state;
695         wpa_s->pairwise_cipher = 0;
696         wpa_s->group_cipher = 0;
697         wpa_s->mgmt_group_cipher = 0;
698         wpa_s->key_mgmt = 0;
699         if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
700                 wpa_s->wpa_state = WPA_DISCONNECTED;
702         if (wpa_s->wpa_state != old_state)
703                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
707 /**
708  * wpa_supplicant_reload_configuration - Reload configuration data
709  * @wpa_s: Pointer to wpa_supplicant data
710  * Returns: 0 on success or -1 if configuration parsing failed
711  *
712  * This function can be used to request that the configuration data is reloaded
713  * (e.g., after configuration file change). This function is reloading
714  * configuration only for one interface, so this may need to be called multiple
715  * times if %wpa_supplicant is controlling multiple interfaces and all
716  * interfaces need reconfiguration.
717  */
718 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
720         struct wpa_config *conf;
721         int reconf_ctrl;
722         int old_ap_scan;
724         if (wpa_s->confname == NULL)
725                 return -1;
726         conf = wpa_config_read(wpa_s->confname);
727         if (conf == NULL) {
728                 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
729                         "file '%s' - exiting", wpa_s->confname);
730                 return -1;
731         }
732         conf->changed_parameters = (unsigned int) -1;
734         reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
735                 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
736                     os_strcmp(conf->ctrl_interface,
737                               wpa_s->conf->ctrl_interface) != 0);
739         if (reconf_ctrl && wpa_s->ctrl_iface) {
740                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
741                 wpa_s->ctrl_iface = NULL;
742         }
744         eapol_sm_invalidate_cached_session(wpa_s->eapol);
745         if (wpa_s->current_ssid) {
746                 wpa_supplicant_deauthenticate(wpa_s,
747                                               WLAN_REASON_DEAUTH_LEAVING);
748         }
750         /*
751          * TODO: should notify EAPOL SM about changes in opensc_engine_path,
752          * pkcs11_engine_path, pkcs11_module_path.
753          */
754         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
755                 /*
756                  * Clear forced success to clear EAP state for next
757                  * authentication.
758                  */
759                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
760         }
761         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
762         wpa_sm_set_config(wpa_s->wpa, NULL);
763         wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
764         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
765         rsn_preauth_deinit(wpa_s->wpa);
767         old_ap_scan = wpa_s->conf->ap_scan;
768         wpa_config_free(wpa_s->conf);
769         wpa_s->conf = conf;
770         if (old_ap_scan != wpa_s->conf->ap_scan)
771                 wpas_notify_ap_scan_changed(wpa_s);
773         if (reconf_ctrl)
774                 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
776         wpa_supplicant_update_config(wpa_s);
778         wpa_supplicant_clear_status(wpa_s);
779         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
780                 wpa_s->reassociate = 1;
781                 wpa_supplicant_req_scan(wpa_s, 0, 0);
782         }
783         wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
784         return 0;
788 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
790         struct wpa_global *global = signal_ctx;
791         struct wpa_supplicant *wpa_s;
792         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
793                 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
794                         sig);
795                 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
796                         wpa_supplicant_terminate_proc(global);
797                 }
798         }
802 enum wpa_cipher cipher_suite2driver(int cipher)
804         switch (cipher) {
805         case WPA_CIPHER_NONE:
806                 return CIPHER_NONE;
807         case WPA_CIPHER_WEP40:
808                 return CIPHER_WEP40;
809         case WPA_CIPHER_WEP104:
810                 return CIPHER_WEP104;
811         case WPA_CIPHER_CCMP:
812                 return CIPHER_CCMP;
813         case WPA_CIPHER_TKIP:
814         default:
815                 return CIPHER_TKIP;
816         }
820 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
822         switch (key_mgmt) {
823         case WPA_KEY_MGMT_NONE:
824                 return KEY_MGMT_NONE;
825         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
826                 return KEY_MGMT_802_1X_NO_WPA;
827         case WPA_KEY_MGMT_IEEE8021X:
828                 return KEY_MGMT_802_1X;
829         case WPA_KEY_MGMT_WPA_NONE:
830                 return KEY_MGMT_WPA_NONE;
831         case WPA_KEY_MGMT_FT_IEEE8021X:
832                 return KEY_MGMT_FT_802_1X;
833         case WPA_KEY_MGMT_FT_PSK:
834                 return KEY_MGMT_FT_PSK;
835         case WPA_KEY_MGMT_IEEE8021X_SHA256:
836                 return KEY_MGMT_802_1X_SHA256;
837         case WPA_KEY_MGMT_PSK_SHA256:
838                 return KEY_MGMT_PSK_SHA256;
839         case WPA_KEY_MGMT_WPS:
840                 return KEY_MGMT_WPS;
841         case WPA_KEY_MGMT_PSK:
842         default:
843                 return KEY_MGMT_PSK;
844         }
848 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
849                                          struct wpa_ssid *ssid,
850                                          struct wpa_ie_data *ie)
852         int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
853         if (ret) {
854                 if (ret == -2) {
855                         wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
856                                 "from association info");
857                 }
858                 return -1;
859         }
861         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
862                 "cipher suites");
863         if (!(ie->group_cipher & ssid->group_cipher)) {
864                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
865                         "cipher 0x%x (mask 0x%x) - reject",
866                         ie->group_cipher, ssid->group_cipher);
867                 return -1;
868         }
869         if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
870                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
871                         "cipher 0x%x (mask 0x%x) - reject",
872                         ie->pairwise_cipher, ssid->pairwise_cipher);
873                 return -1;
874         }
875         if (!(ie->key_mgmt & ssid->key_mgmt)) {
876                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
877                         "management 0x%x (mask 0x%x) - reject",
878                         ie->key_mgmt, ssid->key_mgmt);
879                 return -1;
880         }
882 #ifdef CONFIG_IEEE80211W
883         if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
884             ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
885                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
886                         "that does not support management frame protection - "
887                         "reject");
888                 return -1;
889         }
890 #endif /* CONFIG_IEEE80211W */
892         return 0;
896 /**
897  * wpa_supplicant_set_suites - Set authentication and encryption parameters
898  * @wpa_s: Pointer to wpa_supplicant data
899  * @bss: Scan results for the selected BSS, or %NULL if not available
900  * @ssid: Configuration data for the selected network
901  * @wpa_ie: Buffer for the WPA/RSN IE
902  * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
903  * used buffer length in case the functions returns success.
904  * Returns: 0 on success or -1 on failure
905  *
906  * This function is used to configure authentication and encryption parameters
907  * based on the network configuration and scan result for the selected BSS (if
908  * available).
909  */
910 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
911                               struct wpa_bss *bss, struct wpa_ssid *ssid,
912                               u8 *wpa_ie, size_t *wpa_ie_len)
914         struct wpa_ie_data ie;
915         int sel, proto;
916         const u8 *bss_wpa, *bss_rsn;
918         if (bss) {
919                 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
920                 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
921         } else
922                 bss_wpa = bss_rsn = NULL;
924         if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
925             wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
926             (ie.group_cipher & ssid->group_cipher) &&
927             (ie.pairwise_cipher & ssid->pairwise_cipher) &&
928             (ie.key_mgmt & ssid->key_mgmt)) {
929                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
930                 proto = WPA_PROTO_RSN;
931         } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
932                    wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
933                    (ie.group_cipher & ssid->group_cipher) &&
934                    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
935                    (ie.key_mgmt & ssid->key_mgmt)) {
936                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
937                 proto = WPA_PROTO_WPA;
938         } else if (bss) {
939                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
940                 return -1;
941         } else {
942                 if (ssid->proto & WPA_PROTO_RSN)
943                         proto = WPA_PROTO_RSN;
944                 else
945                         proto = WPA_PROTO_WPA;
946                 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
947                         os_memset(&ie, 0, sizeof(ie));
948                         ie.group_cipher = ssid->group_cipher;
949                         ie.pairwise_cipher = ssid->pairwise_cipher;
950                         ie.key_mgmt = ssid->key_mgmt;
951 #ifdef CONFIG_IEEE80211W
952                         ie.mgmt_group_cipher =
953                                 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
954                                 WPA_CIPHER_AES_128_CMAC : 0;
955 #endif /* CONFIG_IEEE80211W */
956                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
957                                 "based on configuration");
958                 } else
959                         proto = ie.proto;
960         }
962         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
963                 "pairwise %d key_mgmt %d proto %d",
964                 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
965 #ifdef CONFIG_IEEE80211W
966         if (ssid->ieee80211w) {
967                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
968                         ie.mgmt_group_cipher);
969         }
970 #endif /* CONFIG_IEEE80211W */
972         wpa_s->wpa_proto = proto;
973         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
974         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
975                          !!(ssid->proto & WPA_PROTO_RSN));
977         if (bss || !wpa_s->ap_ies_from_associnfo) {
978                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
979                                          bss_wpa ? 2 + bss_wpa[1] : 0) ||
980                     wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
981                                          bss_rsn ? 2 + bss_rsn[1] : 0))
982                         return -1;
983         }
985         sel = ie.group_cipher & ssid->group_cipher;
986         if (sel & WPA_CIPHER_CCMP) {
987                 wpa_s->group_cipher = WPA_CIPHER_CCMP;
988                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
989         } else if (sel & WPA_CIPHER_TKIP) {
990                 wpa_s->group_cipher = WPA_CIPHER_TKIP;
991                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
992         } else if (sel & WPA_CIPHER_WEP104) {
993                 wpa_s->group_cipher = WPA_CIPHER_WEP104;
994                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
995         } else if (sel & WPA_CIPHER_WEP40) {
996                 wpa_s->group_cipher = WPA_CIPHER_WEP40;
997                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
998         } else {
999                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1000                         "cipher");
1001                 return -1;
1002         }
1004         sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1005         if (sel & WPA_CIPHER_CCMP) {
1006                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1007                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1008         } else if (sel & WPA_CIPHER_TKIP) {
1009                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1010                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1011         } else if (sel & WPA_CIPHER_NONE) {
1012                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1013                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1014         } else {
1015                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1016                         "cipher");
1017                 return -1;
1018         }
1020         sel = ie.key_mgmt & ssid->key_mgmt;
1021         if (0) {
1022 #ifdef CONFIG_IEEE80211R
1023         } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1024                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1025                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1026         } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1027                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1028                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1029 #endif /* CONFIG_IEEE80211R */
1030 #ifdef CONFIG_IEEE80211W
1031         } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1032                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1033                 wpa_dbg(wpa_s, MSG_DEBUG,
1034                         "WPA: using KEY_MGMT 802.1X with SHA256");
1035         } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1036                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1037                 wpa_dbg(wpa_s, MSG_DEBUG,
1038                         "WPA: using KEY_MGMT PSK with SHA256");
1039 #endif /* CONFIG_IEEE80211W */
1040         } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1041                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1042                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1043         } else if (sel & WPA_KEY_MGMT_PSK) {
1044                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1045                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1046         } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1047                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1048                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1049         } else {
1050                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1051                         "authenticated key management type");
1052                 return -1;
1053         }
1055         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1056         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1057                          wpa_s->pairwise_cipher);
1058         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1060 #ifdef CONFIG_IEEE80211W
1061         sel = ie.mgmt_group_cipher;
1062         if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1063             !(ie.capabilities & WPA_CAPABILITY_MFPC))
1064                 sel = 0;
1065         if (sel & WPA_CIPHER_AES_128_CMAC) {
1066                 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1067                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1068                         "AES-128-CMAC");
1069         } else {
1070                 wpa_s->mgmt_group_cipher = 0;
1071                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1072         }
1073         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1074                          wpa_s->mgmt_group_cipher);
1075         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1076 #endif /* CONFIG_IEEE80211W */
1078         if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1079                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1080                 return -1;
1081         }
1083         if (ssid->key_mgmt &
1084             (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1085         {
1086                 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1087 #ifndef CONFIG_NO_PBKDF2
1088                 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1089                     ssid->passphrase) {
1090                         u8 psk[PMK_LEN];
1091                         pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1092                                     bss->ssid_len, 4096, psk, PMK_LEN);
1093                         wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1094                                         psk, PMK_LEN);
1095                         wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1096                 }
1097 #endif /* CONFIG_NO_PBKDF2 */
1098         } else
1099                 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1101         return 0;
1105 /**
1106  * wpa_supplicant_associate - Request association
1107  * @wpa_s: Pointer to wpa_supplicant data
1108  * @bss: Scan results for the selected BSS, or %NULL if not available
1109  * @ssid: Configuration data for the selected network
1110  *
1111  * This function is used to request %wpa_supplicant to associate with a BSS.
1112  */
1113 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1114                               struct wpa_bss *bss, struct wpa_ssid *ssid)
1116         u8 wpa_ie[200];
1117         size_t wpa_ie_len;
1118         int use_crypt, ret, i, bssid_changed;
1119         int algs = WPA_AUTH_ALG_OPEN;
1120         enum wpa_cipher cipher_pairwise, cipher_group;
1121         struct wpa_driver_associate_params params;
1122         int wep_keys_set = 0;
1123         struct wpa_driver_capa capa;
1124         int assoc_failed = 0;
1125         struct wpa_ssid *old_ssid;
1127 #ifdef CONFIG_IBSS_RSN
1128         ibss_rsn_deinit(wpa_s->ibss_rsn);
1129         wpa_s->ibss_rsn = NULL;
1130 #endif /* CONFIG_IBSS_RSN */
1132         if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1133             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1134 #ifdef CONFIG_AP
1135                 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1136                         wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1137                                 "mode");
1138                         return;
1139                 }
1140                 wpa_supplicant_create_ap(wpa_s, ssid);
1141                 wpa_s->current_bss = bss;
1142 #else /* CONFIG_AP */
1143                 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1144                         "the build");
1145 #endif /* CONFIG_AP */
1146                 return;
1147         }
1149 #ifdef CONFIG_TDLS
1150         if (bss)
1151                 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1152                                 bss->ie_len);
1153 #endif /* CONFIG_TDLS */
1155         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1156             ssid->mode == IEEE80211_MODE_INFRA) {
1157                 sme_authenticate(wpa_s, bss, ssid);
1158                 return;
1159         }
1161         os_memset(&params, 0, sizeof(params));
1162         wpa_s->reassociate = 0;
1163         if (bss && !wpas_driver_bss_selection(wpa_s)) {
1164 #ifdef CONFIG_IEEE80211R
1165                 const u8 *ie, *md = NULL;
1166 #endif /* CONFIG_IEEE80211R */
1167                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1168                         " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1169                         wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1170                 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1171                 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1172                 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1173                 if (bssid_changed)
1174                         wpas_notify_bssid_changed(wpa_s);
1175 #ifdef CONFIG_IEEE80211R
1176                 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1177                 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1178                         md = ie + 2;
1179                 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1180                 if (md) {
1181                         /* Prepare for the next transition */
1182                         wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1183                 }
1184 #endif /* CONFIG_IEEE80211R */
1185 #ifdef CONFIG_WPS
1186         } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1187                    wpa_s->conf->ap_scan == 2 &&
1188                    (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1189                 /* Use ap_scan==1 style network selection to find the network
1190                  */
1191                 wpa_s->scan_req = 2;
1192                 wpa_s->reassociate = 1;
1193                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1194                 return;
1195 #endif /* CONFIG_WPS */
1196         } else {
1197                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1198                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1199                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1200         }
1201         wpa_supplicant_cancel_sched_scan(wpa_s);
1202         wpa_supplicant_cancel_scan(wpa_s);
1204         /* Starting new association, so clear the possibly used WPA IE from the
1205          * previous association. */
1206         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1208 #ifdef IEEE8021X_EAPOL
1209         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1210                 if (ssid->leap) {
1211                         if (ssid->non_leap == 0)
1212                                 algs = WPA_AUTH_ALG_LEAP;
1213                         else
1214                                 algs |= WPA_AUTH_ALG_LEAP;
1215                 }
1216         }
1217 #endif /* IEEE8021X_EAPOL */
1218         wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1219         if (ssid->auth_alg) {
1220                 algs = ssid->auth_alg;
1221                 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1222                         "0x%x", algs);
1223         }
1225         if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1226                     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1227             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1228                                WPA_KEY_MGMT_FT_IEEE8021X |
1229                                WPA_KEY_MGMT_FT_PSK |
1230                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
1231                                WPA_KEY_MGMT_PSK_SHA256))) {
1232                 int try_opportunistic;
1233                 try_opportunistic = ssid->proactive_key_caching &&
1234                         (ssid->proto & WPA_PROTO_RSN);
1235                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1236                                             wpa_s->current_ssid,
1237                                             try_opportunistic) == 0)
1238                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1239                 wpa_ie_len = sizeof(wpa_ie);
1240                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1241                                               wpa_ie, &wpa_ie_len)) {
1242                         wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1243                                 "key management and encryption suites");
1244                         return;
1245                 }
1246         } else if (ssid->key_mgmt &
1247                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1248                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1249                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1250                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1251                 wpa_ie_len = sizeof(wpa_ie);
1252                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1253                                               wpa_ie, &wpa_ie_len)) {
1254                         wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1255                                 "key management and encryption suites (no "
1256                                 "scan results)");
1257                         return;
1258                 }
1259 #ifdef CONFIG_WPS
1260         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1261                 struct wpabuf *wps_ie;
1262                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1263                 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1264                         wpa_ie_len = wpabuf_len(wps_ie);
1265                         os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1266                 } else
1267                         wpa_ie_len = 0;
1268                 wpabuf_free(wps_ie);
1269                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1270                 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1271                         params.wps = WPS_MODE_PRIVACY;
1272                 else
1273                         params.wps = WPS_MODE_OPEN;
1274                 wpa_s->wpa_proto = 0;
1275 #endif /* CONFIG_WPS */
1276         } else {
1277                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1278                 wpa_ie_len = 0;
1279                 wpa_s->wpa_proto = 0;
1280         }
1282 #ifdef CONFIG_P2P
1283         if (wpa_s->global->p2p) {
1284                 u8 *pos;
1285                 size_t len;
1286                 int res;
1287                 int p2p_group;
1288                 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1289                 pos = wpa_ie + wpa_ie_len;
1290                 len = sizeof(wpa_ie) - wpa_ie_len;
1291                 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1292                 if (res >= 0)
1293                         wpa_ie_len += res;
1294         }
1296         wpa_s->cross_connect_disallowed = 0;
1297         if (bss) {
1298                 struct wpabuf *p2p;
1299                 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1300                 if (p2p) {
1301                         wpa_s->cross_connect_disallowed =
1302                                 p2p_get_cross_connect_disallowed(p2p);
1303                         wpabuf_free(p2p);
1304                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1305                                 "connection",
1306                                 wpa_s->cross_connect_disallowed ?
1307                                 "disallows" : "allows");
1308                 }
1309         }
1310 #endif /* CONFIG_P2P */
1312 #ifdef CONFIG_INTERWORKING
1313         if (wpa_s->conf->interworking) {
1314                 u8 *pos = wpa_ie;
1315                 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1316                         pos += 2 + pos[1];
1317                 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1318                 wpa_ie_len += 6;
1319                 *pos++ = WLAN_EID_EXT_CAPAB;
1320                 *pos++ = 4;
1321                 *pos++ = 0x00;
1322                 *pos++ = 0x00;
1323                 *pos++ = 0x00;
1324                 *pos++ = 0x80; /* Bit 31 - Interworking */
1325         }
1326 #endif /* CONFIG_INTERWORKING */
1328         wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1329         use_crypt = 1;
1330         cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1331         cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1332         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1333             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1334                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1335                         use_crypt = 0;
1336                 if (wpa_set_wep_keys(wpa_s, ssid)) {
1337                         use_crypt = 1;
1338                         wep_keys_set = 1;
1339                 }
1340         }
1341         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1342                 use_crypt = 0;
1344 #ifdef IEEE8021X_EAPOL
1345         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1346                 if ((ssid->eapol_flags &
1347                      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1348                       EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1349                     !wep_keys_set) {
1350                         use_crypt = 0;
1351                 } else {
1352                         /* Assume that dynamic WEP-104 keys will be used and
1353                          * set cipher suites in order for drivers to expect
1354                          * encryption. */
1355                         cipher_pairwise = cipher_group = CIPHER_WEP104;
1356                 }
1357         }
1358 #endif /* IEEE8021X_EAPOL */
1360         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1361                 /* Set the key before (and later after) association */
1362                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1363         }
1365         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1366         if (bss) {
1367                 params.ssid = bss->ssid;
1368                 params.ssid_len = bss->ssid_len;
1369                 if (!wpas_driver_bss_selection(wpa_s)) {
1370                         params.bssid = bss->bssid;
1371                         params.freq = bss->freq;
1372                 }
1373         } else {
1374                 params.ssid = ssid->ssid;
1375                 params.ssid_len = ssid->ssid_len;
1376         }
1377         if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1378             params.freq == 0)
1379                 params.freq = ssid->frequency; /* Initial channel for IBSS */
1380         params.wpa_ie = wpa_ie;
1381         params.wpa_ie_len = wpa_ie_len;
1382         params.pairwise_suite = cipher_pairwise;
1383         params.group_suite = cipher_group;
1384         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1385         params.wpa_proto = wpa_s->wpa_proto;
1386         params.auth_alg = algs;
1387         params.mode = ssid->mode;
1388         for (i = 0; i < NUM_WEP_KEYS; i++) {
1389                 if (ssid->wep_key_len[i])
1390                         params.wep_key[i] = ssid->wep_key[i];
1391                 params.wep_key_len[i] = ssid->wep_key_len[i];
1392         }
1393         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1395         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1396             (params.key_mgmt_suite == KEY_MGMT_PSK ||
1397              params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1398                 params.passphrase = ssid->passphrase;
1399                 if (ssid->psk_set)
1400                         params.psk = ssid->psk;
1401         }
1403         params.drop_unencrypted = use_crypt;
1405 #ifdef CONFIG_IEEE80211W
1406         params.mgmt_frame_protection = ssid->ieee80211w;
1407         if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1408                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1409                 struct wpa_ie_data ie;
1410                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1411                     ie.capabilities &
1412                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1413                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1414                                 "MFP: require MFP");
1415                         params.mgmt_frame_protection =
1416                                 MGMT_FRAME_PROTECTION_REQUIRED;
1417                 }
1418         }
1419 #endif /* CONFIG_IEEE80211W */
1421         params.p2p = ssid->p2p_group;
1423         if (wpa_s->parent->set_sta_uapsd)
1424                 params.uapsd = wpa_s->parent->sta_uapsd;
1425         else
1426                 params.uapsd = -1;
1428         ret = wpa_drv_associate(wpa_s, &params);
1429         if (ret < 0) {
1430                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1431                         "failed");
1432                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1433                         /*
1434                          * The driver is known to mean what is saying, so we
1435                          * can stop right here; the association will not
1436                          * succeed.
1437                          */
1438                         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1439                         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1440                         return;
1441                 }
1442                 /* try to continue anyway; new association will be tried again
1443                  * after timeout */
1444                 assoc_failed = 1;
1445         }
1447         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1448                 /* Set the key after the association just in case association
1449                  * cleared the previously configured key. */
1450                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1451                 /* No need to timeout authentication since there is no key
1452                  * management. */
1453                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1454                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1455 #ifdef CONFIG_IBSS_RSN
1456         } else if (ssid->mode == WPAS_MODE_IBSS &&
1457                    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1458                    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1459                 /*
1460                  * RSN IBSS authentication is per-STA and we can disable the
1461                  * per-BSSID authentication.
1462                  */
1463                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1464 #endif /* CONFIG_IBSS_RSN */
1465         } else {
1466                 /* Timeout for IEEE 802.11 authentication and association */
1467                 int timeout = 60;
1469                 if (assoc_failed) {
1470                         /* give IBSS a bit more time */
1471                         timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1472                 } else if (wpa_s->conf->ap_scan == 1) {
1473                         /* give IBSS a bit more time */
1474                         timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1475                 }
1476                 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1477         }
1479         if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1480             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1481                 /* Set static WEP keys again */
1482                 wpa_set_wep_keys(wpa_s, ssid);
1483         }
1485         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1486                 /*
1487                  * Do not allow EAP session resumption between different
1488                  * network configurations.
1489                  */
1490                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1491         }
1492         old_ssid = wpa_s->current_ssid;
1493         wpa_s->current_ssid = ssid;
1494         wpa_s->current_bss = bss;
1495         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1496         wpa_supplicant_initiate_eapol(wpa_s);
1497         if (old_ssid != wpa_s->current_ssid)
1498                 wpas_notify_network_changed(wpa_s);
1502 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1503                                             const u8 *addr)
1505         struct wpa_ssid *old_ssid;
1507         wpa_clear_keys(wpa_s, addr);
1508         wpa_supplicant_mark_disassoc(wpa_s);
1509         old_ssid = wpa_s->current_ssid;
1510         wpa_s->current_ssid = NULL;
1511         wpa_s->current_bss = NULL;
1512         wpa_sm_set_config(wpa_s->wpa, NULL);
1513         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1514         if (old_ssid != wpa_s->current_ssid)
1515                 wpas_notify_network_changed(wpa_s);
1516         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1520 /**
1521  * wpa_supplicant_disassociate - Disassociate the current connection
1522  * @wpa_s: Pointer to wpa_supplicant data
1523  * @reason_code: IEEE 802.11 reason code for the disassociate frame
1524  *
1525  * This function is used to request %wpa_supplicant to disassociate with the
1526  * current AP.
1527  */
1528 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1529                                  int reason_code)
1531         u8 *addr = NULL;
1533         if (!is_zero_ether_addr(wpa_s->bssid)) {
1534                 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1535                 addr = wpa_s->bssid;
1536         }
1538         wpa_supplicant_clear_connection(wpa_s, addr);
1542 /**
1543  * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1544  * @wpa_s: Pointer to wpa_supplicant data
1545  * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1546  *
1547  * This function is used to request %wpa_supplicant to deauthenticate from the
1548  * current AP.
1549  */
1550 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1551                                    int reason_code)
1553         u8 *addr = NULL;
1555         if (!is_zero_ether_addr(wpa_s->bssid)) {
1556                 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1557                 addr = wpa_s->bssid;
1558         }
1560         wpa_supplicant_clear_connection(wpa_s, addr);
1564 /**
1565  * wpa_supplicant_enable_network - Mark a configured network as enabled
1566  * @wpa_s: wpa_supplicant structure for a network interface
1567  * @ssid: wpa_ssid structure for a configured network or %NULL
1568  *
1569  * Enables the specified network or all networks if no network specified.
1570  */
1571 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1572                                    struct wpa_ssid *ssid)
1574         struct wpa_ssid *other_ssid;
1575         int was_disabled;
1577         if (ssid == NULL) {
1578                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1579                      other_ssid = other_ssid->next) {
1580                         if (other_ssid->disabled == 2)
1581                                 continue; /* do not change persistent P2P group
1582                                            * data */
1583                         if (other_ssid == wpa_s->current_ssid &&
1584                             other_ssid->disabled)
1585                                 wpa_s->reassociate = 1;
1587                         was_disabled = other_ssid->disabled;
1589                         other_ssid->disabled = 0;
1591                         if (was_disabled != other_ssid->disabled)
1592                                 wpas_notify_network_enabled_changed(
1593                                         wpa_s, other_ssid);
1594                 }
1595                 if (wpa_s->reassociate)
1596                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1597         } else if (ssid->disabled && ssid->disabled != 2) {
1598                 if (wpa_s->current_ssid == NULL) {
1599                         /*
1600                          * Try to reassociate since there is no current
1601                          * configuration and a new network was made available.
1602                          */
1603                         wpa_s->reassociate = 1;
1604                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1605                 }
1607                 was_disabled = ssid->disabled;
1609                 ssid->disabled = 0;
1611                 if (was_disabled != ssid->disabled)
1612                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1613         }
1617 /**
1618  * wpa_supplicant_disable_network - Mark a configured network as disabled
1619  * @wpa_s: wpa_supplicant structure for a network interface
1620  * @ssid: wpa_ssid structure for a configured network or %NULL
1621  *
1622  * Disables the specified network or all networks if no network specified.
1623  */
1624 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1625                                     struct wpa_ssid *ssid)
1627         struct wpa_ssid *other_ssid;
1628         int was_disabled;
1630         if (ssid == NULL) {
1631                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1632                      other_ssid = other_ssid->next) {
1633                         was_disabled = other_ssid->disabled;
1634                         if (was_disabled == 2)
1635                                 continue; /* do not change persistent P2P group
1636                                            * data */
1638                         other_ssid->disabled = 1;
1640                         if (was_disabled != other_ssid->disabled)
1641                                 wpas_notify_network_enabled_changed(
1642                                         wpa_s, other_ssid);
1643                 }
1644                 if (wpa_s->current_ssid)
1645                         wpa_supplicant_disassociate(
1646                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1647         } else if (ssid->disabled != 2) {
1648                 if (ssid == wpa_s->current_ssid)
1649                         wpa_supplicant_disassociate(
1650                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1652                 was_disabled = ssid->disabled;
1654                 ssid->disabled = 1;
1656                 if (was_disabled != ssid->disabled)
1657                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1658         }
1662 /**
1663  * wpa_supplicant_select_network - Attempt association with a network
1664  * @wpa_s: wpa_supplicant structure for a network interface
1665  * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1666  */
1667 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1668                                    struct wpa_ssid *ssid)
1671         struct wpa_ssid *other_ssid;
1673         if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1674                 wpa_supplicant_disassociate(
1675                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1677         /*
1678          * Mark all other networks disabled or mark all networks enabled if no
1679          * network specified.
1680          */
1681         for (other_ssid = wpa_s->conf->ssid; other_ssid;
1682              other_ssid = other_ssid->next) {
1683                 int was_disabled = other_ssid->disabled;
1684                 if (was_disabled == 2)
1685                         continue; /* do not change persistent P2P group data */
1687                 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1689                 if (was_disabled != other_ssid->disabled)
1690                         wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1691         }
1693         if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1694                 /* We are already associated with the selected network */
1695                 wpa_printf(MSG_DEBUG, "Already associated with the "
1696                            "selected network - do nothing");
1697                 return;
1698         }
1700         wpa_s->connect_without_scan = NULL;
1701         wpa_s->disconnected = 0;
1702         wpa_s->reassociate = 1;
1703         wpa_supplicant_req_scan(wpa_s, 0, 0);
1705         if (ssid)
1706                 wpas_notify_network_selected(wpa_s, ssid);
1710 /**
1711  * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1712  * @wpa_s: wpa_supplicant structure for a network interface
1713  * @ap_scan: AP scan mode
1714  * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1715  *
1716  */
1717 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1720         int old_ap_scan;
1722         if (ap_scan < 0 || ap_scan > 2)
1723                 return -1;
1725 #ifdef ANDROID
1726         if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1727             wpa_s->wpa_state >= WPA_ASSOCIATING &&
1728             wpa_s->wpa_state < WPA_COMPLETED) {
1729                 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1730                            "associating", wpa_s->conf->ap_scan, ap_scan);
1731                 return 0;
1732         }
1733 #endif /* ANDROID */
1735         old_ap_scan = wpa_s->conf->ap_scan;
1736         wpa_s->conf->ap_scan = ap_scan;
1738         if (old_ap_scan != wpa_s->conf->ap_scan)
1739                 wpas_notify_ap_scan_changed(wpa_s);
1741         return 0;
1745 /**
1746  * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1747  * @wpa_s: wpa_supplicant structure for a network interface
1748  * @expire_age: Expiration age in seconds
1749  * Returns: 0 if succeed or -1 if expire_age has an invalid value
1750  *
1751  */
1752 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1753                                           unsigned int bss_expire_age)
1755         if (bss_expire_age < 10) {
1756                 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1757                         bss_expire_age);
1758                 return -1;
1759         }
1760         wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1761                 bss_expire_age);
1762         wpa_s->conf->bss_expiration_age = bss_expire_age;
1764         return 0;
1768 /**
1769  * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1770  * @wpa_s: wpa_supplicant structure for a network interface
1771  * @expire_count: number of scans after which an unseen BSS is reclaimed
1772  * Returns: 0 if succeed or -1 if expire_count has an invalid value
1773  *
1774  */
1775 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1776                                             unsigned int bss_expire_count)
1778         if (bss_expire_count < 1) {
1779                 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1780                         bss_expire_count);
1781                 return -1;
1782         }
1783         wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1784                 bss_expire_count);
1785         wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1787         return 0;
1791 /**
1792  * wpa_supplicant_set_debug_params - Set global debug params
1793  * @global: wpa_global structure
1794  * @debug_level: debug level
1795  * @debug_timestamp: determines if show timestamp in debug data
1796  * @debug_show_keys: determines if show keys in debug data
1797  * Returns: 0 if succeed or -1 if debug_level has wrong value
1798  */
1799 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1800                                     int debug_timestamp, int debug_show_keys)
1803         int old_level, old_timestamp, old_show_keys;
1805         /* check for allowed debuglevels */
1806         if (debug_level != MSG_EXCESSIVE &&
1807             debug_level != MSG_MSGDUMP &&
1808             debug_level != MSG_DEBUG &&
1809             debug_level != MSG_INFO &&
1810             debug_level != MSG_WARNING &&
1811             debug_level != MSG_ERROR)
1812                 return -1;
1814         old_level = wpa_debug_level;
1815         old_timestamp = wpa_debug_timestamp;
1816         old_show_keys = wpa_debug_show_keys;
1818         wpa_debug_level = debug_level;
1819         wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1820         wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1822         if (wpa_debug_level != old_level)
1823                 wpas_notify_debug_level_changed(global);
1824         if (wpa_debug_timestamp != old_timestamp)
1825                 wpas_notify_debug_timestamp_changed(global);
1826         if (wpa_debug_show_keys != old_show_keys)
1827                 wpas_notify_debug_show_keys_changed(global);
1829         return 0;
1833 /**
1834  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1835  * @wpa_s: Pointer to wpa_supplicant data
1836  * Returns: A pointer to the current network structure or %NULL on failure
1837  */
1838 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1840         struct wpa_ssid *entry;
1841         u8 ssid[MAX_SSID_LEN];
1842         int res;
1843         size_t ssid_len;
1844         u8 bssid[ETH_ALEN];
1845         int wired;
1847         res = wpa_drv_get_ssid(wpa_s, ssid);
1848         if (res < 0) {
1849                 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1850                         "driver");
1851                 return NULL;
1852         }
1853         ssid_len = res;
1855         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1856                 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1857                         "driver");
1858                 return NULL;
1859         }
1861         wired = wpa_s->conf->ap_scan == 0 &&
1862                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1864         entry = wpa_s->conf->ssid;
1865         while (entry) {
1866                 if (!entry->disabled &&
1867                     ((ssid_len == entry->ssid_len &&
1868                       os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1869                     (!entry->bssid_set ||
1870                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1871                         return entry;
1872 #ifdef CONFIG_WPS
1873                 if (!entry->disabled &&
1874                     (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1875                     (entry->ssid == NULL || entry->ssid_len == 0) &&
1876                     (!entry->bssid_set ||
1877                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1878                         return entry;
1879 #endif /* CONFIG_WPS */
1881                 if (!entry->disabled && entry->bssid_set &&
1882                     entry->ssid_len == 0 &&
1883                     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1884                         return entry;
1886                 entry = entry->next;
1887         }
1889         return NULL;
1893 static int select_driver(struct wpa_supplicant *wpa_s, int i)
1895         struct wpa_global *global = wpa_s->global;
1897         if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
1898                 global->drv_priv[i] = wpa_drivers[i]->global_init();
1899                 if (global->drv_priv[i] == NULL) {
1900                         wpa_printf(MSG_ERROR, "Failed to initialize driver "
1901                                    "'%s'", wpa_drivers[i]->name);
1902                         return -1;
1903                 }
1904         }
1906         wpa_s->driver = wpa_drivers[i];
1907         wpa_s->global_drv_priv = global->drv_priv[i];
1909         return 0;
1913 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1914                                      const char *name)
1916         int i;
1917         size_t len;
1918         const char *pos, *driver = name;
1920         if (wpa_s == NULL)
1921                 return -1;
1923         if (wpa_drivers[0] == NULL) {
1924                 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1925                         "wpa_supplicant");
1926                 return -1;
1927         }
1929         if (name == NULL) {
1930                 /* default to first driver in the list */
1931                 return select_driver(wpa_s, 0);
1932         }
1934         do {
1935                 pos = os_strchr(driver, ',');
1936                 if (pos)
1937                         len = pos - driver;
1938                 else
1939                         len = os_strlen(driver);
1941                 for (i = 0; wpa_drivers[i]; i++) {
1942                         if (os_strlen(wpa_drivers[i]->name) == len &&
1943                             os_strncmp(driver, wpa_drivers[i]->name, len) ==
1944                             0)
1945                                 return select_driver(wpa_s, i);
1946                 }
1948                 driver = pos + 1;
1949         } while (pos);
1951         wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1952         return -1;
1956 /**
1957  * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1958  * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1959  *      with struct wpa_driver_ops::init()
1960  * @src_addr: Source address of the EAPOL frame
1961  * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1962  * @len: Length of the EAPOL data
1963  *
1964  * This function is called for each received EAPOL frame. Most driver
1965  * interfaces rely on more generic OS mechanism for receiving frames through
1966  * l2_packet, but if such a mechanism is not available, the driver wrapper may
1967  * take care of received EAPOL frames and deliver them to the core supplicant
1968  * code by calling this function.
1969  */
1970 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1971                              const u8 *buf, size_t len)
1973         struct wpa_supplicant *wpa_s = ctx;
1975         wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1976         wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1978         if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1979                 /*
1980                  * There is possible race condition between receiving the
1981                  * association event and the EAPOL frame since they are coming
1982                  * through different paths from the driver. In order to avoid
1983                  * issues in trying to process the EAPOL frame before receiving
1984                  * association information, lets queue it for processing until
1985                  * the association event is received.
1986                  */
1987                 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1988                         "of received EAPOL frame");
1989                 wpabuf_free(wpa_s->pending_eapol_rx);
1990                 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1991                 if (wpa_s->pending_eapol_rx) {
1992                         os_get_time(&wpa_s->pending_eapol_rx_time);
1993                         os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1994                                   ETH_ALEN);
1995                 }
1996                 return;
1997         }
1999 #ifdef CONFIG_AP
2000         if (wpa_s->ap_iface) {
2001                 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2002                 return;
2003         }
2004 #endif /* CONFIG_AP */
2006         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2007                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2008                         "no key management is configured");
2009                 return;
2010         }
2012         if (wpa_s->eapol_received == 0 &&
2013             (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2014              !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2015              wpa_s->wpa_state != WPA_COMPLETED) &&
2016             (wpa_s->current_ssid == NULL ||
2017              wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2018                 /* Timeout for completing IEEE 802.1X and WPA authentication */
2019                 wpa_supplicant_req_auth_timeout(
2020                         wpa_s,
2021                         (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2022                          wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2023                          wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2024                         70 : 10, 0);
2025         }
2026         wpa_s->eapol_received++;
2028         if (wpa_s->countermeasures) {
2029                 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2030                         "EAPOL packet");
2031                 return;
2032         }
2034 #ifdef CONFIG_IBSS_RSN
2035         if (wpa_s->current_ssid &&
2036             wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2037                 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2038                 return;
2039         }
2040 #endif /* CONFIG_IBSS_RSN */
2042         /* Source address of the incoming EAPOL frame could be compared to the
2043          * current BSSID. However, it is possible that a centralized
2044          * Authenticator could be using another MAC address than the BSSID of
2045          * an AP, so just allow any address to be used for now. The replies are
2046          * still sent to the current BSSID (if available), though. */
2048         os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2049         if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2050             eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2051                 return;
2052         wpa_drv_poll(wpa_s);
2053         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2054                 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2055         else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2056                 /*
2057                  * Set portValid = TRUE here since we are going to skip 4-way
2058                  * handshake processing which would normally set portValid. We
2059                  * need this to allow the EAPOL state machines to be completed
2060                  * without going through EAPOL-Key handshake.
2061                  */
2062                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2063         }
2067 /**
2068  * wpa_supplicant_driver_init - Initialize driver interface parameters
2069  * @wpa_s: Pointer to wpa_supplicant data
2070  * Returns: 0 on success, -1 on failure
2071  *
2072  * This function is called to initialize driver interface parameters.
2073  * wpa_drv_init() must have been called before this function to initialize the
2074  * driver interface.
2075  */
2076 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2078         static int interface_count = 0;
2080         if (wpa_s->driver->send_eapol) {
2081                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2082                 if (addr)
2083                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2084         } else if (!(wpa_s->drv_flags &
2085                      WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2086                 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2087                                            wpa_drv_get_mac_addr(wpa_s),
2088                                            ETH_P_EAPOL,
2089                                            wpa_supplicant_rx_eapol, wpa_s, 0);
2090                 if (wpa_s->l2 == NULL)
2091                         return -1;
2092         } else {
2093                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2094                 if (addr)
2095                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2096         }
2098         if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2099                 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2100                 return -1;
2101         }
2103         wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2104                 MAC2STR(wpa_s->own_addr));
2106         if (wpa_s->bridge_ifname[0]) {
2107                 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2108                         "interface '%s'", wpa_s->bridge_ifname);
2109                 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2110                                               wpa_s->own_addr,
2111                                               ETH_P_EAPOL,
2112                                               wpa_supplicant_rx_eapol, wpa_s,
2113                                               0);
2114                 if (wpa_s->l2_br == NULL) {
2115                         wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2116                                 "connection for the bridge interface '%s'",
2117                                 wpa_s->bridge_ifname);
2118                         return -1;
2119                 }
2120         }
2122         wpa_clear_keys(wpa_s, NULL);
2124         /* Make sure that TKIP countermeasures are not left enabled (could
2125          * happen if wpa_supplicant is killed during countermeasures. */
2126         wpa_drv_set_countermeasures(wpa_s, 0);
2128         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2129         wpa_drv_flush_pmkid(wpa_s);
2131         wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2132         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2133                 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2134                                                       100000))
2135                         wpa_supplicant_req_scan(wpa_s, interface_count,
2136                                                 100000);
2137                 interface_count++;
2138         } else
2139                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2141         return 0;
2145 static int wpa_supplicant_daemon(const char *pid_file)
2147         wpa_printf(MSG_DEBUG, "Daemonize..");
2148         return os_daemonize(pid_file);
2152 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2154         struct wpa_supplicant *wpa_s;
2156         wpa_s = os_zalloc(sizeof(*wpa_s));
2157         if (wpa_s == NULL)
2158                 return NULL;
2159         wpa_s->scan_req = 1;
2160         wpa_s->scan_interval = 5;
2161         wpa_s->new_connection = 1;
2162         wpa_s->parent = wpa_s;
2163         wpa_s->sched_scanning = 0;
2165         return wpa_s;
2169 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2170                                      struct wpa_interface *iface)
2172         const char *ifname, *driver;
2173         struct wpa_driver_capa capa;
2175         wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2176                    "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2177                    iface->confname ? iface->confname : "N/A",
2178                    iface->driver ? iface->driver : "default",
2179                    iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2180                    iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2182         if (iface->confname) {
2183 #ifdef CONFIG_BACKEND_FILE
2184                 wpa_s->confname = os_rel2abs_path(iface->confname);
2185                 if (wpa_s->confname == NULL) {
2186                         wpa_printf(MSG_ERROR, "Failed to get absolute path "
2187                                    "for configuration file '%s'.",
2188                                    iface->confname);
2189                         return -1;
2190                 }
2191                 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2192                            iface->confname, wpa_s->confname);
2193 #else /* CONFIG_BACKEND_FILE */
2194                 wpa_s->confname = os_strdup(iface->confname);
2195 #endif /* CONFIG_BACKEND_FILE */
2196                 wpa_s->conf = wpa_config_read(wpa_s->confname);
2197                 if (wpa_s->conf == NULL) {
2198                         wpa_printf(MSG_ERROR, "Failed to read or parse "
2199                                    "configuration '%s'.", wpa_s->confname);
2200                         return -1;
2201                 }
2203                 /*
2204                  * Override ctrl_interface and driver_param if set on command
2205                  * line.
2206                  */
2207                 if (iface->ctrl_interface) {
2208                         os_free(wpa_s->conf->ctrl_interface);
2209                         wpa_s->conf->ctrl_interface =
2210                                 os_strdup(iface->ctrl_interface);
2211                 }
2213                 if (iface->driver_param) {
2214                         os_free(wpa_s->conf->driver_param);
2215                         wpa_s->conf->driver_param =
2216                                 os_strdup(iface->driver_param);
2217                 }
2218         } else
2219                 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2220                                                      iface->driver_param);
2222         if (wpa_s->conf == NULL) {
2223                 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2224                 return -1;
2225         }
2227         if (iface->ifname == NULL) {
2228                 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2229                 return -1;
2230         }
2231         if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2232                 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2233                            iface->ifname);
2234                 return -1;
2235         }
2236         os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2238         if (iface->bridge_ifname) {
2239                 if (os_strlen(iface->bridge_ifname) >=
2240                     sizeof(wpa_s->bridge_ifname)) {
2241                         wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2242                                    "name '%s'.", iface->bridge_ifname);
2243                         return -1;
2244                 }
2245                 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2246                            sizeof(wpa_s->bridge_ifname));
2247         }
2249         /* RSNA Supplicant Key Management - INITIALIZE */
2250         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2251         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2253         /* Initialize driver interface and register driver event handler before
2254          * L2 receive handler so that association events are processed before
2255          * EAPOL-Key packets if both become available for the same select()
2256          * call. */
2257         driver = iface->driver;
2258 next_driver:
2259         if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2260                 return -1;
2262         wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2263         if (wpa_s->drv_priv == NULL) {
2264                 const char *pos;
2265                 pos = driver ? os_strchr(driver, ',') : NULL;
2266                 if (pos) {
2267                         wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2268                                 "driver interface - try next driver wrapper");
2269                         driver = pos + 1;
2270                         goto next_driver;
2271                 }
2272                 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2273                         "interface");
2274                 return -1;
2275         }
2276         if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2277                 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2278                         "driver_param '%s'", wpa_s->conf->driver_param);
2279                 return -1;
2280         }
2282         ifname = wpa_drv_get_ifname(wpa_s);
2283         if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2284                 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2285                         "interface name with '%s'", ifname);
2286                 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2287         }
2289         if (wpa_supplicant_init_wpa(wpa_s) < 0)
2290                 return -1;
2292         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2293                           wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2294                           NULL);
2295         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2297         if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2298             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2299                              wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2300                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2301                         "dot11RSNAConfigPMKLifetime");
2302                 return -1;
2303         }
2305         if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2306             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2307                              wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2308                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2309                         "dot11RSNAConfigPMKReauthThreshold");
2310                 return -1;
2311         }
2313         if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2314             wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2315                              wpa_s->conf->dot11RSNAConfigSATimeout)) {
2316                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2317                         "dot11RSNAConfigSATimeout");
2318                 return -1;
2319         }
2321         wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2322                                                       &wpa_s->hw.num_modes,
2323                                                       &wpa_s->hw.flags);
2325         if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2326                 wpa_s->drv_capa_known = 1;
2327                 wpa_s->drv_flags = capa.flags;
2328                 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2329                 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2330                 wpa_s->sched_scan_supported = capa.sched_scan_supported;
2331                 wpa_s->max_match_sets = capa.max_match_sets;
2332                 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2333                 wpa_s->max_stations = capa.max_stations;
2334         }
2335         if (wpa_s->max_remain_on_chan == 0)
2336                 wpa_s->max_remain_on_chan = 1000;
2338         if (wpa_supplicant_driver_init(wpa_s) < 0)
2339                 return -1;
2341 #ifdef CONFIG_TDLS
2342         if (wpa_tdls_init(wpa_s->wpa))
2343                 return -1;
2344 #endif /* CONFIG_TDLS */
2346         if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2347             wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2348                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2349                 return -1;
2350         }
2352         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2354         if (wpas_wps_init(wpa_s))
2355                 return -1;
2357         if (wpa_supplicant_init_eapol(wpa_s) < 0)
2358                 return -1;
2359         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2361         wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2362         if (wpa_s->ctrl_iface == NULL) {
2363                 wpa_printf(MSG_ERROR,
2364                            "Failed to initialize control interface '%s'.\n"
2365                            "You may have another wpa_supplicant process "
2366                            "already running or the file was\n"
2367                            "left by an unclean termination of wpa_supplicant "
2368                            "in which case you will need\n"
2369                            "to manually remove this file before starting "
2370                            "wpa_supplicant again.\n",
2371                            wpa_s->conf->ctrl_interface);
2372                 return -1;
2373         }
2375         wpa_s->gas = gas_query_init(wpa_s);
2376         if (wpa_s->gas == NULL) {
2377                 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2378                 return -1;
2379         }
2381 #ifdef CONFIG_P2P
2382         if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2383                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2384                 return -1;
2385         }
2386 #endif /* CONFIG_P2P */
2388         if (wpa_bss_init(wpa_s) < 0)
2389                 return -1;
2391         return 0;
2395 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2396                                         int notify)
2398         if (wpa_s->drv_priv) {
2399                 wpa_supplicant_deauthenticate(wpa_s,
2400                                               WLAN_REASON_DEAUTH_LEAVING);
2402                 wpa_drv_set_countermeasures(wpa_s, 0);
2403                 wpa_clear_keys(wpa_s, NULL);
2404         }
2406         wpa_supplicant_cleanup(wpa_s);
2408         if (notify)
2409                 wpas_notify_iface_removed(wpa_s);
2411         if (wpa_s->drv_priv)
2412                 wpa_drv_deinit(wpa_s);
2416 /**
2417  * wpa_supplicant_add_iface - Add a new network interface
2418  * @global: Pointer to global data from wpa_supplicant_init()
2419  * @iface: Interface configuration options
2420  * Returns: Pointer to the created interface or %NULL on failure
2421  *
2422  * This function is used to add new network interfaces for %wpa_supplicant.
2423  * This can be called before wpa_supplicant_run() to add interfaces before the
2424  * main event loop has been started. In addition, new interfaces can be added
2425  * dynamically while %wpa_supplicant is already running. This could happen,
2426  * e.g., when a hotplug network adapter is inserted.
2427  */
2428 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2429                                                  struct wpa_interface *iface)
2431         struct wpa_supplicant *wpa_s;
2432         struct wpa_interface t_iface;
2433         struct wpa_ssid *ssid;
2435         if (global == NULL || iface == NULL)
2436                 return NULL;
2438         wpa_s = wpa_supplicant_alloc();
2439         if (wpa_s == NULL)
2440                 return NULL;
2442         wpa_s->global = global;
2444         t_iface = *iface;
2445         if (global->params.override_driver) {
2446                 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2447                            "('%s' -> '%s')",
2448                            iface->driver, global->params.override_driver);
2449                 t_iface.driver = global->params.override_driver;
2450         }
2451         if (global->params.override_ctrl_interface) {
2452                 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2453                            "ctrl_interface ('%s' -> '%s')",
2454                            iface->ctrl_interface,
2455                            global->params.override_ctrl_interface);
2456                 t_iface.ctrl_interface =
2457                         global->params.override_ctrl_interface;
2458         }
2459         if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2460                 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2461                            iface->ifname);
2462                 wpa_supplicant_deinit_iface(wpa_s, 0);
2463                 os_free(wpa_s);
2464                 return NULL;
2465         }
2467         /* Notify the control interfaces about new iface */
2468         if (wpas_notify_iface_added(wpa_s)) {
2469                 wpa_supplicant_deinit_iface(wpa_s, 1);
2470                 os_free(wpa_s);
2471                 return NULL;
2472         }
2474         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2475                 wpas_notify_network_added(wpa_s, ssid);
2477         wpa_s->next = global->ifaces;
2478         global->ifaces = wpa_s;
2480         wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2482         return wpa_s;
2486 /**
2487  * wpa_supplicant_remove_iface - Remove a network interface
2488  * @global: Pointer to global data from wpa_supplicant_init()
2489  * @wpa_s: Pointer to the network interface to be removed
2490  * Returns: 0 if interface was removed, -1 if interface was not found
2491  *
2492  * This function can be used to dynamically remove network interfaces from
2493  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2494  * addition, this function is used to remove all remaining interfaces when
2495  * %wpa_supplicant is terminated.
2496  */
2497 int wpa_supplicant_remove_iface(struct wpa_global *global,
2498                                 struct wpa_supplicant *wpa_s)
2500         struct wpa_supplicant *prev;
2502         /* Remove interface from the global list of interfaces */
2503         prev = global->ifaces;
2504         if (prev == wpa_s) {
2505                 global->ifaces = wpa_s->next;
2506         } else {
2507                 while (prev && prev->next != wpa_s)
2508                         prev = prev->next;
2509                 if (prev == NULL)
2510                         return -1;
2511                 prev->next = wpa_s->next;
2512         }
2514         wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2516         if (global->p2p_group_formation == wpa_s)
2517                 global->p2p_group_formation = NULL;
2518         wpa_supplicant_deinit_iface(wpa_s, 1);
2519         os_free(wpa_s);
2521         return 0;
2525 /**
2526  * wpa_supplicant_get_eap_mode - Get the current EAP mode
2527  * @wpa_s: Pointer to the network interface
2528  * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2529  */
2530 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2532         const char *eapol_method;
2534         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2535             wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2536                 return "NO-EAP";
2537         }
2539         eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2540         if (eapol_method == NULL)
2541                 return "UNKNOWN-EAP";
2543         return eapol_method;
2547 /**
2548  * wpa_supplicant_get_iface - Get a new network interface
2549  * @global: Pointer to global data from wpa_supplicant_init()
2550  * @ifname: Interface name
2551  * Returns: Pointer to the interface or %NULL if not found
2552  */
2553 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2554                                                  const char *ifname)
2556         struct wpa_supplicant *wpa_s;
2558         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2559                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2560                         return wpa_s;
2561         }
2562         return NULL;
2566 #ifndef CONFIG_NO_WPA_MSG
2567 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2569         struct wpa_supplicant *wpa_s = ctx;
2570         if (wpa_s == NULL)
2571                 return NULL;
2572         return wpa_s->ifname;
2574 #endif /* CONFIG_NO_WPA_MSG */
2577 /**
2578  * wpa_supplicant_init - Initialize %wpa_supplicant
2579  * @params: Parameters for %wpa_supplicant
2580  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2581  *
2582  * This function is used to initialize %wpa_supplicant. After successful
2583  * initialization, the returned data pointer can be used to add and remove
2584  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2585  */
2586 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2588         struct wpa_global *global;
2589         int ret, i;
2591         if (params == NULL)
2592                 return NULL;
2594 #ifdef CONFIG_DRIVER_NDIS
2595         {
2596                 void driver_ndis_init_ops(void);
2597                 driver_ndis_init_ops();
2598         }
2599 #endif /* CONFIG_DRIVER_NDIS */
2601 #ifndef CONFIG_NO_WPA_MSG
2602         wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2603 #endif /* CONFIG_NO_WPA_MSG */
2605         wpa_debug_open_file(params->wpa_debug_file_path);
2606         if (params->wpa_debug_syslog)
2607                 wpa_debug_open_syslog();
2609         ret = eap_register_methods();
2610         if (ret) {
2611                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2612                 if (ret == -2)
2613                         wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2614                                    "the same EAP type.");
2615                 return NULL;
2616         }
2618         global = os_zalloc(sizeof(*global));
2619         if (global == NULL)
2620                 return NULL;
2621         dl_list_init(&global->p2p_srv_bonjour);
2622         dl_list_init(&global->p2p_srv_upnp);
2623         global->params.daemonize = params->daemonize;
2624         global->params.wait_for_monitor = params->wait_for_monitor;
2625         global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2626         if (params->pid_file)
2627                 global->params.pid_file = os_strdup(params->pid_file);
2628         if (params->ctrl_interface)
2629                 global->params.ctrl_interface =
2630                         os_strdup(params->ctrl_interface);
2631         if (params->override_driver)
2632                 global->params.override_driver =
2633                         os_strdup(params->override_driver);
2634         if (params->override_ctrl_interface)
2635                 global->params.override_ctrl_interface =
2636                         os_strdup(params->override_ctrl_interface);
2637         wpa_debug_level = global->params.wpa_debug_level =
2638                 params->wpa_debug_level;
2639         wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2640                 params->wpa_debug_show_keys;
2641         wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2642                 params->wpa_debug_timestamp;
2644         wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2646         if (eloop_init()) {
2647                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2648                 wpa_supplicant_deinit(global);
2649                 return NULL;
2650         }
2652         random_init(params->entropy_file);
2654         global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2655         if (global->ctrl_iface == NULL) {
2656                 wpa_supplicant_deinit(global);
2657                 return NULL;
2658         }
2660         if (wpas_notify_supplicant_initialized(global)) {
2661                 wpa_supplicant_deinit(global);
2662                 return NULL;
2663         }
2665         for (i = 0; wpa_drivers[i]; i++)
2666                 global->drv_count++;
2667         if (global->drv_count == 0) {
2668                 wpa_printf(MSG_ERROR, "No drivers enabled");
2669                 wpa_supplicant_deinit(global);
2670                 return NULL;
2671         }
2672         global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2673         if (global->drv_priv == NULL) {
2674                 wpa_supplicant_deinit(global);
2675                 return NULL;
2676         }
2678         return global;
2682 /**
2683  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2684  * @global: Pointer to global data from wpa_supplicant_init()
2685  * Returns: 0 after successful event loop run, -1 on failure
2686  *
2687  * This function starts the main event loop and continues running as long as
2688  * there are any remaining events. In most cases, this function is running as
2689  * long as the %wpa_supplicant process in still in use.
2690  */
2691 int wpa_supplicant_run(struct wpa_global *global)
2693         struct wpa_supplicant *wpa_s;
2695         if (global->params.daemonize &&
2696             wpa_supplicant_daemon(global->params.pid_file))
2697                 return -1;
2699         if (global->params.wait_for_monitor) {
2700                 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2701                         if (wpa_s->ctrl_iface)
2702                                 wpa_supplicant_ctrl_iface_wait(
2703                                         wpa_s->ctrl_iface);
2704         }
2706         eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2707         eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2709         eloop_run();
2711         return 0;
2715 /**
2716  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2717  * @global: Pointer to global data from wpa_supplicant_init()
2718  *
2719  * This function is called to deinitialize %wpa_supplicant and to free all
2720  * allocated resources. Remaining network interfaces will also be removed.
2721  */
2722 void wpa_supplicant_deinit(struct wpa_global *global)
2724         int i;
2726         if (global == NULL)
2727                 return;
2729 #ifdef CONFIG_P2P
2730         wpas_p2p_deinit_global(global);
2731 #endif /* CONFIG_P2P */
2733         while (global->ifaces)
2734                 wpa_supplicant_remove_iface(global, global->ifaces);
2736         if (global->ctrl_iface)
2737                 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2739         wpas_notify_supplicant_deinitialized(global);
2741         eap_peer_unregister_methods();
2742 #ifdef CONFIG_AP
2743         eap_server_unregister_methods();
2744 #endif /* CONFIG_AP */
2746         for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2747                 if (!global->drv_priv[i])
2748                         continue;
2749                 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2750         }
2751         os_free(global->drv_priv);
2753         random_deinit();
2755         eloop_destroy();
2757         if (global->params.pid_file) {
2758                 os_daemonize_terminate(global->params.pid_file);
2759                 os_free(global->params.pid_file);
2760         }
2761         os_free(global->params.ctrl_interface);
2762         os_free(global->params.override_driver);
2763         os_free(global->params.override_ctrl_interface);
2765         os_free(global);
2766         wpa_debug_close_syslog();
2767         wpa_debug_close_file();
2771 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2773         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
2774             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2775                 char country[3];
2776                 country[0] = wpa_s->conf->country[0];
2777                 country[1] = wpa_s->conf->country[1];
2778                 country[2] = '\0';
2779                 if (wpa_drv_set_country(wpa_s, country) < 0) {
2780                         wpa_printf(MSG_ERROR, "Failed to set country code "
2781                                    "'%s'", country);
2782                 }
2783         }
2785 #ifdef CONFIG_WPS
2786         wpas_wps_update_config(wpa_s);
2787 #endif /* CONFIG_WPS */
2789 #ifdef CONFIG_P2P
2790         wpas_p2p_update_config(wpa_s);
2791 #endif /* CONFIG_P2P */
2793         wpa_s->conf->changed_parameters = 0;
2797 static void add_freq(int *freqs, int *num_freqs, int freq)
2799         int i;
2801         for (i = 0; i < *num_freqs; i++) {
2802                 if (freqs[i] == freq)
2803                         return;
2804         }
2806         freqs[*num_freqs] = freq;
2807         (*num_freqs)++;
2811 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2813         struct wpa_bss *bss, *cbss;
2814         const int max_freqs = 10;
2815         int *freqs;
2816         int num_freqs = 0;
2818         freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2819         if (freqs == NULL)
2820                 return NULL;
2822         cbss = wpa_s->current_bss;
2824         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2825                 if (bss == cbss)
2826                         continue;
2827                 if (bss->ssid_len == cbss->ssid_len &&
2828                     os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2829                     wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2830                         add_freq(freqs, &num_freqs, bss->freq);
2831                         if (num_freqs == max_freqs)
2832                                 break;
2833                 }
2834         }
2836         if (num_freqs == 0) {
2837                 os_free(freqs);
2838                 freqs = NULL;
2839         }
2841         return freqs;
2845 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2847         int timeout;
2848         int count;
2849         int *freqs = NULL;
2851         /*
2852          * Add the failed BSSID into the blacklist and speed up next scan
2853          * attempt if there could be other APs that could accept association.
2854          * The current blacklist count indicates how many times we have tried
2855          * connecting to this AP and multiple attempts mean that other APs are
2856          * either not available or has already been tried, so that we can start
2857          * increasing the delay here to avoid constant scanning.
2858          */
2859         count = wpa_blacklist_add(wpa_s, bssid);
2860         if (count == 1 && wpa_s->current_bss) {
2861                 /*
2862                  * This BSS was not in the blacklist before. If there is
2863                  * another BSS available for the same ESS, we should try that
2864                  * next. Otherwise, we may as well try this one once more
2865                  * before allowing other, likely worse, ESSes to be considered.
2866                  */
2867                 freqs = get_bss_freqs_in_ess(wpa_s);
2868                 if (freqs) {
2869                         wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2870                                 "has been seen; try it next");
2871                         wpa_blacklist_add(wpa_s, bssid);
2872                         /*
2873                          * On the next scan, go through only the known channels
2874                          * used in this ESS based on previous scans to speed up
2875                          * common load balancing use case.
2876                          */
2877                         os_free(wpa_s->next_scan_freqs);
2878                         wpa_s->next_scan_freqs = freqs;
2879                 }
2880         }
2882         switch (count) {
2883         case 1:
2884                 timeout = 100;
2885                 break;
2886         case 2:
2887                 timeout = 500;
2888                 break;
2889         case 3:
2890                 timeout = 1000;
2891                 break;
2892         default:
2893                 timeout = 5000;
2894         }
2896         /*
2897          * TODO: if more than one possible AP is available in scan results,
2898          * could try the other ones before requesting a new scan.
2899          */
2900         wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2901                                 1000 * (timeout % 1000));
2905 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
2907         return wpa_s->conf->ap_scan == 2 ||
2908                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);