blob: f56b198bde096b645b49de752638d65350bc2729 [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 Shmidtb58836e2014-04-29 14:35:56 -07003126static int wpas_check_wowlan_trigger(const char *start, const char *trigger,
3127 int capa_trigger, u8 *param_trigger)
3128{
3129 if (os_strcmp(start, trigger) != 0)
3130 return 0;
3131 if (!capa_trigger)
3132 return 0;
3133
3134 *param_trigger = 1;
3135 return 1;
3136}
3137
3138
3139int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
3140 struct wpa_driver_capa *capa)
3141{
3142 struct wowlan_triggers triggers;
3143 char *start, *end, *buf;
3144 int last, ret;
3145
3146 if (!wpa_s->conf->wowlan_triggers)
3147 return 0;
3148
3149 buf = os_strdup(wpa_s->conf->wowlan_triggers);
3150 if (buf == NULL)
3151 return -1;
3152
3153 os_memset(&triggers, 0, sizeof(triggers));
3154
3155#define CHECK_TRIGGER(trigger) \
3156 wpas_check_wowlan_trigger(start, #trigger, \
3157 capa->wowlan_triggers.trigger, \
3158 &triggers.trigger)
3159
3160 start = buf;
3161 while (*start != '\0') {
3162 while (isblank(*start))
3163 start++;
3164 if (*start == '\0')
3165 break;
3166 end = start;
3167 while (!isblank(*end) && *end != '\0')
3168 end++;
3169 last = *end == '\0';
3170 *end = '\0';
3171
3172 if (!CHECK_TRIGGER(any) &&
3173 !CHECK_TRIGGER(disconnect) &&
3174 !CHECK_TRIGGER(magic_pkt) &&
3175 !CHECK_TRIGGER(gtk_rekey_failure) &&
3176 !CHECK_TRIGGER(eap_identity_req) &&
3177 !CHECK_TRIGGER(four_way_handshake) &&
3178 !CHECK_TRIGGER(rfkill_release)) {
3179 wpa_printf(MSG_DEBUG,
3180 "Unknown/unsupported wowlan trigger '%s'",
3181 start);
3182 ret = -1;
3183 goto out;
3184 }
3185
3186 if (last)
3187 break;
3188 start = end + 1;
3189 }
3190#undef CHECK_TRIGGER
3191
3192 ret = wpa_drv_wowlan(wpa_s, &triggers);
3193out:
3194 os_free(buf);
3195 return ret;
3196}
3197
3198
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003199static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3200 const char *rn)
3201{
3202 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3203 struct wpa_radio *radio;
3204
3205 while (rn && iface) {
3206 radio = iface->radio;
3207 if (radio && os_strcmp(rn, radio->name) == 0) {
3208 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3209 wpa_s->ifname, rn);
3210 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3211 return radio;
3212 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08003213
3214 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003215 }
3216
3217 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3218 wpa_s->ifname, rn ? rn : "N/A");
3219 radio = os_zalloc(sizeof(*radio));
3220 if (radio == NULL)
3221 return NULL;
3222
3223 if (rn)
3224 os_strlcpy(radio->name, rn, sizeof(radio->name));
3225 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003226 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003227 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3228
3229 return radio;
3230}
3231
3232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003233static void radio_work_free(struct wpa_radio_work *work)
3234{
3235 if (work->wpa_s->scan_work == work) {
3236 /* This should not really happen. */
3237 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3238 work->type, work, work->started);
3239 work->wpa_s->scan_work = NULL;
3240 }
3241
3242#ifdef CONFIG_P2P
3243 if (work->wpa_s->p2p_scan_work == work) {
3244 /* This should not really happen. */
3245 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3246 work->type, work, work->started);
3247 work->wpa_s->p2p_scan_work = NULL;
3248 }
3249#endif /* CONFIG_P2P */
3250
3251 dl_list_del(&work->list);
3252 os_free(work);
3253}
3254
3255
3256static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3257{
3258 struct wpa_radio *radio = eloop_ctx;
3259 struct wpa_radio_work *work;
3260 struct os_reltime now, diff;
3261 struct wpa_supplicant *wpa_s;
3262
3263 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3264 if (work == NULL)
3265 return;
3266
3267 if (work->started)
3268 return; /* already started and still in progress */
3269
3270 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3271 radio_list);
3272 if (wpa_s && wpa_s->external_scan_running) {
3273 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3274 return;
3275 }
3276
3277 os_get_reltime(&now);
3278 os_reltime_sub(&now, &work->time, &diff);
3279 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3280 work->type, work, diff.sec, diff.usec);
3281 work->started = 1;
3282 work->time = now;
3283 work->cb(work, 0);
3284}
3285
3286
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003287/*
3288 * This function removes both started and pending radio works running on
3289 * the provided interface's radio.
3290 * Prior to the removal of the radio work, its callback (cb) is called with
3291 * deinit set to be 1. Each work's callback is responsible for clearing its
3292 * internal data and restoring to a correct state.
3293 * @wpa_s: wpa_supplicant data
3294 * @type: type of works to be removed
3295 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3296 * this interface's works.
3297 */
3298void radio_remove_works(struct wpa_supplicant *wpa_s,
3299 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003300{
3301 struct wpa_radio_work *work, *tmp;
3302 struct wpa_radio *radio = wpa_s->radio;
3303
3304 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3305 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003306 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003307 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003308
3309 /* skip other ifaces' works */
3310 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003311 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003312
3313 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3314 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003315 work->cb(work, 1);
3316 radio_work_free(work);
3317 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003318
3319 /* in case we removed the started work */
3320 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003321}
3322
3323
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003324static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3325{
3326 struct wpa_radio *radio = wpa_s->radio;
3327
3328 if (!radio)
3329 return;
3330
3331 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3332 wpa_s->ifname, radio->name);
3333 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003334 radio_remove_works(wpa_s, NULL, 0);
3335 wpa_s->radio = NULL;
3336 if (!dl_list_empty(&radio->ifaces))
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003337 return; /* Interfaces remain for this radio */
3338
3339 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003340 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003341 os_free(radio);
3342}
3343
3344
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003345void radio_work_check_next(struct wpa_supplicant *wpa_s)
3346{
3347 struct wpa_radio *radio = wpa_s->radio;
3348
3349 if (dl_list_empty(&radio->work))
3350 return;
3351 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3352 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3353}
3354
3355
3356/**
3357 * radio_add_work - Add a radio work item
3358 * @wpa_s: Pointer to wpa_supplicant data
3359 * @freq: Frequency of the offchannel operation in MHz or 0
3360 * @type: Unique identifier for each type of work
3361 * @next: Force as the next work to be executed
3362 * @cb: Callback function for indicating when radio is available
3363 * @ctx: Context pointer for the work (work->ctx in cb())
3364 * Returns: 0 on success, -1 on failure
3365 *
3366 * This function is used to request time for an operation that requires
3367 * exclusive radio control. Once the radio is available, the registered callback
3368 * function will be called. radio_work_done() must be called once the exclusive
3369 * radio operation has been completed, so that the radio is freed for other
3370 * operations. The special case of deinit=1 is used to free the context data
3371 * during interface removal. That does not allow the callback function to start
3372 * the radio operation, i.e., it must free any resources allocated for the radio
3373 * work and return.
3374 *
3375 * The @freq parameter can be used to indicate a single channel on which the
3376 * offchannel operation will occur. This may allow multiple radio work
3377 * operations to be performed in parallel if they apply for the same channel.
3378 * Setting this to 0 indicates that the work item may use multiple channels or
3379 * requires exclusive control of the radio.
3380 */
3381int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3382 const char *type, int next,
3383 void (*cb)(struct wpa_radio_work *work, int deinit),
3384 void *ctx)
3385{
3386 struct wpa_radio_work *work;
3387 int was_empty;
3388
3389 work = os_zalloc(sizeof(*work));
3390 if (work == NULL)
3391 return -1;
3392 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3393 os_get_reltime(&work->time);
3394 work->freq = freq;
3395 work->type = type;
3396 work->wpa_s = wpa_s;
3397 work->cb = cb;
3398 work->ctx = ctx;
3399
3400 was_empty = dl_list_empty(&wpa_s->radio->work);
3401 if (next)
3402 dl_list_add(&wpa_s->radio->work, &work->list);
3403 else
3404 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3405 if (was_empty) {
3406 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3407 radio_work_check_next(wpa_s);
3408 }
3409
3410 return 0;
3411}
3412
3413
3414/**
3415 * radio_work_done - Indicate that a radio work item has been completed
3416 * @work: Completed work
3417 *
3418 * This function is called once the callback function registered with
3419 * radio_add_work() has completed its work.
3420 */
3421void radio_work_done(struct wpa_radio_work *work)
3422{
3423 struct wpa_supplicant *wpa_s = work->wpa_s;
3424 struct os_reltime now, diff;
3425 unsigned int started = work->started;
3426
3427 os_get_reltime(&now);
3428 os_reltime_sub(&now, &work->time, &diff);
3429 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3430 work->type, work, started ? "done" : "canceled",
3431 diff.sec, diff.usec);
3432 radio_work_free(work);
3433 if (started)
3434 radio_work_check_next(wpa_s);
3435}
3436
3437
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003438int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3439{
3440 struct wpa_radio_work *work;
3441 struct wpa_radio *radio = wpa_s->radio;
3442
3443 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3444 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3445 return 1;
3446 }
3447
3448 return 0;
3449}
3450
3451
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003452static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3453 struct wpa_interface *iface)
3454{
3455 const char *ifname, *driver, *rn;
3456
3457 driver = iface->driver;
3458next_driver:
3459 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3460 return -1;
3461
3462 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3463 if (wpa_s->drv_priv == NULL) {
3464 const char *pos;
3465 pos = driver ? os_strchr(driver, ',') : NULL;
3466 if (pos) {
3467 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3468 "driver interface - try next driver wrapper");
3469 driver = pos + 1;
3470 goto next_driver;
3471 }
3472 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3473 "interface");
3474 return -1;
3475 }
3476 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3477 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3478 "driver_param '%s'", wpa_s->conf->driver_param);
3479 return -1;
3480 }
3481
3482 ifname = wpa_drv_get_ifname(wpa_s);
3483 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3484 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3485 "interface name with '%s'", ifname);
3486 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3487 }
3488
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003489 rn = wpa_driver_get_radio_name(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003490 if (rn && rn[0] == '\0')
3491 rn = NULL;
3492
3493 wpa_s->radio = radio_add_interface(wpa_s, rn);
3494 if (wpa_s->radio == NULL)
3495 return -1;
3496
3497 return 0;
3498}
3499
3500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3502 struct wpa_interface *iface)
3503{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504 struct wpa_driver_capa capa;
3505
3506 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3507 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3508 iface->confname ? iface->confname : "N/A",
3509 iface->driver ? iface->driver : "default",
3510 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3511 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3512
3513 if (iface->confname) {
3514#ifdef CONFIG_BACKEND_FILE
3515 wpa_s->confname = os_rel2abs_path(iface->confname);
3516 if (wpa_s->confname == NULL) {
3517 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3518 "for configuration file '%s'.",
3519 iface->confname);
3520 return -1;
3521 }
3522 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3523 iface->confname, wpa_s->confname);
3524#else /* CONFIG_BACKEND_FILE */
3525 wpa_s->confname = os_strdup(iface->confname);
3526#endif /* CONFIG_BACKEND_FILE */
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003527 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 if (wpa_s->conf == NULL) {
3529 wpa_printf(MSG_ERROR, "Failed to read or parse "
3530 "configuration '%s'.", wpa_s->confname);
3531 return -1;
3532 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003533 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3534 wpa_config_read(wpa_s->confanother, wpa_s->conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003535
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003536#ifdef CONFIG_P2P
3537 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3538 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3539#endif /* CONFIG_P2P */
3540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541 /*
3542 * Override ctrl_interface and driver_param if set on command
3543 * line.
3544 */
3545 if (iface->ctrl_interface) {
3546 os_free(wpa_s->conf->ctrl_interface);
3547 wpa_s->conf->ctrl_interface =
3548 os_strdup(iface->ctrl_interface);
3549 }
3550
3551 if (iface->driver_param) {
3552 os_free(wpa_s->conf->driver_param);
3553 wpa_s->conf->driver_param =
3554 os_strdup(iface->driver_param);
3555 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003556
3557 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3558 os_free(wpa_s->conf->ctrl_interface);
3559 wpa_s->conf->ctrl_interface = NULL;
3560 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 } else
3562 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3563 iface->driver_param);
3564
3565 if (wpa_s->conf == NULL) {
3566 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3567 return -1;
3568 }
3569
3570 if (iface->ifname == NULL) {
3571 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3572 return -1;
3573 }
3574 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3575 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3576 iface->ifname);
3577 return -1;
3578 }
3579 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3580
3581 if (iface->bridge_ifname) {
3582 if (os_strlen(iface->bridge_ifname) >=
3583 sizeof(wpa_s->bridge_ifname)) {
3584 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3585 "name '%s'.", iface->bridge_ifname);
3586 return -1;
3587 }
3588 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3589 sizeof(wpa_s->bridge_ifname));
3590 }
3591
3592 /* RSNA Supplicant Key Management - INITIALIZE */
3593 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3594 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3595
3596 /* Initialize driver interface and register driver event handler before
3597 * L2 receive handler so that association events are processed before
3598 * EAPOL-Key packets if both become available for the same select()
3599 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003600 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601 return -1;
3602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3604 return -1;
3605
3606 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3607 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3608 NULL);
3609 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3610
3611 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3612 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3613 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
3614 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3615 "dot11RSNAConfigPMKLifetime");
3616 return -1;
3617 }
3618
3619 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3620 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3621 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
3622 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3623 "dot11RSNAConfigPMKReauthThreshold");
3624 return -1;
3625 }
3626
3627 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3628 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3629 wpa_s->conf->dot11RSNAConfigSATimeout)) {
3630 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3631 "dot11RSNAConfigSATimeout");
3632 return -1;
3633 }
3634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3636 &wpa_s->hw.num_modes,
3637 &wpa_s->hw.flags);
3638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003640 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 wpa_s->drv_flags = capa.flags;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003642 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003643 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003645 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3646 wpa_s->sched_scan_supported = capa.sched_scan_supported;
3647 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
3649 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003650 wpa_s->extended_capa = capa.extended_capa;
3651 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3652 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003653 wpa_s->num_multichan_concurrent =
3654 capa.num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 }
3656 if (wpa_s->max_remain_on_chan == 0)
3657 wpa_s->max_remain_on_chan = 1000;
3658
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003659 /*
3660 * Only take p2p_mgmt parameters when P2P Device is supported.
3661 * Doing it here as it determines whether l2_packet_init() will be done
3662 * during wpa_supplicant_driver_init().
3663 */
3664 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3665 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3666 else
3667 iface->p2p_mgmt = 1;
3668
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003669 if (wpa_s->num_multichan_concurrent == 0)
3670 wpa_s->num_multichan_concurrent = 1;
3671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 if (wpa_supplicant_driver_init(wpa_s) < 0)
3673 return -1;
3674
3675#ifdef CONFIG_TDLS
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003676 if ((!iface->p2p_mgmt ||
3677 !(wpa_s->drv_flags &
3678 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3679 wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 return -1;
3681#endif /* CONFIG_TDLS */
3682
3683 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3684 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
3685 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
3686 return -1;
3687 }
3688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 if (wpas_wps_init(wpa_s))
3690 return -1;
3691
3692 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3693 return -1;
3694 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3695
3696 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3697 if (wpa_s->ctrl_iface == NULL) {
3698 wpa_printf(MSG_ERROR,
3699 "Failed to initialize control interface '%s'.\n"
3700 "You may have another wpa_supplicant process "
3701 "already running or the file was\n"
3702 "left by an unclean termination of wpa_supplicant "
3703 "in which case you will need\n"
3704 "to manually remove this file before starting "
3705 "wpa_supplicant again.\n",
3706 wpa_s->conf->ctrl_interface);
3707 return -1;
3708 }
3709
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003710 wpa_s->gas = gas_query_init(wpa_s);
3711 if (wpa_s->gas == NULL) {
3712 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3713 return -1;
3714 }
3715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003716#ifdef CONFIG_P2P
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003717 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
3719 return -1;
3720 }
3721#endif /* CONFIG_P2P */
3722
3723 if (wpa_bss_init(wpa_s) < 0)
3724 return -1;
3725
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003726 /*
3727 * Set Wake-on-WLAN triggers, if configured.
3728 * Note: We don't restore/remove the triggers on shutdown (it doesn't
3729 * have effect anyway when the interface is down).
3730 */
3731 if (wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
3732 return -1;
3733
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003734#ifdef CONFIG_EAP_PROXY
3735{
3736 size_t len;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003737 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3738 &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003739 if (wpa_s->mnc_len > 0) {
3740 wpa_s->imsi[len] = '\0';
3741 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3742 wpa_s->imsi, wpa_s->mnc_len);
3743 } else {
3744 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3745 }
3746}
3747#endif /* CONFIG_EAP_PROXY */
3748
Dmitry Shmidt04949592012-07-19 12:16:46 -07003749 if (pcsc_reader_init(wpa_s) < 0)
3750 return -1;
3751
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003752 if (wpas_init_ext_pw(wpa_s) < 0)
3753 return -1;
3754
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 return 0;
3756}
3757
3758
3759static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003760 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761{
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003762 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003763 if (wpa_s->drv_priv) {
3764 wpa_supplicant_deauthenticate(wpa_s,
3765 WLAN_REASON_DEAUTH_LEAVING);
3766
3767 wpa_drv_set_countermeasures(wpa_s, 0);
3768 wpa_clear_keys(wpa_s, NULL);
3769 }
3770
3771 wpa_supplicant_cleanup(wpa_s);
3772
Dmitry Shmidt04949592012-07-19 12:16:46 -07003773#ifdef CONFIG_P2P
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003774 if (wpa_s == wpa_s->parent)
3775 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003776 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
3777 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
3778 "the management interface is being removed");
3779 wpas_p2p_deinit_global(wpa_s->global);
3780 }
3781#endif /* CONFIG_P2P */
3782
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003783 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003784 radio_remove_interface(wpa_s);
3785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 if (wpa_s->drv_priv)
3787 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003788
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003789 if (notify)
3790 wpas_notify_iface_removed(wpa_s);
3791
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003792 if (terminate)
3793 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003794
3795 if (wpa_s->ctrl_iface) {
3796 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3797 wpa_s->ctrl_iface = NULL;
3798 }
3799
3800 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003801 wpa_config_free(wpa_s->conf);
3802 wpa_s->conf = NULL;
3803 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003804
3805 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806}
3807
3808
3809/**
3810 * wpa_supplicant_add_iface - Add a new network interface
3811 * @global: Pointer to global data from wpa_supplicant_init()
3812 * @iface: Interface configuration options
3813 * Returns: Pointer to the created interface or %NULL on failure
3814 *
3815 * This function is used to add new network interfaces for %wpa_supplicant.
3816 * This can be called before wpa_supplicant_run() to add interfaces before the
3817 * main event loop has been started. In addition, new interfaces can be added
3818 * dynamically while %wpa_supplicant is already running. This could happen,
3819 * e.g., when a hotplug network adapter is inserted.
3820 */
3821struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3822 struct wpa_interface *iface)
3823{
3824 struct wpa_supplicant *wpa_s;
3825 struct wpa_interface t_iface;
3826 struct wpa_ssid *ssid;
3827
3828 if (global == NULL || iface == NULL)
3829 return NULL;
3830
3831 wpa_s = wpa_supplicant_alloc();
3832 if (wpa_s == NULL)
3833 return NULL;
3834
3835 wpa_s->global = global;
3836
3837 t_iface = *iface;
3838 if (global->params.override_driver) {
3839 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3840 "('%s' -> '%s')",
3841 iface->driver, global->params.override_driver);
3842 t_iface.driver = global->params.override_driver;
3843 }
3844 if (global->params.override_ctrl_interface) {
3845 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3846 "ctrl_interface ('%s' -> '%s')",
3847 iface->ctrl_interface,
3848 global->params.override_ctrl_interface);
3849 t_iface.ctrl_interface =
3850 global->params.override_ctrl_interface;
3851 }
3852 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
3853 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3854 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003855 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 return NULL;
3857 }
3858
3859 /* Notify the control interfaces about new iface */
3860 if (wpas_notify_iface_added(wpa_s)) {
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003861 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 return NULL;
3863 }
3864
3865 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3866 wpas_notify_network_added(wpa_s, ssid);
3867
3868 wpa_s->next = global->ifaces;
3869 global->ifaces = wpa_s;
3870
3871 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003872 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873
3874 return wpa_s;
3875}
3876
3877
3878/**
3879 * wpa_supplicant_remove_iface - Remove a network interface
3880 * @global: Pointer to global data from wpa_supplicant_init()
3881 * @wpa_s: Pointer to the network interface to be removed
3882 * Returns: 0 if interface was removed, -1 if interface was not found
3883 *
3884 * This function can be used to dynamically remove network interfaces from
3885 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3886 * addition, this function is used to remove all remaining interfaces when
3887 * %wpa_supplicant is terminated.
3888 */
3889int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003890 struct wpa_supplicant *wpa_s,
3891 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892{
3893 struct wpa_supplicant *prev;
3894
3895 /* Remove interface from the global list of interfaces */
3896 prev = global->ifaces;
3897 if (prev == wpa_s) {
3898 global->ifaces = wpa_s->next;
3899 } else {
3900 while (prev && prev->next != wpa_s)
3901 prev = prev->next;
3902 if (prev == NULL)
3903 return -1;
3904 prev->next = wpa_s->next;
3905 }
3906
3907 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
3908
3909 if (global->p2p_group_formation == wpa_s)
3910 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003911 if (global->p2p_invite_group == wpa_s)
3912 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003913 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914
3915 return 0;
3916}
3917
3918
3919/**
3920 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3921 * @wpa_s: Pointer to the network interface
3922 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3923 */
3924const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3925{
3926 const char *eapol_method;
3927
3928 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3929 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3930 return "NO-EAP";
3931 }
3932
3933 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3934 if (eapol_method == NULL)
3935 return "UNKNOWN-EAP";
3936
3937 return eapol_method;
3938}
3939
3940
3941/**
3942 * wpa_supplicant_get_iface - Get a new network interface
3943 * @global: Pointer to global data from wpa_supplicant_init()
3944 * @ifname: Interface name
3945 * Returns: Pointer to the interface or %NULL if not found
3946 */
3947struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3948 const char *ifname)
3949{
3950 struct wpa_supplicant *wpa_s;
3951
3952 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3953 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3954 return wpa_s;
3955 }
3956 return NULL;
3957}
3958
3959
3960#ifndef CONFIG_NO_WPA_MSG
3961static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3962{
3963 struct wpa_supplicant *wpa_s = ctx;
3964 if (wpa_s == NULL)
3965 return NULL;
3966 return wpa_s->ifname;
3967}
3968#endif /* CONFIG_NO_WPA_MSG */
3969
3970
3971/**
3972 * wpa_supplicant_init - Initialize %wpa_supplicant
3973 * @params: Parameters for %wpa_supplicant
3974 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3975 *
3976 * This function is used to initialize %wpa_supplicant. After successful
3977 * initialization, the returned data pointer can be used to add and remove
3978 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3979 */
3980struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3981{
3982 struct wpa_global *global;
3983 int ret, i;
3984
3985 if (params == NULL)
3986 return NULL;
3987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003988#ifdef CONFIG_DRIVER_NDIS
3989 {
3990 void driver_ndis_init_ops(void);
3991 driver_ndis_init_ops();
3992 }
3993#endif /* CONFIG_DRIVER_NDIS */
3994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995#ifndef CONFIG_NO_WPA_MSG
3996 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
3997#endif /* CONFIG_NO_WPA_MSG */
3998
3999 wpa_debug_open_file(params->wpa_debug_file_path);
4000 if (params->wpa_debug_syslog)
4001 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004002 if (params->wpa_debug_tracing) {
4003 ret = wpa_debug_open_linux_tracing();
4004 if (ret) {
4005 wpa_printf(MSG_ERROR,
4006 "Failed to enable trace logging");
4007 return NULL;
4008 }
4009 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010
4011 ret = eap_register_methods();
4012 if (ret) {
4013 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
4014 if (ret == -2)
4015 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
4016 "the same EAP type.");
4017 return NULL;
4018 }
4019
4020 global = os_zalloc(sizeof(*global));
4021 if (global == NULL)
4022 return NULL;
4023 dl_list_init(&global->p2p_srv_bonjour);
4024 dl_list_init(&global->p2p_srv_upnp);
4025 global->params.daemonize = params->daemonize;
4026 global->params.wait_for_monitor = params->wait_for_monitor;
4027 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
4028 if (params->pid_file)
4029 global->params.pid_file = os_strdup(params->pid_file);
4030 if (params->ctrl_interface)
4031 global->params.ctrl_interface =
4032 os_strdup(params->ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004033 if (params->ctrl_interface_group)
4034 global->params.ctrl_interface_group =
4035 os_strdup(params->ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004036 if (params->override_driver)
4037 global->params.override_driver =
4038 os_strdup(params->override_driver);
4039 if (params->override_ctrl_interface)
4040 global->params.override_ctrl_interface =
4041 os_strdup(params->override_ctrl_interface);
4042 wpa_debug_level = global->params.wpa_debug_level =
4043 params->wpa_debug_level;
4044 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
4045 params->wpa_debug_show_keys;
4046 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
4047 params->wpa_debug_timestamp;
4048
4049 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
4050
4051 if (eloop_init()) {
4052 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
4053 wpa_supplicant_deinit(global);
4054 return NULL;
4055 }
4056
Jouni Malinen75ecf522011-06-27 15:19:46 -07004057 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058
4059 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
4060 if (global->ctrl_iface == NULL) {
4061 wpa_supplicant_deinit(global);
4062 return NULL;
4063 }
4064
4065 if (wpas_notify_supplicant_initialized(global)) {
4066 wpa_supplicant_deinit(global);
4067 return NULL;
4068 }
4069
4070 for (i = 0; wpa_drivers[i]; i++)
4071 global->drv_count++;
4072 if (global->drv_count == 0) {
4073 wpa_printf(MSG_ERROR, "No drivers enabled");
4074 wpa_supplicant_deinit(global);
4075 return NULL;
4076 }
4077 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
4078 if (global->drv_priv == NULL) {
4079 wpa_supplicant_deinit(global);
4080 return NULL;
4081 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004083#ifdef CONFIG_WIFI_DISPLAY
4084 if (wifi_display_init(global) < 0) {
4085 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
4086 wpa_supplicant_deinit(global);
4087 return NULL;
4088 }
4089#endif /* CONFIG_WIFI_DISPLAY */
4090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091 return global;
4092}
4093
4094
4095/**
4096 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4097 * @global: Pointer to global data from wpa_supplicant_init()
4098 * Returns: 0 after successful event loop run, -1 on failure
4099 *
4100 * This function starts the main event loop and continues running as long as
4101 * there are any remaining events. In most cases, this function is running as
4102 * long as the %wpa_supplicant process in still in use.
4103 */
4104int wpa_supplicant_run(struct wpa_global *global)
4105{
4106 struct wpa_supplicant *wpa_s;
4107
4108 if (global->params.daemonize &&
4109 wpa_supplicant_daemon(global->params.pid_file))
4110 return -1;
4111
4112 if (global->params.wait_for_monitor) {
4113 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4114 if (wpa_s->ctrl_iface)
4115 wpa_supplicant_ctrl_iface_wait(
4116 wpa_s->ctrl_iface);
4117 }
4118
4119 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4120 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
4121
4122 eloop_run();
4123
4124 return 0;
4125}
4126
4127
4128/**
4129 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4130 * @global: Pointer to global data from wpa_supplicant_init()
4131 *
4132 * This function is called to deinitialize %wpa_supplicant and to free all
4133 * allocated resources. Remaining network interfaces will also be removed.
4134 */
4135void wpa_supplicant_deinit(struct wpa_global *global)
4136{
4137 int i;
4138
4139 if (global == NULL)
4140 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004141
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004142#ifdef CONFIG_WIFI_DISPLAY
4143 wifi_display_deinit(global);
4144#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145
4146 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07004147 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148
4149 if (global->ctrl_iface)
4150 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
4151
4152 wpas_notify_supplicant_deinitialized(global);
4153
4154 eap_peer_unregister_methods();
4155#ifdef CONFIG_AP
4156 eap_server_unregister_methods();
4157#endif /* CONFIG_AP */
4158
4159 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
4160 if (!global->drv_priv[i])
4161 continue;
4162 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
4163 }
4164 os_free(global->drv_priv);
4165
4166 random_deinit();
4167
4168 eloop_destroy();
4169
4170 if (global->params.pid_file) {
4171 os_daemonize_terminate(global->params.pid_file);
4172 os_free(global->params.pid_file);
4173 }
4174 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004175 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 os_free(global->params.override_driver);
4177 os_free(global->params.override_ctrl_interface);
4178
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004179 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004180 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004181 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004183 os_free(global);
4184 wpa_debug_close_syslog();
4185 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004186 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187}
4188
4189
4190void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4191{
4192 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4193 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4194 char country[3];
4195 country[0] = wpa_s->conf->country[0];
4196 country[1] = wpa_s->conf->country[1];
4197 country[2] = '\0';
4198 if (wpa_drv_set_country(wpa_s, country) < 0) {
4199 wpa_printf(MSG_ERROR, "Failed to set country code "
4200 "'%s'", country);
4201 }
4202 }
4203
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004204 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4205 wpas_init_ext_pw(wpa_s);
4206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207#ifdef CONFIG_WPS
4208 wpas_wps_update_config(wpa_s);
4209#endif /* CONFIG_WPS */
4210
4211#ifdef CONFIG_P2P
4212 wpas_p2p_update_config(wpa_s);
4213#endif /* CONFIG_P2P */
4214
4215 wpa_s->conf->changed_parameters = 0;
4216}
4217
4218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219static void add_freq(int *freqs, int *num_freqs, int freq)
4220{
4221 int i;
4222
4223 for (i = 0; i < *num_freqs; i++) {
4224 if (freqs[i] == freq)
4225 return;
4226 }
4227
4228 freqs[*num_freqs] = freq;
4229 (*num_freqs)++;
4230}
4231
4232
4233static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4234{
4235 struct wpa_bss *bss, *cbss;
4236 const int max_freqs = 10;
4237 int *freqs;
4238 int num_freqs = 0;
4239
4240 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4241 if (freqs == NULL)
4242 return NULL;
4243
4244 cbss = wpa_s->current_bss;
4245
4246 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4247 if (bss == cbss)
4248 continue;
4249 if (bss->ssid_len == cbss->ssid_len &&
4250 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4251 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4252 add_freq(freqs, &num_freqs, bss->freq);
4253 if (num_freqs == max_freqs)
4254 break;
4255 }
4256 }
4257
4258 if (num_freqs == 0) {
4259 os_free(freqs);
4260 freqs = NULL;
4261 }
4262
4263 return freqs;
4264}
4265
4266
4267void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4268{
4269 int timeout;
4270 int count;
4271 int *freqs = NULL;
4272
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004273 wpas_connect_work_done(wpa_s);
4274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004276 * Remove possible authentication timeout since the connection failed.
4277 */
4278 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4279
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004280 if (wpa_s->disconnected) {
4281 /*
4282 * There is no point in blacklisting the AP if this event is
4283 * generated based on local request to disconnect.
4284 */
4285 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4286 "indication since interface has been put into "
4287 "disconnected state");
4288 return;
4289 }
4290
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004291 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 * Add the failed BSSID into the blacklist and speed up next scan
4293 * attempt if there could be other APs that could accept association.
4294 * The current blacklist count indicates how many times we have tried
4295 * connecting to this AP and multiple attempts mean that other APs are
4296 * either not available or has already been tried, so that we can start
4297 * increasing the delay here to avoid constant scanning.
4298 */
4299 count = wpa_blacklist_add(wpa_s, bssid);
4300 if (count == 1 && wpa_s->current_bss) {
4301 /*
4302 * This BSS was not in the blacklist before. If there is
4303 * another BSS available for the same ESS, we should try that
4304 * next. Otherwise, we may as well try this one once more
4305 * before allowing other, likely worse, ESSes to be considered.
4306 */
4307 freqs = get_bss_freqs_in_ess(wpa_s);
4308 if (freqs) {
4309 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4310 "has been seen; try it next");
4311 wpa_blacklist_add(wpa_s, bssid);
4312 /*
4313 * On the next scan, go through only the known channels
4314 * used in this ESS based on previous scans to speed up
4315 * common load balancing use case.
4316 */
4317 os_free(wpa_s->next_scan_freqs);
4318 wpa_s->next_scan_freqs = freqs;
4319 }
4320 }
4321
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004322 /*
4323 * Add previous failure count in case the temporary blacklist was
4324 * cleared due to no other BSSes being available.
4325 */
4326 count += wpa_s->extra_blacklist_count;
4327
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004328 if (count > 3 && wpa_s->current_ssid) {
4329 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4330 "consider temporary network disabling");
4331 wpas_auth_failed(wpa_s);
4332 }
4333
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004334 switch (count) {
4335 case 1:
4336 timeout = 100;
4337 break;
4338 case 2:
4339 timeout = 500;
4340 break;
4341 case 3:
4342 timeout = 1000;
4343 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004344 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004346 break;
4347 default:
4348 timeout = 10000;
4349 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 }
4351
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004352 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4353 "ms", count, timeout);
4354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 /*
4356 * TODO: if more than one possible AP is available in scan results,
4357 * could try the other ones before requesting a new scan.
4358 */
4359 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4360 1000 * (timeout % 1000));
4361}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362
4363
4364int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4365{
4366 return wpa_s->conf->ap_scan == 2 ||
4367 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4368}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004369
Dmitry Shmidt04949592012-07-19 12:16:46 -07004370
4371#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4372int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4373 struct wpa_ssid *ssid,
4374 const char *field,
4375 const char *value)
4376{
4377#ifdef IEEE8021X_EAPOL
4378 struct eap_peer_config *eap = &ssid->eap;
4379
4380 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4381 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4382 (const u8 *) value, os_strlen(value));
4383
4384 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4385 case WPA_CTRL_REQ_EAP_IDENTITY:
4386 os_free(eap->identity);
4387 eap->identity = (u8 *) os_strdup(value);
4388 eap->identity_len = os_strlen(value);
4389 eap->pending_req_identity = 0;
4390 if (ssid == wpa_s->current_ssid)
4391 wpa_s->reassociate = 1;
4392 break;
4393 case WPA_CTRL_REQ_EAP_PASSWORD:
4394 os_free(eap->password);
4395 eap->password = (u8 *) os_strdup(value);
4396 eap->password_len = os_strlen(value);
4397 eap->pending_req_password = 0;
4398 if (ssid == wpa_s->current_ssid)
4399 wpa_s->reassociate = 1;
4400 break;
4401 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4402 os_free(eap->new_password);
4403 eap->new_password = (u8 *) os_strdup(value);
4404 eap->new_password_len = os_strlen(value);
4405 eap->pending_req_new_password = 0;
4406 if (ssid == wpa_s->current_ssid)
4407 wpa_s->reassociate = 1;
4408 break;
4409 case WPA_CTRL_REQ_EAP_PIN:
4410 os_free(eap->pin);
4411 eap->pin = os_strdup(value);
4412 eap->pending_req_pin = 0;
4413 if (ssid == wpa_s->current_ssid)
4414 wpa_s->reassociate = 1;
4415 break;
4416 case WPA_CTRL_REQ_EAP_OTP:
4417 os_free(eap->otp);
4418 eap->otp = (u8 *) os_strdup(value);
4419 eap->otp_len = os_strlen(value);
4420 os_free(eap->pending_req_otp);
4421 eap->pending_req_otp = NULL;
4422 eap->pending_req_otp_len = 0;
4423 break;
4424 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4425 os_free(eap->private_key_passwd);
4426 eap->private_key_passwd = (u8 *) os_strdup(value);
4427 eap->pending_req_passphrase = 0;
4428 if (ssid == wpa_s->current_ssid)
4429 wpa_s->reassociate = 1;
4430 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004431 case WPA_CTRL_REQ_SIM:
4432 os_free(eap->external_sim_resp);
4433 eap->external_sim_resp = os_strdup(value);
4434 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004435 default:
4436 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4437 return -1;
4438 }
4439
4440 return 0;
4441#else /* IEEE8021X_EAPOL */
4442 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4443 return -1;
4444#endif /* IEEE8021X_EAPOL */
4445}
4446#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
4447
4448
4449int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4450{
4451 int i;
4452 unsigned int drv_enc;
4453
4454 if (ssid == NULL)
4455 return 1;
4456
4457 if (ssid->disabled)
4458 return 1;
4459
4460 if (wpa_s && wpa_s->drv_capa_known)
4461 drv_enc = wpa_s->drv_enc;
4462 else
4463 drv_enc = (unsigned int) -1;
4464
4465 for (i = 0; i < NUM_WEP_KEYS; i++) {
4466 size_t len = ssid->wep_key_len[i];
4467 if (len == 0)
4468 continue;
4469 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4470 continue;
4471 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4472 continue;
4473 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4474 continue;
4475 return 1; /* invalid WEP key */
4476 }
4477
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004478 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004479 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004480 return 1;
4481
Dmitry Shmidt04949592012-07-19 12:16:46 -07004482 return 0;
4483}
4484
4485
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004486int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004487{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004488 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004489 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004490 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004491 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004492 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004493}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004494
4495
4496void wpas_auth_failed(struct wpa_supplicant *wpa_s)
4497{
4498 struct wpa_ssid *ssid = wpa_s->current_ssid;
4499 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004500 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004501
4502 if (ssid == NULL) {
4503 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4504 "SSID block");
4505 return;
4506 }
4507
4508 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4509 return;
4510
4511 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07004512
4513#ifdef CONFIG_P2P
4514 if (ssid->p2p_group &&
4515 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4516 /*
4517 * Skip the wait time since there is a short timeout on the
4518 * connection to a P2P group.
4519 */
4520 return;
4521 }
4522#endif /* CONFIG_P2P */
4523
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004524 if (ssid->auth_failures > 50)
4525 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004526 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004527 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004528 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004529 dur = 90;
4530 else if (ssid->auth_failures > 3)
4531 dur = 60;
4532 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004533 dur = 30;
4534 else if (ssid->auth_failures > 1)
4535 dur = 20;
4536 else
4537 dur = 10;
4538
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004539 if (ssid->auth_failures > 1 &&
4540 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4541 dur += os_random() % (ssid->auth_failures * 10);
4542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004543 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004544 if (now.sec + dur <= ssid->disabled_until.sec)
4545 return;
4546
4547 ssid->disabled_until.sec = now.sec + dur;
4548
4549 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
4550 "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
4551 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
4552 ssid->auth_failures, dur);
4553}
4554
4555
4556void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4557 struct wpa_ssid *ssid, int clear_failures)
4558{
4559 if (ssid == NULL)
4560 return;
4561
4562 if (ssid->disabled_until.sec) {
4563 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4564 "id=%d ssid=\"%s\"",
4565 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4566 }
4567 ssid->disabled_until.sec = 0;
4568 ssid->disabled_until.usec = 0;
4569 if (clear_failures)
4570 ssid->auth_failures = 0;
4571}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004572
4573
4574int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4575{
4576 size_t i;
4577
4578 if (wpa_s->disallow_aps_bssid == NULL)
4579 return 0;
4580
4581 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4582 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4583 bssid, ETH_ALEN) == 0)
4584 return 1;
4585 }
4586
4587 return 0;
4588}
4589
4590
4591int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4592 size_t ssid_len)
4593{
4594 size_t i;
4595
4596 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4597 return 0;
4598
4599 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4600 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4601 if (ssid_len == s->ssid_len &&
4602 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4603 return 1;
4604 }
4605
4606 return 0;
4607}
4608
4609
4610/**
4611 * wpas_request_connection - Request a new connection
4612 * @wpa_s: Pointer to the network interface
4613 *
4614 * This function is used to request a new connection to be found. It will mark
4615 * the interface to allow reassociation and request a new scan to find a
4616 * suitable network to connect to.
4617 */
4618void wpas_request_connection(struct wpa_supplicant *wpa_s)
4619{
4620 wpa_s->normal_scans = 0;
4621 wpa_supplicant_reinit_autoscan(wpa_s);
4622 wpa_s->extra_blacklist_count = 0;
4623 wpa_s->disconnected = 0;
4624 wpa_s->reassociate = 1;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004625
4626 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4627 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004628}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004629
4630
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004631void dump_freq_array(struct wpa_supplicant *wpa_s, const char *title,
4632 int *freq_array, unsigned int len)
4633{
4634 unsigned int i;
4635
4636 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4637 len, title);
4638 for (i = 0; i < len; i++)
4639 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", i, freq_array[i]);
4640}
4641
4642
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004643/*
4644 * Find the operating frequencies of any of the virtual interfaces that
4645 * are using the same radio as the current interface.
4646 */
4647int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4648 int *freq_array, unsigned int len)
4649{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004650 struct wpa_supplicant *ifs;
4651 u8 bssid[ETH_ALEN];
4652 int freq;
4653 unsigned int idx = 0, i;
4654
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004655 wpa_dbg(wpa_s, MSG_DEBUG,
4656 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004657 os_memset(freq_array, 0, sizeof(int) * len);
4658
4659 /* First add the frequency of the local interface */
4660 if (wpa_s->current_ssid != NULL && wpa_s->assoc_freq != 0) {
4661 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4662 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)
4663 freq_array[idx++] = wpa_s->current_ssid->frequency;
4664 else if (wpa_drv_get_bssid(wpa_s, bssid) == 0)
4665 freq_array[idx++] = wpa_s->assoc_freq;
4666 }
4667
4668 /* If get_radio_name is not supported, use only the local freq */
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07004669 if (!wpa_driver_get_radio_name(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004670 freq = wpa_drv_shared_freq(wpa_s);
4671 if (freq > 0 && idx < len &&
4672 (idx == 0 || freq_array[0] != freq))
4673 freq_array[idx++] = freq;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004674 dump_freq_array(wpa_s, "No get_radio_name", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004675 return idx;
4676 }
4677
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004678 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4679 radio_list) {
4680 if (wpa_s == ifs)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004681 continue;
4682
4683 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4684 continue;
4685
4686 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4687 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4688 freq = ifs->current_ssid->frequency;
4689 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4690 freq = ifs->assoc_freq;
4691 else
4692 continue;
4693
4694 /* Hold only distinct freqs */
4695 for (i = 0; i < idx; i++)
4696 if (freq_array[i] == freq)
4697 break;
4698
4699 if (i == idx)
4700 freq_array[idx++] = freq;
4701 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004702
4703 dump_freq_array(wpa_s, "completed iteration", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004704 return idx;
4705}