blob: 561099cb9ad468c32af2d482e6ff7b410531c356 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
8 * This file implements functions for registering and unregistering
9 * %wpa_supplicant interfaces. In addition, this file contains number of
10 * functions for managing network connections.
11 */
12
13#include "includes.h"
14
15#include "common.h"
16#include "crypto/random.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080017#include "crypto/sha1.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "eapol_supp/eapol_supp_sm.h"
19#include "eap_peer/eap.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070020#include "eap_peer/eap_proxy.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "eap_server/eap_methods.h"
22#include "rsn_supp/wpa.h"
23#include "eloop.h"
24#include "config.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025#include "utils/ext_password.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "l2_packet/l2_packet.h"
27#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ctrl_iface.h"
30#include "pcsc_funcs.h"
31#include "common/version.h"
32#include "rsn_supp/preauth.h"
33#include "rsn_supp/pmksa_cache.h"
34#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "common/ieee802_11_defs.h"
36#include "p2p/p2p.h"
37#include "blacklist.h"
38#include "wpas_glue.h"
39#include "wps_supplicant.h"
40#include "ibss_rsn.h"
41#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "ap.h"
44#include "p2p_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "notify.h"
47#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049#include "bss.h"
50#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051#include "offchannel.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070052#include "hs20_supplicant.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070053#include "wnm_sta.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070054
55const char *wpa_supplicant_version =
56"wpa_supplicant v" VERSION_STR "\n"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057"Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058
59const char *wpa_supplicant_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080060"This software may be distributed under the terms of the BSD license.\n"
61"See README for more details.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062#ifdef EAP_TLS_OPENSSL
63"\nThis product includes software developed by the OpenSSL Project\n"
64"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
65#endif /* EAP_TLS_OPENSSL */
66;
67
68#ifndef CONFIG_NO_STDOUT_DEBUG
69/* Long text divided into parts in order to fit in C89 strings size limits. */
70const char *wpa_supplicant_full_license1 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080071"";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072const char *wpa_supplicant_full_license2 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080073"This software may be distributed under the terms of the BSD license.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074"\n"
75"Redistribution and use in source and binary forms, with or without\n"
76"modification, are permitted provided that the following conditions are\n"
77"met:\n"
78"\n";
79const char *wpa_supplicant_full_license3 =
80"1. Redistributions of source code must retain the above copyright\n"
81" notice, this list of conditions and the following disclaimer.\n"
82"\n"
83"2. Redistributions in binary form must reproduce the above copyright\n"
84" notice, this list of conditions and the following disclaimer in the\n"
85" documentation and/or other materials provided with the distribution.\n"
86"\n";
87const char *wpa_supplicant_full_license4 =
88"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
89" names of its contributors may be used to endorse or promote products\n"
90" derived from this software without specific prior written permission.\n"
91"\n"
92"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
93"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
94"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
95"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
96const char *wpa_supplicant_full_license5 =
97"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
98"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
99"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
100"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
101"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
102"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
103"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
104"\n";
105#endif /* CONFIG_NO_STDOUT_DEBUG */
106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107/* Configure default/group WEP keys for static WEP */
108int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
109{
110 int i, set = 0;
111
112 for (i = 0; i < NUM_WEP_KEYS; i++) {
113 if (ssid->wep_key_len[i] == 0)
114 continue;
115
116 set = 1;
117 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
118 i, i == ssid->wep_tx_keyidx, NULL, 0,
119 ssid->wep_key[i], ssid->wep_key_len[i]);
120 }
121
122 return set;
123}
124
125
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700126int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
127 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128{
129 u8 key[32];
130 size_t keylen;
131 enum wpa_alg alg;
132 u8 seq[6] = { 0 };
133
134 /* IBSS/WPA-None uses only one key (Group) for both receiving and
135 * sending unicast and multicast packets. */
136
137 if (ssid->mode != WPAS_MODE_IBSS) {
138 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
139 "IBSS/ad-hoc) for WPA-None", ssid->mode);
140 return -1;
141 }
142
143 if (!ssid->psk_set) {
144 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
145 "WPA-None");
146 return -1;
147 }
148
149 switch (wpa_s->group_cipher) {
150 case WPA_CIPHER_CCMP:
151 os_memcpy(key, ssid->psk, 16);
152 keylen = 16;
153 alg = WPA_ALG_CCMP;
154 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700155 case WPA_CIPHER_GCMP:
156 os_memcpy(key, ssid->psk, 16);
157 keylen = 16;
158 alg = WPA_ALG_GCMP;
159 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 case WPA_CIPHER_TKIP:
161 /* WPA-None uses the same Michael MIC key for both TX and RX */
162 os_memcpy(key, ssid->psk, 16 + 8);
163 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
164 keylen = 32;
165 alg = WPA_ALG_TKIP;
166 break;
167 default:
168 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
169 "WPA-None", wpa_s->group_cipher);
170 return -1;
171 }
172
173 /* TODO: should actually remember the previously used seq#, both for TX
174 * and RX from each STA.. */
175
176 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
177}
178
179
180static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
181{
182 struct wpa_supplicant *wpa_s = eloop_ctx;
183 const u8 *bssid = wpa_s->bssid;
184 if (is_zero_ether_addr(bssid))
185 bssid = wpa_s->pending_bssid;
186 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
187 MAC2STR(bssid));
188 wpa_blacklist_add(wpa_s, bssid);
189 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800190 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 wpa_s->reassociate = 1;
192
193 /*
194 * If we timed out, the AP or the local radio may be busy.
195 * So, wait a second until scanning again.
196 */
197 wpa_supplicant_req_scan(wpa_s, 1, 0);
198}
199
200
201/**
202 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
203 * @wpa_s: Pointer to wpa_supplicant data
204 * @sec: Number of seconds after which to time out authentication
205 * @usec: Number of microseconds after which to time out authentication
206 *
207 * This function is used to schedule a timeout for the current authentication
208 * attempt.
209 */
210void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
211 int sec, int usec)
212{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700213 if (wpa_s->conf->ap_scan == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
215 return;
216
217 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
218 "%d usec", sec, usec);
219 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
220 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
221}
222
223
224/**
225 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
226 * @wpa_s: Pointer to wpa_supplicant data
227 *
228 * This function is used to cancel authentication timeout scheduled with
229 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
230 * been completed.
231 */
232void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
233{
234 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
235 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
236 wpa_blacklist_del(wpa_s, wpa_s->bssid);
237}
238
239
240/**
241 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
242 * @wpa_s: Pointer to wpa_supplicant data
243 *
244 * This function is used to configure EAPOL state machine based on the selected
245 * authentication mode.
246 */
247void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
248{
249#ifdef IEEE8021X_EAPOL
250 struct eapol_config eapol_conf;
251 struct wpa_ssid *ssid = wpa_s->current_ssid;
252
253#ifdef CONFIG_IBSS_RSN
254 if (ssid->mode == WPAS_MODE_IBSS &&
255 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
256 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
257 /*
258 * RSN IBSS authentication is per-STA and we can disable the
259 * per-BSSID EAPOL authentication.
260 */
261 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
262 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
263 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
264 return;
265 }
266#endif /* CONFIG_IBSS_RSN */
267
268 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
269 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
270
271 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
272 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
273 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
274 else
275 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
276
277 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
278 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
279 eapol_conf.accept_802_1x_keys = 1;
280 eapol_conf.required_keys = 0;
281 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
282 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
283 }
284 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
285 eapol_conf.required_keys |=
286 EAPOL_REQUIRE_KEY_BROADCAST;
287 }
288
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700289 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290 eapol_conf.required_keys = 0;
291 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700292 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 eapol_conf.workaround = ssid->eap_workaround;
294 eapol_conf.eap_disabled =
295 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
296 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
297 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700298 eapol_conf.external_sim = wpa_s->conf->external_sim;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
300#endif /* IEEE8021X_EAPOL */
301}
302
303
304/**
305 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
306 * @wpa_s: Pointer to wpa_supplicant data
307 * @ssid: Configuration data for the network
308 *
309 * This function is used to configure WPA state machine and related parameters
310 * to a mode where WPA is not enabled. This is called as part of the
311 * authentication configuration when the selected network does not use WPA.
312 */
313void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
314 struct wpa_ssid *ssid)
315{
316 int i;
317
318 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
319 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
320 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
321 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
322 else
323 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
324 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
325 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
326 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
327 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
328 wpa_s->group_cipher = WPA_CIPHER_NONE;
329 wpa_s->mgmt_group_cipher = 0;
330
331 for (i = 0; i < NUM_WEP_KEYS; i++) {
332 if (ssid->wep_key_len[i] > 5) {
333 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
334 wpa_s->group_cipher = WPA_CIPHER_WEP104;
335 break;
336 } else if (ssid->wep_key_len[i] > 0) {
337 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
338 wpa_s->group_cipher = WPA_CIPHER_WEP40;
339 break;
340 }
341 }
342
343 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
344 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
345 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
346 wpa_s->pairwise_cipher);
347 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
348#ifdef CONFIG_IEEE80211W
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
350 wpa_s->mgmt_group_cipher);
351#endif /* CONFIG_IEEE80211W */
352
353 pmksa_cache_clear_current(wpa_s->wpa);
354}
355
356
Dmitry Shmidt04949592012-07-19 12:16:46 -0700357void free_hw_features(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358{
359 int i;
360 if (wpa_s->hw.modes == NULL)
361 return;
362
363 for (i = 0; i < wpa_s->hw.num_modes; i++) {
364 os_free(wpa_s->hw.modes[i].channels);
365 os_free(wpa_s->hw.modes[i].rates);
366 }
367
368 os_free(wpa_s->hw.modes);
369 wpa_s->hw.modes = NULL;
370}
371
372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
374{
375 bgscan_deinit(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700376 autoscan_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 scard_deinit(wpa_s->scard);
378 wpa_s->scard = NULL;
379 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
380 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
381 l2_packet_deinit(wpa_s->l2);
382 wpa_s->l2 = NULL;
383 if (wpa_s->l2_br) {
384 l2_packet_deinit(wpa_s->l2_br);
385 wpa_s->l2_br = NULL;
386 }
387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 if (wpa_s->conf != NULL) {
389 struct wpa_ssid *ssid;
390 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
391 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 }
393
394 os_free(wpa_s->confname);
395 wpa_s->confname = NULL;
396
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -0700397 os_free(wpa_s->confanother);
398 wpa_s->confanother = NULL;
399
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800400#ifdef CONFIG_P2P
401 os_free(wpa_s->conf_p2p_dev);
402 wpa_s->conf_p2p_dev = NULL;
403#endif /* CONFIG_P2P */
404
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 wpa_sm_set_eapol(wpa_s->wpa, NULL);
406 eapol_sm_deinit(wpa_s->eapol);
407 wpa_s->eapol = NULL;
408
409 rsn_preauth_deinit(wpa_s->wpa);
410
411#ifdef CONFIG_TDLS
412 wpa_tdls_deinit(wpa_s->wpa);
413#endif /* CONFIG_TDLS */
414
415 pmksa_candidate_free(wpa_s->wpa);
416 wpa_sm_deinit(wpa_s->wpa);
417 wpa_s->wpa = NULL;
418 wpa_blacklist_clear(wpa_s);
419
420 wpa_bss_deinit(wpa_s);
421
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700422 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 wpa_supplicant_cancel_scan(wpa_s);
424 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800425 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
426#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
427 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
428 wpa_s, NULL);
429#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430
431 wpas_wps_deinit(wpa_s);
432
433 wpabuf_free(wpa_s->pending_eapol_rx);
434 wpa_s->pending_eapol_rx = NULL;
435
436#ifdef CONFIG_IBSS_RSN
437 ibss_rsn_deinit(wpa_s->ibss_rsn);
438 wpa_s->ibss_rsn = NULL;
439#endif /* CONFIG_IBSS_RSN */
440
441 sme_deinit(wpa_s);
442
443#ifdef CONFIG_AP
444 wpa_supplicant_ap_deinit(wpa_s);
445#endif /* CONFIG_AP */
446
447#ifdef CONFIG_P2P
448 wpas_p2p_deinit(wpa_s);
449#endif /* CONFIG_P2P */
450
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800451#ifdef CONFIG_OFFCHANNEL
452 offchannel_deinit(wpa_s);
453#endif /* CONFIG_OFFCHANNEL */
454
455 wpa_supplicant_cancel_sched_scan(wpa_s);
456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 os_free(wpa_s->next_scan_freqs);
458 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800459
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800460 os_free(wpa_s->manual_scan_freqs);
461 wpa_s->manual_scan_freqs = NULL;
462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463 gas_query_deinit(wpa_s->gas);
464 wpa_s->gas = NULL;
465
466 free_hw_features(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700467
468 os_free(wpa_s->bssid_filter);
469 wpa_s->bssid_filter = NULL;
470
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800471 os_free(wpa_s->disallow_aps_bssid);
472 wpa_s->disallow_aps_bssid = NULL;
473 os_free(wpa_s->disallow_aps_ssid);
474 wpa_s->disallow_aps_ssid = NULL;
475
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 wnm_bss_keep_alive_deinit(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700477#ifdef CONFIG_WNM
478 wnm_deallocate_memory(wpa_s);
479#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700480
481 ext_password_deinit(wpa_s->ext_pw);
482 wpa_s->ext_pw = NULL;
483
484 wpabuf_free(wpa_s->last_gas_resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800485 wpa_s->last_gas_resp = NULL;
486 wpabuf_free(wpa_s->prev_gas_resp);
487 wpa_s->prev_gas_resp = NULL;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700488
489 os_free(wpa_s->last_scan_res);
490 wpa_s->last_scan_res = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800491
492#ifdef CONFIG_HS20
493 hs20_free_osu_prov(wpa_s);
494#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495}
496
497
498/**
499 * wpa_clear_keys - Clear keys configured for the driver
500 * @wpa_s: Pointer to wpa_supplicant data
501 * @addr: Previously used BSSID or %NULL if not available
502 *
503 * This function clears the encryption keys that has been previously configured
504 * for the driver.
505 */
506void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
507{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800508 int i, max;
509
510#ifdef CONFIG_IEEE80211W
511 max = 6;
512#else /* CONFIG_IEEE80211W */
513 max = 4;
514#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515
516 /* MLME-DELETEKEYS.request */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800517 for (i = 0; i < max; i++) {
518 if (wpa_s->keys_cleared & BIT(i))
519 continue;
520 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
521 NULL, 0);
522 }
523 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
524 !is_zero_ether_addr(addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
526 0);
527 /* MLME-SETPROTECTION.request(None) */
528 wpa_drv_mlme_setprotection(
529 wpa_s, addr,
530 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
531 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
532 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800533 wpa_s->keys_cleared = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534}
535
536
537/**
538 * wpa_supplicant_state_txt - Get the connection state name as a text string
539 * @state: State (wpa_state; WPA_*)
540 * Returns: The state name as a printable text string
541 */
542const char * wpa_supplicant_state_txt(enum wpa_states state)
543{
544 switch (state) {
545 case WPA_DISCONNECTED:
546 return "DISCONNECTED";
547 case WPA_INACTIVE:
548 return "INACTIVE";
549 case WPA_INTERFACE_DISABLED:
550 return "INTERFACE_DISABLED";
551 case WPA_SCANNING:
552 return "SCANNING";
553 case WPA_AUTHENTICATING:
554 return "AUTHENTICATING";
555 case WPA_ASSOCIATING:
556 return "ASSOCIATING";
557 case WPA_ASSOCIATED:
558 return "ASSOCIATED";
559 case WPA_4WAY_HANDSHAKE:
560 return "4WAY_HANDSHAKE";
561 case WPA_GROUP_HANDSHAKE:
562 return "GROUP_HANDSHAKE";
563 case WPA_COMPLETED:
564 return "COMPLETED";
565 default:
566 return "UNKNOWN";
567 }
568}
569
570
571#ifdef CONFIG_BGSCAN
572
573static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
574{
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800575 const char *name;
576
577 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
578 name = wpa_s->current_ssid->bgscan;
579 else
580 name = wpa_s->conf->bgscan;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800581 if (name == NULL || name[0] == '\0')
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800582 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800583 if (wpas_driver_bss_selection(wpa_s))
584 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
586 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800587#ifdef CONFIG_P2P
588 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
589 return;
590#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700591
592 bgscan_deinit(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800593 if (wpa_s->current_ssid) {
594 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
596 "bgscan");
597 /*
598 * Live without bgscan; it is only used as a roaming
599 * optimization, so the initial connection is not
600 * affected.
601 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700602 } else {
603 struct wpa_scan_results *scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 wpa_s->bgscan_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700605 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
606 0);
607 if (scan_res) {
608 bgscan_notify_scan(wpa_s, scan_res);
609 wpa_scan_results_free(scan_res);
610 }
611 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 } else
613 wpa_s->bgscan_ssid = NULL;
614}
615
616
617static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
618{
619 if (wpa_s->bgscan_ssid != NULL) {
620 bgscan_deinit(wpa_s);
621 wpa_s->bgscan_ssid = NULL;
622 }
623}
624
625#endif /* CONFIG_BGSCAN */
626
627
Dmitry Shmidt04949592012-07-19 12:16:46 -0700628static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
629{
630 if (autoscan_init(wpa_s, 0))
631 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
632}
633
634
635static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
636{
637 autoscan_deinit(wpa_s);
638}
639
640
641void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
642{
643 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
644 wpa_s->wpa_state == WPA_SCANNING) {
645 autoscan_deinit(wpa_s);
646 wpa_supplicant_start_autoscan(wpa_s);
647 }
648}
649
650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651/**
652 * wpa_supplicant_set_state - Set current connection state
653 * @wpa_s: Pointer to wpa_supplicant data
654 * @state: The new connection state
655 *
656 * This function is called whenever the connection state changes, e.g.,
657 * association is completed for WPA/WPA2 4-Way Handshake is started.
658 */
659void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
660 enum wpa_states state)
661{
662 enum wpa_states old_state = wpa_s->wpa_state;
663
664 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
665 wpa_supplicant_state_txt(wpa_s->wpa_state),
666 wpa_supplicant_state_txt(state));
667
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -0800668 if (state == WPA_INTERFACE_DISABLED) {
669 /* Assure normal scan when interface is restored */
670 wpa_s->normal_scans = 0;
671 }
672
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800673 if (state == WPA_COMPLETED)
674 wpas_connect_work_done(wpa_s);
675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 if (state != WPA_SCANNING)
677 wpa_supplicant_notify_scanning(wpa_s, 0);
678
679 if (state == WPA_COMPLETED && wpa_s->new_connection) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700681#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
Dmitry Shmidt8f0dbf42013-11-08 13:35:41 -0800683 MACSTR " completed [id=%d id_str=%s]",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800684 MAC2STR(wpa_s->bssid),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 ssid ? ssid->id : -1,
686 ssid && ssid->id_str ? ssid->id_str : "");
687#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 wpas_clear_temp_disabled(wpa_s, ssid, 1);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800689 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 wpa_s->new_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_drv_set_operstate(wpa_s, 1);
692#ifndef IEEE8021X_EAPOL
693 wpa_drv_set_supp_port(wpa_s, 1);
694#endif /* IEEE8021X_EAPOL */
695 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700696 wpa_s->known_wps_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697#ifdef CONFIG_P2P
698 wpas_p2p_completed(wpa_s);
699#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700700
701 sme_sched_obss_scan(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
703 state == WPA_ASSOCIATED) {
704 wpa_s->new_connection = 1;
705 wpa_drv_set_operstate(wpa_s, 0);
706#ifndef IEEE8021X_EAPOL
707 wpa_drv_set_supp_port(wpa_s, 0);
708#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700709 sme_sched_obss_scan(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 }
711 wpa_s->wpa_state = state;
712
713#ifdef CONFIG_BGSCAN
714 if (state == WPA_COMPLETED)
715 wpa_supplicant_start_bgscan(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800716 else if (state < WPA_ASSOCIATED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 wpa_supplicant_stop_bgscan(wpa_s);
718#endif /* CONFIG_BGSCAN */
719
Dmitry Shmidt04949592012-07-19 12:16:46 -0700720 if (state == WPA_AUTHENTICATING)
721 wpa_supplicant_stop_autoscan(wpa_s);
722
723 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
724 wpa_supplicant_start_autoscan(wpa_s);
725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 if (wpa_s->wpa_state != old_state) {
727 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
728
729 if (wpa_s->wpa_state == WPA_COMPLETED ||
730 old_state == WPA_COMPLETED)
731 wpas_notify_auth_changed(wpa_s);
732 }
733}
734
735
736void wpa_supplicant_terminate_proc(struct wpa_global *global)
737{
738 int pending = 0;
739#ifdef CONFIG_WPS
740 struct wpa_supplicant *wpa_s = global->ifaces;
741 while (wpa_s) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800742 struct wpa_supplicant *next = wpa_s->next;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700743#ifdef CONFIG_P2P
744 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
745 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
746 wpas_p2p_disconnect(wpa_s);
747#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 if (wpas_wps_terminate_pending(wpa_s) == 1)
749 pending = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800750 wpa_s = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 }
752#endif /* CONFIG_WPS */
753 if (pending)
754 return;
755 eloop_terminate();
756}
757
758
759static void wpa_supplicant_terminate(int sig, void *signal_ctx)
760{
761 struct wpa_global *global = signal_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_supplicant_terminate_proc(global);
763}
764
765
766void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
767{
768 enum wpa_states old_state = wpa_s->wpa_state;
769
770 wpa_s->pairwise_cipher = 0;
771 wpa_s->group_cipher = 0;
772 wpa_s->mgmt_group_cipher = 0;
773 wpa_s->key_mgmt = 0;
774 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700775 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776
777 if (wpa_s->wpa_state != old_state)
778 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
779}
780
781
782/**
783 * wpa_supplicant_reload_configuration - Reload configuration data
784 * @wpa_s: Pointer to wpa_supplicant data
785 * Returns: 0 on success or -1 if configuration parsing failed
786 *
787 * This function can be used to request that the configuration data is reloaded
788 * (e.g., after configuration file change). This function is reloading
789 * configuration only for one interface, so this may need to be called multiple
790 * times if %wpa_supplicant is controlling multiple interfaces and all
791 * interfaces need reconfiguration.
792 */
793int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
794{
795 struct wpa_config *conf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 int reconf_ctrl;
797 int old_ap_scan;
798
799 if (wpa_s->confname == NULL)
800 return -1;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700801 conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 if (conf == NULL) {
803 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
804 "file '%s' - exiting", wpa_s->confname);
805 return -1;
806 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700807 wpa_config_read(wpa_s->confanother, conf);
808
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 conf->changed_parameters = (unsigned int) -1;
810
811 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
812 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
813 os_strcmp(conf->ctrl_interface,
814 wpa_s->conf->ctrl_interface) != 0);
815
816 if (reconf_ctrl && wpa_s->ctrl_iface) {
817 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
818 wpa_s->ctrl_iface = NULL;
819 }
820
821 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800822 if (wpa_s->current_ssid) {
823 wpa_supplicant_deauthenticate(wpa_s,
824 WLAN_REASON_DEAUTH_LEAVING);
825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826
827 /*
828 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
829 * pkcs11_engine_path, pkcs11_module_path.
830 */
831 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
832 /*
833 * Clear forced success to clear EAP state for next
834 * authentication.
835 */
836 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
837 }
838 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
839 wpa_sm_set_config(wpa_s->wpa, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800840 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
842 rsn_preauth_deinit(wpa_s->wpa);
843
844 old_ap_scan = wpa_s->conf->ap_scan;
845 wpa_config_free(wpa_s->conf);
846 wpa_s->conf = conf;
847 if (old_ap_scan != wpa_s->conf->ap_scan)
848 wpas_notify_ap_scan_changed(wpa_s);
849
850 if (reconf_ctrl)
851 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
852
853 wpa_supplicant_update_config(wpa_s);
854
855 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 wpa_s->reassociate = 1;
858 wpa_supplicant_req_scan(wpa_s, 0, 0);
859 }
860 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
861 return 0;
862}
863
864
865static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
866{
867 struct wpa_global *global = signal_ctx;
868 struct wpa_supplicant *wpa_s;
869 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
870 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
871 sig);
872 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
873 wpa_supplicant_terminate_proc(global);
874 }
875 }
876}
877
878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
880 struct wpa_ssid *ssid,
881 struct wpa_ie_data *ie)
882{
883 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
884 if (ret) {
885 if (ret == -2) {
886 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
887 "from association info");
888 }
889 return -1;
890 }
891
892 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
893 "cipher suites");
894 if (!(ie->group_cipher & ssid->group_cipher)) {
895 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
896 "cipher 0x%x (mask 0x%x) - reject",
897 ie->group_cipher, ssid->group_cipher);
898 return -1;
899 }
900 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
901 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
902 "cipher 0x%x (mask 0x%x) - reject",
903 ie->pairwise_cipher, ssid->pairwise_cipher);
904 return -1;
905 }
906 if (!(ie->key_mgmt & ssid->key_mgmt)) {
907 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
908 "management 0x%x (mask 0x%x) - reject",
909 ie->key_mgmt, ssid->key_mgmt);
910 return -1;
911 }
912
913#ifdef CONFIG_IEEE80211W
914 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800915 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
916 wpa_s->conf->pmf : ssid->ieee80211w) ==
917 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
919 "that does not support management frame protection - "
920 "reject");
921 return -1;
922 }
923#endif /* CONFIG_IEEE80211W */
924
925 return 0;
926}
927
928
929/**
930 * wpa_supplicant_set_suites - Set authentication and encryption parameters
931 * @wpa_s: Pointer to wpa_supplicant data
932 * @bss: Scan results for the selected BSS, or %NULL if not available
933 * @ssid: Configuration data for the selected network
934 * @wpa_ie: Buffer for the WPA/RSN IE
935 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
936 * used buffer length in case the functions returns success.
937 * Returns: 0 on success or -1 on failure
938 *
939 * This function is used to configure authentication and encryption parameters
940 * based on the network configuration and scan result for the selected BSS (if
941 * available).
942 */
943int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
944 struct wpa_bss *bss, struct wpa_ssid *ssid,
945 u8 *wpa_ie, size_t *wpa_ie_len)
946{
947 struct wpa_ie_data ie;
948 int sel, proto;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800949 const u8 *bss_wpa, *bss_rsn, *bss_osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
951 if (bss) {
952 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
953 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800954 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955 } else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800956 bss_wpa = bss_rsn = bss_osen = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957
958 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
959 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
960 (ie.group_cipher & ssid->group_cipher) &&
961 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
962 (ie.key_mgmt & ssid->key_mgmt)) {
963 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
964 proto = WPA_PROTO_RSN;
965 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
966 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
967 (ie.group_cipher & ssid->group_cipher) &&
968 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
969 (ie.key_mgmt & ssid->key_mgmt)) {
970 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
971 proto = WPA_PROTO_WPA;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800972#ifdef CONFIG_HS20
973 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
974 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
975 /* TODO: parse OSEN element */
976 ie.group_cipher = WPA_CIPHER_CCMP;
977 ie.pairwise_cipher = WPA_CIPHER_CCMP;
978 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
979 proto = WPA_PROTO_OSEN;
980#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 } else if (bss) {
982 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
983 return -1;
984 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800985 if (ssid->proto & WPA_PROTO_OSEN)
986 proto = WPA_PROTO_OSEN;
987 else if (ssid->proto & WPA_PROTO_RSN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 proto = WPA_PROTO_RSN;
989 else
990 proto = WPA_PROTO_WPA;
991 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
992 os_memset(&ie, 0, sizeof(ie));
993 ie.group_cipher = ssid->group_cipher;
994 ie.pairwise_cipher = ssid->pairwise_cipher;
995 ie.key_mgmt = ssid->key_mgmt;
996#ifdef CONFIG_IEEE80211W
997 ie.mgmt_group_cipher =
998 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
999 WPA_CIPHER_AES_128_CMAC : 0;
1000#endif /* CONFIG_IEEE80211W */
1001 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1002 "based on configuration");
1003 } else
1004 proto = ie.proto;
1005 }
1006
1007 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1008 "pairwise %d key_mgmt %d proto %d",
1009 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1010#ifdef CONFIG_IEEE80211W
1011 if (ssid->ieee80211w) {
1012 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1013 ie.mgmt_group_cipher);
1014 }
1015#endif /* CONFIG_IEEE80211W */
1016
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001017 wpa_s->wpa_proto = proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1019 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001020 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021
1022 if (bss || !wpa_s->ap_ies_from_associnfo) {
1023 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1024 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1025 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1026 bss_rsn ? 2 + bss_rsn[1] : 0))
1027 return -1;
1028 }
1029
1030 sel = ie.group_cipher & ssid->group_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001031 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1032 if (wpa_s->group_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1034 "cipher");
1035 return -1;
1036 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001037 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1038 wpa_cipher_txt(wpa_s->group_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039
1040 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001041 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1042 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1044 "cipher");
1045 return -1;
1046 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001047 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1048 wpa_cipher_txt(wpa_s->pairwise_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049
1050 sel = ie.key_mgmt & ssid->key_mgmt;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001051#ifdef CONFIG_SAE
1052 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1053 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1054#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 if (0) {
1056#ifdef CONFIG_IEEE80211R
1057 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1058 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1059 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1060 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1061 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1062 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1063#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001064#ifdef CONFIG_SAE
1065 } else if (sel & WPA_KEY_MGMT_SAE) {
1066 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1067 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1068 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1069 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1070 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1071#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072#ifdef CONFIG_IEEE80211W
1073 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1074 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1075 wpa_dbg(wpa_s, MSG_DEBUG,
1076 "WPA: using KEY_MGMT 802.1X with SHA256");
1077 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1078 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1079 wpa_dbg(wpa_s, MSG_DEBUG,
1080 "WPA: using KEY_MGMT PSK with SHA256");
1081#endif /* CONFIG_IEEE80211W */
1082 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1083 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1084 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1085 } else if (sel & WPA_KEY_MGMT_PSK) {
1086 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1087 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1088 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1089 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1090 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001091#ifdef CONFIG_HS20
1092 } else if (sel & WPA_KEY_MGMT_OSEN) {
1093 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1094 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1095#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001096 } else {
1097 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1098 "authenticated key management type");
1099 return -1;
1100 }
1101
1102 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1103 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1104 wpa_s->pairwise_cipher);
1105 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1106
1107#ifdef CONFIG_IEEE80211W
1108 sel = ie.mgmt_group_cipher;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001109 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1110 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1112 sel = 0;
1113 if (sel & WPA_CIPHER_AES_128_CMAC) {
1114 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1115 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1116 "AES-128-CMAC");
1117 } else {
1118 wpa_s->mgmt_group_cipher = 0;
1119 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1120 }
1121 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1122 wpa_s->mgmt_group_cipher);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001123 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1124 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1125 wpa_s->conf->pmf : ssid->ieee80211w));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126#endif /* CONFIG_IEEE80211W */
1127
1128 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1129 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1130 return -1;
1131 }
1132
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001133 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135#ifndef CONFIG_NO_PBKDF2
1136 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1137 ssid->passphrase) {
1138 u8 psk[PMK_LEN];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001139 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1140 4096, psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1142 psk, PMK_LEN);
1143 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1144 }
1145#endif /* CONFIG_NO_PBKDF2 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001146#ifdef CONFIG_EXT_PASSWORD
1147 if (ssid->ext_psk) {
1148 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1149 ssid->ext_psk);
1150 char pw_str[64 + 1];
1151 u8 psk[PMK_LEN];
1152
1153 if (pw == NULL) {
1154 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1155 "found from external storage");
1156 return -1;
1157 }
1158
1159 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1160 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1161 "PSK length %d in external storage",
1162 (int) wpabuf_len(pw));
1163 ext_password_free(pw);
1164 return -1;
1165 }
1166
1167 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1168 pw_str[wpabuf_len(pw)] = '\0';
1169
1170#ifndef CONFIG_NO_PBKDF2
1171 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1172 {
1173 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1174 4096, psk, PMK_LEN);
1175 os_memset(pw_str, 0, sizeof(pw_str));
1176 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1177 "external passphrase)",
1178 psk, PMK_LEN);
1179 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1180 } else
1181#endif /* CONFIG_NO_PBKDF2 */
1182 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1183 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1184 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1185 "Invalid PSK hex string");
1186 os_memset(pw_str, 0, sizeof(pw_str));
1187 ext_password_free(pw);
1188 return -1;
1189 }
1190 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1191 } else {
1192 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1193 "PSK available");
1194 os_memset(pw_str, 0, sizeof(pw_str));
1195 ext_password_free(pw);
1196 return -1;
1197 }
1198
1199 os_memset(pw_str, 0, sizeof(pw_str));
1200 ext_password_free(pw);
1201 }
1202#endif /* CONFIG_EXT_PASSWORD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001203 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1205
1206 return 0;
1207}
1208
1209
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001210static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1211{
1212 *pos = 0x00;
1213
1214 switch (idx) {
1215 case 0: /* Bits 0-7 */
1216 break;
1217 case 1: /* Bits 8-15 */
1218 break;
1219 case 2: /* Bits 16-23 */
1220#ifdef CONFIG_WNM
1221 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1222 *pos |= 0x08; /* Bit 19 - BSS Transition */
1223#endif /* CONFIG_WNM */
1224 break;
1225 case 3: /* Bits 24-31 */
1226#ifdef CONFIG_WNM
1227 *pos |= 0x02; /* Bit 25 - SSID List */
1228#endif /* CONFIG_WNM */
1229#ifdef CONFIG_INTERWORKING
1230 if (wpa_s->conf->interworking)
1231 *pos |= 0x80; /* Bit 31 - Interworking */
1232#endif /* CONFIG_INTERWORKING */
1233 break;
1234 case 4: /* Bits 32-39 */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001235#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001236 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1237 *pos |= 0x01; /* Bit 32 - QoS Map */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001238#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001239 break;
1240 case 5: /* Bits 40-47 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001241#ifdef CONFIG_HS20
1242 if (wpa_s->conf->hs20)
1243 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1244#endif /* CONFIG_HS20 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001245 break;
1246 case 6: /* Bits 48-55 */
1247 break;
1248 }
1249}
1250
1251
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001252int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf)
1253{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001254 u8 *pos = buf;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001255 u8 len = 6, i;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001256
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001257 if (len < wpa_s->extended_capa_len)
1258 len = wpa_s->extended_capa_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001259
1260 *pos++ = WLAN_EID_EXT_CAPAB;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001261 *pos++ = len;
1262 for (i = 0; i < len; i++, pos++) {
1263 wpas_ext_capab_byte(wpa_s, pos, i);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001264
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001265 if (i < wpa_s->extended_capa_len) {
1266 *pos &= ~wpa_s->extended_capa_mask[i];
1267 *pos |= wpa_s->extended_capa[i];
1268 }
1269 }
1270
1271 while (len > 0 && buf[1 + len] == 0) {
1272 len--;
1273 buf[1] = len;
1274 }
1275 if (len == 0)
1276 return 0;
1277
1278 return 2 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001279}
1280
1281
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001282static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1283 struct wpa_bss *test_bss)
1284{
1285 struct wpa_bss *bss;
1286
1287 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1288 if (bss == test_bss)
1289 return 1;
1290 }
1291
1292 return 0;
1293}
1294
1295
1296static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1297 struct wpa_ssid *test_ssid)
1298{
1299 struct wpa_ssid *ssid;
1300
1301 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1302 if (ssid == test_ssid)
1303 return 1;
1304 }
1305
1306 return 0;
1307}
1308
1309
1310int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1311 struct wpa_ssid *test_ssid)
1312{
1313 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1314 return 0;
1315
1316 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1317}
1318
1319
1320void wpas_connect_work_free(struct wpa_connect_work *cwork)
1321{
1322 if (cwork == NULL)
1323 return;
1324 os_free(cwork);
1325}
1326
1327
1328void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1329{
1330 struct wpa_connect_work *cwork;
1331 struct wpa_radio_work *work = wpa_s->connect_work;
1332
1333 if (!work)
1334 return;
1335
1336 wpa_s->connect_work = NULL;
1337 cwork = work->ctx;
1338 work->ctx = NULL;
1339 wpas_connect_work_free(cwork);
1340 radio_work_done(work);
1341}
1342
1343
1344static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346/**
1347 * wpa_supplicant_associate - Request association
1348 * @wpa_s: Pointer to wpa_supplicant data
1349 * @bss: Scan results for the selected BSS, or %NULL if not available
1350 * @ssid: Configuration data for the selected network
1351 *
1352 * This function is used to request %wpa_supplicant to associate with a BSS.
1353 */
1354void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1355 struct wpa_bss *bss, struct wpa_ssid *ssid)
1356{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001357 struct wpa_connect_work *cwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358
1359#ifdef CONFIG_IBSS_RSN
1360 ibss_rsn_deinit(wpa_s->ibss_rsn);
1361 wpa_s->ibss_rsn = NULL;
1362#endif /* CONFIG_IBSS_RSN */
1363
1364 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1365 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1366#ifdef CONFIG_AP
1367 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1368 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1369 "mode");
1370 return;
1371 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001372 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1373 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001374 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1375 wpas_p2p_ap_setup_failed(wpa_s);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001376 return;
1377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 wpa_s->current_bss = bss;
1379#else /* CONFIG_AP */
1380 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1381 "the build");
1382#endif /* CONFIG_AP */
1383 return;
1384 }
1385
1386#ifdef CONFIG_TDLS
1387 if (bss)
1388 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1389 bss->ie_len);
1390#endif /* CONFIG_TDLS */
1391
1392 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1393 ssid->mode == IEEE80211_MODE_INFRA) {
1394 sme_authenticate(wpa_s, bss, ssid);
1395 return;
1396 }
1397
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001398 if (wpa_s->connect_work) {
1399 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1400 return;
1401 }
1402
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001403 if (radio_work_pending(wpa_s, "connect")) {
1404 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1405 return;
1406 }
1407
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001408 cwork = os_zalloc(sizeof(*cwork));
1409 if (cwork == NULL)
1410 return;
1411
1412 cwork->bss = bss;
1413 cwork->ssid = ssid;
1414
1415 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1416 wpas_start_assoc_cb, cwork) < 0) {
1417 os_free(cwork);
1418 }
1419}
1420
1421
1422static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1423{
1424 struct wpa_connect_work *cwork = work->ctx;
1425 struct wpa_bss *bss = cwork->bss;
1426 struct wpa_ssid *ssid = cwork->ssid;
1427 struct wpa_supplicant *wpa_s = work->wpa_s;
1428 u8 wpa_ie[200];
1429 size_t wpa_ie_len;
1430 int use_crypt, ret, i, bssid_changed;
1431 int algs = WPA_AUTH_ALG_OPEN;
1432 unsigned int cipher_pairwise, cipher_group;
1433 struct wpa_driver_associate_params params;
1434 int wep_keys_set = 0;
1435 int assoc_failed = 0;
1436 struct wpa_ssid *old_ssid;
1437#ifdef CONFIG_HT_OVERRIDES
1438 struct ieee80211_ht_capabilities htcaps;
1439 struct ieee80211_ht_capabilities htcaps_mask;
1440#endif /* CONFIG_HT_OVERRIDES */
1441
1442 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001443 if (work->started) {
1444 wpa_s->connect_work = NULL;
1445
1446 /* cancel possible auth. timeout */
1447 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1448 NULL);
1449 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 wpas_connect_work_free(cwork);
1451 return;
1452 }
1453
1454 wpa_s->connect_work = work;
1455
1456 if (!wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1457 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1458 wpas_connect_work_done(wpa_s);
1459 return;
1460 }
1461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 os_memset(&params, 0, sizeof(params));
1463 wpa_s->reassociate = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001464 wpa_s->eap_expected_failure = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001465 if (bss && !wpas_driver_bss_selection(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466#ifdef CONFIG_IEEE80211R
1467 const u8 *ie, *md = NULL;
1468#endif /* CONFIG_IEEE80211R */
1469 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1470 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1471 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1472 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1473 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1474 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1475 if (bssid_changed)
1476 wpas_notify_bssid_changed(wpa_s);
1477#ifdef CONFIG_IEEE80211R
1478 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1479 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1480 md = ie + 2;
1481 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1482 if (md) {
1483 /* Prepare for the next transition */
1484 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1485 }
1486#endif /* CONFIG_IEEE80211R */
1487#ifdef CONFIG_WPS
1488 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1489 wpa_s->conf->ap_scan == 2 &&
1490 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1491 /* Use ap_scan==1 style network selection to find the network
1492 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001493 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 wpa_s->reassociate = 1;
1495 wpa_supplicant_req_scan(wpa_s, 0, 0);
1496 return;
1497#endif /* CONFIG_WPS */
1498 } else {
1499 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1500 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1501 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1502 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001503 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 wpa_supplicant_cancel_scan(wpa_s);
1505
1506 /* Starting new association, so clear the possibly used WPA IE from the
1507 * previous association. */
1508 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1509
1510#ifdef IEEE8021X_EAPOL
1511 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1512 if (ssid->leap) {
1513 if (ssid->non_leap == 0)
1514 algs = WPA_AUTH_ALG_LEAP;
1515 else
1516 algs |= WPA_AUTH_ALG_LEAP;
1517 }
1518 }
1519#endif /* IEEE8021X_EAPOL */
1520 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1521 if (ssid->auth_alg) {
1522 algs = ssid->auth_alg;
1523 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1524 "0x%x", algs);
1525 }
1526
1527 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1528 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001529 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 int try_opportunistic;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001531 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1532 wpa_s->conf->okc :
1533 ssid->proactive_key_caching) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 (ssid->proto & WPA_PROTO_RSN);
1535 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001536 ssid, try_opportunistic) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1538 wpa_ie_len = sizeof(wpa_ie);
1539 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1540 wpa_ie, &wpa_ie_len)) {
1541 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1542 "key management and encryption suites");
1543 return;
1544 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001545 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1546 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1547 /*
1548 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1549 * use non-WPA since the scan results did not indicate that the
1550 * AP is using WPA or WPA2.
1551 */
1552 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1553 wpa_ie_len = 0;
1554 wpa_s->wpa_proto = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 wpa_ie_len = sizeof(wpa_ie);
1557 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1558 wpa_ie, &wpa_ie_len)) {
1559 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1560 "key management and encryption suites (no "
1561 "scan results)");
1562 return;
1563 }
1564#ifdef CONFIG_WPS
1565 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1566 struct wpabuf *wps_ie;
1567 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1568 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1569 wpa_ie_len = wpabuf_len(wps_ie);
1570 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1571 } else
1572 wpa_ie_len = 0;
1573 wpabuf_free(wps_ie);
1574 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1575 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1576 params.wps = WPS_MODE_PRIVACY;
1577 else
1578 params.wps = WPS_MODE_OPEN;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001579 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580#endif /* CONFIG_WPS */
1581 } else {
1582 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1583 wpa_ie_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001584 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 }
1586
1587#ifdef CONFIG_P2P
1588 if (wpa_s->global->p2p) {
1589 u8 *pos;
1590 size_t len;
1591 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 pos = wpa_ie + wpa_ie_len;
1593 len = sizeof(wpa_ie) - wpa_ie_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001594 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1595 ssid->p2p_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596 if (res >= 0)
1597 wpa_ie_len += res;
1598 }
1599
1600 wpa_s->cross_connect_disallowed = 0;
1601 if (bss) {
1602 struct wpabuf *p2p;
1603 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1604 if (p2p) {
1605 wpa_s->cross_connect_disallowed =
1606 p2p_get_cross_connect_disallowed(p2p);
1607 wpabuf_free(p2p);
1608 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1609 "connection",
1610 wpa_s->cross_connect_disallowed ?
1611 "disallows" : "allows");
1612 }
1613 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001614
1615 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616#endif /* CONFIG_P2P */
1617
Dmitry Shmidt04949592012-07-19 12:16:46 -07001618#ifdef CONFIG_HS20
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001619 if (is_hs20_network(wpa_s, ssid, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001620 struct wpabuf *hs20;
1621 hs20 = wpabuf_alloc(20);
1622 if (hs20) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001623 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
1624 wpas_hs20_add_indication(hs20, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001625 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1626 wpabuf_len(hs20));
1627 wpa_ie_len += wpabuf_len(hs20);
1628 wpabuf_free(hs20);
1629 }
1630 }
1631#endif /* CONFIG_HS20 */
1632
Dmitry Shmidt56052862013-10-04 10:23:25 -07001633 /*
1634 * Workaround: Add Extended Capabilities element only if the AP
1635 * included this element in Beacon/Probe Response frames. Some older
1636 * APs seem to have interoperability issues if this element is
1637 * included, so while the standard may require us to include the
1638 * element in all cases, it is justifiable to skip it to avoid
1639 * interoperability issues.
1640 */
1641 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
1642 u8 ext_capab[10];
1643 int ext_capab_len;
1644 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab);
1645 if (ext_capab_len > 0) {
1646 u8 *pos = wpa_ie;
1647 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1648 pos += 2 + pos[1];
1649 os_memmove(pos + ext_capab_len, pos,
1650 wpa_ie_len - (pos - wpa_ie));
1651 wpa_ie_len += ext_capab_len;
1652 os_memcpy(pos, ext_capab, ext_capab_len);
1653 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001654 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1657 use_crypt = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001658 cipher_pairwise = wpa_s->pairwise_cipher;
1659 cipher_group = wpa_s->group_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1661 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1662 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1663 use_crypt = 0;
1664 if (wpa_set_wep_keys(wpa_s, ssid)) {
1665 use_crypt = 1;
1666 wep_keys_set = 1;
1667 }
1668 }
1669 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1670 use_crypt = 0;
1671
1672#ifdef IEEE8021X_EAPOL
1673 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1674 if ((ssid->eapol_flags &
1675 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1676 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1677 !wep_keys_set) {
1678 use_crypt = 0;
1679 } else {
1680 /* Assume that dynamic WEP-104 keys will be used and
1681 * set cipher suites in order for drivers to expect
1682 * encryption. */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001683 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 }
1685 }
1686#endif /* IEEE8021X_EAPOL */
1687
1688 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1689 /* Set the key before (and later after) association */
1690 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1691 }
1692
1693 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1694 if (bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 params.ssid = bss->ssid;
1696 params.ssid_len = bss->ssid_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001697 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1698 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1699 MACSTR " freq=%u MHz based on scan results "
1700 "(bssid_set=%d)",
1701 MAC2STR(bss->bssid), bss->freq,
1702 ssid->bssid_set);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001703 params.bssid = bss->bssid;
1704 params.freq = bss->freq;
1705 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001706 params.bssid_hint = bss->bssid;
1707 params.freq_hint = bss->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 } else {
1709 params.ssid = ssid->ssid;
1710 params.ssid_len = ssid->ssid_len;
1711 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001712
1713 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1714 wpa_s->conf->ap_scan == 2) {
1715 params.bssid = ssid->bssid;
1716 params.fixed_bssid = 1;
1717 }
1718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1720 params.freq == 0)
1721 params.freq = ssid->frequency; /* Initial channel for IBSS */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08001722
1723 if (ssid->mode == WPAS_MODE_IBSS) {
1724 if (ssid->beacon_int)
1725 params.beacon_int = ssid->beacon_int;
1726 else
1727 params.beacon_int = wpa_s->conf->beacon_int;
1728 }
1729
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 params.wpa_ie = wpa_ie;
1731 params.wpa_ie_len = wpa_ie_len;
1732 params.pairwise_suite = cipher_pairwise;
1733 params.group_suite = cipher_group;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001734 params.key_mgmt_suite = wpa_s->key_mgmt;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001735 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736 params.auth_alg = algs;
1737 params.mode = ssid->mode;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001738 params.bg_scan_period = ssid->bg_scan_period;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 for (i = 0; i < NUM_WEP_KEYS; i++) {
1740 if (ssid->wep_key_len[i])
1741 params.wep_key[i] = ssid->wep_key[i];
1742 params.wep_key_len[i] = ssid->wep_key_len[i];
1743 }
1744 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1745
1746 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001747 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1748 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 params.passphrase = ssid->passphrase;
1750 if (ssid->psk_set)
1751 params.psk = ssid->psk;
1752 }
1753
1754 params.drop_unencrypted = use_crypt;
1755
1756#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001757 params.mgmt_frame_protection =
1758 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1759 wpa_s->conf->pmf : ssid->ieee80211w;
1760 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1762 struct wpa_ie_data ie;
1763 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1764 ie.capabilities &
1765 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1766 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1767 "MFP: require MFP");
1768 params.mgmt_frame_protection =
1769 MGMT_FRAME_PROTECTION_REQUIRED;
1770 }
1771 }
1772#endif /* CONFIG_IEEE80211W */
1773
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001774 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
1776 if (wpa_s->parent->set_sta_uapsd)
1777 params.uapsd = wpa_s->parent->sta_uapsd;
1778 else
1779 params.uapsd = -1;
1780
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001781#ifdef CONFIG_HT_OVERRIDES
1782 os_memset(&htcaps, 0, sizeof(htcaps));
1783 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1784 params.htcaps = (u8 *) &htcaps;
1785 params.htcaps_mask = (u8 *) &htcaps_mask;
1786 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1787#endif /* CONFIG_HT_OVERRIDES */
1788
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001789#ifdef CONFIG_P2P
1790 /*
1791 * If multi-channel concurrency is not supported, check for any
1792 * frequency conflict. In case of any frequency conflict, remove the
1793 * least prioritized connection.
1794 */
1795 if (wpa_s->num_multichan_concurrent < 2) {
1796 int freq = wpa_drv_shared_freq(wpa_s);
1797 if (freq > 0 && freq != params.freq) {
1798 wpa_printf(MSG_DEBUG, "Shared interface with conflicting frequency found (%d != %d)",
1799 freq, params.freq);
1800 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
1801 params.freq,
1802 ssid) < 0)
1803 return;
1804 }
1805 }
1806#endif /* CONFIG_P2P */
1807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001808 ret = wpa_drv_associate(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 if (ret < 0) {
1810 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1811 "failed");
1812 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1813 /*
1814 * The driver is known to mean what is saying, so we
1815 * can stop right here; the association will not
1816 * succeed.
1817 */
1818 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001819 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1821 return;
1822 }
1823 /* try to continue anyway; new association will be tried again
1824 * after timeout */
1825 assoc_failed = 1;
1826 }
1827
1828 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1829 /* Set the key after the association just in case association
1830 * cleared the previously configured key. */
1831 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1832 /* No need to timeout authentication since there is no key
1833 * management. */
1834 wpa_supplicant_cancel_auth_timeout(wpa_s);
1835 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1836#ifdef CONFIG_IBSS_RSN
1837 } else if (ssid->mode == WPAS_MODE_IBSS &&
1838 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1839 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1840 /*
1841 * RSN IBSS authentication is per-STA and we can disable the
1842 * per-BSSID authentication.
1843 */
1844 wpa_supplicant_cancel_auth_timeout(wpa_s);
1845#endif /* CONFIG_IBSS_RSN */
1846 } else {
1847 /* Timeout for IEEE 802.11 authentication and association */
1848 int timeout = 60;
1849
1850 if (assoc_failed) {
1851 /* give IBSS a bit more time */
1852 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1853 } else if (wpa_s->conf->ap_scan == 1) {
1854 /* give IBSS a bit more time */
1855 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1856 }
1857 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1858 }
1859
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001860 if (wep_keys_set &&
1861 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 /* Set static WEP keys again */
1863 wpa_set_wep_keys(wpa_s, ssid);
1864 }
1865
1866 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1867 /*
1868 * Do not allow EAP session resumption between different
1869 * network configurations.
1870 */
1871 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1872 }
1873 old_ssid = wpa_s->current_ssid;
1874 wpa_s->current_ssid = ssid;
1875 wpa_s->current_bss = bss;
1876 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1877 wpa_supplicant_initiate_eapol(wpa_s);
1878 if (old_ssid != wpa_s->current_ssid)
1879 wpas_notify_network_changed(wpa_s);
1880}
1881
1882
1883static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1884 const u8 *addr)
1885{
1886 struct wpa_ssid *old_ssid;
1887
1888 wpa_clear_keys(wpa_s, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 old_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001890 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 wpa_sm_set_config(wpa_s->wpa, NULL);
1892 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1893 if (old_ssid != wpa_s->current_ssid)
1894 wpas_notify_network_changed(wpa_s);
1895 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1896}
1897
1898
1899/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001900 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1901 * @wpa_s: Pointer to wpa_supplicant data
1902 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1903 *
1904 * This function is used to request %wpa_supplicant to deauthenticate from the
1905 * current AP.
1906 */
1907void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1908 int reason_code)
1909{
1910 u8 *addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001911 union wpa_event_data event;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001912 int zero_addr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001914 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
1915 " pending_bssid=" MACSTR " reason=%d state=%s",
1916 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1917 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
1918
1919 if (!is_zero_ether_addr(wpa_s->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 addr = wpa_s->bssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001921 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1922 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
1923 wpa_s->wpa_state == WPA_ASSOCIATING))
1924 addr = wpa_s->pending_bssid;
1925 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1926 /*
1927 * When using driver-based BSS selection, we may not know the
1928 * BSSID with which we are currently trying to associate. We
1929 * need to notify the driver of this disconnection even in such
1930 * a case, so use the all zeros address here.
1931 */
1932 addr = wpa_s->bssid;
1933 zero_addr = 1;
1934 }
1935
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001936#ifdef CONFIG_TDLS
1937 wpa_tdls_teardown_peers(wpa_s->wpa);
1938#endif /* CONFIG_TDLS */
1939
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001940 if (addr) {
1941 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001942 os_memset(&event, 0, sizeof(event));
1943 event.deauth_info.reason_code = (u16) reason_code;
1944 event.deauth_info.locally_generated = 1;
1945 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001946 if (zero_addr)
1947 addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001948 }
1949
1950 wpa_supplicant_clear_connection(wpa_s, addr);
1951}
1952
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001953static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
1954 struct wpa_ssid *ssid)
1955{
1956 if (!ssid || !ssid->disabled || ssid->disabled == 2)
1957 return;
1958
1959 ssid->disabled = 0;
1960 wpas_clear_temp_disabled(wpa_s, ssid, 1);
1961 wpas_notify_network_enabled_changed(wpa_s, ssid);
1962
1963 /*
1964 * Try to reassociate since there is no current configuration and a new
1965 * network was made available.
1966 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001967 if (!wpa_s->current_ssid && !wpa_s->disconnected)
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001968 wpa_s->reassociate = 1;
1969}
1970
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971
1972/**
1973 * wpa_supplicant_enable_network - Mark a configured network as enabled
1974 * @wpa_s: wpa_supplicant structure for a network interface
1975 * @ssid: wpa_ssid structure for a configured network or %NULL
1976 *
1977 * Enables the specified network or all networks if no network specified.
1978 */
1979void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1980 struct wpa_ssid *ssid)
1981{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001983 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
1984 wpa_supplicant_enable_one_network(wpa_s, ssid);
1985 } else
1986 wpa_supplicant_enable_one_network(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001988 if (wpa_s->reassociate && !wpa_s->disconnected) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001989 if (wpa_s->sched_scanning) {
1990 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
1991 "new network to scan filters");
1992 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001993 }
1994
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07001995 if (wpa_supplicant_fast_associate(wpa_s) != 1)
1996 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 }
1998}
1999
2000
2001/**
2002 * wpa_supplicant_disable_network - Mark a configured network as disabled
2003 * @wpa_s: wpa_supplicant structure for a network interface
2004 * @ssid: wpa_ssid structure for a configured network or %NULL
2005 *
2006 * Disables the specified network or all networks if no network specified.
2007 */
2008void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2009 struct wpa_ssid *ssid)
2010{
2011 struct wpa_ssid *other_ssid;
2012 int was_disabled;
2013
2014 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002015 if (wpa_s->sched_scanning)
2016 wpa_supplicant_cancel_sched_scan(wpa_s);
2017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2019 other_ssid = other_ssid->next) {
2020 was_disabled = other_ssid->disabled;
2021 if (was_disabled == 2)
2022 continue; /* do not change persistent P2P group
2023 * data */
2024
2025 other_ssid->disabled = 1;
2026
2027 if (was_disabled != other_ssid->disabled)
2028 wpas_notify_network_enabled_changed(
2029 wpa_s, other_ssid);
2030 }
2031 if (wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002032 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2034 } else if (ssid->disabled != 2) {
2035 if (ssid == wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002036 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002037 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2038
2039 was_disabled = ssid->disabled;
2040
2041 ssid->disabled = 1;
2042
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002043 if (was_disabled != ssid->disabled) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002045 if (wpa_s->sched_scanning) {
2046 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2047 "to remove network from filters");
2048 wpa_supplicant_cancel_sched_scan(wpa_s);
2049 wpa_supplicant_req_scan(wpa_s, 0, 0);
2050 }
2051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 }
2053}
2054
2055
2056/**
2057 * wpa_supplicant_select_network - Attempt association with a network
2058 * @wpa_s: wpa_supplicant structure for a network interface
2059 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2060 */
2061void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2062 struct wpa_ssid *ssid)
2063{
2064
2065 struct wpa_ssid *other_ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002066 int disconnected = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002068 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002069 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002070 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002071 disconnected = 1;
2072 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002074 if (ssid)
2075 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 /*
2078 * Mark all other networks disabled or mark all networks enabled if no
2079 * network specified.
2080 */
2081 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2082 other_ssid = other_ssid->next) {
2083 int was_disabled = other_ssid->disabled;
2084 if (was_disabled == 2)
2085 continue; /* do not change persistent P2P group data */
2086
2087 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002088 if (was_disabled && !other_ssid->disabled)
2089 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
2091 if (was_disabled != other_ssid->disabled)
2092 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
2093 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002094
2095 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2096 /* We are already associated with the selected network */
2097 wpa_printf(MSG_DEBUG, "Already associated with the "
2098 "selected network - do nothing");
2099 return;
2100 }
2101
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002102 if (ssid) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002103 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002104 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2105 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07002106 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 wpa_s->disconnected = 0;
2108 wpa_s->reassociate = 1;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002109
2110 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2111 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112
2113 if (ssid)
2114 wpas_notify_network_selected(wpa_s, ssid);
2115}
2116
2117
2118/**
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002119 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2120 * @wpa_s: wpa_supplicant structure for a network interface
2121 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2122 * @pkcs11_module_path: PKCS #11 module path or NULL
2123 * Returns: 0 on success; -1 on failure
2124 *
2125 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2126 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2127 * module path fails the paths will be reset to the default value (NULL).
2128 */
2129int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2130 const char *pkcs11_engine_path,
2131 const char *pkcs11_module_path)
2132{
2133 char *pkcs11_engine_path_copy = NULL;
2134 char *pkcs11_module_path_copy = NULL;
2135
2136 if (pkcs11_engine_path != NULL) {
2137 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2138 if (pkcs11_engine_path_copy == NULL)
2139 return -1;
2140 }
2141 if (pkcs11_module_path != NULL) {
2142 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002143 if (pkcs11_module_path_copy == NULL) {
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002144 os_free(pkcs11_engine_path_copy);
2145 return -1;
2146 }
2147 }
2148
2149 os_free(wpa_s->conf->pkcs11_engine_path);
2150 os_free(wpa_s->conf->pkcs11_module_path);
2151 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2152 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2153
2154 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2155 eapol_sm_deinit(wpa_s->eapol);
2156 wpa_s->eapol = NULL;
2157 if (wpa_supplicant_init_eapol(wpa_s)) {
2158 /* Error -> Reset paths to the default value (NULL) once. */
2159 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2160 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2161 NULL);
2162
2163 return -1;
2164 }
2165 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2166
2167 return 0;
2168}
2169
2170
2171/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2173 * @wpa_s: wpa_supplicant structure for a network interface
2174 * @ap_scan: AP scan mode
2175 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2176 *
2177 */
2178int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2179{
2180
2181 int old_ap_scan;
2182
2183 if (ap_scan < 0 || ap_scan > 2)
2184 return -1;
2185
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002186#ifdef ANDROID
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002187 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2188 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2189 wpa_s->wpa_state < WPA_COMPLETED) {
2190 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2191 "associating", wpa_s->conf->ap_scan, ap_scan);
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002192 return 0;
2193 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002194#endif /* ANDROID */
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 old_ap_scan = wpa_s->conf->ap_scan;
2197 wpa_s->conf->ap_scan = ap_scan;
2198
2199 if (old_ap_scan != wpa_s->conf->ap_scan)
2200 wpas_notify_ap_scan_changed(wpa_s);
2201
2202 return 0;
2203}
2204
2205
2206/**
2207 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2208 * @wpa_s: wpa_supplicant structure for a network interface
2209 * @expire_age: Expiration age in seconds
2210 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2211 *
2212 */
2213int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2214 unsigned int bss_expire_age)
2215{
2216 if (bss_expire_age < 10) {
2217 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2218 bss_expire_age);
2219 return -1;
2220 }
2221 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2222 bss_expire_age);
2223 wpa_s->conf->bss_expiration_age = bss_expire_age;
2224
2225 return 0;
2226}
2227
2228
2229/**
2230 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2231 * @wpa_s: wpa_supplicant structure for a network interface
2232 * @expire_count: number of scans after which an unseen BSS is reclaimed
2233 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2234 *
2235 */
2236int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2237 unsigned int bss_expire_count)
2238{
2239 if (bss_expire_count < 1) {
2240 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2241 bss_expire_count);
2242 return -1;
2243 }
2244 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2245 bss_expire_count);
2246 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2247
2248 return 0;
2249}
2250
2251
2252/**
Dmitry Shmidt04949592012-07-19 12:16:46 -07002253 * wpa_supplicant_set_scan_interval - Set scan interval
2254 * @wpa_s: wpa_supplicant structure for a network interface
2255 * @scan_interval: scan interval in seconds
2256 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2257 *
2258 */
2259int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2260 int scan_interval)
2261{
2262 if (scan_interval < 0) {
2263 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2264 scan_interval);
2265 return -1;
2266 }
2267 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2268 scan_interval);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002269 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002270
2271 return 0;
2272}
2273
2274
2275/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 * wpa_supplicant_set_debug_params - Set global debug params
2277 * @global: wpa_global structure
2278 * @debug_level: debug level
2279 * @debug_timestamp: determines if show timestamp in debug data
2280 * @debug_show_keys: determines if show keys in debug data
2281 * Returns: 0 if succeed or -1 if debug_level has wrong value
2282 */
2283int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2284 int debug_timestamp, int debug_show_keys)
2285{
2286
2287 int old_level, old_timestamp, old_show_keys;
2288
2289 /* check for allowed debuglevels */
2290 if (debug_level != MSG_EXCESSIVE &&
2291 debug_level != MSG_MSGDUMP &&
2292 debug_level != MSG_DEBUG &&
2293 debug_level != MSG_INFO &&
2294 debug_level != MSG_WARNING &&
2295 debug_level != MSG_ERROR)
2296 return -1;
2297
2298 old_level = wpa_debug_level;
2299 old_timestamp = wpa_debug_timestamp;
2300 old_show_keys = wpa_debug_show_keys;
2301
2302 wpa_debug_level = debug_level;
2303 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2304 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2305
2306 if (wpa_debug_level != old_level)
2307 wpas_notify_debug_level_changed(global);
2308 if (wpa_debug_timestamp != old_timestamp)
2309 wpas_notify_debug_timestamp_changed(global);
2310 if (wpa_debug_show_keys != old_show_keys)
2311 wpas_notify_debug_show_keys_changed(global);
2312
2313 return 0;
2314}
2315
2316
2317/**
2318 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2319 * @wpa_s: Pointer to wpa_supplicant data
2320 * Returns: A pointer to the current network structure or %NULL on failure
2321 */
2322struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2323{
2324 struct wpa_ssid *entry;
2325 u8 ssid[MAX_SSID_LEN];
2326 int res;
2327 size_t ssid_len;
2328 u8 bssid[ETH_ALEN];
2329 int wired;
2330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002331 res = wpa_drv_get_ssid(wpa_s, ssid);
2332 if (res < 0) {
2333 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2334 "driver");
2335 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002336 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002337 ssid_len = res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002339 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2341 "driver");
2342 return NULL;
2343 }
2344
2345 wired = wpa_s->conf->ap_scan == 0 &&
2346 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
2347
2348 entry = wpa_s->conf->ssid;
2349 while (entry) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002350 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002351 ((ssid_len == entry->ssid_len &&
2352 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2353 (!entry->bssid_set ||
2354 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2355 return entry;
2356#ifdef CONFIG_WPS
Dmitry Shmidt04949592012-07-19 12:16:46 -07002357 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2359 (entry->ssid == NULL || entry->ssid_len == 0) &&
2360 (!entry->bssid_set ||
2361 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2362 return entry;
2363#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002364
Dmitry Shmidt04949592012-07-19 12:16:46 -07002365 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002366 entry->ssid_len == 0 &&
2367 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2368 return entry;
2369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 entry = entry->next;
2371 }
2372
2373 return NULL;
2374}
2375
2376
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002377static int select_driver(struct wpa_supplicant *wpa_s, int i)
2378{
2379 struct wpa_global *global = wpa_s->global;
2380
2381 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2382 global->drv_priv[i] = wpa_drivers[i]->global_init();
2383 if (global->drv_priv[i] == NULL) {
2384 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2385 "'%s'", wpa_drivers[i]->name);
2386 return -1;
2387 }
2388 }
2389
2390 wpa_s->driver = wpa_drivers[i];
2391 wpa_s->global_drv_priv = global->drv_priv[i];
2392
2393 return 0;
2394}
2395
2396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2398 const char *name)
2399{
2400 int i;
2401 size_t len;
2402 const char *pos, *driver = name;
2403
2404 if (wpa_s == NULL)
2405 return -1;
2406
2407 if (wpa_drivers[0] == NULL) {
2408 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2409 "wpa_supplicant");
2410 return -1;
2411 }
2412
2413 if (name == NULL) {
2414 /* default to first driver in the list */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002415 return select_driver(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 }
2417
2418 do {
2419 pos = os_strchr(driver, ',');
2420 if (pos)
2421 len = pos - driver;
2422 else
2423 len = os_strlen(driver);
2424
2425 for (i = 0; wpa_drivers[i]; i++) {
2426 if (os_strlen(wpa_drivers[i]->name) == len &&
2427 os_strncmp(driver, wpa_drivers[i]->name, len) ==
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002428 0) {
2429 /* First driver that succeeds wins */
2430 if (select_driver(wpa_s, i) == 0)
2431 return 0;
2432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 }
2434
2435 driver = pos + 1;
2436 } while (pos);
2437
2438 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
2439 return -1;
2440}
2441
2442
2443/**
2444 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2445 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2446 * with struct wpa_driver_ops::init()
2447 * @src_addr: Source address of the EAPOL frame
2448 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2449 * @len: Length of the EAPOL data
2450 *
2451 * This function is called for each received EAPOL frame. Most driver
2452 * interfaces rely on more generic OS mechanism for receiving frames through
2453 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2454 * take care of received EAPOL frames and deliver them to the core supplicant
2455 * code by calling this function.
2456 */
2457void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2458 const u8 *buf, size_t len)
2459{
2460 struct wpa_supplicant *wpa_s = ctx;
2461
2462 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
2463 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2464
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002465#ifdef CONFIG_PEERKEY
2466 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2467 wpa_s->current_ssid->peerkey &&
2468 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2469 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2470 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2471 return;
2472 }
2473#endif /* CONFIG_PEERKEY */
2474
Jouni Malinena05074c2012-12-21 21:35:35 +02002475 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2476 (wpa_s->last_eapol_matches_bssid &&
2477#ifdef CONFIG_AP
2478 !wpa_s->ap_iface &&
2479#endif /* CONFIG_AP */
2480 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481 /*
2482 * There is possible race condition between receiving the
2483 * association event and the EAPOL frame since they are coming
2484 * through different paths from the driver. In order to avoid
2485 * issues in trying to process the EAPOL frame before receiving
2486 * association information, lets queue it for processing until
Jouni Malinena05074c2012-12-21 21:35:35 +02002487 * the association event is received. This may also be needed in
2488 * driver-based roaming case, so also use src_addr != BSSID as a
2489 * trigger if we have previously confirmed that the
2490 * Authenticator uses BSSID as the src_addr (which is not the
2491 * case with wired IEEE 802.1X).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492 */
2493 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
Jouni Malinena05074c2012-12-21 21:35:35 +02002494 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2495 wpa_supplicant_state_txt(wpa_s->wpa_state),
2496 MAC2STR(wpa_s->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 wpabuf_free(wpa_s->pending_eapol_rx);
2498 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2499 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002500 os_get_reltime(&wpa_s->pending_eapol_rx_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2502 ETH_ALEN);
2503 }
2504 return;
2505 }
2506
Jouni Malinena05074c2012-12-21 21:35:35 +02002507 wpa_s->last_eapol_matches_bssid =
2508 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510#ifdef CONFIG_AP
2511 if (wpa_s->ap_iface) {
2512 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2513 return;
2514 }
2515#endif /* CONFIG_AP */
2516
2517 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2518 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2519 "no key management is configured");
2520 return;
2521 }
2522
2523 if (wpa_s->eapol_received == 0 &&
2524 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2525 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2526 wpa_s->wpa_state != WPA_COMPLETED) &&
2527 (wpa_s->current_ssid == NULL ||
2528 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2529 /* Timeout for completing IEEE 802.1X and WPA authentication */
2530 wpa_supplicant_req_auth_timeout(
2531 wpa_s,
2532 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2533 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2534 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2535 70 : 10, 0);
2536 }
2537 wpa_s->eapol_received++;
2538
2539 if (wpa_s->countermeasures) {
2540 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2541 "EAPOL packet");
2542 return;
2543 }
2544
2545#ifdef CONFIG_IBSS_RSN
2546 if (wpa_s->current_ssid &&
2547 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2548 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2549 return;
2550 }
2551#endif /* CONFIG_IBSS_RSN */
2552
2553 /* Source address of the incoming EAPOL frame could be compared to the
2554 * current BSSID. However, it is possible that a centralized
2555 * Authenticator could be using another MAC address than the BSSID of
2556 * an AP, so just allow any address to be used for now. The replies are
2557 * still sent to the current BSSID (if available), though. */
2558
2559 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2560 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2561 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2562 return;
2563 wpa_drv_poll(wpa_s);
2564 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2565 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2566 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2567 /*
2568 * Set portValid = TRUE here since we are going to skip 4-way
2569 * handshake processing which would normally set portValid. We
2570 * need this to allow the EAPOL state machines to be completed
2571 * without going through EAPOL-Key handshake.
2572 */
2573 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2574 }
2575}
2576
2577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 if (wpa_s->driver->send_eapol) {
2581 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2582 if (addr)
2583 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002584 } else if ((!wpa_s->p2p_mgmt ||
2585 !(wpa_s->drv_flags &
2586 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
2587 !(wpa_s->drv_flags &
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589 l2_packet_deinit(wpa_s->l2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2591 wpa_drv_get_mac_addr(wpa_s),
2592 ETH_P_EAPOL,
2593 wpa_supplicant_rx_eapol, wpa_s, 0);
2594 if (wpa_s->l2 == NULL)
2595 return -1;
2596 } else {
2597 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2598 if (addr)
2599 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2600 }
2601
2602 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2603 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2604 return -1;
2605 }
2606
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002607 return 0;
2608}
2609
2610
Dmitry Shmidt04949592012-07-19 12:16:46 -07002611static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2612 const u8 *buf, size_t len)
2613{
2614 struct wpa_supplicant *wpa_s = ctx;
2615 const struct l2_ethhdr *eth;
2616
2617 if (len < sizeof(*eth))
2618 return;
2619 eth = (const struct l2_ethhdr *) buf;
2620
2621 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2622 !(eth->h_dest[0] & 0x01)) {
2623 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2624 " (bridge - not for this interface - ignore)",
2625 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2626 return;
2627 }
2628
2629 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2630 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2631 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2632 len - sizeof(*eth));
2633}
2634
2635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002636/**
2637 * wpa_supplicant_driver_init - Initialize driver interface parameters
2638 * @wpa_s: Pointer to wpa_supplicant data
2639 * Returns: 0 on success, -1 on failure
2640 *
2641 * This function is called to initialize driver interface parameters.
2642 * wpa_drv_init() must have been called before this function to initialize the
2643 * driver interface.
2644 */
2645int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2646{
2647 static int interface_count = 0;
2648
2649 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2650 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002652 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2653 MAC2STR(wpa_s->own_addr));
2654 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2655
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002656 if (wpa_s->bridge_ifname[0]) {
2657 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2658 "interface '%s'", wpa_s->bridge_ifname);
2659 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2660 wpa_s->own_addr,
2661 ETH_P_EAPOL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002662 wpa_supplicant_rx_eapol_bridge,
2663 wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 if (wpa_s->l2_br == NULL) {
2665 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2666 "connection for the bridge interface '%s'",
2667 wpa_s->bridge_ifname);
2668 return -1;
2669 }
2670 }
2671
2672 wpa_clear_keys(wpa_s, NULL);
2673
2674 /* Make sure that TKIP countermeasures are not left enabled (could
2675 * happen if wpa_supplicant is killed during countermeasures. */
2676 wpa_drv_set_countermeasures(wpa_s, 0);
2677
2678 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2679 wpa_drv_flush_pmkid(wpa_s);
2680
2681 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002682 wpa_s->prev_scan_wildcard = 0;
2683
Dmitry Shmidt04949592012-07-19 12:16:46 -07002684 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08002685 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2686 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2687 interface_count = 0;
2688 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002689 if (!wpa_s->p2p_mgmt &&
2690 wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002691 100000))
2692 wpa_supplicant_req_scan(wpa_s, interface_count,
2693 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 interface_count++;
2695 } else
2696 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2697
2698 return 0;
2699}
2700
2701
2702static int wpa_supplicant_daemon(const char *pid_file)
2703{
2704 wpa_printf(MSG_DEBUG, "Daemonize..");
2705 return os_daemonize(pid_file);
2706}
2707
2708
2709static struct wpa_supplicant * wpa_supplicant_alloc(void)
2710{
2711 struct wpa_supplicant *wpa_s;
2712
2713 wpa_s = os_zalloc(sizeof(*wpa_s));
2714 if (wpa_s == NULL)
2715 return NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002716 wpa_s->scan_req = INITIAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 wpa_s->scan_interval = 5;
2718 wpa_s->new_connection = 1;
2719 wpa_s->parent = wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002720 wpa_s->sched_scanning = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721
2722 return wpa_s;
2723}
2724
2725
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002726#ifdef CONFIG_HT_OVERRIDES
2727
2728static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2729 struct ieee80211_ht_capabilities *htcaps,
2730 struct ieee80211_ht_capabilities *htcaps_mask,
2731 const char *ht_mcs)
2732{
2733 /* parse ht_mcs into hex array */
2734 int i;
2735 const char *tmp = ht_mcs;
2736 char *end = NULL;
2737
2738 /* If ht_mcs is null, do not set anything */
2739 if (!ht_mcs)
2740 return 0;
2741
2742 /* This is what we are setting in the kernel */
2743 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2744
2745 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2746
2747 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2748 errno = 0;
2749 long v = strtol(tmp, &end, 16);
2750 if (errno == 0) {
2751 wpa_msg(wpa_s, MSG_DEBUG,
2752 "htcap value[%i]: %ld end: %p tmp: %p",
2753 i, v, end, tmp);
2754 if (end == tmp)
2755 break;
2756
2757 htcaps->supported_mcs_set[i] = v;
2758 tmp = end;
2759 } else {
2760 wpa_msg(wpa_s, MSG_ERROR,
2761 "Failed to parse ht-mcs: %s, error: %s\n",
2762 ht_mcs, strerror(errno));
2763 return -1;
2764 }
2765 }
2766
2767 /*
2768 * If we were able to parse any values, then set mask for the MCS set.
2769 */
2770 if (i) {
2771 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2772 IEEE80211_HT_MCS_MASK_LEN - 1);
2773 /* skip the 3 reserved bits */
2774 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2775 0x1f;
2776 }
2777
2778 return 0;
2779}
2780
2781
2782static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2783 struct ieee80211_ht_capabilities *htcaps,
2784 struct ieee80211_ht_capabilities *htcaps_mask,
2785 int disabled)
2786{
2787 u16 msk;
2788
2789 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2790
2791 if (disabled == -1)
2792 return 0;
2793
2794 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2795 htcaps_mask->ht_capabilities_info |= msk;
2796 if (disabled)
2797 htcaps->ht_capabilities_info &= msk;
2798 else
2799 htcaps->ht_capabilities_info |= msk;
2800
2801 return 0;
2802}
2803
2804
2805static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2806 struct ieee80211_ht_capabilities *htcaps,
2807 struct ieee80211_ht_capabilities *htcaps_mask,
2808 int factor)
2809{
2810 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2811
2812 if (factor == -1)
2813 return 0;
2814
2815 if (factor < 0 || factor > 3) {
2816 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2817 "Must be 0-3 or -1", factor);
2818 return -EINVAL;
2819 }
2820
2821 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2822 htcaps->a_mpdu_params &= ~0x3;
2823 htcaps->a_mpdu_params |= factor & 0x3;
2824
2825 return 0;
2826}
2827
2828
2829static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2830 struct ieee80211_ht_capabilities *htcaps,
2831 struct ieee80211_ht_capabilities *htcaps_mask,
2832 int density)
2833{
2834 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2835
2836 if (density == -1)
2837 return 0;
2838
2839 if (density < 0 || density > 7) {
2840 wpa_msg(wpa_s, MSG_ERROR,
2841 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2842 density);
2843 return -EINVAL;
2844 }
2845
2846 htcaps_mask->a_mpdu_params |= 0x1C;
2847 htcaps->a_mpdu_params &= ~(0x1C);
2848 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2849
2850 return 0;
2851}
2852
2853
2854static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2855 struct ieee80211_ht_capabilities *htcaps,
2856 struct ieee80211_ht_capabilities *htcaps_mask,
2857 int disabled)
2858{
2859 /* Masking these out disables HT40 */
2860 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2861 HT_CAP_INFO_SHORT_GI40MHZ);
2862
2863 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2864
2865 if (disabled)
2866 htcaps->ht_capabilities_info &= ~msk;
2867 else
2868 htcaps->ht_capabilities_info |= msk;
2869
2870 htcaps_mask->ht_capabilities_info |= msk;
2871
2872 return 0;
2873}
2874
2875
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002876static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
2877 struct ieee80211_ht_capabilities *htcaps,
2878 struct ieee80211_ht_capabilities *htcaps_mask,
2879 int disabled)
2880{
2881 /* Masking these out disables SGI */
2882 u16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
2883 HT_CAP_INFO_SHORT_GI40MHZ);
2884
2885 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
2886
2887 if (disabled)
2888 htcaps->ht_capabilities_info &= ~msk;
2889 else
2890 htcaps->ht_capabilities_info |= msk;
2891
2892 htcaps_mask->ht_capabilities_info |= msk;
2893
2894 return 0;
2895}
2896
2897
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002898void wpa_supplicant_apply_ht_overrides(
2899 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2900 struct wpa_driver_associate_params *params)
2901{
2902 struct ieee80211_ht_capabilities *htcaps;
2903 struct ieee80211_ht_capabilities *htcaps_mask;
2904
2905 if (!ssid)
2906 return;
2907
2908 params->disable_ht = ssid->disable_ht;
2909 if (!params->htcaps || !params->htcaps_mask)
2910 return;
2911
2912 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2913 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2914 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2915 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2916 ssid->disable_max_amsdu);
2917 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2918 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2919 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002920 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002921}
2922
2923#endif /* CONFIG_HT_OVERRIDES */
2924
2925
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002926#ifdef CONFIG_VHT_OVERRIDES
2927void wpa_supplicant_apply_vht_overrides(
2928 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2929 struct wpa_driver_associate_params *params)
2930{
2931 struct ieee80211_vht_capabilities *vhtcaps;
2932 struct ieee80211_vht_capabilities *vhtcaps_mask;
2933
2934 if (!ssid)
2935 return;
2936
2937 params->disable_vht = ssid->disable_vht;
2938
2939 vhtcaps = (void *) params->vhtcaps;
2940 vhtcaps_mask = (void *) params->vhtcaps_mask;
2941
2942 if (!vhtcaps || !vhtcaps_mask)
2943 return;
2944
2945 vhtcaps->vht_capabilities_info = ssid->vht_capa;
2946 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
2947
2948#define OVERRIDE_MCS(i) \
2949 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
2950 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
2951 3 << 2 * (i - 1); \
2952 vhtcaps->vht_supported_mcs_set.tx_map |= \
2953 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
2954 } \
2955 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
2956 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
2957 3 << 2 * (i - 1); \
2958 vhtcaps->vht_supported_mcs_set.rx_map |= \
2959 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
2960 }
2961
2962 OVERRIDE_MCS(1);
2963 OVERRIDE_MCS(2);
2964 OVERRIDE_MCS(3);
2965 OVERRIDE_MCS(4);
2966 OVERRIDE_MCS(5);
2967 OVERRIDE_MCS(6);
2968 OVERRIDE_MCS(7);
2969 OVERRIDE_MCS(8);
2970}
2971#endif /* CONFIG_VHT_OVERRIDES */
2972
2973
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
2975{
2976#ifdef PCSC_FUNCS
2977 size_t len;
2978
2979 if (!wpa_s->conf->pcsc_reader)
2980 return 0;
2981
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08002982 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002983 if (!wpa_s->scard)
2984 return 1;
2985
2986 if (wpa_s->conf->pcsc_pin &&
2987 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
2988 scard_deinit(wpa_s->scard);
2989 wpa_s->scard = NULL;
2990 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
2991 return -1;
2992 }
2993
2994 len = sizeof(wpa_s->imsi) - 1;
2995 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
2996 scard_deinit(wpa_s->scard);
2997 wpa_s->scard = NULL;
2998 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
2999 return -1;
3000 }
3001 wpa_s->imsi[len] = '\0';
3002
3003 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3004
3005 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3006 wpa_s->imsi, wpa_s->mnc_len);
3007
3008 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3009 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3010#endif /* PCSC_FUNCS */
3011
3012 return 0;
3013}
3014
3015
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003016int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3017{
3018 char *val, *pos;
3019
3020 ext_password_deinit(wpa_s->ext_pw);
3021 wpa_s->ext_pw = NULL;
3022 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3023
3024 if (!wpa_s->conf->ext_password_backend)
3025 return 0;
3026
3027 val = os_strdup(wpa_s->conf->ext_password_backend);
3028 if (val == NULL)
3029 return -1;
3030 pos = os_strchr(val, ':');
3031 if (pos)
3032 *pos++ = '\0';
3033
3034 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3035
3036 wpa_s->ext_pw = ext_password_init(val, pos);
3037 os_free(val);
3038 if (wpa_s->ext_pw == NULL) {
3039 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3040 return -1;
3041 }
3042 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3043
3044 return 0;
3045}
3046
3047
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003048static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3049 const char *rn)
3050{
3051 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3052 struct wpa_radio *radio;
3053
3054 while (rn && iface) {
3055 radio = iface->radio;
3056 if (radio && os_strcmp(rn, radio->name) == 0) {
3057 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3058 wpa_s->ifname, rn);
3059 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3060 return radio;
3061 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08003062
3063 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003064 }
3065
3066 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3067 wpa_s->ifname, rn ? rn : "N/A");
3068 radio = os_zalloc(sizeof(*radio));
3069 if (radio == NULL)
3070 return NULL;
3071
3072 if (rn)
3073 os_strlcpy(radio->name, rn, sizeof(radio->name));
3074 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003075 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003076 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3077
3078 return radio;
3079}
3080
3081
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003082static void radio_work_free(struct wpa_radio_work *work)
3083{
3084 if (work->wpa_s->scan_work == work) {
3085 /* This should not really happen. */
3086 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3087 work->type, work, work->started);
3088 work->wpa_s->scan_work = NULL;
3089 }
3090
3091#ifdef CONFIG_P2P
3092 if (work->wpa_s->p2p_scan_work == work) {
3093 /* This should not really happen. */
3094 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3095 work->type, work, work->started);
3096 work->wpa_s->p2p_scan_work = NULL;
3097 }
3098#endif /* CONFIG_P2P */
3099
3100 dl_list_del(&work->list);
3101 os_free(work);
3102}
3103
3104
3105static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3106{
3107 struct wpa_radio *radio = eloop_ctx;
3108 struct wpa_radio_work *work;
3109 struct os_reltime now, diff;
3110 struct wpa_supplicant *wpa_s;
3111
3112 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3113 if (work == NULL)
3114 return;
3115
3116 if (work->started)
3117 return; /* already started and still in progress */
3118
3119 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3120 radio_list);
3121 if (wpa_s && wpa_s->external_scan_running) {
3122 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3123 return;
3124 }
3125
3126 os_get_reltime(&now);
3127 os_reltime_sub(&now, &work->time, &diff);
3128 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3129 work->type, work, diff.sec, diff.usec);
3130 work->started = 1;
3131 work->time = now;
3132 work->cb(work, 0);
3133}
3134
3135
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003136/*
3137 * This function removes both started and pending radio works running on
3138 * the provided interface's radio.
3139 * Prior to the removal of the radio work, its callback (cb) is called with
3140 * deinit set to be 1. Each work's callback is responsible for clearing its
3141 * internal data and restoring to a correct state.
3142 * @wpa_s: wpa_supplicant data
3143 * @type: type of works to be removed
3144 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3145 * this interface's works.
3146 */
3147void radio_remove_works(struct wpa_supplicant *wpa_s,
3148 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003149{
3150 struct wpa_radio_work *work, *tmp;
3151 struct wpa_radio *radio = wpa_s->radio;
3152
3153 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3154 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003155 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003156 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003157
3158 /* skip other ifaces' works */
3159 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003160 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003161
3162 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3163 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003164 work->cb(work, 1);
3165 radio_work_free(work);
3166 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003167
3168 /* in case we removed the started work */
3169 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003170}
3171
3172
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003173static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3174{
3175 struct wpa_radio *radio = wpa_s->radio;
3176
3177 if (!radio)
3178 return;
3179
3180 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3181 wpa_s->ifname, radio->name);
3182 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003183 if (!dl_list_empty(&radio->ifaces)) {
3184 wpa_s->radio = NULL;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003185 return; /* Interfaces remain for this radio */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003186 }
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003187
3188 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003189 radio_remove_works(wpa_s, NULL, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003190 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3191 wpa_s->radio = NULL;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003192 os_free(radio);
3193}
3194
3195
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003196void radio_work_check_next(struct wpa_supplicant *wpa_s)
3197{
3198 struct wpa_radio *radio = wpa_s->radio;
3199
3200 if (dl_list_empty(&radio->work))
3201 return;
3202 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3203 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3204}
3205
3206
3207/**
3208 * radio_add_work - Add a radio work item
3209 * @wpa_s: Pointer to wpa_supplicant data
3210 * @freq: Frequency of the offchannel operation in MHz or 0
3211 * @type: Unique identifier for each type of work
3212 * @next: Force as the next work to be executed
3213 * @cb: Callback function for indicating when radio is available
3214 * @ctx: Context pointer for the work (work->ctx in cb())
3215 * Returns: 0 on success, -1 on failure
3216 *
3217 * This function is used to request time for an operation that requires
3218 * exclusive radio control. Once the radio is available, the registered callback
3219 * function will be called. radio_work_done() must be called once the exclusive
3220 * radio operation has been completed, so that the radio is freed for other
3221 * operations. The special case of deinit=1 is used to free the context data
3222 * during interface removal. That does not allow the callback function to start
3223 * the radio operation, i.e., it must free any resources allocated for the radio
3224 * work and return.
3225 *
3226 * The @freq parameter can be used to indicate a single channel on which the
3227 * offchannel operation will occur. This may allow multiple radio work
3228 * operations to be performed in parallel if they apply for the same channel.
3229 * Setting this to 0 indicates that the work item may use multiple channels or
3230 * requires exclusive control of the radio.
3231 */
3232int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3233 const char *type, int next,
3234 void (*cb)(struct wpa_radio_work *work, int deinit),
3235 void *ctx)
3236{
3237 struct wpa_radio_work *work;
3238 int was_empty;
3239
3240 work = os_zalloc(sizeof(*work));
3241 if (work == NULL)
3242 return -1;
3243 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3244 os_get_reltime(&work->time);
3245 work->freq = freq;
3246 work->type = type;
3247 work->wpa_s = wpa_s;
3248 work->cb = cb;
3249 work->ctx = ctx;
3250
3251 was_empty = dl_list_empty(&wpa_s->radio->work);
3252 if (next)
3253 dl_list_add(&wpa_s->radio->work, &work->list);
3254 else
3255 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3256 if (was_empty) {
3257 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3258 radio_work_check_next(wpa_s);
3259 }
3260
3261 return 0;
3262}
3263
3264
3265/**
3266 * radio_work_done - Indicate that a radio work item has been completed
3267 * @work: Completed work
3268 *
3269 * This function is called once the callback function registered with
3270 * radio_add_work() has completed its work.
3271 */
3272void radio_work_done(struct wpa_radio_work *work)
3273{
3274 struct wpa_supplicant *wpa_s = work->wpa_s;
3275 struct os_reltime now, diff;
3276 unsigned int started = work->started;
3277
3278 os_get_reltime(&now);
3279 os_reltime_sub(&now, &work->time, &diff);
3280 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3281 work->type, work, started ? "done" : "canceled",
3282 diff.sec, diff.usec);
3283 radio_work_free(work);
3284 if (started)
3285 radio_work_check_next(wpa_s);
3286}
3287
3288
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003289int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3290{
3291 struct wpa_radio_work *work;
3292 struct wpa_radio *radio = wpa_s->radio;
3293
3294 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3295 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3296 return 1;
3297 }
3298
3299 return 0;
3300}
3301
3302
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003303static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3304 struct wpa_interface *iface)
3305{
3306 const char *ifname, *driver, *rn;
3307
3308 driver = iface->driver;
3309next_driver:
3310 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3311 return -1;
3312
3313 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3314 if (wpa_s->drv_priv == NULL) {
3315 const char *pos;
3316 pos = driver ? os_strchr(driver, ',') : NULL;
3317 if (pos) {
3318 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3319 "driver interface - try next driver wrapper");
3320 driver = pos + 1;
3321 goto next_driver;
3322 }
3323 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3324 "interface");
3325 return -1;
3326 }
3327 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3328 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3329 "driver_param '%s'", wpa_s->conf->driver_param);
3330 return -1;
3331 }
3332
3333 ifname = wpa_drv_get_ifname(wpa_s);
3334 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3335 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3336 "interface name with '%s'", ifname);
3337 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3338 }
3339
3340 if (wpa_s->driver->get_radio_name)
3341 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
3342 else
3343 rn = NULL;
3344 if (rn && rn[0] == '\0')
3345 rn = NULL;
3346
3347 wpa_s->radio = radio_add_interface(wpa_s, rn);
3348 if (wpa_s->radio == NULL)
3349 return -1;
3350
3351 return 0;
3352}
3353
3354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3356 struct wpa_interface *iface)
3357{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 struct wpa_driver_capa capa;
3359
3360 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3361 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3362 iface->confname ? iface->confname : "N/A",
3363 iface->driver ? iface->driver : "default",
3364 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3365 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3366
3367 if (iface->confname) {
3368#ifdef CONFIG_BACKEND_FILE
3369 wpa_s->confname = os_rel2abs_path(iface->confname);
3370 if (wpa_s->confname == NULL) {
3371 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3372 "for configuration file '%s'.",
3373 iface->confname);
3374 return -1;
3375 }
3376 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3377 iface->confname, wpa_s->confname);
3378#else /* CONFIG_BACKEND_FILE */
3379 wpa_s->confname = os_strdup(iface->confname);
3380#endif /* CONFIG_BACKEND_FILE */
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003381 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382 if (wpa_s->conf == NULL) {
3383 wpa_printf(MSG_ERROR, "Failed to read or parse "
3384 "configuration '%s'.", wpa_s->confname);
3385 return -1;
3386 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003387 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3388 wpa_config_read(wpa_s->confanother, wpa_s->conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003389
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003390#ifdef CONFIG_P2P
3391 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3392 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3393#endif /* CONFIG_P2P */
3394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 /*
3396 * Override ctrl_interface and driver_param if set on command
3397 * line.
3398 */
3399 if (iface->ctrl_interface) {
3400 os_free(wpa_s->conf->ctrl_interface);
3401 wpa_s->conf->ctrl_interface =
3402 os_strdup(iface->ctrl_interface);
3403 }
3404
3405 if (iface->driver_param) {
3406 os_free(wpa_s->conf->driver_param);
3407 wpa_s->conf->driver_param =
3408 os_strdup(iface->driver_param);
3409 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003410
3411 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3412 os_free(wpa_s->conf->ctrl_interface);
3413 wpa_s->conf->ctrl_interface = NULL;
3414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003415 } else
3416 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3417 iface->driver_param);
3418
3419 if (wpa_s->conf == NULL) {
3420 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3421 return -1;
3422 }
3423
3424 if (iface->ifname == NULL) {
3425 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3426 return -1;
3427 }
3428 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3429 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3430 iface->ifname);
3431 return -1;
3432 }
3433 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3434
3435 if (iface->bridge_ifname) {
3436 if (os_strlen(iface->bridge_ifname) >=
3437 sizeof(wpa_s->bridge_ifname)) {
3438 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3439 "name '%s'.", iface->bridge_ifname);
3440 return -1;
3441 }
3442 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3443 sizeof(wpa_s->bridge_ifname));
3444 }
3445
3446 /* RSNA Supplicant Key Management - INITIALIZE */
3447 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3448 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3449
3450 /* Initialize driver interface and register driver event handler before
3451 * L2 receive handler so that association events are processed before
3452 * EAPOL-Key packets if both become available for the same select()
3453 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003454 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 return -1;
3456
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003457 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3458 return -1;
3459
3460 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3461 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3462 NULL);
3463 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3464
3465 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3466 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3467 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
3468 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3469 "dot11RSNAConfigPMKLifetime");
3470 return -1;
3471 }
3472
3473 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3474 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3475 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
3476 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3477 "dot11RSNAConfigPMKReauthThreshold");
3478 return -1;
3479 }
3480
3481 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3482 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3483 wpa_s->conf->dot11RSNAConfigSATimeout)) {
3484 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3485 "dot11RSNAConfigSATimeout");
3486 return -1;
3487 }
3488
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003489 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3490 &wpa_s->hw.num_modes,
3491 &wpa_s->hw.flags);
3492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003494 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 wpa_s->drv_flags = capa.flags;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003496 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003497 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003499 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3500 wpa_s->sched_scan_supported = capa.sched_scan_supported;
3501 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
3503 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003504 wpa_s->extended_capa = capa.extended_capa;
3505 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3506 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003507 wpa_s->num_multichan_concurrent =
3508 capa.num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003509 }
3510 if (wpa_s->max_remain_on_chan == 0)
3511 wpa_s->max_remain_on_chan = 1000;
3512
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003513 /*
3514 * Only take p2p_mgmt parameters when P2P Device is supported.
3515 * Doing it here as it determines whether l2_packet_init() will be done
3516 * during wpa_supplicant_driver_init().
3517 */
3518 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3519 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3520 else
3521 iface->p2p_mgmt = 1;
3522
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003523 if (wpa_s->num_multichan_concurrent == 0)
3524 wpa_s->num_multichan_concurrent = 1;
3525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 if (wpa_supplicant_driver_init(wpa_s) < 0)
3527 return -1;
3528
3529#ifdef CONFIG_TDLS
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003530 if ((!iface->p2p_mgmt ||
3531 !(wpa_s->drv_flags &
3532 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3533 wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 return -1;
3535#endif /* CONFIG_TDLS */
3536
3537 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3538 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
3539 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
3540 return -1;
3541 }
3542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543 if (wpas_wps_init(wpa_s))
3544 return -1;
3545
3546 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3547 return -1;
3548 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3549
3550 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3551 if (wpa_s->ctrl_iface == NULL) {
3552 wpa_printf(MSG_ERROR,
3553 "Failed to initialize control interface '%s'.\n"
3554 "You may have another wpa_supplicant process "
3555 "already running or the file was\n"
3556 "left by an unclean termination of wpa_supplicant "
3557 "in which case you will need\n"
3558 "to manually remove this file before starting "
3559 "wpa_supplicant again.\n",
3560 wpa_s->conf->ctrl_interface);
3561 return -1;
3562 }
3563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003564 wpa_s->gas = gas_query_init(wpa_s);
3565 if (wpa_s->gas == NULL) {
3566 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3567 return -1;
3568 }
3569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003570#ifdef CONFIG_P2P
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003571 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
3573 return -1;
3574 }
3575#endif /* CONFIG_P2P */
3576
3577 if (wpa_bss_init(wpa_s) < 0)
3578 return -1;
3579
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003580#ifdef CONFIG_EAP_PROXY
3581{
3582 size_t len;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003583 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3584 &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003585 if (wpa_s->mnc_len > 0) {
3586 wpa_s->imsi[len] = '\0';
3587 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3588 wpa_s->imsi, wpa_s->mnc_len);
3589 } else {
3590 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3591 }
3592}
3593#endif /* CONFIG_EAP_PROXY */
3594
Dmitry Shmidt04949592012-07-19 12:16:46 -07003595 if (pcsc_reader_init(wpa_s) < 0)
3596 return -1;
3597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598 if (wpas_init_ext_pw(wpa_s) < 0)
3599 return -1;
3600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 return 0;
3602}
3603
3604
3605static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003606 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607{
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003608 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 if (wpa_s->drv_priv) {
3610 wpa_supplicant_deauthenticate(wpa_s,
3611 WLAN_REASON_DEAUTH_LEAVING);
3612
3613 wpa_drv_set_countermeasures(wpa_s, 0);
3614 wpa_clear_keys(wpa_s, NULL);
3615 }
3616
3617 wpa_supplicant_cleanup(wpa_s);
3618
Dmitry Shmidt04949592012-07-19 12:16:46 -07003619#ifdef CONFIG_P2P
3620 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
3621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
3622 "the management interface is being removed");
3623 wpas_p2p_deinit_global(wpa_s->global);
3624 }
3625#endif /* CONFIG_P2P */
3626
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003627 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003628 radio_remove_interface(wpa_s);
3629
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003630 if (wpa_s->drv_priv)
3631 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003632
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003633 if (notify)
3634 wpas_notify_iface_removed(wpa_s);
3635
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003636 if (terminate)
3637 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003638
3639 if (wpa_s->ctrl_iface) {
3640 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3641 wpa_s->ctrl_iface = NULL;
3642 }
3643
3644 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003645 wpa_config_free(wpa_s->conf);
3646 wpa_s->conf = NULL;
3647 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003648
3649 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650}
3651
3652
3653/**
3654 * wpa_supplicant_add_iface - Add a new network interface
3655 * @global: Pointer to global data from wpa_supplicant_init()
3656 * @iface: Interface configuration options
3657 * Returns: Pointer to the created interface or %NULL on failure
3658 *
3659 * This function is used to add new network interfaces for %wpa_supplicant.
3660 * This can be called before wpa_supplicant_run() to add interfaces before the
3661 * main event loop has been started. In addition, new interfaces can be added
3662 * dynamically while %wpa_supplicant is already running. This could happen,
3663 * e.g., when a hotplug network adapter is inserted.
3664 */
3665struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3666 struct wpa_interface *iface)
3667{
3668 struct wpa_supplicant *wpa_s;
3669 struct wpa_interface t_iface;
3670 struct wpa_ssid *ssid;
3671
3672 if (global == NULL || iface == NULL)
3673 return NULL;
3674
3675 wpa_s = wpa_supplicant_alloc();
3676 if (wpa_s == NULL)
3677 return NULL;
3678
3679 wpa_s->global = global;
3680
3681 t_iface = *iface;
3682 if (global->params.override_driver) {
3683 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3684 "('%s' -> '%s')",
3685 iface->driver, global->params.override_driver);
3686 t_iface.driver = global->params.override_driver;
3687 }
3688 if (global->params.override_ctrl_interface) {
3689 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3690 "ctrl_interface ('%s' -> '%s')",
3691 iface->ctrl_interface,
3692 global->params.override_ctrl_interface);
3693 t_iface.ctrl_interface =
3694 global->params.override_ctrl_interface;
3695 }
3696 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
3697 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3698 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003699 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 return NULL;
3701 }
3702
3703 /* Notify the control interfaces about new iface */
3704 if (wpas_notify_iface_added(wpa_s)) {
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003705 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706 return NULL;
3707 }
3708
3709 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3710 wpas_notify_network_added(wpa_s, ssid);
3711
3712 wpa_s->next = global->ifaces;
3713 global->ifaces = wpa_s;
3714
3715 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003716 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003717
3718 return wpa_s;
3719}
3720
3721
3722/**
3723 * wpa_supplicant_remove_iface - Remove a network interface
3724 * @global: Pointer to global data from wpa_supplicant_init()
3725 * @wpa_s: Pointer to the network interface to be removed
3726 * Returns: 0 if interface was removed, -1 if interface was not found
3727 *
3728 * This function can be used to dynamically remove network interfaces from
3729 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3730 * addition, this function is used to remove all remaining interfaces when
3731 * %wpa_supplicant is terminated.
3732 */
3733int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003734 struct wpa_supplicant *wpa_s,
3735 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003736{
3737 struct wpa_supplicant *prev;
3738
3739 /* Remove interface from the global list of interfaces */
3740 prev = global->ifaces;
3741 if (prev == wpa_s) {
3742 global->ifaces = wpa_s->next;
3743 } else {
3744 while (prev && prev->next != wpa_s)
3745 prev = prev->next;
3746 if (prev == NULL)
3747 return -1;
3748 prev->next = wpa_s->next;
3749 }
3750
3751 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
3752
3753 if (global->p2p_group_formation == wpa_s)
3754 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003755 if (global->p2p_invite_group == wpa_s)
3756 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003757 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758
3759 return 0;
3760}
3761
3762
3763/**
3764 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3765 * @wpa_s: Pointer to the network interface
3766 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3767 */
3768const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3769{
3770 const char *eapol_method;
3771
3772 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3773 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3774 return "NO-EAP";
3775 }
3776
3777 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3778 if (eapol_method == NULL)
3779 return "UNKNOWN-EAP";
3780
3781 return eapol_method;
3782}
3783
3784
3785/**
3786 * wpa_supplicant_get_iface - Get a new network interface
3787 * @global: Pointer to global data from wpa_supplicant_init()
3788 * @ifname: Interface name
3789 * Returns: Pointer to the interface or %NULL if not found
3790 */
3791struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3792 const char *ifname)
3793{
3794 struct wpa_supplicant *wpa_s;
3795
3796 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3797 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3798 return wpa_s;
3799 }
3800 return NULL;
3801}
3802
3803
3804#ifndef CONFIG_NO_WPA_MSG
3805static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3806{
3807 struct wpa_supplicant *wpa_s = ctx;
3808 if (wpa_s == NULL)
3809 return NULL;
3810 return wpa_s->ifname;
3811}
3812#endif /* CONFIG_NO_WPA_MSG */
3813
3814
3815/**
3816 * wpa_supplicant_init - Initialize %wpa_supplicant
3817 * @params: Parameters for %wpa_supplicant
3818 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3819 *
3820 * This function is used to initialize %wpa_supplicant. After successful
3821 * initialization, the returned data pointer can be used to add and remove
3822 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3823 */
3824struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3825{
3826 struct wpa_global *global;
3827 int ret, i;
3828
3829 if (params == NULL)
3830 return NULL;
3831
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003832#ifdef CONFIG_DRIVER_NDIS
3833 {
3834 void driver_ndis_init_ops(void);
3835 driver_ndis_init_ops();
3836 }
3837#endif /* CONFIG_DRIVER_NDIS */
3838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003839#ifndef CONFIG_NO_WPA_MSG
3840 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
3841#endif /* CONFIG_NO_WPA_MSG */
3842
3843 wpa_debug_open_file(params->wpa_debug_file_path);
3844 if (params->wpa_debug_syslog)
3845 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07003846 if (params->wpa_debug_tracing) {
3847 ret = wpa_debug_open_linux_tracing();
3848 if (ret) {
3849 wpa_printf(MSG_ERROR,
3850 "Failed to enable trace logging");
3851 return NULL;
3852 }
3853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854
3855 ret = eap_register_methods();
3856 if (ret) {
3857 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
3858 if (ret == -2)
3859 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
3860 "the same EAP type.");
3861 return NULL;
3862 }
3863
3864 global = os_zalloc(sizeof(*global));
3865 if (global == NULL)
3866 return NULL;
3867 dl_list_init(&global->p2p_srv_bonjour);
3868 dl_list_init(&global->p2p_srv_upnp);
3869 global->params.daemonize = params->daemonize;
3870 global->params.wait_for_monitor = params->wait_for_monitor;
3871 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
3872 if (params->pid_file)
3873 global->params.pid_file = os_strdup(params->pid_file);
3874 if (params->ctrl_interface)
3875 global->params.ctrl_interface =
3876 os_strdup(params->ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003877 if (params->ctrl_interface_group)
3878 global->params.ctrl_interface_group =
3879 os_strdup(params->ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 if (params->override_driver)
3881 global->params.override_driver =
3882 os_strdup(params->override_driver);
3883 if (params->override_ctrl_interface)
3884 global->params.override_ctrl_interface =
3885 os_strdup(params->override_ctrl_interface);
3886 wpa_debug_level = global->params.wpa_debug_level =
3887 params->wpa_debug_level;
3888 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
3889 params->wpa_debug_show_keys;
3890 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
3891 params->wpa_debug_timestamp;
3892
3893 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
3894
3895 if (eloop_init()) {
3896 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
3897 wpa_supplicant_deinit(global);
3898 return NULL;
3899 }
3900
Jouni Malinen75ecf522011-06-27 15:19:46 -07003901 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902
3903 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
3904 if (global->ctrl_iface == NULL) {
3905 wpa_supplicant_deinit(global);
3906 return NULL;
3907 }
3908
3909 if (wpas_notify_supplicant_initialized(global)) {
3910 wpa_supplicant_deinit(global);
3911 return NULL;
3912 }
3913
3914 for (i = 0; wpa_drivers[i]; i++)
3915 global->drv_count++;
3916 if (global->drv_count == 0) {
3917 wpa_printf(MSG_ERROR, "No drivers enabled");
3918 wpa_supplicant_deinit(global);
3919 return NULL;
3920 }
3921 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
3922 if (global->drv_priv == NULL) {
3923 wpa_supplicant_deinit(global);
3924 return NULL;
3925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003927#ifdef CONFIG_WIFI_DISPLAY
3928 if (wifi_display_init(global) < 0) {
3929 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
3930 wpa_supplicant_deinit(global);
3931 return NULL;
3932 }
3933#endif /* CONFIG_WIFI_DISPLAY */
3934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935 return global;
3936}
3937
3938
3939/**
3940 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
3941 * @global: Pointer to global data from wpa_supplicant_init()
3942 * Returns: 0 after successful event loop run, -1 on failure
3943 *
3944 * This function starts the main event loop and continues running as long as
3945 * there are any remaining events. In most cases, this function is running as
3946 * long as the %wpa_supplicant process in still in use.
3947 */
3948int wpa_supplicant_run(struct wpa_global *global)
3949{
3950 struct wpa_supplicant *wpa_s;
3951
3952 if (global->params.daemonize &&
3953 wpa_supplicant_daemon(global->params.pid_file))
3954 return -1;
3955
3956 if (global->params.wait_for_monitor) {
3957 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
3958 if (wpa_s->ctrl_iface)
3959 wpa_supplicant_ctrl_iface_wait(
3960 wpa_s->ctrl_iface);
3961 }
3962
3963 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
3964 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
3965
3966 eloop_run();
3967
3968 return 0;
3969}
3970
3971
3972/**
3973 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
3974 * @global: Pointer to global data from wpa_supplicant_init()
3975 *
3976 * This function is called to deinitialize %wpa_supplicant and to free all
3977 * allocated resources. Remaining network interfaces will also be removed.
3978 */
3979void wpa_supplicant_deinit(struct wpa_global *global)
3980{
3981 int i;
3982
3983 if (global == NULL)
3984 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003985
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003986#ifdef CONFIG_WIFI_DISPLAY
3987 wifi_display_deinit(global);
3988#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989
3990 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003991 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992
3993 if (global->ctrl_iface)
3994 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
3995
3996 wpas_notify_supplicant_deinitialized(global);
3997
3998 eap_peer_unregister_methods();
3999#ifdef CONFIG_AP
4000 eap_server_unregister_methods();
4001#endif /* CONFIG_AP */
4002
4003 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
4004 if (!global->drv_priv[i])
4005 continue;
4006 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
4007 }
4008 os_free(global->drv_priv);
4009
4010 random_deinit();
4011
4012 eloop_destroy();
4013
4014 if (global->params.pid_file) {
4015 os_daemonize_terminate(global->params.pid_file);
4016 os_free(global->params.pid_file);
4017 }
4018 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004019 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020 os_free(global->params.override_driver);
4021 os_free(global->params.override_ctrl_interface);
4022
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004023 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004024 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004025 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 os_free(global);
4028 wpa_debug_close_syslog();
4029 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004030 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004031}
4032
4033
4034void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4035{
4036 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4037 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4038 char country[3];
4039 country[0] = wpa_s->conf->country[0];
4040 country[1] = wpa_s->conf->country[1];
4041 country[2] = '\0';
4042 if (wpa_drv_set_country(wpa_s, country) < 0) {
4043 wpa_printf(MSG_ERROR, "Failed to set country code "
4044 "'%s'", country);
4045 }
4046 }
4047
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004048 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4049 wpas_init_ext_pw(wpa_s);
4050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051#ifdef CONFIG_WPS
4052 wpas_wps_update_config(wpa_s);
4053#endif /* CONFIG_WPS */
4054
4055#ifdef CONFIG_P2P
4056 wpas_p2p_update_config(wpa_s);
4057#endif /* CONFIG_P2P */
4058
4059 wpa_s->conf->changed_parameters = 0;
4060}
4061
4062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004063static void add_freq(int *freqs, int *num_freqs, int freq)
4064{
4065 int i;
4066
4067 for (i = 0; i < *num_freqs; i++) {
4068 if (freqs[i] == freq)
4069 return;
4070 }
4071
4072 freqs[*num_freqs] = freq;
4073 (*num_freqs)++;
4074}
4075
4076
4077static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4078{
4079 struct wpa_bss *bss, *cbss;
4080 const int max_freqs = 10;
4081 int *freqs;
4082 int num_freqs = 0;
4083
4084 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4085 if (freqs == NULL)
4086 return NULL;
4087
4088 cbss = wpa_s->current_bss;
4089
4090 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4091 if (bss == cbss)
4092 continue;
4093 if (bss->ssid_len == cbss->ssid_len &&
4094 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4095 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4096 add_freq(freqs, &num_freqs, bss->freq);
4097 if (num_freqs == max_freqs)
4098 break;
4099 }
4100 }
4101
4102 if (num_freqs == 0) {
4103 os_free(freqs);
4104 freqs = NULL;
4105 }
4106
4107 return freqs;
4108}
4109
4110
4111void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4112{
4113 int timeout;
4114 int count;
4115 int *freqs = NULL;
4116
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004117 wpas_connect_work_done(wpa_s);
4118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004120 * Remove possible authentication timeout since the connection failed.
4121 */
4122 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4123
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004124 if (wpa_s->disconnected) {
4125 /*
4126 * There is no point in blacklisting the AP if this event is
4127 * generated based on local request to disconnect.
4128 */
4129 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4130 "indication since interface has been put into "
4131 "disconnected state");
4132 return;
4133 }
4134
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004135 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 * Add the failed BSSID into the blacklist and speed up next scan
4137 * attempt if there could be other APs that could accept association.
4138 * The current blacklist count indicates how many times we have tried
4139 * connecting to this AP and multiple attempts mean that other APs are
4140 * either not available or has already been tried, so that we can start
4141 * increasing the delay here to avoid constant scanning.
4142 */
4143 count = wpa_blacklist_add(wpa_s, bssid);
4144 if (count == 1 && wpa_s->current_bss) {
4145 /*
4146 * This BSS was not in the blacklist before. If there is
4147 * another BSS available for the same ESS, we should try that
4148 * next. Otherwise, we may as well try this one once more
4149 * before allowing other, likely worse, ESSes to be considered.
4150 */
4151 freqs = get_bss_freqs_in_ess(wpa_s);
4152 if (freqs) {
4153 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4154 "has been seen; try it next");
4155 wpa_blacklist_add(wpa_s, bssid);
4156 /*
4157 * On the next scan, go through only the known channels
4158 * used in this ESS based on previous scans to speed up
4159 * common load balancing use case.
4160 */
4161 os_free(wpa_s->next_scan_freqs);
4162 wpa_s->next_scan_freqs = freqs;
4163 }
4164 }
4165
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004166 /*
4167 * Add previous failure count in case the temporary blacklist was
4168 * cleared due to no other BSSes being available.
4169 */
4170 count += wpa_s->extra_blacklist_count;
4171
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004172 if (count > 3 && wpa_s->current_ssid) {
4173 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4174 "consider temporary network disabling");
4175 wpas_auth_failed(wpa_s);
4176 }
4177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 switch (count) {
4179 case 1:
4180 timeout = 100;
4181 break;
4182 case 2:
4183 timeout = 500;
4184 break;
4185 case 3:
4186 timeout = 1000;
4187 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004188 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004190 break;
4191 default:
4192 timeout = 10000;
4193 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 }
4195
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004196 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4197 "ms", count, timeout);
4198
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004199 /*
4200 * TODO: if more than one possible AP is available in scan results,
4201 * could try the other ones before requesting a new scan.
4202 */
4203 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4204 1000 * (timeout % 1000));
4205}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004206
4207
4208int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4209{
4210 return wpa_s->conf->ap_scan == 2 ||
4211 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4212}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004213
Dmitry Shmidt04949592012-07-19 12:16:46 -07004214
4215#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4216int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4217 struct wpa_ssid *ssid,
4218 const char *field,
4219 const char *value)
4220{
4221#ifdef IEEE8021X_EAPOL
4222 struct eap_peer_config *eap = &ssid->eap;
4223
4224 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4225 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4226 (const u8 *) value, os_strlen(value));
4227
4228 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4229 case WPA_CTRL_REQ_EAP_IDENTITY:
4230 os_free(eap->identity);
4231 eap->identity = (u8 *) os_strdup(value);
4232 eap->identity_len = os_strlen(value);
4233 eap->pending_req_identity = 0;
4234 if (ssid == wpa_s->current_ssid)
4235 wpa_s->reassociate = 1;
4236 break;
4237 case WPA_CTRL_REQ_EAP_PASSWORD:
4238 os_free(eap->password);
4239 eap->password = (u8 *) os_strdup(value);
4240 eap->password_len = os_strlen(value);
4241 eap->pending_req_password = 0;
4242 if (ssid == wpa_s->current_ssid)
4243 wpa_s->reassociate = 1;
4244 break;
4245 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4246 os_free(eap->new_password);
4247 eap->new_password = (u8 *) os_strdup(value);
4248 eap->new_password_len = os_strlen(value);
4249 eap->pending_req_new_password = 0;
4250 if (ssid == wpa_s->current_ssid)
4251 wpa_s->reassociate = 1;
4252 break;
4253 case WPA_CTRL_REQ_EAP_PIN:
4254 os_free(eap->pin);
4255 eap->pin = os_strdup(value);
4256 eap->pending_req_pin = 0;
4257 if (ssid == wpa_s->current_ssid)
4258 wpa_s->reassociate = 1;
4259 break;
4260 case WPA_CTRL_REQ_EAP_OTP:
4261 os_free(eap->otp);
4262 eap->otp = (u8 *) os_strdup(value);
4263 eap->otp_len = os_strlen(value);
4264 os_free(eap->pending_req_otp);
4265 eap->pending_req_otp = NULL;
4266 eap->pending_req_otp_len = 0;
4267 break;
4268 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4269 os_free(eap->private_key_passwd);
4270 eap->private_key_passwd = (u8 *) os_strdup(value);
4271 eap->pending_req_passphrase = 0;
4272 if (ssid == wpa_s->current_ssid)
4273 wpa_s->reassociate = 1;
4274 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004275 case WPA_CTRL_REQ_SIM:
4276 os_free(eap->external_sim_resp);
4277 eap->external_sim_resp = os_strdup(value);
4278 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004279 default:
4280 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4281 return -1;
4282 }
4283
4284 return 0;
4285#else /* IEEE8021X_EAPOL */
4286 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4287 return -1;
4288#endif /* IEEE8021X_EAPOL */
4289}
4290#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
4291
4292
4293int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4294{
4295 int i;
4296 unsigned int drv_enc;
4297
4298 if (ssid == NULL)
4299 return 1;
4300
4301 if (ssid->disabled)
4302 return 1;
4303
4304 if (wpa_s && wpa_s->drv_capa_known)
4305 drv_enc = wpa_s->drv_enc;
4306 else
4307 drv_enc = (unsigned int) -1;
4308
4309 for (i = 0; i < NUM_WEP_KEYS; i++) {
4310 size_t len = ssid->wep_key_len[i];
4311 if (len == 0)
4312 continue;
4313 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4314 continue;
4315 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4316 continue;
4317 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4318 continue;
4319 return 1; /* invalid WEP key */
4320 }
4321
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004322 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
4323 !ssid->ext_psk)
4324 return 1;
4325
Dmitry Shmidt04949592012-07-19 12:16:46 -07004326 return 0;
4327}
4328
4329
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004330int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004331{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004332 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004333 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004334 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004335 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004336 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004337}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004338
4339
4340void wpas_auth_failed(struct wpa_supplicant *wpa_s)
4341{
4342 struct wpa_ssid *ssid = wpa_s->current_ssid;
4343 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004344 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004345
4346 if (ssid == NULL) {
4347 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4348 "SSID block");
4349 return;
4350 }
4351
4352 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4353 return;
4354
4355 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07004356
4357#ifdef CONFIG_P2P
4358 if (ssid->p2p_group &&
4359 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4360 /*
4361 * Skip the wait time since there is a short timeout on the
4362 * connection to a P2P group.
4363 */
4364 return;
4365 }
4366#endif /* CONFIG_P2P */
4367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004368 if (ssid->auth_failures > 50)
4369 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004370 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004371 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004372 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004373 dur = 90;
4374 else if (ssid->auth_failures > 3)
4375 dur = 60;
4376 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004377 dur = 30;
4378 else if (ssid->auth_failures > 1)
4379 dur = 20;
4380 else
4381 dur = 10;
4382
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004383 if (ssid->auth_failures > 1 &&
4384 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4385 dur += os_random() % (ssid->auth_failures * 10);
4386
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004387 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004388 if (now.sec + dur <= ssid->disabled_until.sec)
4389 return;
4390
4391 ssid->disabled_until.sec = now.sec + dur;
4392
4393 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
4394 "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
4395 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
4396 ssid->auth_failures, dur);
4397}
4398
4399
4400void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4401 struct wpa_ssid *ssid, int clear_failures)
4402{
4403 if (ssid == NULL)
4404 return;
4405
4406 if (ssid->disabled_until.sec) {
4407 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4408 "id=%d ssid=\"%s\"",
4409 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4410 }
4411 ssid->disabled_until.sec = 0;
4412 ssid->disabled_until.usec = 0;
4413 if (clear_failures)
4414 ssid->auth_failures = 0;
4415}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004416
4417
4418int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4419{
4420 size_t i;
4421
4422 if (wpa_s->disallow_aps_bssid == NULL)
4423 return 0;
4424
4425 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4426 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4427 bssid, ETH_ALEN) == 0)
4428 return 1;
4429 }
4430
4431 return 0;
4432}
4433
4434
4435int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4436 size_t ssid_len)
4437{
4438 size_t i;
4439
4440 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4441 return 0;
4442
4443 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4444 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4445 if (ssid_len == s->ssid_len &&
4446 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4447 return 1;
4448 }
4449
4450 return 0;
4451}
4452
4453
4454/**
4455 * wpas_request_connection - Request a new connection
4456 * @wpa_s: Pointer to the network interface
4457 *
4458 * This function is used to request a new connection to be found. It will mark
4459 * the interface to allow reassociation and request a new scan to find a
4460 * suitable network to connect to.
4461 */
4462void wpas_request_connection(struct wpa_supplicant *wpa_s)
4463{
4464 wpa_s->normal_scans = 0;
4465 wpa_supplicant_reinit_autoscan(wpa_s);
4466 wpa_s->extra_blacklist_count = 0;
4467 wpa_s->disconnected = 0;
4468 wpa_s->reassociate = 1;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004469
4470 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4471 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004472}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004473
4474
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004475void dump_freq_array(struct wpa_supplicant *wpa_s, const char *title,
4476 int *freq_array, unsigned int len)
4477{
4478 unsigned int i;
4479
4480 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4481 len, title);
4482 for (i = 0; i < len; i++)
4483 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", i, freq_array[i]);
4484}
4485
4486
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004487/*
4488 * Find the operating frequencies of any of the virtual interfaces that
4489 * are using the same radio as the current interface.
4490 */
4491int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4492 int *freq_array, unsigned int len)
4493{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004494 struct wpa_supplicant *ifs;
4495 u8 bssid[ETH_ALEN];
4496 int freq;
4497 unsigned int idx = 0, i;
4498
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004499 wpa_dbg(wpa_s, MSG_DEBUG,
4500 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004501 os_memset(freq_array, 0, sizeof(int) * len);
4502
4503 /* First add the frequency of the local interface */
4504 if (wpa_s->current_ssid != NULL && wpa_s->assoc_freq != 0) {
4505 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4506 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)
4507 freq_array[idx++] = wpa_s->current_ssid->frequency;
4508 else if (wpa_drv_get_bssid(wpa_s, bssid) == 0)
4509 freq_array[idx++] = wpa_s->assoc_freq;
4510 }
4511
4512 /* If get_radio_name is not supported, use only the local freq */
4513 if (!wpa_s->driver->get_radio_name) {
4514 freq = wpa_drv_shared_freq(wpa_s);
4515 if (freq > 0 && idx < len &&
4516 (idx == 0 || freq_array[0] != freq))
4517 freq_array[idx++] = freq;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004518 dump_freq_array(wpa_s, "No get_radio_name", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004519 return idx;
4520 }
4521
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004522 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4523 radio_list) {
4524 if (wpa_s == ifs)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004525 continue;
4526
4527 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4528 continue;
4529
4530 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4531 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4532 freq = ifs->current_ssid->frequency;
4533 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4534 freq = ifs->assoc_freq;
4535 else
4536 continue;
4537
4538 /* Hold only distinct freqs */
4539 for (i = 0; i < idx; i++)
4540 if (freq_array[i] == freq)
4541 break;
4542
4543 if (i == idx)
4544 freq_array[idx++] = freq;
4545 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004546
4547 dump_freq_array(wpa_s, "completed iteration", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004548 return idx;
4549}