blob: af7b847b83a269a50f8fa86e63cce3d8d0334f04 [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 Shmidtd11f0192014-03-24 12:09:47 -0700463 os_free(wpa_s->manual_sched_scan_freqs);
464 wpa_s->manual_sched_scan_freqs = NULL;
465
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800466 gas_query_deinit(wpa_s->gas);
467 wpa_s->gas = NULL;
468
469 free_hw_features(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700470
471 os_free(wpa_s->bssid_filter);
472 wpa_s->bssid_filter = NULL;
473
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800474 os_free(wpa_s->disallow_aps_bssid);
475 wpa_s->disallow_aps_bssid = NULL;
476 os_free(wpa_s->disallow_aps_ssid);
477 wpa_s->disallow_aps_ssid = NULL;
478
Dmitry Shmidt04949592012-07-19 12:16:46 -0700479 wnm_bss_keep_alive_deinit(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700480#ifdef CONFIG_WNM
481 wnm_deallocate_memory(wpa_s);
482#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483
484 ext_password_deinit(wpa_s->ext_pw);
485 wpa_s->ext_pw = NULL;
486
487 wpabuf_free(wpa_s->last_gas_resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800488 wpa_s->last_gas_resp = NULL;
489 wpabuf_free(wpa_s->prev_gas_resp);
490 wpa_s->prev_gas_resp = NULL;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700491
492 os_free(wpa_s->last_scan_res);
493 wpa_s->last_scan_res = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800494
495#ifdef CONFIG_HS20
496 hs20_free_osu_prov(wpa_s);
497#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498}
499
500
501/**
502 * wpa_clear_keys - Clear keys configured for the driver
503 * @wpa_s: Pointer to wpa_supplicant data
504 * @addr: Previously used BSSID or %NULL if not available
505 *
506 * This function clears the encryption keys that has been previously configured
507 * for the driver.
508 */
509void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
510{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800511 int i, max;
512
513#ifdef CONFIG_IEEE80211W
514 max = 6;
515#else /* CONFIG_IEEE80211W */
516 max = 4;
517#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518
519 /* MLME-DELETEKEYS.request */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800520 for (i = 0; i < max; i++) {
521 if (wpa_s->keys_cleared & BIT(i))
522 continue;
523 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
524 NULL, 0);
525 }
526 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
527 !is_zero_ether_addr(addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
529 0);
530 /* MLME-SETPROTECTION.request(None) */
531 wpa_drv_mlme_setprotection(
532 wpa_s, addr,
533 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
534 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
535 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800536 wpa_s->keys_cleared = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537}
538
539
540/**
541 * wpa_supplicant_state_txt - Get the connection state name as a text string
542 * @state: State (wpa_state; WPA_*)
543 * Returns: The state name as a printable text string
544 */
545const char * wpa_supplicant_state_txt(enum wpa_states state)
546{
547 switch (state) {
548 case WPA_DISCONNECTED:
549 return "DISCONNECTED";
550 case WPA_INACTIVE:
551 return "INACTIVE";
552 case WPA_INTERFACE_DISABLED:
553 return "INTERFACE_DISABLED";
554 case WPA_SCANNING:
555 return "SCANNING";
556 case WPA_AUTHENTICATING:
557 return "AUTHENTICATING";
558 case WPA_ASSOCIATING:
559 return "ASSOCIATING";
560 case WPA_ASSOCIATED:
561 return "ASSOCIATED";
562 case WPA_4WAY_HANDSHAKE:
563 return "4WAY_HANDSHAKE";
564 case WPA_GROUP_HANDSHAKE:
565 return "GROUP_HANDSHAKE";
566 case WPA_COMPLETED:
567 return "COMPLETED";
568 default:
569 return "UNKNOWN";
570 }
571}
572
573
574#ifdef CONFIG_BGSCAN
575
576static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
577{
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800578 const char *name;
579
580 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
581 name = wpa_s->current_ssid->bgscan;
582 else
583 name = wpa_s->conf->bgscan;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800584 if (name == NULL || name[0] == '\0')
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800585 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586 if (wpas_driver_bss_selection(wpa_s))
587 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
589 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800590#ifdef CONFIG_P2P
591 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
592 return;
593#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594
595 bgscan_deinit(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800596 if (wpa_s->current_ssid) {
597 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
599 "bgscan");
600 /*
601 * Live without bgscan; it is only used as a roaming
602 * optimization, so the initial connection is not
603 * affected.
604 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700605 } else {
606 struct wpa_scan_results *scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 wpa_s->bgscan_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700608 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
609 0);
610 if (scan_res) {
611 bgscan_notify_scan(wpa_s, scan_res);
612 wpa_scan_results_free(scan_res);
613 }
614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 } else
616 wpa_s->bgscan_ssid = NULL;
617}
618
619
620static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
621{
622 if (wpa_s->bgscan_ssid != NULL) {
623 bgscan_deinit(wpa_s);
624 wpa_s->bgscan_ssid = NULL;
625 }
626}
627
628#endif /* CONFIG_BGSCAN */
629
630
Dmitry Shmidt04949592012-07-19 12:16:46 -0700631static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
632{
633 if (autoscan_init(wpa_s, 0))
634 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
635}
636
637
638static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
639{
640 autoscan_deinit(wpa_s);
641}
642
643
644void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
645{
646 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
647 wpa_s->wpa_state == WPA_SCANNING) {
648 autoscan_deinit(wpa_s);
649 wpa_supplicant_start_autoscan(wpa_s);
650 }
651}
652
653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654/**
655 * wpa_supplicant_set_state - Set current connection state
656 * @wpa_s: Pointer to wpa_supplicant data
657 * @state: The new connection state
658 *
659 * This function is called whenever the connection state changes, e.g.,
660 * association is completed for WPA/WPA2 4-Way Handshake is started.
661 */
662void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
663 enum wpa_states state)
664{
665 enum wpa_states old_state = wpa_s->wpa_state;
666
667 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
668 wpa_supplicant_state_txt(wpa_s->wpa_state),
669 wpa_supplicant_state_txt(state));
670
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -0800671 if (state == WPA_INTERFACE_DISABLED) {
672 /* Assure normal scan when interface is restored */
673 wpa_s->normal_scans = 0;
674 }
675
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700676 if (state == WPA_COMPLETED) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800677 wpas_connect_work_done(wpa_s);
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700678 /* Reinitialize normal_scan counter */
679 wpa_s->normal_scans = 0;
680 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 if (state != WPA_SCANNING)
683 wpa_supplicant_notify_scanning(wpa_s, 0);
684
685 if (state == WPA_COMPLETED && wpa_s->new_connection) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700687#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
Dmitry Shmidt8f0dbf42013-11-08 13:35:41 -0800689 MACSTR " completed [id=%d id_str=%s]",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800690 MAC2STR(wpa_s->bssid),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 ssid ? ssid->id : -1,
692 ssid && ssid->id_str ? ssid->id_str : "");
693#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700694 wpas_clear_temp_disabled(wpa_s, ssid, 1);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800695 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 wpa_s->new_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 wpa_drv_set_operstate(wpa_s, 1);
698#ifndef IEEE8021X_EAPOL
699 wpa_drv_set_supp_port(wpa_s, 1);
700#endif /* IEEE8021X_EAPOL */
701 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700702 wpa_s->known_wps_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703#ifdef CONFIG_P2P
704 wpas_p2p_completed(wpa_s);
705#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700706
707 sme_sched_obss_scan(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
709 state == WPA_ASSOCIATED) {
710 wpa_s->new_connection = 1;
711 wpa_drv_set_operstate(wpa_s, 0);
712#ifndef IEEE8021X_EAPOL
713 wpa_drv_set_supp_port(wpa_s, 0);
714#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700715 sme_sched_obss_scan(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 }
717 wpa_s->wpa_state = state;
718
719#ifdef CONFIG_BGSCAN
720 if (state == WPA_COMPLETED)
721 wpa_supplicant_start_bgscan(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800722 else if (state < WPA_ASSOCIATED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 wpa_supplicant_stop_bgscan(wpa_s);
724#endif /* CONFIG_BGSCAN */
725
Dmitry Shmidt04949592012-07-19 12:16:46 -0700726 if (state == WPA_AUTHENTICATING)
727 wpa_supplicant_stop_autoscan(wpa_s);
728
729 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
730 wpa_supplicant_start_autoscan(wpa_s);
731
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 if (wpa_s->wpa_state != old_state) {
733 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
734
735 if (wpa_s->wpa_state == WPA_COMPLETED ||
736 old_state == WPA_COMPLETED)
737 wpas_notify_auth_changed(wpa_s);
738 }
739}
740
741
742void wpa_supplicant_terminate_proc(struct wpa_global *global)
743{
744 int pending = 0;
745#ifdef CONFIG_WPS
746 struct wpa_supplicant *wpa_s = global->ifaces;
747 while (wpa_s) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800748 struct wpa_supplicant *next = wpa_s->next;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700749#ifdef CONFIG_P2P
750 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
751 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
752 wpas_p2p_disconnect(wpa_s);
753#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 if (wpas_wps_terminate_pending(wpa_s) == 1)
755 pending = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800756 wpa_s = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 }
758#endif /* CONFIG_WPS */
759 if (pending)
760 return;
761 eloop_terminate();
762}
763
764
765static void wpa_supplicant_terminate(int sig, void *signal_ctx)
766{
767 struct wpa_global *global = signal_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 wpa_supplicant_terminate_proc(global);
769}
770
771
772void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
773{
774 enum wpa_states old_state = wpa_s->wpa_state;
775
776 wpa_s->pairwise_cipher = 0;
777 wpa_s->group_cipher = 0;
778 wpa_s->mgmt_group_cipher = 0;
779 wpa_s->key_mgmt = 0;
780 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700781 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782
783 if (wpa_s->wpa_state != old_state)
784 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
785}
786
787
788/**
789 * wpa_supplicant_reload_configuration - Reload configuration data
790 * @wpa_s: Pointer to wpa_supplicant data
791 * Returns: 0 on success or -1 if configuration parsing failed
792 *
793 * This function can be used to request that the configuration data is reloaded
794 * (e.g., after configuration file change). This function is reloading
795 * configuration only for one interface, so this may need to be called multiple
796 * times if %wpa_supplicant is controlling multiple interfaces and all
797 * interfaces need reconfiguration.
798 */
799int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
800{
801 struct wpa_config *conf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 int reconf_ctrl;
803 int old_ap_scan;
804
805 if (wpa_s->confname == NULL)
806 return -1;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700807 conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 if (conf == NULL) {
809 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
810 "file '%s' - exiting", wpa_s->confname);
811 return -1;
812 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700813 wpa_config_read(wpa_s->confanother, conf);
814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 conf->changed_parameters = (unsigned int) -1;
816
817 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
818 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
819 os_strcmp(conf->ctrl_interface,
820 wpa_s->conf->ctrl_interface) != 0);
821
822 if (reconf_ctrl && wpa_s->ctrl_iface) {
823 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
824 wpa_s->ctrl_iface = NULL;
825 }
826
827 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800828 if (wpa_s->current_ssid) {
829 wpa_supplicant_deauthenticate(wpa_s,
830 WLAN_REASON_DEAUTH_LEAVING);
831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832
833 /*
834 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
835 * pkcs11_engine_path, pkcs11_module_path.
836 */
837 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
838 /*
839 * Clear forced success to clear EAP state for next
840 * authentication.
841 */
842 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
843 }
844 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
845 wpa_sm_set_config(wpa_s->wpa, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
848 rsn_preauth_deinit(wpa_s->wpa);
849
850 old_ap_scan = wpa_s->conf->ap_scan;
851 wpa_config_free(wpa_s->conf);
852 wpa_s->conf = conf;
853 if (old_ap_scan != wpa_s->conf->ap_scan)
854 wpas_notify_ap_scan_changed(wpa_s);
855
856 if (reconf_ctrl)
857 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
858
859 wpa_supplicant_update_config(wpa_s);
860
861 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700862 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 wpa_s->reassociate = 1;
864 wpa_supplicant_req_scan(wpa_s, 0, 0);
865 }
866 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
867 return 0;
868}
869
870
871static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
872{
873 struct wpa_global *global = signal_ctx;
874 struct wpa_supplicant *wpa_s;
875 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
876 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
877 sig);
878 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
879 wpa_supplicant_terminate_proc(global);
880 }
881 }
882}
883
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
886 struct wpa_ssid *ssid,
887 struct wpa_ie_data *ie)
888{
889 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
890 if (ret) {
891 if (ret == -2) {
892 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
893 "from association info");
894 }
895 return -1;
896 }
897
898 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
899 "cipher suites");
900 if (!(ie->group_cipher & ssid->group_cipher)) {
901 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
902 "cipher 0x%x (mask 0x%x) - reject",
903 ie->group_cipher, ssid->group_cipher);
904 return -1;
905 }
906 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
907 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
908 "cipher 0x%x (mask 0x%x) - reject",
909 ie->pairwise_cipher, ssid->pairwise_cipher);
910 return -1;
911 }
912 if (!(ie->key_mgmt & ssid->key_mgmt)) {
913 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
914 "management 0x%x (mask 0x%x) - reject",
915 ie->key_mgmt, ssid->key_mgmt);
916 return -1;
917 }
918
919#ifdef CONFIG_IEEE80211W
920 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800921 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
922 wpa_s->conf->pmf : ssid->ieee80211w) ==
923 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
925 "that does not support management frame protection - "
926 "reject");
927 return -1;
928 }
929#endif /* CONFIG_IEEE80211W */
930
931 return 0;
932}
933
934
935/**
936 * wpa_supplicant_set_suites - Set authentication and encryption parameters
937 * @wpa_s: Pointer to wpa_supplicant data
938 * @bss: Scan results for the selected BSS, or %NULL if not available
939 * @ssid: Configuration data for the selected network
940 * @wpa_ie: Buffer for the WPA/RSN IE
941 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
942 * used buffer length in case the functions returns success.
943 * Returns: 0 on success or -1 on failure
944 *
945 * This function is used to configure authentication and encryption parameters
946 * based on the network configuration and scan result for the selected BSS (if
947 * available).
948 */
949int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
950 struct wpa_bss *bss, struct wpa_ssid *ssid,
951 u8 *wpa_ie, size_t *wpa_ie_len)
952{
953 struct wpa_ie_data ie;
954 int sel, proto;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800955 const u8 *bss_wpa, *bss_rsn, *bss_osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956
957 if (bss) {
958 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
959 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800960 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 } else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800962 bss_wpa = bss_rsn = bss_osen = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963
964 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
965 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
966 (ie.group_cipher & ssid->group_cipher) &&
967 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
968 (ie.key_mgmt & ssid->key_mgmt)) {
969 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
970 proto = WPA_PROTO_RSN;
971 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
972 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
973 (ie.group_cipher & ssid->group_cipher) &&
974 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
975 (ie.key_mgmt & ssid->key_mgmt)) {
976 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
977 proto = WPA_PROTO_WPA;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800978#ifdef CONFIG_HS20
979 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
980 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
981 /* TODO: parse OSEN element */
982 ie.group_cipher = WPA_CIPHER_CCMP;
983 ie.pairwise_cipher = WPA_CIPHER_CCMP;
984 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
985 proto = WPA_PROTO_OSEN;
986#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 } else if (bss) {
988 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
989 return -1;
990 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800991 if (ssid->proto & WPA_PROTO_OSEN)
992 proto = WPA_PROTO_OSEN;
993 else if (ssid->proto & WPA_PROTO_RSN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 proto = WPA_PROTO_RSN;
995 else
996 proto = WPA_PROTO_WPA;
997 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
998 os_memset(&ie, 0, sizeof(ie));
999 ie.group_cipher = ssid->group_cipher;
1000 ie.pairwise_cipher = ssid->pairwise_cipher;
1001 ie.key_mgmt = ssid->key_mgmt;
1002#ifdef CONFIG_IEEE80211W
1003 ie.mgmt_group_cipher =
1004 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
1005 WPA_CIPHER_AES_128_CMAC : 0;
1006#endif /* CONFIG_IEEE80211W */
1007 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1008 "based on configuration");
1009 } else
1010 proto = ie.proto;
1011 }
1012
1013 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1014 "pairwise %d key_mgmt %d proto %d",
1015 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1016#ifdef CONFIG_IEEE80211W
1017 if (ssid->ieee80211w) {
1018 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1019 ie.mgmt_group_cipher);
1020 }
1021#endif /* CONFIG_IEEE80211W */
1022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001023 wpa_s->wpa_proto = proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1025 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001026 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027
1028 if (bss || !wpa_s->ap_ies_from_associnfo) {
1029 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1030 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1031 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1032 bss_rsn ? 2 + bss_rsn[1] : 0))
1033 return -1;
1034 }
1035
1036 sel = ie.group_cipher & ssid->group_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001037 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1038 if (wpa_s->group_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1040 "cipher");
1041 return -1;
1042 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001043 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1044 wpa_cipher_txt(wpa_s->group_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045
1046 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001047 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1048 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1050 "cipher");
1051 return -1;
1052 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001053 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1054 wpa_cipher_txt(wpa_s->pairwise_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055
1056 sel = ie.key_mgmt & ssid->key_mgmt;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001057#ifdef CONFIG_SAE
1058 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1059 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1060#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 if (0) {
1062#ifdef CONFIG_IEEE80211R
1063 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1064 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1065 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1066 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1067 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1068 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1069#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001070#ifdef CONFIG_SAE
1071 } else if (sel & WPA_KEY_MGMT_SAE) {
1072 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1073 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1074 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1075 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1076 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1077#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078#ifdef CONFIG_IEEE80211W
1079 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1080 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1081 wpa_dbg(wpa_s, MSG_DEBUG,
1082 "WPA: using KEY_MGMT 802.1X with SHA256");
1083 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1084 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1085 wpa_dbg(wpa_s, MSG_DEBUG,
1086 "WPA: using KEY_MGMT PSK with SHA256");
1087#endif /* CONFIG_IEEE80211W */
1088 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1089 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1090 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1091 } else if (sel & WPA_KEY_MGMT_PSK) {
1092 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1093 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1094 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1095 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1096 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001097#ifdef CONFIG_HS20
1098 } else if (sel & WPA_KEY_MGMT_OSEN) {
1099 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1100 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1101#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 } else {
1103 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1104 "authenticated key management type");
1105 return -1;
1106 }
1107
1108 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1109 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1110 wpa_s->pairwise_cipher);
1111 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1112
1113#ifdef CONFIG_IEEE80211W
1114 sel = ie.mgmt_group_cipher;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001115 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1116 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1118 sel = 0;
1119 if (sel & WPA_CIPHER_AES_128_CMAC) {
1120 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1121 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1122 "AES-128-CMAC");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001123 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1124 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1125 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1126 "BIP-GMAC-128");
1127 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1128 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1129 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1130 "BIP-GMAC-256");
1131 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1132 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1133 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1134 "BIP-CMAC-256");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001135 } else {
1136 wpa_s->mgmt_group_cipher = 0;
1137 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1138 }
1139 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1140 wpa_s->mgmt_group_cipher);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001141 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1142 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1143 wpa_s->conf->pmf : ssid->ieee80211w));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144#endif /* CONFIG_IEEE80211W */
1145
1146 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1147 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1148 return -1;
1149 }
1150
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001151 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153#ifndef CONFIG_NO_PBKDF2
1154 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1155 ssid->passphrase) {
1156 u8 psk[PMK_LEN];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001157 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1158 4096, psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001159 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1160 psk, PMK_LEN);
1161 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1162 }
1163#endif /* CONFIG_NO_PBKDF2 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001164#ifdef CONFIG_EXT_PASSWORD
1165 if (ssid->ext_psk) {
1166 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1167 ssid->ext_psk);
1168 char pw_str[64 + 1];
1169 u8 psk[PMK_LEN];
1170
1171 if (pw == NULL) {
1172 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1173 "found from external storage");
1174 return -1;
1175 }
1176
1177 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1178 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1179 "PSK length %d in external storage",
1180 (int) wpabuf_len(pw));
1181 ext_password_free(pw);
1182 return -1;
1183 }
1184
1185 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1186 pw_str[wpabuf_len(pw)] = '\0';
1187
1188#ifndef CONFIG_NO_PBKDF2
1189 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1190 {
1191 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1192 4096, psk, PMK_LEN);
1193 os_memset(pw_str, 0, sizeof(pw_str));
1194 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1195 "external passphrase)",
1196 psk, PMK_LEN);
1197 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1198 } else
1199#endif /* CONFIG_NO_PBKDF2 */
1200 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1201 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1202 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1203 "Invalid PSK hex string");
1204 os_memset(pw_str, 0, sizeof(pw_str));
1205 ext_password_free(pw);
1206 return -1;
1207 }
1208 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1209 } else {
1210 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1211 "PSK available");
1212 os_memset(pw_str, 0, sizeof(pw_str));
1213 ext_password_free(pw);
1214 return -1;
1215 }
1216
1217 os_memset(pw_str, 0, sizeof(pw_str));
1218 ext_password_free(pw);
1219 }
1220#endif /* CONFIG_EXT_PASSWORD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1223
1224 return 0;
1225}
1226
1227
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001228static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1229{
1230 *pos = 0x00;
1231
1232 switch (idx) {
1233 case 0: /* Bits 0-7 */
1234 break;
1235 case 1: /* Bits 8-15 */
1236 break;
1237 case 2: /* Bits 16-23 */
1238#ifdef CONFIG_WNM
1239 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1240 *pos |= 0x08; /* Bit 19 - BSS Transition */
1241#endif /* CONFIG_WNM */
1242 break;
1243 case 3: /* Bits 24-31 */
1244#ifdef CONFIG_WNM
1245 *pos |= 0x02; /* Bit 25 - SSID List */
1246#endif /* CONFIG_WNM */
1247#ifdef CONFIG_INTERWORKING
1248 if (wpa_s->conf->interworking)
1249 *pos |= 0x80; /* Bit 31 - Interworking */
1250#endif /* CONFIG_INTERWORKING */
1251 break;
1252 case 4: /* Bits 32-39 */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001253#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001254 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1255 *pos |= 0x01; /* Bit 32 - QoS Map */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001256#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001257 break;
1258 case 5: /* Bits 40-47 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001259#ifdef CONFIG_HS20
1260 if (wpa_s->conf->hs20)
1261 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1262#endif /* CONFIG_HS20 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001263 break;
1264 case 6: /* Bits 48-55 */
1265 break;
1266 }
1267}
1268
1269
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001270int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf)
1271{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001272 u8 *pos = buf;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001273 u8 len = 6, i;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001274
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001275 if (len < wpa_s->extended_capa_len)
1276 len = wpa_s->extended_capa_len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001277
1278 *pos++ = WLAN_EID_EXT_CAPAB;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001279 *pos++ = len;
1280 for (i = 0; i < len; i++, pos++) {
1281 wpas_ext_capab_byte(wpa_s, pos, i);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001282
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001283 if (i < wpa_s->extended_capa_len) {
1284 *pos &= ~wpa_s->extended_capa_mask[i];
1285 *pos |= wpa_s->extended_capa[i];
1286 }
1287 }
1288
1289 while (len > 0 && buf[1 + len] == 0) {
1290 len--;
1291 buf[1] = len;
1292 }
1293 if (len == 0)
1294 return 0;
1295
1296 return 2 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001297}
1298
1299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001300static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1301 struct wpa_bss *test_bss)
1302{
1303 struct wpa_bss *bss;
1304
1305 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1306 if (bss == test_bss)
1307 return 1;
1308 }
1309
1310 return 0;
1311}
1312
1313
1314static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1315 struct wpa_ssid *test_ssid)
1316{
1317 struct wpa_ssid *ssid;
1318
1319 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1320 if (ssid == test_ssid)
1321 return 1;
1322 }
1323
1324 return 0;
1325}
1326
1327
1328int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1329 struct wpa_ssid *test_ssid)
1330{
1331 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1332 return 0;
1333
1334 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1335}
1336
1337
1338void wpas_connect_work_free(struct wpa_connect_work *cwork)
1339{
1340 if (cwork == NULL)
1341 return;
1342 os_free(cwork);
1343}
1344
1345
1346void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1347{
1348 struct wpa_connect_work *cwork;
1349 struct wpa_radio_work *work = wpa_s->connect_work;
1350
1351 if (!work)
1352 return;
1353
1354 wpa_s->connect_work = NULL;
1355 cwork = work->ctx;
1356 work->ctx = NULL;
1357 wpas_connect_work_free(cwork);
1358 radio_work_done(work);
1359}
1360
1361
1362static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364/**
1365 * wpa_supplicant_associate - Request association
1366 * @wpa_s: Pointer to wpa_supplicant data
1367 * @bss: Scan results for the selected BSS, or %NULL if not available
1368 * @ssid: Configuration data for the selected network
1369 *
1370 * This function is used to request %wpa_supplicant to associate with a BSS.
1371 */
1372void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1373 struct wpa_bss *bss, struct wpa_ssid *ssid)
1374{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001375 struct wpa_connect_work *cwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376
1377#ifdef CONFIG_IBSS_RSN
1378 ibss_rsn_deinit(wpa_s->ibss_rsn);
1379 wpa_s->ibss_rsn = NULL;
1380#endif /* CONFIG_IBSS_RSN */
1381
1382 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1383 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1384#ifdef CONFIG_AP
1385 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1386 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1387 "mode");
1388 return;
1389 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001390 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1391 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001392 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1393 wpas_p2p_ap_setup_failed(wpa_s);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001394 return;
1395 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 wpa_s->current_bss = bss;
1397#else /* CONFIG_AP */
1398 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1399 "the build");
1400#endif /* CONFIG_AP */
1401 return;
1402 }
1403
1404#ifdef CONFIG_TDLS
1405 if (bss)
1406 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1407 bss->ie_len);
1408#endif /* CONFIG_TDLS */
1409
1410 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1411 ssid->mode == IEEE80211_MODE_INFRA) {
1412 sme_authenticate(wpa_s, bss, ssid);
1413 return;
1414 }
1415
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001416 if (wpa_s->connect_work) {
1417 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1418 return;
1419 }
1420
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001421 if (radio_work_pending(wpa_s, "connect")) {
1422 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1423 return;
1424 }
1425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001426 cwork = os_zalloc(sizeof(*cwork));
1427 if (cwork == NULL)
1428 return;
1429
1430 cwork->bss = bss;
1431 cwork->ssid = ssid;
1432
1433 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1434 wpas_start_assoc_cb, cwork) < 0) {
1435 os_free(cwork);
1436 }
1437}
1438
1439
1440static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1441{
1442 struct wpa_connect_work *cwork = work->ctx;
1443 struct wpa_bss *bss = cwork->bss;
1444 struct wpa_ssid *ssid = cwork->ssid;
1445 struct wpa_supplicant *wpa_s = work->wpa_s;
1446 u8 wpa_ie[200];
1447 size_t wpa_ie_len;
1448 int use_crypt, ret, i, bssid_changed;
1449 int algs = WPA_AUTH_ALG_OPEN;
1450 unsigned int cipher_pairwise, cipher_group;
1451 struct wpa_driver_associate_params params;
1452 int wep_keys_set = 0;
1453 int assoc_failed = 0;
1454 struct wpa_ssid *old_ssid;
1455#ifdef CONFIG_HT_OVERRIDES
1456 struct ieee80211_ht_capabilities htcaps;
1457 struct ieee80211_ht_capabilities htcaps_mask;
1458#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001459#ifdef CONFIG_VHT_OVERRIDES
1460 struct ieee80211_vht_capabilities vhtcaps;
1461 struct ieee80211_vht_capabilities vhtcaps_mask;
1462#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001463
1464 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001465 if (work->started) {
1466 wpa_s->connect_work = NULL;
1467
1468 /* cancel possible auth. timeout */
1469 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1470 NULL);
1471 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001472 wpas_connect_work_free(cwork);
1473 return;
1474 }
1475
1476 wpa_s->connect_work = work;
1477
1478 if (!wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1479 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1480 wpas_connect_work_done(wpa_s);
1481 return;
1482 }
1483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 os_memset(&params, 0, sizeof(params));
1485 wpa_s->reassociate = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001486 wpa_s->eap_expected_failure = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001487 if (bss && !wpas_driver_bss_selection(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488#ifdef CONFIG_IEEE80211R
1489 const u8 *ie, *md = NULL;
1490#endif /* CONFIG_IEEE80211R */
1491 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1492 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1493 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1494 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1495 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1496 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1497 if (bssid_changed)
1498 wpas_notify_bssid_changed(wpa_s);
1499#ifdef CONFIG_IEEE80211R
1500 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1501 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1502 md = ie + 2;
1503 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1504 if (md) {
1505 /* Prepare for the next transition */
1506 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1507 }
1508#endif /* CONFIG_IEEE80211R */
1509#ifdef CONFIG_WPS
1510 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1511 wpa_s->conf->ap_scan == 2 &&
1512 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1513 /* Use ap_scan==1 style network selection to find the network
1514 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001515 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 wpa_s->reassociate = 1;
1517 wpa_supplicant_req_scan(wpa_s, 0, 0);
1518 return;
1519#endif /* CONFIG_WPS */
1520 } else {
1521 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1522 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1523 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1524 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001525 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 wpa_supplicant_cancel_scan(wpa_s);
1527
1528 /* Starting new association, so clear the possibly used WPA IE from the
1529 * previous association. */
1530 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1531
1532#ifdef IEEE8021X_EAPOL
1533 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1534 if (ssid->leap) {
1535 if (ssid->non_leap == 0)
1536 algs = WPA_AUTH_ALG_LEAP;
1537 else
1538 algs |= WPA_AUTH_ALG_LEAP;
1539 }
1540 }
1541#endif /* IEEE8021X_EAPOL */
1542 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1543 if (ssid->auth_alg) {
1544 algs = ssid->auth_alg;
1545 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1546 "0x%x", algs);
1547 }
1548
1549 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1550 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552 int try_opportunistic;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001553 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1554 wpa_s->conf->okc :
1555 ssid->proactive_key_caching) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 (ssid->proto & WPA_PROTO_RSN);
1557 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001558 ssid, try_opportunistic) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1560 wpa_ie_len = sizeof(wpa_ie);
1561 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1562 wpa_ie, &wpa_ie_len)) {
1563 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1564 "key management and encryption suites");
1565 return;
1566 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001567 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1568 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1569 /*
1570 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1571 * use non-WPA since the scan results did not indicate that the
1572 * AP is using WPA or WPA2.
1573 */
1574 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1575 wpa_ie_len = 0;
1576 wpa_s->wpa_proto = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001577 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 wpa_ie_len = sizeof(wpa_ie);
1579 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1580 wpa_ie, &wpa_ie_len)) {
1581 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1582 "key management and encryption suites (no "
1583 "scan results)");
1584 return;
1585 }
1586#ifdef CONFIG_WPS
1587 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1588 struct wpabuf *wps_ie;
1589 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1590 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1591 wpa_ie_len = wpabuf_len(wps_ie);
1592 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1593 } else
1594 wpa_ie_len = 0;
1595 wpabuf_free(wps_ie);
1596 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1597 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1598 params.wps = WPS_MODE_PRIVACY;
1599 else
1600 params.wps = WPS_MODE_OPEN;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001601 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602#endif /* CONFIG_WPS */
1603 } else {
1604 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1605 wpa_ie_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001606 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
1608
1609#ifdef CONFIG_P2P
1610 if (wpa_s->global->p2p) {
1611 u8 *pos;
1612 size_t len;
1613 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 pos = wpa_ie + wpa_ie_len;
1615 len = sizeof(wpa_ie) - wpa_ie_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001616 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1617 ssid->p2p_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 if (res >= 0)
1619 wpa_ie_len += res;
1620 }
1621
1622 wpa_s->cross_connect_disallowed = 0;
1623 if (bss) {
1624 struct wpabuf *p2p;
1625 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1626 if (p2p) {
1627 wpa_s->cross_connect_disallowed =
1628 p2p_get_cross_connect_disallowed(p2p);
1629 wpabuf_free(p2p);
1630 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1631 "connection",
1632 wpa_s->cross_connect_disallowed ?
1633 "disallows" : "allows");
1634 }
1635 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001636
1637 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638#endif /* CONFIG_P2P */
1639
Dmitry Shmidt04949592012-07-19 12:16:46 -07001640#ifdef CONFIG_HS20
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001641 if (is_hs20_network(wpa_s, ssid, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001642 struct wpabuf *hs20;
1643 hs20 = wpabuf_alloc(20);
1644 if (hs20) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001645 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
1646 wpas_hs20_add_indication(hs20, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001647 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1648 wpabuf_len(hs20));
1649 wpa_ie_len += wpabuf_len(hs20);
1650 wpabuf_free(hs20);
1651 }
1652 }
1653#endif /* CONFIG_HS20 */
1654
Dmitry Shmidt56052862013-10-04 10:23:25 -07001655 /*
1656 * Workaround: Add Extended Capabilities element only if the AP
1657 * included this element in Beacon/Probe Response frames. Some older
1658 * APs seem to have interoperability issues if this element is
1659 * included, so while the standard may require us to include the
1660 * element in all cases, it is justifiable to skip it to avoid
1661 * interoperability issues.
1662 */
1663 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
1664 u8 ext_capab[10];
1665 int ext_capab_len;
1666 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab);
1667 if (ext_capab_len > 0) {
1668 u8 *pos = wpa_ie;
1669 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1670 pos += 2 + pos[1];
1671 os_memmove(pos + ext_capab_len, pos,
1672 wpa_ie_len - (pos - wpa_ie));
1673 wpa_ie_len += ext_capab_len;
1674 os_memcpy(pos, ext_capab, ext_capab_len);
1675 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001676 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1679 use_crypt = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001680 cipher_pairwise = wpa_s->pairwise_cipher;
1681 cipher_group = wpa_s->group_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1683 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1684 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1685 use_crypt = 0;
1686 if (wpa_set_wep_keys(wpa_s, ssid)) {
1687 use_crypt = 1;
1688 wep_keys_set = 1;
1689 }
1690 }
1691 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1692 use_crypt = 0;
1693
1694#ifdef IEEE8021X_EAPOL
1695 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1696 if ((ssid->eapol_flags &
1697 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1698 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1699 !wep_keys_set) {
1700 use_crypt = 0;
1701 } else {
1702 /* Assume that dynamic WEP-104 keys will be used and
1703 * set cipher suites in order for drivers to expect
1704 * encryption. */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001705 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 }
1707 }
1708#endif /* IEEE8021X_EAPOL */
1709
1710 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1711 /* Set the key before (and later after) association */
1712 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1713 }
1714
1715 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1716 if (bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 params.ssid = bss->ssid;
1718 params.ssid_len = bss->ssid_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001719 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1720 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1721 MACSTR " freq=%u MHz based on scan results "
1722 "(bssid_set=%d)",
1723 MAC2STR(bss->bssid), bss->freq,
1724 ssid->bssid_set);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001725 params.bssid = bss->bssid;
1726 params.freq = bss->freq;
1727 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001728 params.bssid_hint = bss->bssid;
1729 params.freq_hint = bss->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 } else {
1731 params.ssid = ssid->ssid;
1732 params.ssid_len = ssid->ssid_len;
1733 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001734
1735 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1736 wpa_s->conf->ap_scan == 2) {
1737 params.bssid = ssid->bssid;
1738 params.fixed_bssid = 1;
1739 }
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1742 params.freq == 0)
1743 params.freq = ssid->frequency; /* Initial channel for IBSS */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08001744
1745 if (ssid->mode == WPAS_MODE_IBSS) {
1746 if (ssid->beacon_int)
1747 params.beacon_int = ssid->beacon_int;
1748 else
1749 params.beacon_int = wpa_s->conf->beacon_int;
1750 }
1751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 params.wpa_ie = wpa_ie;
1753 params.wpa_ie_len = wpa_ie_len;
1754 params.pairwise_suite = cipher_pairwise;
1755 params.group_suite = cipher_group;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001756 params.key_mgmt_suite = wpa_s->key_mgmt;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001757 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758 params.auth_alg = algs;
1759 params.mode = ssid->mode;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001760 params.bg_scan_period = ssid->bg_scan_period;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 for (i = 0; i < NUM_WEP_KEYS; i++) {
1762 if (ssid->wep_key_len[i])
1763 params.wep_key[i] = ssid->wep_key[i];
1764 params.wep_key_len[i] = ssid->wep_key_len[i];
1765 }
1766 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1767
1768 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001769 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1770 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 params.passphrase = ssid->passphrase;
1772 if (ssid->psk_set)
1773 params.psk = ssid->psk;
1774 }
1775
1776 params.drop_unencrypted = use_crypt;
1777
1778#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001779 params.mgmt_frame_protection =
1780 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1781 wpa_s->conf->pmf : ssid->ieee80211w;
1782 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1784 struct wpa_ie_data ie;
1785 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1786 ie.capabilities &
1787 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1788 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1789 "MFP: require MFP");
1790 params.mgmt_frame_protection =
1791 MGMT_FRAME_PROTECTION_REQUIRED;
1792 }
1793 }
1794#endif /* CONFIG_IEEE80211W */
1795
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001796 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797
1798 if (wpa_s->parent->set_sta_uapsd)
1799 params.uapsd = wpa_s->parent->sta_uapsd;
1800 else
1801 params.uapsd = -1;
1802
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001803#ifdef CONFIG_HT_OVERRIDES
1804 os_memset(&htcaps, 0, sizeof(htcaps));
1805 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1806 params.htcaps = (u8 *) &htcaps;
1807 params.htcaps_mask = (u8 *) &htcaps_mask;
1808 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1809#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001810#ifdef CONFIG_VHT_OVERRIDES
1811 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1812 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1813 params.vhtcaps = &vhtcaps;
1814 params.vhtcaps_mask = &vhtcaps_mask;
1815 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
1816#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001817
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001818#ifdef CONFIG_P2P
1819 /*
1820 * If multi-channel concurrency is not supported, check for any
1821 * frequency conflict. In case of any frequency conflict, remove the
1822 * least prioritized connection.
1823 */
1824 if (wpa_s->num_multichan_concurrent < 2) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001825 int freq, num;
1826 num = get_shared_radio_freqs(wpa_s, &freq, 1);
1827 if (num > 0 && freq > 0 && freq != params.freq) {
1828 wpa_printf(MSG_DEBUG,
1829 "Assoc conflicting freq found (%d != %d)",
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001830 freq, params.freq);
1831 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
1832 params.freq,
1833 ssid) < 0)
1834 return;
1835 }
1836 }
1837#endif /* CONFIG_P2P */
1838
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001839 ret = wpa_drv_associate(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 if (ret < 0) {
1841 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1842 "failed");
1843 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1844 /*
1845 * The driver is known to mean what is saying, so we
1846 * can stop right here; the association will not
1847 * succeed.
1848 */
1849 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001850 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1852 return;
1853 }
1854 /* try to continue anyway; new association will be tried again
1855 * after timeout */
1856 assoc_failed = 1;
1857 }
1858
1859 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1860 /* Set the key after the association just in case association
1861 * cleared the previously configured key. */
1862 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1863 /* No need to timeout authentication since there is no key
1864 * management. */
1865 wpa_supplicant_cancel_auth_timeout(wpa_s);
1866 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1867#ifdef CONFIG_IBSS_RSN
1868 } else if (ssid->mode == WPAS_MODE_IBSS &&
1869 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1870 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1871 /*
1872 * RSN IBSS authentication is per-STA and we can disable the
1873 * per-BSSID authentication.
1874 */
1875 wpa_supplicant_cancel_auth_timeout(wpa_s);
1876#endif /* CONFIG_IBSS_RSN */
1877 } else {
1878 /* Timeout for IEEE 802.11 authentication and association */
1879 int timeout = 60;
1880
1881 if (assoc_failed) {
1882 /* give IBSS a bit more time */
1883 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1884 } else if (wpa_s->conf->ap_scan == 1) {
1885 /* give IBSS a bit more time */
1886 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1887 }
1888 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1889 }
1890
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001891 if (wep_keys_set &&
1892 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 /* Set static WEP keys again */
1894 wpa_set_wep_keys(wpa_s, ssid);
1895 }
1896
1897 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1898 /*
1899 * Do not allow EAP session resumption between different
1900 * network configurations.
1901 */
1902 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1903 }
1904 old_ssid = wpa_s->current_ssid;
1905 wpa_s->current_ssid = ssid;
1906 wpa_s->current_bss = bss;
1907 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1908 wpa_supplicant_initiate_eapol(wpa_s);
1909 if (old_ssid != wpa_s->current_ssid)
1910 wpas_notify_network_changed(wpa_s);
1911}
1912
1913
1914static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1915 const u8 *addr)
1916{
1917 struct wpa_ssid *old_ssid;
1918
1919 wpa_clear_keys(wpa_s, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001920 old_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001921 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922 wpa_sm_set_config(wpa_s->wpa, NULL);
1923 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1924 if (old_ssid != wpa_s->current_ssid)
1925 wpas_notify_network_changed(wpa_s);
1926 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1927}
1928
1929
1930/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1932 * @wpa_s: Pointer to wpa_supplicant data
1933 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1934 *
1935 * This function is used to request %wpa_supplicant to deauthenticate from the
1936 * current AP.
1937 */
1938void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1939 int reason_code)
1940{
1941 u8 *addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001942 union wpa_event_data event;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001943 int zero_addr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001945 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
1946 " pending_bssid=" MACSTR " reason=%d state=%s",
1947 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1948 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
1949
1950 if (!is_zero_ether_addr(wpa_s->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 addr = wpa_s->bssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001952 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1953 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
1954 wpa_s->wpa_state == WPA_ASSOCIATING))
1955 addr = wpa_s->pending_bssid;
1956 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1957 /*
1958 * When using driver-based BSS selection, we may not know the
1959 * BSSID with which we are currently trying to associate. We
1960 * need to notify the driver of this disconnection even in such
1961 * a case, so use the all zeros address here.
1962 */
1963 addr = wpa_s->bssid;
1964 zero_addr = 1;
1965 }
1966
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001967#ifdef CONFIG_TDLS
1968 wpa_tdls_teardown_peers(wpa_s->wpa);
1969#endif /* CONFIG_TDLS */
1970
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001971 if (addr) {
1972 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001973 os_memset(&event, 0, sizeof(event));
1974 event.deauth_info.reason_code = (u16) reason_code;
1975 event.deauth_info.locally_generated = 1;
1976 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001977 if (zero_addr)
1978 addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 }
1980
1981 wpa_supplicant_clear_connection(wpa_s, addr);
1982}
1983
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001984static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
1985 struct wpa_ssid *ssid)
1986{
1987 if (!ssid || !ssid->disabled || ssid->disabled == 2)
1988 return;
1989
1990 ssid->disabled = 0;
1991 wpas_clear_temp_disabled(wpa_s, ssid, 1);
1992 wpas_notify_network_enabled_changed(wpa_s, ssid);
1993
1994 /*
1995 * Try to reassociate since there is no current configuration and a new
1996 * network was made available.
1997 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07001998 if (!wpa_s->current_ssid && !wpa_s->disconnected)
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001999 wpa_s->reassociate = 1;
2000}
2001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002
2003/**
2004 * wpa_supplicant_enable_network - Mark a configured network as enabled
2005 * @wpa_s: wpa_supplicant structure for a network interface
2006 * @ssid: wpa_ssid structure for a configured network or %NULL
2007 *
2008 * Enables the specified network or all networks if no network specified.
2009 */
2010void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2011 struct wpa_ssid *ssid)
2012{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002014 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2015 wpa_supplicant_enable_one_network(wpa_s, ssid);
2016 } else
2017 wpa_supplicant_enable_one_network(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002019 if (wpa_s->reassociate && !wpa_s->disconnected) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002020 if (wpa_s->sched_scanning) {
2021 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2022 "new network to scan filters");
2023 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 }
2025
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002026 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2027 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 }
2029}
2030
2031
2032/**
2033 * wpa_supplicant_disable_network - Mark a configured network as disabled
2034 * @wpa_s: wpa_supplicant structure for a network interface
2035 * @ssid: wpa_ssid structure for a configured network or %NULL
2036 *
2037 * Disables the specified network or all networks if no network specified.
2038 */
2039void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2040 struct wpa_ssid *ssid)
2041{
2042 struct wpa_ssid *other_ssid;
2043 int was_disabled;
2044
2045 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002046 if (wpa_s->sched_scanning)
2047 wpa_supplicant_cancel_sched_scan(wpa_s);
2048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2050 other_ssid = other_ssid->next) {
2051 was_disabled = other_ssid->disabled;
2052 if (was_disabled == 2)
2053 continue; /* do not change persistent P2P group
2054 * data */
2055
2056 other_ssid->disabled = 1;
2057
2058 if (was_disabled != other_ssid->disabled)
2059 wpas_notify_network_enabled_changed(
2060 wpa_s, other_ssid);
2061 }
2062 if (wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002063 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2065 } else if (ssid->disabled != 2) {
2066 if (ssid == wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002067 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2069
2070 was_disabled = ssid->disabled;
2071
2072 ssid->disabled = 1;
2073
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002074 if (was_disabled != ssid->disabled) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002076 if (wpa_s->sched_scanning) {
2077 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2078 "to remove network from filters");
2079 wpa_supplicant_cancel_sched_scan(wpa_s);
2080 wpa_supplicant_req_scan(wpa_s, 0, 0);
2081 }
2082 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083 }
2084}
2085
2086
2087/**
2088 * wpa_supplicant_select_network - Attempt association with a network
2089 * @wpa_s: wpa_supplicant structure for a network interface
2090 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2091 */
2092void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2093 struct wpa_ssid *ssid)
2094{
2095
2096 struct wpa_ssid *other_ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002097 int disconnected = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002099 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002100 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002102 disconnected = 1;
2103 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002105 if (ssid)
2106 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 /*
2109 * Mark all other networks disabled or mark all networks enabled if no
2110 * network specified.
2111 */
2112 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2113 other_ssid = other_ssid->next) {
2114 int was_disabled = other_ssid->disabled;
2115 if (was_disabled == 2)
2116 continue; /* do not change persistent P2P group data */
2117
2118 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002119 if (was_disabled && !other_ssid->disabled)
2120 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002121
2122 if (was_disabled != other_ssid->disabled)
2123 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
2124 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002125
2126 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2127 /* We are already associated with the selected network */
2128 wpa_printf(MSG_DEBUG, "Already associated with the "
2129 "selected network - do nothing");
2130 return;
2131 }
2132
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002133 if (ssid) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002134 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002135 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2136 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07002137 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 wpa_s->disconnected = 0;
2139 wpa_s->reassociate = 1;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002140
2141 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2142 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143
2144 if (ssid)
2145 wpas_notify_network_selected(wpa_s, ssid);
2146}
2147
2148
2149/**
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002150 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2151 * @wpa_s: wpa_supplicant structure for a network interface
2152 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2153 * @pkcs11_module_path: PKCS #11 module path or NULL
2154 * Returns: 0 on success; -1 on failure
2155 *
2156 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2157 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2158 * module path fails the paths will be reset to the default value (NULL).
2159 */
2160int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2161 const char *pkcs11_engine_path,
2162 const char *pkcs11_module_path)
2163{
2164 char *pkcs11_engine_path_copy = NULL;
2165 char *pkcs11_module_path_copy = NULL;
2166
2167 if (pkcs11_engine_path != NULL) {
2168 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2169 if (pkcs11_engine_path_copy == NULL)
2170 return -1;
2171 }
2172 if (pkcs11_module_path != NULL) {
2173 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002174 if (pkcs11_module_path_copy == NULL) {
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002175 os_free(pkcs11_engine_path_copy);
2176 return -1;
2177 }
2178 }
2179
2180 os_free(wpa_s->conf->pkcs11_engine_path);
2181 os_free(wpa_s->conf->pkcs11_module_path);
2182 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2183 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2184
2185 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2186 eapol_sm_deinit(wpa_s->eapol);
2187 wpa_s->eapol = NULL;
2188 if (wpa_supplicant_init_eapol(wpa_s)) {
2189 /* Error -> Reset paths to the default value (NULL) once. */
2190 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2191 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2192 NULL);
2193
2194 return -1;
2195 }
2196 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2197
2198 return 0;
2199}
2200
2201
2202/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2204 * @wpa_s: wpa_supplicant structure for a network interface
2205 * @ap_scan: AP scan mode
2206 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2207 *
2208 */
2209int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2210{
2211
2212 int old_ap_scan;
2213
2214 if (ap_scan < 0 || ap_scan > 2)
2215 return -1;
2216
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002217#ifdef ANDROID
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2219 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2220 wpa_s->wpa_state < WPA_COMPLETED) {
2221 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2222 "associating", wpa_s->conf->ap_scan, ap_scan);
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002223 return 0;
2224 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002225#endif /* ANDROID */
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 old_ap_scan = wpa_s->conf->ap_scan;
2228 wpa_s->conf->ap_scan = ap_scan;
2229
2230 if (old_ap_scan != wpa_s->conf->ap_scan)
2231 wpas_notify_ap_scan_changed(wpa_s);
2232
2233 return 0;
2234}
2235
2236
2237/**
2238 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2239 * @wpa_s: wpa_supplicant structure for a network interface
2240 * @expire_age: Expiration age in seconds
2241 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2242 *
2243 */
2244int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2245 unsigned int bss_expire_age)
2246{
2247 if (bss_expire_age < 10) {
2248 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2249 bss_expire_age);
2250 return -1;
2251 }
2252 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2253 bss_expire_age);
2254 wpa_s->conf->bss_expiration_age = bss_expire_age;
2255
2256 return 0;
2257}
2258
2259
2260/**
2261 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2262 * @wpa_s: wpa_supplicant structure for a network interface
2263 * @expire_count: number of scans after which an unseen BSS is reclaimed
2264 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2265 *
2266 */
2267int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2268 unsigned int bss_expire_count)
2269{
2270 if (bss_expire_count < 1) {
2271 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2272 bss_expire_count);
2273 return -1;
2274 }
2275 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2276 bss_expire_count);
2277 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2278
2279 return 0;
2280}
2281
2282
2283/**
Dmitry Shmidt04949592012-07-19 12:16:46 -07002284 * wpa_supplicant_set_scan_interval - Set scan interval
2285 * @wpa_s: wpa_supplicant structure for a network interface
2286 * @scan_interval: scan interval in seconds
2287 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2288 *
2289 */
2290int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2291 int scan_interval)
2292{
2293 if (scan_interval < 0) {
2294 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2295 scan_interval);
2296 return -1;
2297 }
2298 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2299 scan_interval);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002300 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002301
2302 return 0;
2303}
2304
2305
2306/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307 * wpa_supplicant_set_debug_params - Set global debug params
2308 * @global: wpa_global structure
2309 * @debug_level: debug level
2310 * @debug_timestamp: determines if show timestamp in debug data
2311 * @debug_show_keys: determines if show keys in debug data
2312 * Returns: 0 if succeed or -1 if debug_level has wrong value
2313 */
2314int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2315 int debug_timestamp, int debug_show_keys)
2316{
2317
2318 int old_level, old_timestamp, old_show_keys;
2319
2320 /* check for allowed debuglevels */
2321 if (debug_level != MSG_EXCESSIVE &&
2322 debug_level != MSG_MSGDUMP &&
2323 debug_level != MSG_DEBUG &&
2324 debug_level != MSG_INFO &&
2325 debug_level != MSG_WARNING &&
2326 debug_level != MSG_ERROR)
2327 return -1;
2328
2329 old_level = wpa_debug_level;
2330 old_timestamp = wpa_debug_timestamp;
2331 old_show_keys = wpa_debug_show_keys;
2332
2333 wpa_debug_level = debug_level;
2334 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2335 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2336
2337 if (wpa_debug_level != old_level)
2338 wpas_notify_debug_level_changed(global);
2339 if (wpa_debug_timestamp != old_timestamp)
2340 wpas_notify_debug_timestamp_changed(global);
2341 if (wpa_debug_show_keys != old_show_keys)
2342 wpas_notify_debug_show_keys_changed(global);
2343
2344 return 0;
2345}
2346
2347
2348/**
2349 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2350 * @wpa_s: Pointer to wpa_supplicant data
2351 * Returns: A pointer to the current network structure or %NULL on failure
2352 */
2353struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2354{
2355 struct wpa_ssid *entry;
2356 u8 ssid[MAX_SSID_LEN];
2357 int res;
2358 size_t ssid_len;
2359 u8 bssid[ETH_ALEN];
2360 int wired;
2361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002362 res = wpa_drv_get_ssid(wpa_s, ssid);
2363 if (res < 0) {
2364 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2365 "driver");
2366 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002368 ssid_len = res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002370 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2372 "driver");
2373 return NULL;
2374 }
2375
2376 wired = wpa_s->conf->ap_scan == 0 &&
2377 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
2378
2379 entry = wpa_s->conf->ssid;
2380 while (entry) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002381 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 ((ssid_len == entry->ssid_len &&
2383 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2384 (!entry->bssid_set ||
2385 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2386 return entry;
2387#ifdef CONFIG_WPS
Dmitry Shmidt04949592012-07-19 12:16:46 -07002388 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2390 (entry->ssid == NULL || entry->ssid_len == 0) &&
2391 (!entry->bssid_set ||
2392 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2393 return entry;
2394#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395
Dmitry Shmidt04949592012-07-19 12:16:46 -07002396 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002397 entry->ssid_len == 0 &&
2398 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2399 return entry;
2400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 entry = entry->next;
2402 }
2403
2404 return NULL;
2405}
2406
2407
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408static int select_driver(struct wpa_supplicant *wpa_s, int i)
2409{
2410 struct wpa_global *global = wpa_s->global;
2411
2412 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2413 global->drv_priv[i] = wpa_drivers[i]->global_init();
2414 if (global->drv_priv[i] == NULL) {
2415 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2416 "'%s'", wpa_drivers[i]->name);
2417 return -1;
2418 }
2419 }
2420
2421 wpa_s->driver = wpa_drivers[i];
2422 wpa_s->global_drv_priv = global->drv_priv[i];
2423
2424 return 0;
2425}
2426
2427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2429 const char *name)
2430{
2431 int i;
2432 size_t len;
2433 const char *pos, *driver = name;
2434
2435 if (wpa_s == NULL)
2436 return -1;
2437
2438 if (wpa_drivers[0] == NULL) {
2439 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2440 "wpa_supplicant");
2441 return -1;
2442 }
2443
2444 if (name == NULL) {
2445 /* default to first driver in the list */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002446 return select_driver(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002447 }
2448
2449 do {
2450 pos = os_strchr(driver, ',');
2451 if (pos)
2452 len = pos - driver;
2453 else
2454 len = os_strlen(driver);
2455
2456 for (i = 0; wpa_drivers[i]; i++) {
2457 if (os_strlen(wpa_drivers[i]->name) == len &&
2458 os_strncmp(driver, wpa_drivers[i]->name, len) ==
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002459 0) {
2460 /* First driver that succeeds wins */
2461 if (select_driver(wpa_s, i) == 0)
2462 return 0;
2463 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002464 }
2465
2466 driver = pos + 1;
2467 } while (pos);
2468
2469 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
2470 return -1;
2471}
2472
2473
2474/**
2475 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2476 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2477 * with struct wpa_driver_ops::init()
2478 * @src_addr: Source address of the EAPOL frame
2479 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2480 * @len: Length of the EAPOL data
2481 *
2482 * This function is called for each received EAPOL frame. Most driver
2483 * interfaces rely on more generic OS mechanism for receiving frames through
2484 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2485 * take care of received EAPOL frames and deliver them to the core supplicant
2486 * code by calling this function.
2487 */
2488void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2489 const u8 *buf, size_t len)
2490{
2491 struct wpa_supplicant *wpa_s = ctx;
2492
2493 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
2494 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2495
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002496#ifdef CONFIG_PEERKEY
2497 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2498 wpa_s->current_ssid->peerkey &&
2499 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2500 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2501 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2502 return;
2503 }
2504#endif /* CONFIG_PEERKEY */
2505
Jouni Malinena05074c2012-12-21 21:35:35 +02002506 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2507 (wpa_s->last_eapol_matches_bssid &&
2508#ifdef CONFIG_AP
2509 !wpa_s->ap_iface &&
2510#endif /* CONFIG_AP */
2511 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 /*
2513 * There is possible race condition between receiving the
2514 * association event and the EAPOL frame since they are coming
2515 * through different paths from the driver. In order to avoid
2516 * issues in trying to process the EAPOL frame before receiving
2517 * association information, lets queue it for processing until
Jouni Malinena05074c2012-12-21 21:35:35 +02002518 * the association event is received. This may also be needed in
2519 * driver-based roaming case, so also use src_addr != BSSID as a
2520 * trigger if we have previously confirmed that the
2521 * Authenticator uses BSSID as the src_addr (which is not the
2522 * case with wired IEEE 802.1X).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 */
2524 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
Jouni Malinena05074c2012-12-21 21:35:35 +02002525 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2526 wpa_supplicant_state_txt(wpa_s->wpa_state),
2527 MAC2STR(wpa_s->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 wpabuf_free(wpa_s->pending_eapol_rx);
2529 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2530 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002531 os_get_reltime(&wpa_s->pending_eapol_rx_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2533 ETH_ALEN);
2534 }
2535 return;
2536 }
2537
Jouni Malinena05074c2012-12-21 21:35:35 +02002538 wpa_s->last_eapol_matches_bssid =
2539 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541#ifdef CONFIG_AP
2542 if (wpa_s->ap_iface) {
2543 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2544 return;
2545 }
2546#endif /* CONFIG_AP */
2547
2548 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2549 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2550 "no key management is configured");
2551 return;
2552 }
2553
2554 if (wpa_s->eapol_received == 0 &&
2555 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2556 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2557 wpa_s->wpa_state != WPA_COMPLETED) &&
2558 (wpa_s->current_ssid == NULL ||
2559 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2560 /* Timeout for completing IEEE 802.1X and WPA authentication */
2561 wpa_supplicant_req_auth_timeout(
2562 wpa_s,
2563 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2564 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2565 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2566 70 : 10, 0);
2567 }
2568 wpa_s->eapol_received++;
2569
2570 if (wpa_s->countermeasures) {
2571 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2572 "EAPOL packet");
2573 return;
2574 }
2575
2576#ifdef CONFIG_IBSS_RSN
2577 if (wpa_s->current_ssid &&
2578 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2579 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2580 return;
2581 }
2582#endif /* CONFIG_IBSS_RSN */
2583
2584 /* Source address of the incoming EAPOL frame could be compared to the
2585 * current BSSID. However, it is possible that a centralized
2586 * Authenticator could be using another MAC address than the BSSID of
2587 * an AP, so just allow any address to be used for now. The replies are
2588 * still sent to the current BSSID (if available), though. */
2589
2590 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2591 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2592 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2593 return;
2594 wpa_drv_poll(wpa_s);
2595 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2596 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2597 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2598 /*
2599 * Set portValid = TRUE here since we are going to skip 4-way
2600 * handshake processing which would normally set portValid. We
2601 * need this to allow the EAPOL state machines to be completed
2602 * without going through EAPOL-Key handshake.
2603 */
2604 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2605 }
2606}
2607
2608
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002609int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 if (wpa_s->driver->send_eapol) {
2612 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2613 if (addr)
2614 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002615 } else if ((!wpa_s->p2p_mgmt ||
2616 !(wpa_s->drv_flags &
2617 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
2618 !(wpa_s->drv_flags &
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002620 l2_packet_deinit(wpa_s->l2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2622 wpa_drv_get_mac_addr(wpa_s),
2623 ETH_P_EAPOL,
2624 wpa_supplicant_rx_eapol, wpa_s, 0);
2625 if (wpa_s->l2 == NULL)
2626 return -1;
2627 } else {
2628 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2629 if (addr)
2630 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2631 }
2632
2633 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2634 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2635 return -1;
2636 }
2637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638 return 0;
2639}
2640
2641
Dmitry Shmidt04949592012-07-19 12:16:46 -07002642static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2643 const u8 *buf, size_t len)
2644{
2645 struct wpa_supplicant *wpa_s = ctx;
2646 const struct l2_ethhdr *eth;
2647
2648 if (len < sizeof(*eth))
2649 return;
2650 eth = (const struct l2_ethhdr *) buf;
2651
2652 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2653 !(eth->h_dest[0] & 0x01)) {
2654 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2655 " (bridge - not for this interface - ignore)",
2656 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2657 return;
2658 }
2659
2660 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2661 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2662 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2663 len - sizeof(*eth));
2664}
2665
2666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002667/**
2668 * wpa_supplicant_driver_init - Initialize driver interface parameters
2669 * @wpa_s: Pointer to wpa_supplicant data
2670 * Returns: 0 on success, -1 on failure
2671 *
2672 * This function is called to initialize driver interface parameters.
2673 * wpa_drv_init() must have been called before this function to initialize the
2674 * driver interface.
2675 */
2676int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2677{
2678 static int interface_count = 0;
2679
2680 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2681 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002683 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2684 MAC2STR(wpa_s->own_addr));
2685 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002687 if (wpa_s->bridge_ifname[0]) {
2688 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2689 "interface '%s'", wpa_s->bridge_ifname);
2690 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2691 wpa_s->own_addr,
2692 ETH_P_EAPOL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002693 wpa_supplicant_rx_eapol_bridge,
2694 wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695 if (wpa_s->l2_br == NULL) {
2696 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2697 "connection for the bridge interface '%s'",
2698 wpa_s->bridge_ifname);
2699 return -1;
2700 }
2701 }
2702
2703 wpa_clear_keys(wpa_s, NULL);
2704
2705 /* Make sure that TKIP countermeasures are not left enabled (could
2706 * happen if wpa_supplicant is killed during countermeasures. */
2707 wpa_drv_set_countermeasures(wpa_s, 0);
2708
2709 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2710 wpa_drv_flush_pmkid(wpa_s);
2711
2712 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002713 wpa_s->prev_scan_wildcard = 0;
2714
Dmitry Shmidt04949592012-07-19 12:16:46 -07002715 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08002716 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2717 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2718 interface_count = 0;
2719 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002720 if (!wpa_s->p2p_mgmt &&
Dmitry Shmidt98660862014-03-11 17:26:21 -07002721 wpa_supplicant_delayed_sched_scan(wpa_s,
2722 interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002723 100000))
Dmitry Shmidt98660862014-03-11 17:26:21 -07002724 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002725 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 interface_count++;
2727 } else
2728 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2729
2730 return 0;
2731}
2732
2733
2734static int wpa_supplicant_daemon(const char *pid_file)
2735{
2736 wpa_printf(MSG_DEBUG, "Daemonize..");
2737 return os_daemonize(pid_file);
2738}
2739
2740
2741static struct wpa_supplicant * wpa_supplicant_alloc(void)
2742{
2743 struct wpa_supplicant *wpa_s;
2744
2745 wpa_s = os_zalloc(sizeof(*wpa_s));
2746 if (wpa_s == NULL)
2747 return NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002748 wpa_s->scan_req = INITIAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 wpa_s->scan_interval = 5;
2750 wpa_s->new_connection = 1;
2751 wpa_s->parent = wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002752 wpa_s->sched_scanning = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002753
2754 return wpa_s;
2755}
2756
2757
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002758#ifdef CONFIG_HT_OVERRIDES
2759
2760static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2761 struct ieee80211_ht_capabilities *htcaps,
2762 struct ieee80211_ht_capabilities *htcaps_mask,
2763 const char *ht_mcs)
2764{
2765 /* parse ht_mcs into hex array */
2766 int i;
2767 const char *tmp = ht_mcs;
2768 char *end = NULL;
2769
2770 /* If ht_mcs is null, do not set anything */
2771 if (!ht_mcs)
2772 return 0;
2773
2774 /* This is what we are setting in the kernel */
2775 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2776
2777 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2778
2779 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2780 errno = 0;
2781 long v = strtol(tmp, &end, 16);
2782 if (errno == 0) {
2783 wpa_msg(wpa_s, MSG_DEBUG,
2784 "htcap value[%i]: %ld end: %p tmp: %p",
2785 i, v, end, tmp);
2786 if (end == tmp)
2787 break;
2788
2789 htcaps->supported_mcs_set[i] = v;
2790 tmp = end;
2791 } else {
2792 wpa_msg(wpa_s, MSG_ERROR,
2793 "Failed to parse ht-mcs: %s, error: %s\n",
2794 ht_mcs, strerror(errno));
2795 return -1;
2796 }
2797 }
2798
2799 /*
2800 * If we were able to parse any values, then set mask for the MCS set.
2801 */
2802 if (i) {
2803 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2804 IEEE80211_HT_MCS_MASK_LEN - 1);
2805 /* skip the 3 reserved bits */
2806 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2807 0x1f;
2808 }
2809
2810 return 0;
2811}
2812
2813
2814static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2815 struct ieee80211_ht_capabilities *htcaps,
2816 struct ieee80211_ht_capabilities *htcaps_mask,
2817 int disabled)
2818{
2819 u16 msk;
2820
2821 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2822
2823 if (disabled == -1)
2824 return 0;
2825
2826 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2827 htcaps_mask->ht_capabilities_info |= msk;
2828 if (disabled)
2829 htcaps->ht_capabilities_info &= msk;
2830 else
2831 htcaps->ht_capabilities_info |= msk;
2832
2833 return 0;
2834}
2835
2836
2837static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2838 struct ieee80211_ht_capabilities *htcaps,
2839 struct ieee80211_ht_capabilities *htcaps_mask,
2840 int factor)
2841{
2842 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2843
2844 if (factor == -1)
2845 return 0;
2846
2847 if (factor < 0 || factor > 3) {
2848 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2849 "Must be 0-3 or -1", factor);
2850 return -EINVAL;
2851 }
2852
2853 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2854 htcaps->a_mpdu_params &= ~0x3;
2855 htcaps->a_mpdu_params |= factor & 0x3;
2856
2857 return 0;
2858}
2859
2860
2861static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2862 struct ieee80211_ht_capabilities *htcaps,
2863 struct ieee80211_ht_capabilities *htcaps_mask,
2864 int density)
2865{
2866 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2867
2868 if (density == -1)
2869 return 0;
2870
2871 if (density < 0 || density > 7) {
2872 wpa_msg(wpa_s, MSG_ERROR,
2873 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2874 density);
2875 return -EINVAL;
2876 }
2877
2878 htcaps_mask->a_mpdu_params |= 0x1C;
2879 htcaps->a_mpdu_params &= ~(0x1C);
2880 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2881
2882 return 0;
2883}
2884
2885
2886static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2887 struct ieee80211_ht_capabilities *htcaps,
2888 struct ieee80211_ht_capabilities *htcaps_mask,
2889 int disabled)
2890{
2891 /* Masking these out disables HT40 */
2892 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2893 HT_CAP_INFO_SHORT_GI40MHZ);
2894
2895 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2896
2897 if (disabled)
2898 htcaps->ht_capabilities_info &= ~msk;
2899 else
2900 htcaps->ht_capabilities_info |= msk;
2901
2902 htcaps_mask->ht_capabilities_info |= msk;
2903
2904 return 0;
2905}
2906
2907
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002908static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
2909 struct ieee80211_ht_capabilities *htcaps,
2910 struct ieee80211_ht_capabilities *htcaps_mask,
2911 int disabled)
2912{
2913 /* Masking these out disables SGI */
2914 u16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
2915 HT_CAP_INFO_SHORT_GI40MHZ);
2916
2917 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
2918
2919 if (disabled)
2920 htcaps->ht_capabilities_info &= ~msk;
2921 else
2922 htcaps->ht_capabilities_info |= msk;
2923
2924 htcaps_mask->ht_capabilities_info |= msk;
2925
2926 return 0;
2927}
2928
2929
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002930static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
2931 struct ieee80211_ht_capabilities *htcaps,
2932 struct ieee80211_ht_capabilities *htcaps_mask,
2933 int disabled)
2934{
2935 /* Masking these out disables LDPC */
2936 u16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
2937
2938 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
2939
2940 if (disabled)
2941 htcaps->ht_capabilities_info &= ~msk;
2942 else
2943 htcaps->ht_capabilities_info |= msk;
2944
2945 htcaps_mask->ht_capabilities_info |= msk;
2946
2947 return 0;
2948}
2949
2950
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002951void wpa_supplicant_apply_ht_overrides(
2952 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2953 struct wpa_driver_associate_params *params)
2954{
2955 struct ieee80211_ht_capabilities *htcaps;
2956 struct ieee80211_ht_capabilities *htcaps_mask;
2957
2958 if (!ssid)
2959 return;
2960
2961 params->disable_ht = ssid->disable_ht;
2962 if (!params->htcaps || !params->htcaps_mask)
2963 return;
2964
2965 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2966 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2967 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2968 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2969 ssid->disable_max_amsdu);
2970 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2971 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2972 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002973 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002974 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002975
2976 if (ssid->ht40_intolerant) {
2977 u16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
2978 htcaps->ht_capabilities_info |= bit;
2979 htcaps_mask->ht_capabilities_info |= bit;
2980 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002981}
2982
2983#endif /* CONFIG_HT_OVERRIDES */
2984
2985
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002986#ifdef CONFIG_VHT_OVERRIDES
2987void wpa_supplicant_apply_vht_overrides(
2988 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2989 struct wpa_driver_associate_params *params)
2990{
2991 struct ieee80211_vht_capabilities *vhtcaps;
2992 struct ieee80211_vht_capabilities *vhtcaps_mask;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002993#ifdef CONFIG_HT_OVERRIDES
2994 int max_ampdu;
2995 const u32 max_ampdu_mask = VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX;
2996#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002997
2998 if (!ssid)
2999 return;
3000
3001 params->disable_vht = ssid->disable_vht;
3002
3003 vhtcaps = (void *) params->vhtcaps;
3004 vhtcaps_mask = (void *) params->vhtcaps_mask;
3005
3006 if (!vhtcaps || !vhtcaps_mask)
3007 return;
3008
3009 vhtcaps->vht_capabilities_info = ssid->vht_capa;
3010 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
3011
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003012#ifdef CONFIG_HT_OVERRIDES
3013 /* if max ampdu is <= 3, we have to make the HT cap the same */
3014 if (ssid->vht_capa_mask & max_ampdu_mask) {
3015 max_ampdu = (ssid->vht_capa & max_ampdu_mask) >>
3016 find_first_bit(max_ampdu_mask);
3017
3018 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
3019 wpa_set_ampdu_factor(wpa_s,
3020 (void *) params->htcaps,
3021 (void *) params->htcaps_mask,
3022 max_ampdu);
3023 }
3024#endif /* CONFIG_HT_OVERRIDES */
3025
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003026#define OVERRIDE_MCS(i) \
3027 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
3028 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
3029 3 << 2 * (i - 1); \
3030 vhtcaps->vht_supported_mcs_set.tx_map |= \
3031 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
3032 } \
3033 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
3034 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
3035 3 << 2 * (i - 1); \
3036 vhtcaps->vht_supported_mcs_set.rx_map |= \
3037 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
3038 }
3039
3040 OVERRIDE_MCS(1);
3041 OVERRIDE_MCS(2);
3042 OVERRIDE_MCS(3);
3043 OVERRIDE_MCS(4);
3044 OVERRIDE_MCS(5);
3045 OVERRIDE_MCS(6);
3046 OVERRIDE_MCS(7);
3047 OVERRIDE_MCS(8);
3048}
3049#endif /* CONFIG_VHT_OVERRIDES */
3050
3051
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
3053{
3054#ifdef PCSC_FUNCS
3055 size_t len;
3056
3057 if (!wpa_s->conf->pcsc_reader)
3058 return 0;
3059
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003060 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003061 if (!wpa_s->scard)
3062 return 1;
3063
3064 if (wpa_s->conf->pcsc_pin &&
3065 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
3066 scard_deinit(wpa_s->scard);
3067 wpa_s->scard = NULL;
3068 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
3069 return -1;
3070 }
3071
3072 len = sizeof(wpa_s->imsi) - 1;
3073 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
3074 scard_deinit(wpa_s->scard);
3075 wpa_s->scard = NULL;
3076 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
3077 return -1;
3078 }
3079 wpa_s->imsi[len] = '\0';
3080
3081 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3082
3083 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3084 wpa_s->imsi, wpa_s->mnc_len);
3085
3086 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3087 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3088#endif /* PCSC_FUNCS */
3089
3090 return 0;
3091}
3092
3093
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003094int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3095{
3096 char *val, *pos;
3097
3098 ext_password_deinit(wpa_s->ext_pw);
3099 wpa_s->ext_pw = NULL;
3100 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3101
3102 if (!wpa_s->conf->ext_password_backend)
3103 return 0;
3104
3105 val = os_strdup(wpa_s->conf->ext_password_backend);
3106 if (val == NULL)
3107 return -1;
3108 pos = os_strchr(val, ':');
3109 if (pos)
3110 *pos++ = '\0';
3111
3112 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3113
3114 wpa_s->ext_pw = ext_password_init(val, pos);
3115 os_free(val);
3116 if (wpa_s->ext_pw == NULL) {
3117 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3118 return -1;
3119 }
3120 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3121
3122 return 0;
3123}
3124
3125
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003126static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3127 const char *rn)
3128{
3129 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3130 struct wpa_radio *radio;
3131
3132 while (rn && iface) {
3133 radio = iface->radio;
3134 if (radio && os_strcmp(rn, radio->name) == 0) {
3135 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3136 wpa_s->ifname, rn);
3137 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3138 return radio;
3139 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08003140
3141 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003142 }
3143
3144 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3145 wpa_s->ifname, rn ? rn : "N/A");
3146 radio = os_zalloc(sizeof(*radio));
3147 if (radio == NULL)
3148 return NULL;
3149
3150 if (rn)
3151 os_strlcpy(radio->name, rn, sizeof(radio->name));
3152 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003153 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003154 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3155
3156 return radio;
3157}
3158
3159
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003160static void radio_work_free(struct wpa_radio_work *work)
3161{
3162 if (work->wpa_s->scan_work == work) {
3163 /* This should not really happen. */
3164 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3165 work->type, work, work->started);
3166 work->wpa_s->scan_work = NULL;
3167 }
3168
3169#ifdef CONFIG_P2P
3170 if (work->wpa_s->p2p_scan_work == work) {
3171 /* This should not really happen. */
3172 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3173 work->type, work, work->started);
3174 work->wpa_s->p2p_scan_work = NULL;
3175 }
3176#endif /* CONFIG_P2P */
3177
3178 dl_list_del(&work->list);
3179 os_free(work);
3180}
3181
3182
3183static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3184{
3185 struct wpa_radio *radio = eloop_ctx;
3186 struct wpa_radio_work *work;
3187 struct os_reltime now, diff;
3188 struct wpa_supplicant *wpa_s;
3189
3190 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3191 if (work == NULL)
3192 return;
3193
3194 if (work->started)
3195 return; /* already started and still in progress */
3196
3197 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3198 radio_list);
3199 if (wpa_s && wpa_s->external_scan_running) {
3200 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3201 return;
3202 }
3203
3204 os_get_reltime(&now);
3205 os_reltime_sub(&now, &work->time, &diff);
3206 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3207 work->type, work, diff.sec, diff.usec);
3208 work->started = 1;
3209 work->time = now;
3210 work->cb(work, 0);
3211}
3212
3213
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003214/*
3215 * This function removes both started and pending radio works running on
3216 * the provided interface's radio.
3217 * Prior to the removal of the radio work, its callback (cb) is called with
3218 * deinit set to be 1. Each work's callback is responsible for clearing its
3219 * internal data and restoring to a correct state.
3220 * @wpa_s: wpa_supplicant data
3221 * @type: type of works to be removed
3222 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3223 * this interface's works.
3224 */
3225void radio_remove_works(struct wpa_supplicant *wpa_s,
3226 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003227{
3228 struct wpa_radio_work *work, *tmp;
3229 struct wpa_radio *radio = wpa_s->radio;
3230
3231 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3232 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003233 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003234 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003235
3236 /* skip other ifaces' works */
3237 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003238 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003239
3240 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3241 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003242 work->cb(work, 1);
3243 radio_work_free(work);
3244 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003245
3246 /* in case we removed the started work */
3247 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003248}
3249
3250
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003251static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3252{
3253 struct wpa_radio *radio = wpa_s->radio;
3254
3255 if (!radio)
3256 return;
3257
3258 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3259 wpa_s->ifname, radio->name);
3260 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003261 radio_remove_works(wpa_s, NULL, 0);
3262 wpa_s->radio = NULL;
3263 if (!dl_list_empty(&radio->ifaces))
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003264 return; /* Interfaces remain for this radio */
3265
3266 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003267 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003268 os_free(radio);
3269}
3270
3271
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003272void radio_work_check_next(struct wpa_supplicant *wpa_s)
3273{
3274 struct wpa_radio *radio = wpa_s->radio;
3275
3276 if (dl_list_empty(&radio->work))
3277 return;
3278 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3279 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3280}
3281
3282
3283/**
3284 * radio_add_work - Add a radio work item
3285 * @wpa_s: Pointer to wpa_supplicant data
3286 * @freq: Frequency of the offchannel operation in MHz or 0
3287 * @type: Unique identifier for each type of work
3288 * @next: Force as the next work to be executed
3289 * @cb: Callback function for indicating when radio is available
3290 * @ctx: Context pointer for the work (work->ctx in cb())
3291 * Returns: 0 on success, -1 on failure
3292 *
3293 * This function is used to request time for an operation that requires
3294 * exclusive radio control. Once the radio is available, the registered callback
3295 * function will be called. radio_work_done() must be called once the exclusive
3296 * radio operation has been completed, so that the radio is freed for other
3297 * operations. The special case of deinit=1 is used to free the context data
3298 * during interface removal. That does not allow the callback function to start
3299 * the radio operation, i.e., it must free any resources allocated for the radio
3300 * work and return.
3301 *
3302 * The @freq parameter can be used to indicate a single channel on which the
3303 * offchannel operation will occur. This may allow multiple radio work
3304 * operations to be performed in parallel if they apply for the same channel.
3305 * Setting this to 0 indicates that the work item may use multiple channels or
3306 * requires exclusive control of the radio.
3307 */
3308int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3309 const char *type, int next,
3310 void (*cb)(struct wpa_radio_work *work, int deinit),
3311 void *ctx)
3312{
3313 struct wpa_radio_work *work;
3314 int was_empty;
3315
3316 work = os_zalloc(sizeof(*work));
3317 if (work == NULL)
3318 return -1;
3319 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3320 os_get_reltime(&work->time);
3321 work->freq = freq;
3322 work->type = type;
3323 work->wpa_s = wpa_s;
3324 work->cb = cb;
3325 work->ctx = ctx;
3326
3327 was_empty = dl_list_empty(&wpa_s->radio->work);
3328 if (next)
3329 dl_list_add(&wpa_s->radio->work, &work->list);
3330 else
3331 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3332 if (was_empty) {
3333 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3334 radio_work_check_next(wpa_s);
3335 }
3336
3337 return 0;
3338}
3339
3340
3341/**
3342 * radio_work_done - Indicate that a radio work item has been completed
3343 * @work: Completed work
3344 *
3345 * This function is called once the callback function registered with
3346 * radio_add_work() has completed its work.
3347 */
3348void radio_work_done(struct wpa_radio_work *work)
3349{
3350 struct wpa_supplicant *wpa_s = work->wpa_s;
3351 struct os_reltime now, diff;
3352 unsigned int started = work->started;
3353
3354 os_get_reltime(&now);
3355 os_reltime_sub(&now, &work->time, &diff);
3356 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3357 work->type, work, started ? "done" : "canceled",
3358 diff.sec, diff.usec);
3359 radio_work_free(work);
3360 if (started)
3361 radio_work_check_next(wpa_s);
3362}
3363
3364
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003365int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3366{
3367 struct wpa_radio_work *work;
3368 struct wpa_radio *radio = wpa_s->radio;
3369
3370 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3371 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3372 return 1;
3373 }
3374
3375 return 0;
3376}
3377
3378
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003379static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3380 struct wpa_interface *iface)
3381{
3382 const char *ifname, *driver, *rn;
3383
3384 driver = iface->driver;
3385next_driver:
3386 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3387 return -1;
3388
3389 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3390 if (wpa_s->drv_priv == NULL) {
3391 const char *pos;
3392 pos = driver ? os_strchr(driver, ',') : NULL;
3393 if (pos) {
3394 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3395 "driver interface - try next driver wrapper");
3396 driver = pos + 1;
3397 goto next_driver;
3398 }
3399 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3400 "interface");
3401 return -1;
3402 }
3403 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3404 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3405 "driver_param '%s'", wpa_s->conf->driver_param);
3406 return -1;
3407 }
3408
3409 ifname = wpa_drv_get_ifname(wpa_s);
3410 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3411 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3412 "interface name with '%s'", ifname);
3413 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3414 }
3415
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003416 rn = wpa_driver_get_radio_name(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003417 if (rn && rn[0] == '\0')
3418 rn = NULL;
3419
3420 wpa_s->radio = radio_add_interface(wpa_s, rn);
3421 if (wpa_s->radio == NULL)
3422 return -1;
3423
3424 return 0;
3425}
3426
3427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3429 struct wpa_interface *iface)
3430{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003431 struct wpa_driver_capa capa;
3432
3433 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3434 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3435 iface->confname ? iface->confname : "N/A",
3436 iface->driver ? iface->driver : "default",
3437 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3438 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3439
3440 if (iface->confname) {
3441#ifdef CONFIG_BACKEND_FILE
3442 wpa_s->confname = os_rel2abs_path(iface->confname);
3443 if (wpa_s->confname == NULL) {
3444 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3445 "for configuration file '%s'.",
3446 iface->confname);
3447 return -1;
3448 }
3449 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3450 iface->confname, wpa_s->confname);
3451#else /* CONFIG_BACKEND_FILE */
3452 wpa_s->confname = os_strdup(iface->confname);
3453#endif /* CONFIG_BACKEND_FILE */
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003454 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 if (wpa_s->conf == NULL) {
3456 wpa_printf(MSG_ERROR, "Failed to read or parse "
3457 "configuration '%s'.", wpa_s->confname);
3458 return -1;
3459 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003460 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3461 wpa_config_read(wpa_s->confanother, wpa_s->conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003462
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003463#ifdef CONFIG_P2P
3464 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3465 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3466#endif /* CONFIG_P2P */
3467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003468 /*
3469 * Override ctrl_interface and driver_param if set on command
3470 * line.
3471 */
3472 if (iface->ctrl_interface) {
3473 os_free(wpa_s->conf->ctrl_interface);
3474 wpa_s->conf->ctrl_interface =
3475 os_strdup(iface->ctrl_interface);
3476 }
3477
3478 if (iface->driver_param) {
3479 os_free(wpa_s->conf->driver_param);
3480 wpa_s->conf->driver_param =
3481 os_strdup(iface->driver_param);
3482 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003483
3484 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3485 os_free(wpa_s->conf->ctrl_interface);
3486 wpa_s->conf->ctrl_interface = NULL;
3487 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 } else
3489 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3490 iface->driver_param);
3491
3492 if (wpa_s->conf == NULL) {
3493 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3494 return -1;
3495 }
3496
3497 if (iface->ifname == NULL) {
3498 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3499 return -1;
3500 }
3501 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3502 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3503 iface->ifname);
3504 return -1;
3505 }
3506 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3507
3508 if (iface->bridge_ifname) {
3509 if (os_strlen(iface->bridge_ifname) >=
3510 sizeof(wpa_s->bridge_ifname)) {
3511 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3512 "name '%s'.", iface->bridge_ifname);
3513 return -1;
3514 }
3515 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3516 sizeof(wpa_s->bridge_ifname));
3517 }
3518
3519 /* RSNA Supplicant Key Management - INITIALIZE */
3520 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3521 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3522
3523 /* Initialize driver interface and register driver event handler before
3524 * L2 receive handler so that association events are processed before
3525 * EAPOL-Key packets if both become available for the same select()
3526 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003527 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 return -1;
3529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3531 return -1;
3532
3533 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3534 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3535 NULL);
3536 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3537
3538 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3539 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3540 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
3541 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3542 "dot11RSNAConfigPMKLifetime");
3543 return -1;
3544 }
3545
3546 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3547 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3548 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
3549 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3550 "dot11RSNAConfigPMKReauthThreshold");
3551 return -1;
3552 }
3553
3554 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3555 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3556 wpa_s->conf->dot11RSNAConfigSATimeout)) {
3557 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3558 "dot11RSNAConfigSATimeout");
3559 return -1;
3560 }
3561
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003562 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3563 &wpa_s->hw.num_modes,
3564 &wpa_s->hw.flags);
3565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003567 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568 wpa_s->drv_flags = capa.flags;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003569 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003570 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003572 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3573 wpa_s->sched_scan_supported = capa.sched_scan_supported;
3574 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003575 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
3576 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003577 wpa_s->extended_capa = capa.extended_capa;
3578 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3579 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003580 wpa_s->num_multichan_concurrent =
3581 capa.num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003582 }
3583 if (wpa_s->max_remain_on_chan == 0)
3584 wpa_s->max_remain_on_chan = 1000;
3585
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003586 /*
3587 * Only take p2p_mgmt parameters when P2P Device is supported.
3588 * Doing it here as it determines whether l2_packet_init() will be done
3589 * during wpa_supplicant_driver_init().
3590 */
3591 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3592 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3593 else
3594 iface->p2p_mgmt = 1;
3595
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003596 if (wpa_s->num_multichan_concurrent == 0)
3597 wpa_s->num_multichan_concurrent = 1;
3598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 if (wpa_supplicant_driver_init(wpa_s) < 0)
3600 return -1;
3601
3602#ifdef CONFIG_TDLS
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003603 if ((!iface->p2p_mgmt ||
3604 !(wpa_s->drv_flags &
3605 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3606 wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607 return -1;
3608#endif /* CONFIG_TDLS */
3609
3610 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3611 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
3612 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
3613 return -1;
3614 }
3615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 if (wpas_wps_init(wpa_s))
3617 return -1;
3618
3619 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3620 return -1;
3621 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3622
3623 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3624 if (wpa_s->ctrl_iface == NULL) {
3625 wpa_printf(MSG_ERROR,
3626 "Failed to initialize control interface '%s'.\n"
3627 "You may have another wpa_supplicant process "
3628 "already running or the file was\n"
3629 "left by an unclean termination of wpa_supplicant "
3630 "in which case you will need\n"
3631 "to manually remove this file before starting "
3632 "wpa_supplicant again.\n",
3633 wpa_s->conf->ctrl_interface);
3634 return -1;
3635 }
3636
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003637 wpa_s->gas = gas_query_init(wpa_s);
3638 if (wpa_s->gas == NULL) {
3639 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3640 return -1;
3641 }
3642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643#ifdef CONFIG_P2P
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003644 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
3646 return -1;
3647 }
3648#endif /* CONFIG_P2P */
3649
3650 if (wpa_bss_init(wpa_s) < 0)
3651 return -1;
3652
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003653#ifdef CONFIG_EAP_PROXY
3654{
3655 size_t len;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003656 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3657 &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003658 if (wpa_s->mnc_len > 0) {
3659 wpa_s->imsi[len] = '\0';
3660 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3661 wpa_s->imsi, wpa_s->mnc_len);
3662 } else {
3663 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3664 }
3665}
3666#endif /* CONFIG_EAP_PROXY */
3667
Dmitry Shmidt04949592012-07-19 12:16:46 -07003668 if (pcsc_reader_init(wpa_s) < 0)
3669 return -1;
3670
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003671 if (wpas_init_ext_pw(wpa_s) < 0)
3672 return -1;
3673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 return 0;
3675}
3676
3677
3678static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003679 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680{
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003681 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 if (wpa_s->drv_priv) {
3683 wpa_supplicant_deauthenticate(wpa_s,
3684 WLAN_REASON_DEAUTH_LEAVING);
3685
3686 wpa_drv_set_countermeasures(wpa_s, 0);
3687 wpa_clear_keys(wpa_s, NULL);
3688 }
3689
3690 wpa_supplicant_cleanup(wpa_s);
3691
Dmitry Shmidt04949592012-07-19 12:16:46 -07003692#ifdef CONFIG_P2P
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003693 if (wpa_s == wpa_s->parent)
3694 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003695 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
3696 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
3697 "the management interface is being removed");
3698 wpas_p2p_deinit_global(wpa_s->global);
3699 }
3700#endif /* CONFIG_P2P */
3701
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003702 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003703 radio_remove_interface(wpa_s);
3704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705 if (wpa_s->drv_priv)
3706 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003707
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003708 if (notify)
3709 wpas_notify_iface_removed(wpa_s);
3710
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003711 if (terminate)
3712 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003713
3714 if (wpa_s->ctrl_iface) {
3715 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3716 wpa_s->ctrl_iface = NULL;
3717 }
3718
3719 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003720 wpa_config_free(wpa_s->conf);
3721 wpa_s->conf = NULL;
3722 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003723
3724 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725}
3726
3727
3728/**
3729 * wpa_supplicant_add_iface - Add a new network interface
3730 * @global: Pointer to global data from wpa_supplicant_init()
3731 * @iface: Interface configuration options
3732 * Returns: Pointer to the created interface or %NULL on failure
3733 *
3734 * This function is used to add new network interfaces for %wpa_supplicant.
3735 * This can be called before wpa_supplicant_run() to add interfaces before the
3736 * main event loop has been started. In addition, new interfaces can be added
3737 * dynamically while %wpa_supplicant is already running. This could happen,
3738 * e.g., when a hotplug network adapter is inserted.
3739 */
3740struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3741 struct wpa_interface *iface)
3742{
3743 struct wpa_supplicant *wpa_s;
3744 struct wpa_interface t_iface;
3745 struct wpa_ssid *ssid;
3746
3747 if (global == NULL || iface == NULL)
3748 return NULL;
3749
3750 wpa_s = wpa_supplicant_alloc();
3751 if (wpa_s == NULL)
3752 return NULL;
3753
3754 wpa_s->global = global;
3755
3756 t_iface = *iface;
3757 if (global->params.override_driver) {
3758 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3759 "('%s' -> '%s')",
3760 iface->driver, global->params.override_driver);
3761 t_iface.driver = global->params.override_driver;
3762 }
3763 if (global->params.override_ctrl_interface) {
3764 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3765 "ctrl_interface ('%s' -> '%s')",
3766 iface->ctrl_interface,
3767 global->params.override_ctrl_interface);
3768 t_iface.ctrl_interface =
3769 global->params.override_ctrl_interface;
3770 }
3771 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
3772 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3773 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003774 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 return NULL;
3776 }
3777
3778 /* Notify the control interfaces about new iface */
3779 if (wpas_notify_iface_added(wpa_s)) {
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003780 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003781 return NULL;
3782 }
3783
3784 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3785 wpas_notify_network_added(wpa_s, ssid);
3786
3787 wpa_s->next = global->ifaces;
3788 global->ifaces = wpa_s;
3789
3790 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003791 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003792
3793 return wpa_s;
3794}
3795
3796
3797/**
3798 * wpa_supplicant_remove_iface - Remove a network interface
3799 * @global: Pointer to global data from wpa_supplicant_init()
3800 * @wpa_s: Pointer to the network interface to be removed
3801 * Returns: 0 if interface was removed, -1 if interface was not found
3802 *
3803 * This function can be used to dynamically remove network interfaces from
3804 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3805 * addition, this function is used to remove all remaining interfaces when
3806 * %wpa_supplicant is terminated.
3807 */
3808int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003809 struct wpa_supplicant *wpa_s,
3810 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811{
3812 struct wpa_supplicant *prev;
3813
3814 /* Remove interface from the global list of interfaces */
3815 prev = global->ifaces;
3816 if (prev == wpa_s) {
3817 global->ifaces = wpa_s->next;
3818 } else {
3819 while (prev && prev->next != wpa_s)
3820 prev = prev->next;
3821 if (prev == NULL)
3822 return -1;
3823 prev->next = wpa_s->next;
3824 }
3825
3826 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
3827
3828 if (global->p2p_group_formation == wpa_s)
3829 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003830 if (global->p2p_invite_group == wpa_s)
3831 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003832 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833
3834 return 0;
3835}
3836
3837
3838/**
3839 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3840 * @wpa_s: Pointer to the network interface
3841 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3842 */
3843const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3844{
3845 const char *eapol_method;
3846
3847 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3848 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3849 return "NO-EAP";
3850 }
3851
3852 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3853 if (eapol_method == NULL)
3854 return "UNKNOWN-EAP";
3855
3856 return eapol_method;
3857}
3858
3859
3860/**
3861 * wpa_supplicant_get_iface - Get a new network interface
3862 * @global: Pointer to global data from wpa_supplicant_init()
3863 * @ifname: Interface name
3864 * Returns: Pointer to the interface or %NULL if not found
3865 */
3866struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3867 const char *ifname)
3868{
3869 struct wpa_supplicant *wpa_s;
3870
3871 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3872 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3873 return wpa_s;
3874 }
3875 return NULL;
3876}
3877
3878
3879#ifndef CONFIG_NO_WPA_MSG
3880static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3881{
3882 struct wpa_supplicant *wpa_s = ctx;
3883 if (wpa_s == NULL)
3884 return NULL;
3885 return wpa_s->ifname;
3886}
3887#endif /* CONFIG_NO_WPA_MSG */
3888
3889
3890/**
3891 * wpa_supplicant_init - Initialize %wpa_supplicant
3892 * @params: Parameters for %wpa_supplicant
3893 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3894 *
3895 * This function is used to initialize %wpa_supplicant. After successful
3896 * initialization, the returned data pointer can be used to add and remove
3897 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3898 */
3899struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3900{
3901 struct wpa_global *global;
3902 int ret, i;
3903
3904 if (params == NULL)
3905 return NULL;
3906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003907#ifdef CONFIG_DRIVER_NDIS
3908 {
3909 void driver_ndis_init_ops(void);
3910 driver_ndis_init_ops();
3911 }
3912#endif /* CONFIG_DRIVER_NDIS */
3913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914#ifndef CONFIG_NO_WPA_MSG
3915 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
3916#endif /* CONFIG_NO_WPA_MSG */
3917
3918 wpa_debug_open_file(params->wpa_debug_file_path);
3919 if (params->wpa_debug_syslog)
3920 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07003921 if (params->wpa_debug_tracing) {
3922 ret = wpa_debug_open_linux_tracing();
3923 if (ret) {
3924 wpa_printf(MSG_ERROR,
3925 "Failed to enable trace logging");
3926 return NULL;
3927 }
3928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003929
3930 ret = eap_register_methods();
3931 if (ret) {
3932 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
3933 if (ret == -2)
3934 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
3935 "the same EAP type.");
3936 return NULL;
3937 }
3938
3939 global = os_zalloc(sizeof(*global));
3940 if (global == NULL)
3941 return NULL;
3942 dl_list_init(&global->p2p_srv_bonjour);
3943 dl_list_init(&global->p2p_srv_upnp);
3944 global->params.daemonize = params->daemonize;
3945 global->params.wait_for_monitor = params->wait_for_monitor;
3946 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
3947 if (params->pid_file)
3948 global->params.pid_file = os_strdup(params->pid_file);
3949 if (params->ctrl_interface)
3950 global->params.ctrl_interface =
3951 os_strdup(params->ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003952 if (params->ctrl_interface_group)
3953 global->params.ctrl_interface_group =
3954 os_strdup(params->ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955 if (params->override_driver)
3956 global->params.override_driver =
3957 os_strdup(params->override_driver);
3958 if (params->override_ctrl_interface)
3959 global->params.override_ctrl_interface =
3960 os_strdup(params->override_ctrl_interface);
3961 wpa_debug_level = global->params.wpa_debug_level =
3962 params->wpa_debug_level;
3963 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
3964 params->wpa_debug_show_keys;
3965 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
3966 params->wpa_debug_timestamp;
3967
3968 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
3969
3970 if (eloop_init()) {
3971 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
3972 wpa_supplicant_deinit(global);
3973 return NULL;
3974 }
3975
Jouni Malinen75ecf522011-06-27 15:19:46 -07003976 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003977
3978 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
3979 if (global->ctrl_iface == NULL) {
3980 wpa_supplicant_deinit(global);
3981 return NULL;
3982 }
3983
3984 if (wpas_notify_supplicant_initialized(global)) {
3985 wpa_supplicant_deinit(global);
3986 return NULL;
3987 }
3988
3989 for (i = 0; wpa_drivers[i]; i++)
3990 global->drv_count++;
3991 if (global->drv_count == 0) {
3992 wpa_printf(MSG_ERROR, "No drivers enabled");
3993 wpa_supplicant_deinit(global);
3994 return NULL;
3995 }
3996 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
3997 if (global->drv_priv == NULL) {
3998 wpa_supplicant_deinit(global);
3999 return NULL;
4000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004001
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004002#ifdef CONFIG_WIFI_DISPLAY
4003 if (wifi_display_init(global) < 0) {
4004 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
4005 wpa_supplicant_deinit(global);
4006 return NULL;
4007 }
4008#endif /* CONFIG_WIFI_DISPLAY */
4009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 return global;
4011}
4012
4013
4014/**
4015 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4016 * @global: Pointer to global data from wpa_supplicant_init()
4017 * Returns: 0 after successful event loop run, -1 on failure
4018 *
4019 * This function starts the main event loop and continues running as long as
4020 * there are any remaining events. In most cases, this function is running as
4021 * long as the %wpa_supplicant process in still in use.
4022 */
4023int wpa_supplicant_run(struct wpa_global *global)
4024{
4025 struct wpa_supplicant *wpa_s;
4026
4027 if (global->params.daemonize &&
4028 wpa_supplicant_daemon(global->params.pid_file))
4029 return -1;
4030
4031 if (global->params.wait_for_monitor) {
4032 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4033 if (wpa_s->ctrl_iface)
4034 wpa_supplicant_ctrl_iface_wait(
4035 wpa_s->ctrl_iface);
4036 }
4037
4038 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4039 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
4040
4041 eloop_run();
4042
4043 return 0;
4044}
4045
4046
4047/**
4048 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4049 * @global: Pointer to global data from wpa_supplicant_init()
4050 *
4051 * This function is called to deinitialize %wpa_supplicant and to free all
4052 * allocated resources. Remaining network interfaces will also be removed.
4053 */
4054void wpa_supplicant_deinit(struct wpa_global *global)
4055{
4056 int i;
4057
4058 if (global == NULL)
4059 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004060
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004061#ifdef CONFIG_WIFI_DISPLAY
4062 wifi_display_deinit(global);
4063#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064
4065 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07004066 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067
4068 if (global->ctrl_iface)
4069 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
4070
4071 wpas_notify_supplicant_deinitialized(global);
4072
4073 eap_peer_unregister_methods();
4074#ifdef CONFIG_AP
4075 eap_server_unregister_methods();
4076#endif /* CONFIG_AP */
4077
4078 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
4079 if (!global->drv_priv[i])
4080 continue;
4081 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
4082 }
4083 os_free(global->drv_priv);
4084
4085 random_deinit();
4086
4087 eloop_destroy();
4088
4089 if (global->params.pid_file) {
4090 os_daemonize_terminate(global->params.pid_file);
4091 os_free(global->params.pid_file);
4092 }
4093 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004094 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095 os_free(global->params.override_driver);
4096 os_free(global->params.override_ctrl_interface);
4097
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004098 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004099 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004100 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 os_free(global);
4103 wpa_debug_close_syslog();
4104 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004105 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106}
4107
4108
4109void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4110{
4111 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4112 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4113 char country[3];
4114 country[0] = wpa_s->conf->country[0];
4115 country[1] = wpa_s->conf->country[1];
4116 country[2] = '\0';
4117 if (wpa_drv_set_country(wpa_s, country) < 0) {
4118 wpa_printf(MSG_ERROR, "Failed to set country code "
4119 "'%s'", country);
4120 }
4121 }
4122
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4124 wpas_init_ext_pw(wpa_s);
4125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126#ifdef CONFIG_WPS
4127 wpas_wps_update_config(wpa_s);
4128#endif /* CONFIG_WPS */
4129
4130#ifdef CONFIG_P2P
4131 wpas_p2p_update_config(wpa_s);
4132#endif /* CONFIG_P2P */
4133
4134 wpa_s->conf->changed_parameters = 0;
4135}
4136
4137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138static void add_freq(int *freqs, int *num_freqs, int freq)
4139{
4140 int i;
4141
4142 for (i = 0; i < *num_freqs; i++) {
4143 if (freqs[i] == freq)
4144 return;
4145 }
4146
4147 freqs[*num_freqs] = freq;
4148 (*num_freqs)++;
4149}
4150
4151
4152static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4153{
4154 struct wpa_bss *bss, *cbss;
4155 const int max_freqs = 10;
4156 int *freqs;
4157 int num_freqs = 0;
4158
4159 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4160 if (freqs == NULL)
4161 return NULL;
4162
4163 cbss = wpa_s->current_bss;
4164
4165 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4166 if (bss == cbss)
4167 continue;
4168 if (bss->ssid_len == cbss->ssid_len &&
4169 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4170 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4171 add_freq(freqs, &num_freqs, bss->freq);
4172 if (num_freqs == max_freqs)
4173 break;
4174 }
4175 }
4176
4177 if (num_freqs == 0) {
4178 os_free(freqs);
4179 freqs = NULL;
4180 }
4181
4182 return freqs;
4183}
4184
4185
4186void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4187{
4188 int timeout;
4189 int count;
4190 int *freqs = NULL;
4191
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004192 wpas_connect_work_done(wpa_s);
4193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004195 * Remove possible authentication timeout since the connection failed.
4196 */
4197 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4198
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004199 if (wpa_s->disconnected) {
4200 /*
4201 * There is no point in blacklisting the AP if this event is
4202 * generated based on local request to disconnect.
4203 */
4204 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4205 "indication since interface has been put into "
4206 "disconnected state");
4207 return;
4208 }
4209
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004210 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 * Add the failed BSSID into the blacklist and speed up next scan
4212 * attempt if there could be other APs that could accept association.
4213 * The current blacklist count indicates how many times we have tried
4214 * connecting to this AP and multiple attempts mean that other APs are
4215 * either not available or has already been tried, so that we can start
4216 * increasing the delay here to avoid constant scanning.
4217 */
4218 count = wpa_blacklist_add(wpa_s, bssid);
4219 if (count == 1 && wpa_s->current_bss) {
4220 /*
4221 * This BSS was not in the blacklist before. If there is
4222 * another BSS available for the same ESS, we should try that
4223 * next. Otherwise, we may as well try this one once more
4224 * before allowing other, likely worse, ESSes to be considered.
4225 */
4226 freqs = get_bss_freqs_in_ess(wpa_s);
4227 if (freqs) {
4228 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4229 "has been seen; try it next");
4230 wpa_blacklist_add(wpa_s, bssid);
4231 /*
4232 * On the next scan, go through only the known channels
4233 * used in this ESS based on previous scans to speed up
4234 * common load balancing use case.
4235 */
4236 os_free(wpa_s->next_scan_freqs);
4237 wpa_s->next_scan_freqs = freqs;
4238 }
4239 }
4240
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004241 /*
4242 * Add previous failure count in case the temporary blacklist was
4243 * cleared due to no other BSSes being available.
4244 */
4245 count += wpa_s->extra_blacklist_count;
4246
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004247 if (count > 3 && wpa_s->current_ssid) {
4248 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4249 "consider temporary network disabling");
4250 wpas_auth_failed(wpa_s);
4251 }
4252
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 switch (count) {
4254 case 1:
4255 timeout = 100;
4256 break;
4257 case 2:
4258 timeout = 500;
4259 break;
4260 case 3:
4261 timeout = 1000;
4262 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004263 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004265 break;
4266 default:
4267 timeout = 10000;
4268 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 }
4270
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004271 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4272 "ms", count, timeout);
4273
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 /*
4275 * TODO: if more than one possible AP is available in scan results,
4276 * could try the other ones before requesting a new scan.
4277 */
4278 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4279 1000 * (timeout % 1000));
4280}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004281
4282
4283int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4284{
4285 return wpa_s->conf->ap_scan == 2 ||
4286 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4287}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004288
Dmitry Shmidt04949592012-07-19 12:16:46 -07004289
4290#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4291int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4292 struct wpa_ssid *ssid,
4293 const char *field,
4294 const char *value)
4295{
4296#ifdef IEEE8021X_EAPOL
4297 struct eap_peer_config *eap = &ssid->eap;
4298
4299 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4300 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4301 (const u8 *) value, os_strlen(value));
4302
4303 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4304 case WPA_CTRL_REQ_EAP_IDENTITY:
4305 os_free(eap->identity);
4306 eap->identity = (u8 *) os_strdup(value);
4307 eap->identity_len = os_strlen(value);
4308 eap->pending_req_identity = 0;
4309 if (ssid == wpa_s->current_ssid)
4310 wpa_s->reassociate = 1;
4311 break;
4312 case WPA_CTRL_REQ_EAP_PASSWORD:
4313 os_free(eap->password);
4314 eap->password = (u8 *) os_strdup(value);
4315 eap->password_len = os_strlen(value);
4316 eap->pending_req_password = 0;
4317 if (ssid == wpa_s->current_ssid)
4318 wpa_s->reassociate = 1;
4319 break;
4320 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4321 os_free(eap->new_password);
4322 eap->new_password = (u8 *) os_strdup(value);
4323 eap->new_password_len = os_strlen(value);
4324 eap->pending_req_new_password = 0;
4325 if (ssid == wpa_s->current_ssid)
4326 wpa_s->reassociate = 1;
4327 break;
4328 case WPA_CTRL_REQ_EAP_PIN:
4329 os_free(eap->pin);
4330 eap->pin = os_strdup(value);
4331 eap->pending_req_pin = 0;
4332 if (ssid == wpa_s->current_ssid)
4333 wpa_s->reassociate = 1;
4334 break;
4335 case WPA_CTRL_REQ_EAP_OTP:
4336 os_free(eap->otp);
4337 eap->otp = (u8 *) os_strdup(value);
4338 eap->otp_len = os_strlen(value);
4339 os_free(eap->pending_req_otp);
4340 eap->pending_req_otp = NULL;
4341 eap->pending_req_otp_len = 0;
4342 break;
4343 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4344 os_free(eap->private_key_passwd);
4345 eap->private_key_passwd = (u8 *) os_strdup(value);
4346 eap->pending_req_passphrase = 0;
4347 if (ssid == wpa_s->current_ssid)
4348 wpa_s->reassociate = 1;
4349 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004350 case WPA_CTRL_REQ_SIM:
4351 os_free(eap->external_sim_resp);
4352 eap->external_sim_resp = os_strdup(value);
4353 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004354 default:
4355 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4356 return -1;
4357 }
4358
4359 return 0;
4360#else /* IEEE8021X_EAPOL */
4361 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4362 return -1;
4363#endif /* IEEE8021X_EAPOL */
4364}
4365#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
4366
4367
4368int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4369{
4370 int i;
4371 unsigned int drv_enc;
4372
4373 if (ssid == NULL)
4374 return 1;
4375
4376 if (ssid->disabled)
4377 return 1;
4378
4379 if (wpa_s && wpa_s->drv_capa_known)
4380 drv_enc = wpa_s->drv_enc;
4381 else
4382 drv_enc = (unsigned int) -1;
4383
4384 for (i = 0; i < NUM_WEP_KEYS; i++) {
4385 size_t len = ssid->wep_key_len[i];
4386 if (len == 0)
4387 continue;
4388 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4389 continue;
4390 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4391 continue;
4392 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4393 continue;
4394 return 1; /* invalid WEP key */
4395 }
4396
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004397 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004398 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004399 return 1;
4400
Dmitry Shmidt04949592012-07-19 12:16:46 -07004401 return 0;
4402}
4403
4404
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004405int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004406{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004407 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004408 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004409 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004410 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004411 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004412}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004413
4414
4415void wpas_auth_failed(struct wpa_supplicant *wpa_s)
4416{
4417 struct wpa_ssid *ssid = wpa_s->current_ssid;
4418 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004419 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004420
4421 if (ssid == NULL) {
4422 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4423 "SSID block");
4424 return;
4425 }
4426
4427 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4428 return;
4429
4430 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07004431
4432#ifdef CONFIG_P2P
4433 if (ssid->p2p_group &&
4434 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4435 /*
4436 * Skip the wait time since there is a short timeout on the
4437 * connection to a P2P group.
4438 */
4439 return;
4440 }
4441#endif /* CONFIG_P2P */
4442
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004443 if (ssid->auth_failures > 50)
4444 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004445 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004446 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004447 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004448 dur = 90;
4449 else if (ssid->auth_failures > 3)
4450 dur = 60;
4451 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004452 dur = 30;
4453 else if (ssid->auth_failures > 1)
4454 dur = 20;
4455 else
4456 dur = 10;
4457
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004458 if (ssid->auth_failures > 1 &&
4459 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4460 dur += os_random() % (ssid->auth_failures * 10);
4461
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004462 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004463 if (now.sec + dur <= ssid->disabled_until.sec)
4464 return;
4465
4466 ssid->disabled_until.sec = now.sec + dur;
4467
4468 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
4469 "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
4470 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
4471 ssid->auth_failures, dur);
4472}
4473
4474
4475void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4476 struct wpa_ssid *ssid, int clear_failures)
4477{
4478 if (ssid == NULL)
4479 return;
4480
4481 if (ssid->disabled_until.sec) {
4482 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4483 "id=%d ssid=\"%s\"",
4484 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4485 }
4486 ssid->disabled_until.sec = 0;
4487 ssid->disabled_until.usec = 0;
4488 if (clear_failures)
4489 ssid->auth_failures = 0;
4490}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004491
4492
4493int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4494{
4495 size_t i;
4496
4497 if (wpa_s->disallow_aps_bssid == NULL)
4498 return 0;
4499
4500 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4501 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4502 bssid, ETH_ALEN) == 0)
4503 return 1;
4504 }
4505
4506 return 0;
4507}
4508
4509
4510int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4511 size_t ssid_len)
4512{
4513 size_t i;
4514
4515 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4516 return 0;
4517
4518 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4519 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4520 if (ssid_len == s->ssid_len &&
4521 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4522 return 1;
4523 }
4524
4525 return 0;
4526}
4527
4528
4529/**
4530 * wpas_request_connection - Request a new connection
4531 * @wpa_s: Pointer to the network interface
4532 *
4533 * This function is used to request a new connection to be found. It will mark
4534 * the interface to allow reassociation and request a new scan to find a
4535 * suitable network to connect to.
4536 */
4537void wpas_request_connection(struct wpa_supplicant *wpa_s)
4538{
4539 wpa_s->normal_scans = 0;
4540 wpa_supplicant_reinit_autoscan(wpa_s);
4541 wpa_s->extra_blacklist_count = 0;
4542 wpa_s->disconnected = 0;
4543 wpa_s->reassociate = 1;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004544
4545 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4546 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004547}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004548
4549
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004550void dump_freq_array(struct wpa_supplicant *wpa_s, const char *title,
4551 int *freq_array, unsigned int len)
4552{
4553 unsigned int i;
4554
4555 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4556 len, title);
4557 for (i = 0; i < len; i++)
4558 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", i, freq_array[i]);
4559}
4560
4561
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004562/*
4563 * Find the operating frequencies of any of the virtual interfaces that
4564 * are using the same radio as the current interface.
4565 */
4566int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4567 int *freq_array, unsigned int len)
4568{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004569 struct wpa_supplicant *ifs;
4570 u8 bssid[ETH_ALEN];
4571 int freq;
4572 unsigned int idx = 0, i;
4573
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004574 wpa_dbg(wpa_s, MSG_DEBUG,
4575 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004576 os_memset(freq_array, 0, sizeof(int) * len);
4577
4578 /* First add the frequency of the local interface */
4579 if (wpa_s->current_ssid != NULL && wpa_s->assoc_freq != 0) {
4580 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4581 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)
4582 freq_array[idx++] = wpa_s->current_ssid->frequency;
4583 else if (wpa_drv_get_bssid(wpa_s, bssid) == 0)
4584 freq_array[idx++] = wpa_s->assoc_freq;
4585 }
4586
4587 /* If get_radio_name is not supported, use only the local freq */
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07004588 if (!wpa_driver_get_radio_name(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004589 freq = wpa_drv_shared_freq(wpa_s);
4590 if (freq > 0 && idx < len &&
4591 (idx == 0 || freq_array[0] != freq))
4592 freq_array[idx++] = freq;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004593 dump_freq_array(wpa_s, "No get_radio_name", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004594 return idx;
4595 }
4596
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004597 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4598 radio_list) {
4599 if (wpa_s == ifs)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004600 continue;
4601
4602 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4603 continue;
4604
4605 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4606 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4607 freq = ifs->current_ssid->frequency;
4608 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4609 freq = ifs->assoc_freq;
4610 else
4611 continue;
4612
4613 /* Hold only distinct freqs */
4614 for (i = 0; i < idx; i++)
4615 if (freq_array[i] == freq)
4616 break;
4617
4618 if (i == idx)
4619 freq_array[idx++] = freq;
4620 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004621
4622 dump_freq_array(wpa_s, "completed iteration", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004623 return idx;
4624}