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