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