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