]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpa_supplicant.c
Add helper function for generating random MAC addresses with same OUI
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant
b1ae396f 3 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 *
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"
d47fa330 16#include "crypto/random.h"
7d232e23 17#include "crypto/sha1.h"
6fc6879b
JM
18#include "eapol_supp/eapol_supp_sm.h"
19#include "eap_peer/eap.h"
ec7b97ab 20#include "eap_peer/eap_proxy.h"
3ec97afe 21#include "eap_server/eap_methods.h"
3acb5005 22#include "rsn_supp/wpa.h"
6fc6879b 23#include "eloop.h"
6fc6879b 24#include "config.h"
306ae225 25#include "utils/ext_password.h"
6fc6879b
JM
26#include "l2_packet/l2_packet.h"
27#include "wpa_supplicant_i.h"
2d5b792d 28#include "driver_i.h"
6fc6879b 29#include "ctrl_iface.h"
6fc6879b 30#include "pcsc_funcs.h"
90973fb2 31#include "common/version.h"
3acb5005
JM
32#include "rsn_supp/preauth.h"
33#include "rsn_supp/pmksa_cache.h"
90973fb2 34#include "common/wpa_ctrl.h"
90973fb2 35#include "common/ieee802_11_defs.h"
72044390 36#include "p2p/p2p.h"
6fc6879b
JM
37#include "blacklist.h"
38#include "wpas_glue.h"
116654ce 39#include "wps_supplicant.h"
11ef8d35 40#include "ibss_rsn.h"
c2a04078 41#include "sme.h"
04ea7b79 42#include "gas_query.h"
1f1b62a0 43#include "ap.h"
b22128ef 44#include "p2p_supplicant.h"
9675ce35 45#include "wifi_display.h"
8bac466b 46#include "notify.h"
60b94c98 47#include "bgscan.h"
7c865c68 48#include "autoscan.h"
83922c2d 49#include "bss.h"
9ba9fa07 50#include "scan.h"
24f6497c 51#include "offchannel.h"
cb418324 52#include "hs20_supplicant.h"
e27d20bb 53#include "wnm_sta.h"
dd10abcc 54#include "wpas_kay.h"
6fc6879b
JM
55
56const char *wpa_supplicant_version =
57"wpa_supplicant v" VERSION_STR "\n"
b1ae396f 58"Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
6fc6879b
JM
59
60const char *wpa_supplicant_license =
331f89ff
JM
61"This software may be distributed under the terms of the BSD license.\n"
62"See README for more details.\n"
6fc6879b
JM
63#ifdef EAP_TLS_OPENSSL
64"\nThis product includes software developed by the OpenSSL Project\n"
65"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
66#endif /* EAP_TLS_OPENSSL */
67;
68
69#ifndef CONFIG_NO_STDOUT_DEBUG
70/* Long text divided into parts in order to fit in C89 strings size limits. */
71const char *wpa_supplicant_full_license1 =
331f89ff 72"";
6fc6879b 73const char *wpa_supplicant_full_license2 =
331f89ff 74"This software may be distributed under the terms of the BSD license.\n"
6fc6879b
JM
75"\n"
76"Redistribution and use in source and binary forms, with or without\n"
77"modification, are permitted provided that the following conditions are\n"
78"met:\n"
79"\n";
80const char *wpa_supplicant_full_license3 =
81"1. Redistributions of source code must retain the above copyright\n"
82" notice, this list of conditions and the following disclaimer.\n"
83"\n"
84"2. Redistributions in binary form must reproduce the above copyright\n"
85" notice, this list of conditions and the following disclaimer in the\n"
86" documentation and/or other materials provided with the distribution.\n"
87"\n";
88const char *wpa_supplicant_full_license4 =
89"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
90" names of its contributors may be used to endorse or promote products\n"
91" derived from this software without specific prior written permission.\n"
92"\n"
93"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
94"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
95"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
96"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
97const char *wpa_supplicant_full_license5 =
98"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
99"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
100"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
101"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
102"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
103"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
104"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
105"\n";
106#endif /* CONFIG_NO_STDOUT_DEBUG */
107
6fc6879b 108/* Configure default/group WEP keys for static WEP */
0194fedb 109int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
6fc6879b
JM
110{
111 int i, set = 0;
112
113 for (i = 0; i < NUM_WEP_KEYS; i++) {
114 if (ssid->wep_key_len[i] == 0)
115 continue;
116
117 set = 1;
0382097e 118 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
da64c266 119 i, i == ssid->wep_tx_keyidx, NULL, 0,
6fc6879b
JM
120 ssid->wep_key[i], ssid->wep_key_len[i]);
121 }
122
123 return set;
124}
125
126
6ea1f413
JM
127int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
128 struct wpa_ssid *ssid)
6fc6879b
JM
129{
130 u8 key[32];
131 size_t keylen;
71934751 132 enum wpa_alg alg;
6fc6879b
JM
133 u8 seq[6] = { 0 };
134
135 /* IBSS/WPA-None uses only one key (Group) for both receiving and
136 * sending unicast and multicast packets. */
137
d7dcba70 138 if (ssid->mode != WPAS_MODE_IBSS) {
f049052b
BG
139 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
140 "IBSS/ad-hoc) for WPA-None", ssid->mode);
6fc6879b
JM
141 return -1;
142 }
143
144 if (!ssid->psk_set) {
f049052b
BG
145 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
146 "WPA-None");
6fc6879b
JM
147 return -1;
148 }
149
150 switch (wpa_s->group_cipher) {
151 case WPA_CIPHER_CCMP:
152 os_memcpy(key, ssid->psk, 16);
153 keylen = 16;
154 alg = WPA_ALG_CCMP;
155 break;
eb7719ff
JM
156 case WPA_CIPHER_GCMP:
157 os_memcpy(key, ssid->psk, 16);
158 keylen = 16;
159 alg = WPA_ALG_GCMP;
160 break;
6fc6879b
JM
161 case WPA_CIPHER_TKIP:
162 /* WPA-None uses the same Michael MIC key for both TX and RX */
163 os_memcpy(key, ssid->psk, 16 + 8);
164 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
165 keylen = 32;
166 alg = WPA_ALG_TKIP;
167 break;
168 default:
f049052b
BG
169 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
170 "WPA-None", wpa_s->group_cipher);
6fc6879b
JM
171 return -1;
172 }
173
174 /* TODO: should actually remember the previously used seq#, both for TX
175 * and RX from each STA.. */
176
0382097e 177 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
6fc6879b
JM
178}
179
180
181static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
182{
183 struct wpa_supplicant *wpa_s = eloop_ctx;
184 const u8 *bssid = wpa_s->bssid;
a8e16edc 185 if (is_zero_ether_addr(bssid))
6fc6879b
JM
186 bssid = wpa_s->pending_bssid;
187 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
188 MAC2STR(bssid));
189 wpa_blacklist_add(wpa_s, bssid);
190 wpa_sm_notify_disassoc(wpa_s->wpa);
07783eaa 191 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6fc6879b 192 wpa_s->reassociate = 1;
48b84f18
BG
193
194 /*
195 * If we timed out, the AP or the local radio may be busy.
196 * So, wait a second until scanning again.
197 */
198 wpa_supplicant_req_scan(wpa_s, 1, 0);
6fc6879b
JM
199}
200
201
202/**
203 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
204 * @wpa_s: Pointer to wpa_supplicant data
205 * @sec: Number of seconds after which to time out authentication
206 * @usec: Number of microseconds after which to time out authentication
207 *
208 * This function is used to schedule a timeout for the current authentication
209 * attempt.
210 */
211void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
212 int sec, int usec)
213{
a2a535f8 214 if (wpa_s->conf->ap_scan == 0 &&
c2a04078 215 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b
JM
216 return;
217
f049052b 218 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
6fc6879b
JM
219 "%d usec", sec, usec);
220 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
221 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
222}
223
224
225/**
226 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
227 * @wpa_s: Pointer to wpa_supplicant data
228 *
229 * This function is used to cancel authentication timeout scheduled with
230 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
231 * been completed.
232 */
233void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
234{
f049052b 235 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
6fc6879b
JM
236 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
237 wpa_blacklist_del(wpa_s, wpa_s->bssid);
238}
239
240
241/**
242 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
243 * @wpa_s: Pointer to wpa_supplicant data
244 *
245 * This function is used to configure EAPOL state machine based on the selected
246 * authentication mode.
247 */
248void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
249{
250#ifdef IEEE8021X_EAPOL
251 struct eapol_config eapol_conf;
252 struct wpa_ssid *ssid = wpa_s->current_ssid;
253
53895c3b 254#ifdef CONFIG_IBSS_RSN
d7dcba70 255 if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
256 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
257 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
258 /*
259 * RSN IBSS authentication is per-STA and we can disable the
260 * per-BSSID EAPOL authentication.
261 */
262 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
263 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
264 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
265 return;
266 }
267#endif /* CONFIG_IBSS_RSN */
268
0a40ec6a
JM
269 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
270 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271
6fc6879b
JM
272 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
273 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
274 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
275 else
276 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
277
278 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
279 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
280 eapol_conf.accept_802_1x_keys = 1;
281 eapol_conf.required_keys = 0;
282 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
283 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
284 }
285 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
286 eapol_conf.required_keys |=
287 EAPOL_REQUIRE_KEY_BROADCAST;
288 }
289
a2a535f8 290 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
6fc6879b 291 eapol_conf.required_keys = 0;
6fc6879b 292 }
a2a535f8 293 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
6fc6879b 294 eapol_conf.workaround = ssid->eap_workaround;
56586197
JM
295 eapol_conf.eap_disabled =
296 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
ad08c363
JM
297 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
298 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
a5d44ac0 299 eapol_conf.external_sim = wpa_s->conf->external_sim;
b08d5fa7 300 eapol_conf.wps = wpa_s->key_mgmt == WPA_KEY_MGMT_WPS;
6fc6879b
JM
301 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
302#endif /* IEEE8021X_EAPOL */
dd10abcc
HW
303
304 ieee802_1x_alloc_kay_sm(wpa_s, ssid);
6fc6879b
JM
305}
306
307
308/**
309 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
310 * @wpa_s: Pointer to wpa_supplicant data
311 * @ssid: Configuration data for the network
312 *
313 * This function is used to configure WPA state machine and related parameters
314 * to a mode where WPA is not enabled. This is called as part of the
315 * authentication configuration when the selected network does not use WPA.
316 */
317void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
318 struct wpa_ssid *ssid)
319{
320 int i;
321
ad08c363
JM
322 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
323 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
324 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
6fc6879b
JM
325 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
326 else
327 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
328 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
329 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
330 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
331 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
332 wpa_s->group_cipher = WPA_CIPHER_NONE;
333 wpa_s->mgmt_group_cipher = 0;
334
335 for (i = 0; i < NUM_WEP_KEYS; i++) {
336 if (ssid->wep_key_len[i] > 5) {
337 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
338 wpa_s->group_cipher = WPA_CIPHER_WEP104;
339 break;
340 } else if (ssid->wep_key_len[i] > 0) {
341 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
342 wpa_s->group_cipher = WPA_CIPHER_WEP40;
343 break;
344 }
345 }
346
347 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
348 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
350 wpa_s->pairwise_cipher);
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
352#ifdef CONFIG_IEEE80211W
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
354 wpa_s->mgmt_group_cipher);
355#endif /* CONFIG_IEEE80211W */
356
357 pmksa_cache_clear_current(wpa_s->wpa);
358}
359
360
6979582c 361void free_hw_features(struct wpa_supplicant *wpa_s)
6bf731e8
CL
362{
363 int i;
364 if (wpa_s->hw.modes == NULL)
365 return;
366
367 for (i = 0; i < wpa_s->hw.num_modes; i++) {
368 os_free(wpa_s->hw.modes[i].channels);
369 os_free(wpa_s->hw.modes[i].rates);
370 }
371
372 os_free(wpa_s->hw.modes);
373 wpa_s->hw.modes = NULL;
374}
375
376
6fc6879b
JM
377static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
378{
86bd36f0
JM
379 int i;
380
60b94c98 381 bgscan_deinit(wpa_s);
7c865c68 382 autoscan_deinit(wpa_s);
6fc6879b
JM
383 scard_deinit(wpa_s->scard);
384 wpa_s->scard = NULL;
385 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
386 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
387 l2_packet_deinit(wpa_s->l2);
388 wpa_s->l2 = NULL;
389 if (wpa_s->l2_br) {
390 l2_packet_deinit(wpa_s->l2_br);
391 wpa_s->l2_br = NULL;
392 }
393
6fc6879b 394 if (wpa_s->conf != NULL) {
8e56d189
JM
395 struct wpa_ssid *ssid;
396 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
397 wpas_notify_network_removed(wpa_s, ssid);
6fc6879b
JM
398 }
399
400 os_free(wpa_s->confname);
401 wpa_s->confname = NULL;
402
e6304cad
DS
403 os_free(wpa_s->confanother);
404 wpa_s->confanother = NULL;
405
c16a7590
IP
406#ifdef CONFIG_P2P
407 os_free(wpa_s->conf_p2p_dev);
408 wpa_s->conf_p2p_dev = NULL;
409#endif /* CONFIG_P2P */
410
6fc6879b
JM
411 wpa_sm_set_eapol(wpa_s->wpa, NULL);
412 eapol_sm_deinit(wpa_s->eapol);
413 wpa_s->eapol = NULL;
414
415 rsn_preauth_deinit(wpa_s->wpa);
416
281ff0aa
GP
417#ifdef CONFIG_TDLS
418 wpa_tdls_deinit(wpa_s->wpa);
419#endif /* CONFIG_TDLS */
420
6fc6879b
JM
421 pmksa_candidate_free(wpa_s->wpa);
422 wpa_sm_deinit(wpa_s->wpa);
423 wpa_s->wpa = NULL;
424 wpa_blacklist_clear(wpa_s);
425
83922c2d 426 wpa_bss_deinit(wpa_s);
6fc6879b 427
831770bf 428 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
6fc6879b
JM
429 wpa_supplicant_cancel_scan(wpa_s);
430 wpa_supplicant_cancel_auth_timeout(wpa_s);
01a17491
JM
431 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
432#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
433 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
434 wpa_s, NULL);
435#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b 436
116654ce 437 wpas_wps_deinit(wpa_s);
11ef8d35 438
1ff73338
JM
439 wpabuf_free(wpa_s->pending_eapol_rx);
440 wpa_s->pending_eapol_rx = NULL;
441
11ef8d35
JM
442#ifdef CONFIG_IBSS_RSN
443 ibss_rsn_deinit(wpa_s->ibss_rsn);
444 wpa_s->ibss_rsn = NULL;
445#endif /* CONFIG_IBSS_RSN */
c2a04078 446
e29853bb 447 sme_deinit(wpa_s);
2d5b792d
JM
448
449#ifdef CONFIG_AP
450 wpa_supplicant_ap_deinit(wpa_s);
451#endif /* CONFIG_AP */
b22128ef 452
b22128ef 453 wpas_p2p_deinit(wpa_s);
f47d639d 454
24f6497c
JM
455#ifdef CONFIG_OFFCHANNEL
456 offchannel_deinit(wpa_s);
457#endif /* CONFIG_OFFCHANNEL */
458
a4cba8f1
LC
459 wpa_supplicant_cancel_sched_scan(wpa_s);
460
f47d639d
JM
461 os_free(wpa_s->next_scan_freqs);
462 wpa_s->next_scan_freqs = NULL;
fee52342
JM
463
464 os_free(wpa_s->manual_scan_freqs);
465 wpa_s->manual_scan_freqs = NULL;
04ea7b79 466
d3c9c35f
DS
467 os_free(wpa_s->manual_sched_scan_freqs);
468 wpa_s->manual_sched_scan_freqs = NULL;
469
04ea7b79
JM
470 gas_query_deinit(wpa_s->gas);
471 wpa_s->gas = NULL;
6bf731e8
CL
472
473 free_hw_features(wpa_s);
d445a5cd 474
dd10abcc
HW
475 ieee802_1x_dealloc_kay_sm(wpa_s);
476
d445a5cd
JM
477 os_free(wpa_s->bssid_filter);
478 wpa_s->bssid_filter = NULL;
b6668734 479
6407f413
JM
480 os_free(wpa_s->disallow_aps_bssid);
481 wpa_s->disallow_aps_bssid = NULL;
482 os_free(wpa_s->disallow_aps_ssid);
483 wpa_s->disallow_aps_ssid = NULL;
484
b6668734 485 wnm_bss_keep_alive_deinit(wpa_s);
e27d20bb
VK
486#ifdef CONFIG_WNM
487 wnm_deallocate_memory(wpa_s);
488#endif /* CONFIG_WNM */
306ae225
JM
489
490 ext_password_deinit(wpa_s->ext_pw);
491 wpa_s->ext_pw = NULL;
b1f12296
JM
492
493 wpabuf_free(wpa_s->last_gas_resp);
b6a9590b
JM
494 wpa_s->last_gas_resp = NULL;
495 wpabuf_free(wpa_s->prev_gas_resp);
496 wpa_s->prev_gas_resp = NULL;
a297201d
JM
497
498 os_free(wpa_s->last_scan_res);
499 wpa_s->last_scan_res = NULL;
b572df86
JM
500
501#ifdef CONFIG_HS20
fb2ac53d 502 hs20_deinit(wpa_s);
b572df86 503#endif /* CONFIG_HS20 */
86bd36f0
JM
504
505 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
506 wpabuf_free(wpa_s->vendor_elem[i]);
507 wpa_s->vendor_elem[i] = NULL;
508 }
6fc6879b
JM
509}
510
511
512/**
513 * wpa_clear_keys - Clear keys configured for the driver
514 * @wpa_s: Pointer to wpa_supplicant data
515 * @addr: Previously used BSSID or %NULL if not available
516 *
517 * This function clears the encryption keys that has been previously configured
518 * for the driver.
519 */
520void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
521{
2f30cac3 522 int i, max;
6fc6879b 523
0e27f655 524#ifdef CONFIG_IEEE80211W
2f30cac3
JM
525 max = 6;
526#else /* CONFIG_IEEE80211W */
527 max = 4;
0e27f655 528#endif /* CONFIG_IEEE80211W */
2f30cac3
JM
529
530 /* MLME-DELETEKEYS.request */
531 for (i = 0; i < max; i++) {
532 if (wpa_s->keys_cleared & BIT(i))
533 continue;
534 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
535 NULL, 0);
536 }
537 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
538 !is_zero_ether_addr(addr)) {
6fc6879b
JM
539 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
540 0);
541 /* MLME-SETPROTECTION.request(None) */
542 wpa_drv_mlme_setprotection(
543 wpa_s, addr,
544 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
545 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
546 }
2f30cac3 547 wpa_s->keys_cleared = (u32) -1;
6fc6879b
JM
548}
549
550
551/**
552 * wpa_supplicant_state_txt - Get the connection state name as a text string
553 * @state: State (wpa_state; WPA_*)
554 * Returns: The state name as a printable text string
555 */
71934751 556const char * wpa_supplicant_state_txt(enum wpa_states state)
6fc6879b
JM
557{
558 switch (state) {
559 case WPA_DISCONNECTED:
560 return "DISCONNECTED";
561 case WPA_INACTIVE:
562 return "INACTIVE";
8401a6b0
JM
563 case WPA_INTERFACE_DISABLED:
564 return "INTERFACE_DISABLED";
6fc6879b
JM
565 case WPA_SCANNING:
566 return "SCANNING";
c2a04078
JM
567 case WPA_AUTHENTICATING:
568 return "AUTHENTICATING";
6fc6879b
JM
569 case WPA_ASSOCIATING:
570 return "ASSOCIATING";
571 case WPA_ASSOCIATED:
572 return "ASSOCIATED";
573 case WPA_4WAY_HANDSHAKE:
574 return "4WAY_HANDSHAKE";
575 case WPA_GROUP_HANDSHAKE:
576 return "GROUP_HANDSHAKE";
577 case WPA_COMPLETED:
578 return "COMPLETED";
579 default:
580 return "UNKNOWN";
581 }
582}
583
584
cfe53c9a
PS
585#ifdef CONFIG_BGSCAN
586
587static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
588{
31392709
HD
589 const char *name;
590
591 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
592 name = wpa_s->current_ssid->bgscan;
593 else
594 name = wpa_s->conf->bgscan;
268043d5 595 if (name == NULL || name[0] == '\0')
31392709 596 return;
0096c427
JM
597 if (wpas_driver_bss_selection(wpa_s))
598 return;
cfe53c9a
PS
599 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
600 return;
aa109830
DS
601#ifdef CONFIG_P2P
602 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
603 return;
604#endif /* CONFIG_P2P */
cfe53c9a
PS
605
606 bgscan_deinit(wpa_s);
31392709
HD
607 if (wpa_s->current_ssid) {
608 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
cfe53c9a
PS
609 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
610 "bgscan");
611 /*
612 * Live without bgscan; it is only used as a roaming
613 * optimization, so the initial connection is not
614 * affected.
615 */
6409b7a7
YD
616 } else {
617 struct wpa_scan_results *scan_res;
cfe53c9a 618 wpa_s->bgscan_ssid = wpa_s->current_ssid;
6409b7a7
YD
619 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
620 0);
621 if (scan_res) {
622 bgscan_notify_scan(wpa_s, scan_res);
623 wpa_scan_results_free(scan_res);
624 }
625 }
cfe53c9a
PS
626 } else
627 wpa_s->bgscan_ssid = NULL;
628}
629
630
631static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
632{
633 if (wpa_s->bgscan_ssid != NULL) {
634 bgscan_deinit(wpa_s);
635 wpa_s->bgscan_ssid = NULL;
636 }
637}
638
639#endif /* CONFIG_BGSCAN */
640
641
7c865c68
TB
642static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
643{
99218999 644 if (autoscan_init(wpa_s, 0))
7c865c68
TB
645 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
646}
647
648
649static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
650{
651 autoscan_deinit(wpa_s);
652}
653
654
c3d12238
JM
655void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
656{
657 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
658 wpa_s->wpa_state == WPA_SCANNING) {
659 autoscan_deinit(wpa_s);
660 wpa_supplicant_start_autoscan(wpa_s);
661 }
662}
663
664
6fc6879b
JM
665/**
666 * wpa_supplicant_set_state - Set current connection state
667 * @wpa_s: Pointer to wpa_supplicant data
668 * @state: The new connection state
669 *
670 * This function is called whenever the connection state changes, e.g.,
671 * association is completed for WPA/WPA2 4-Way Handshake is started.
672 */
71934751
JM
673void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
674 enum wpa_states state)
6fc6879b 675{
27f43d8d
MH
676 enum wpa_states old_state = wpa_s->wpa_state;
677
f049052b
BG
678 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
679 wpa_supplicant_state_txt(wpa_s->wpa_state),
680 wpa_supplicant_state_txt(state));
6fc6879b 681
5ddd07cb
AS
682 if (state == WPA_INTERFACE_DISABLED) {
683 /* Assure normal scan when interface is restored */
684 wpa_s->normal_scans = 0;
685 }
686
0cf24fda 687 if (state == WPA_COMPLETED) {
6ac4b15e 688 wpas_connect_work_done(wpa_s);
0cf24fda
LC
689 /* Reinitialize normal_scan counter */
690 wpa_s->normal_scans = 0;
691 }
6ac4b15e 692
cb8564b1
DW
693 if (state != WPA_SCANNING)
694 wpa_supplicant_notify_scanning(wpa_s, 0);
695
6fc6879b 696 if (state == WPA_COMPLETED && wpa_s->new_connection) {
6fc6879b 697 struct wpa_ssid *ssid = wpa_s->current_ssid;
7d37a357 698#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
6fc6879b 699 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
1cfc6787
JM
700 MACSTR " completed [id=%d id_str=%s]",
701 MAC2STR(wpa_s->bssid),
6fc6879b
JM
702 ssid ? ssid->id : -1,
703 ssid && ssid->id_str ? ssid->id_str : "");
704#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
00e5e3d5 705 wpas_clear_temp_disabled(wpa_s, ssid, 1);
f1a52633 706 wpa_s->extra_blacklist_count = 0;
6fc6879b 707 wpa_s->new_connection = 0;
6fc6879b 708 wpa_drv_set_operstate(wpa_s, 1);
99ac2913
FF
709#ifndef IEEE8021X_EAPOL
710 wpa_drv_set_supp_port(wpa_s, 1);
711#endif /* IEEE8021X_EAPOL */
17a4734d 712 wpa_s->after_wps = 0;
4d9fb08d 713 wpa_s->known_wps_freq = 0;
b22128ef 714 wpas_p2p_completed(wpa_s);
c3701c66
RM
715
716 sme_sched_obss_scan(wpa_s, 1);
6fc6879b
JM
717 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
718 state == WPA_ASSOCIATED) {
719 wpa_s->new_connection = 1;
720 wpa_drv_set_operstate(wpa_s, 0);
99ac2913
FF
721#ifndef IEEE8021X_EAPOL
722 wpa_drv_set_supp_port(wpa_s, 0);
723#endif /* IEEE8021X_EAPOL */
c3701c66 724 sme_sched_obss_scan(wpa_s, 0);
6fc6879b
JM
725 }
726 wpa_s->wpa_state = state;
27f43d8d 727
cfe53c9a
PS
728#ifdef CONFIG_BGSCAN
729 if (state == WPA_COMPLETED)
730 wpa_supplicant_start_bgscan(wpa_s);
37271232 731 else if (state < WPA_ASSOCIATED)
cfe53c9a
PS
732 wpa_supplicant_stop_bgscan(wpa_s);
733#endif /* CONFIG_BGSCAN */
734
7c865c68
TB
735 if (state == WPA_AUTHENTICATING)
736 wpa_supplicant_stop_autoscan(wpa_s);
737
738 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
739 wpa_supplicant_start_autoscan(wpa_s);
740
5bbf9f10 741 if (wpa_s->wpa_state != old_state) {
27f43d8d 742 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
5bbf9f10 743
e3bd6e9d
IP
744 /*
745 * Notify the P2P Device interface about a state change in one
746 * of the interfaces.
747 */
748 wpas_p2p_indicate_state_change(wpa_s);
e3bd6e9d 749
5bbf9f10
PS
750 if (wpa_s->wpa_state == WPA_COMPLETED ||
751 old_state == WPA_COMPLETED)
752 wpas_notify_auth_changed(wpa_s);
753 }
6fc6879b
JM
754}
755
756
1a1bf008
JM
757void wpa_supplicant_terminate_proc(struct wpa_global *global)
758{
759 int pending = 0;
760#ifdef CONFIG_WPS
761 struct wpa_supplicant *wpa_s = global->ifaces;
762 while (wpa_s) {
ab41595f 763 struct wpa_supplicant *next = wpa_s->next;
5516ed32
EA
764 if (wpas_wps_terminate_pending(wpa_s) == 1)
765 pending = 1;
20625e97
JM
766#ifdef CONFIG_P2P
767 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
768 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
769 wpas_p2p_disconnect(wpa_s);
770#endif /* CONFIG_P2P */
ab41595f 771 wpa_s = next;
1a1bf008
JM
772 }
773#endif /* CONFIG_WPS */
774 if (pending)
775 return;
776 eloop_terminate();
777}
778
779
0456ea16 780static void wpa_supplicant_terminate(int sig, void *signal_ctx)
6fc6879b 781{
0456ea16 782 struct wpa_global *global = signal_ctx;
1a1bf008 783 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
784}
785
786
b22128ef 787void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
6fc6879b 788{
71934751 789 enum wpa_states old_state = wpa_s->wpa_state;
27f43d8d 790
6fc6879b
JM
791 wpa_s->pairwise_cipher = 0;
792 wpa_s->group_cipher = 0;
793 wpa_s->mgmt_group_cipher = 0;
794 wpa_s->key_mgmt = 0;
8401a6b0 795 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
99218999 796 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
27f43d8d
MH
797
798 if (wpa_s->wpa_state != old_state)
799 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
6fc6879b
JM
800}
801
802
803/**
804 * wpa_supplicant_reload_configuration - Reload configuration data
805 * @wpa_s: Pointer to wpa_supplicant data
806 * Returns: 0 on success or -1 if configuration parsing failed
807 *
808 * This function can be used to request that the configuration data is reloaded
809 * (e.g., after configuration file change). This function is reloading
810 * configuration only for one interface, so this may need to be called multiple
811 * times if %wpa_supplicant is controlling multiple interfaces and all
812 * interfaces need reconfiguration.
813 */
814int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
815{
816 struct wpa_config *conf;
817 int reconf_ctrl;
8bac466b
JM
818 int old_ap_scan;
819
6fc6879b
JM
820 if (wpa_s->confname == NULL)
821 return -1;
e6304cad 822 conf = wpa_config_read(wpa_s->confname, NULL);
6fc6879b
JM
823 if (conf == NULL) {
824 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
825 "file '%s' - exiting", wpa_s->confname);
826 return -1;
827 }
e6304cad
DS
828 wpa_config_read(wpa_s->confanother, conf);
829
611aea7d 830 conf->changed_parameters = (unsigned int) -1;
6fc6879b
JM
831
832 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
833 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
834 os_strcmp(conf->ctrl_interface,
835 wpa_s->conf->ctrl_interface) != 0);
836
837 if (reconf_ctrl && wpa_s->ctrl_iface) {
838 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
839 wpa_s->ctrl_iface = NULL;
840 }
841
842 eapol_sm_invalidate_cached_session(wpa_s->eapol);
7b7ce8aa
JM
843 if (wpa_s->current_ssid) {
844 wpa_supplicant_deauthenticate(wpa_s,
845 WLAN_REASON_DEAUTH_LEAVING);
846 }
8bac466b 847
6fc6879b
JM
848 /*
849 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
850 * pkcs11_engine_path, pkcs11_module_path.
851 */
56586197 852 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
853 /*
854 * Clear forced success to clear EAP state for next
855 * authentication.
856 */
857 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
858 }
859 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
860 wpa_sm_set_config(wpa_s->wpa, NULL);
d8a790b9 861 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6fc6879b
JM
862 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
863 rsn_preauth_deinit(wpa_s->wpa);
8bac466b
JM
864
865 old_ap_scan = wpa_s->conf->ap_scan;
6fc6879b
JM
866 wpa_config_free(wpa_s->conf);
867 wpa_s->conf = conf;
8bac466b
JM
868 if (old_ap_scan != wpa_s->conf->ap_scan)
869 wpas_notify_ap_scan_changed(wpa_s);
870
6fc6879b
JM
871 if (reconf_ctrl)
872 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
873
611aea7d
JM
874 wpa_supplicant_update_config(wpa_s);
875
6fc6879b 876 wpa_supplicant_clear_status(wpa_s);
349493bd 877 if (wpa_supplicant_enabled_networks(wpa_s)) {
43a38635
JM
878 wpa_s->reassociate = 1;
879 wpa_supplicant_req_scan(wpa_s, 0, 0);
880 }
f049052b 881 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
6fc6879b
JM
882 return 0;
883}
884
885
0456ea16 886static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
6fc6879b 887{
0456ea16 888 struct wpa_global *global = signal_ctx;
6fc6879b 889 struct wpa_supplicant *wpa_s;
6fc6879b 890 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
f049052b
BG
891 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
892 sig);
6fc6879b 893 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1a1bf008 894 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
895 }
896 }
897}
898
899
6fc6879b
JM
900static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
901 struct wpa_ssid *ssid,
902 struct wpa_ie_data *ie)
903{
904 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
905 if (ret) {
906 if (ret == -2) {
907 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
908 "from association info");
909 }
910 return -1;
911 }
912
f049052b
BG
913 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
914 "cipher suites");
6fc6879b
JM
915 if (!(ie->group_cipher & ssid->group_cipher)) {
916 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
917 "cipher 0x%x (mask 0x%x) - reject",
918 ie->group_cipher, ssid->group_cipher);
919 return -1;
920 }
921 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
922 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
923 "cipher 0x%x (mask 0x%x) - reject",
924 ie->pairwise_cipher, ssid->pairwise_cipher);
925 return -1;
926 }
927 if (!(ie->key_mgmt & ssid->key_mgmt)) {
928 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
929 "management 0x%x (mask 0x%x) - reject",
930 ie->key_mgmt, ssid->key_mgmt);
931 return -1;
932 }
933
934#ifdef CONFIG_IEEE80211W
0b60b0aa 935 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
62d49803
JM
936 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
937 wpa_s->conf->pmf : ssid->ieee80211w) ==
938 MGMT_FRAME_PROTECTION_REQUIRED) {
6fc6879b
JM
939 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
940 "that does not support management frame protection - "
941 "reject");
942 return -1;
943 }
944#endif /* CONFIG_IEEE80211W */
945
946 return 0;
947}
948
949
950/**
951 * wpa_supplicant_set_suites - Set authentication and encryption parameters
952 * @wpa_s: Pointer to wpa_supplicant data
953 * @bss: Scan results for the selected BSS, or %NULL if not available
954 * @ssid: Configuration data for the selected network
955 * @wpa_ie: Buffer for the WPA/RSN IE
956 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
957 * used buffer length in case the functions returns success.
958 * Returns: 0 on success or -1 on failure
959 *
960 * This function is used to configure authentication and encryption parameters
961 * based on the network configuration and scan result for the selected BSS (if
962 * available).
963 */
964int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
6fa81a3b 965 struct wpa_bss *bss, struct wpa_ssid *ssid,
6fc6879b
JM
966 u8 *wpa_ie, size_t *wpa_ie_len)
967{
968 struct wpa_ie_data ie;
969 int sel, proto;
df0f01d9 970 const u8 *bss_wpa, *bss_rsn, *bss_osen;
6fc6879b
JM
971
972 if (bss) {
6fa81a3b
JM
973 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
974 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
df0f01d9 975 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
6fc6879b 976 } else
df0f01d9 977 bss_wpa = bss_rsn = bss_osen = NULL;
6fc6879b
JM
978
979 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
980 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
981 (ie.group_cipher & ssid->group_cipher) &&
982 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
983 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 984 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
6fc6879b
JM
985 proto = WPA_PROTO_RSN;
986 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
987 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
988 (ie.group_cipher & ssid->group_cipher) &&
989 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
990 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 991 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
6fc6879b 992 proto = WPA_PROTO_WPA;
df0f01d9
JM
993#ifdef CONFIG_HS20
994 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
995 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
996 /* TODO: parse OSEN element */
137ff332 997 os_memset(&ie, 0, sizeof(ie));
df0f01d9
JM
998 ie.group_cipher = WPA_CIPHER_CCMP;
999 ie.pairwise_cipher = WPA_CIPHER_CCMP;
1000 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
1001 proto = WPA_PROTO_OSEN;
1002#endif /* CONFIG_HS20 */
6fc6879b
JM
1003 } else if (bss) {
1004 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1005 return -1;
1006 } else {
df0f01d9
JM
1007 if (ssid->proto & WPA_PROTO_OSEN)
1008 proto = WPA_PROTO_OSEN;
1009 else if (ssid->proto & WPA_PROTO_RSN)
6fc6879b
JM
1010 proto = WPA_PROTO_RSN;
1011 else
1012 proto = WPA_PROTO_WPA;
1013 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1014 os_memset(&ie, 0, sizeof(ie));
1015 ie.group_cipher = ssid->group_cipher;
1016 ie.pairwise_cipher = ssid->pairwise_cipher;
1017 ie.key_mgmt = ssid->key_mgmt;
1018#ifdef CONFIG_IEEE80211W
1019 ie.mgmt_group_cipher =
70f8cc8e 1020 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
6fc6879b
JM
1021 WPA_CIPHER_AES_128_CMAC : 0;
1022#endif /* CONFIG_IEEE80211W */
f049052b
BG
1023 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1024 "based on configuration");
6fc6879b
JM
1025 } else
1026 proto = ie.proto;
1027 }
1028
f049052b
BG
1029 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1030 "pairwise %d key_mgmt %d proto %d",
1031 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
6fc6879b
JM
1032#ifdef CONFIG_IEEE80211W
1033 if (ssid->ieee80211w) {
f049052b
BG
1034 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1035 ie.mgmt_group_cipher);
6fc6879b
JM
1036 }
1037#endif /* CONFIG_IEEE80211W */
1038
64fa840a 1039 wpa_s->wpa_proto = proto;
6fc6879b
JM
1040 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1041 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
df0f01d9 1042 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
6fc6879b
JM
1043
1044 if (bss || !wpa_s->ap_ies_from_associnfo) {
1045 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1046 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1047 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1048 bss_rsn ? 2 + bss_rsn[1] : 0))
1049 return -1;
1050 }
1051
1052 sel = ie.group_cipher & ssid->group_cipher;
edbd2a19
JM
1053 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1054 if (wpa_s->group_cipher < 0) {
f049052b
BG
1055 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1056 "cipher");
6fc6879b
JM
1057 return -1;
1058 }
edbd2a19
JM
1059 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1060 wpa_cipher_txt(wpa_s->group_cipher));
6fc6879b
JM
1061
1062 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
edbd2a19
JM
1063 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1064 if (wpa_s->pairwise_cipher < 0) {
f049052b
BG
1065 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1066 "cipher");
6fc6879b
JM
1067 return -1;
1068 }
edbd2a19
JM
1069 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1070 wpa_cipher_txt(wpa_s->pairwise_cipher));
6fc6879b
JM
1071
1072 sel = ie.key_mgmt & ssid->key_mgmt;
c10347f2
JM
1073#ifdef CONFIG_SAE
1074 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1075 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1076#endif /* CONFIG_SAE */
6fc6879b
JM
1077 if (0) {
1078#ifdef CONFIG_IEEE80211R
1079 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1080 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
f049052b 1081 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
6fc6879b
JM
1082 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1083 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
f049052b 1084 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
6fc6879b 1085#endif /* CONFIG_IEEE80211R */
c10347f2
JM
1086#ifdef CONFIG_SAE
1087 } else if (sel & WPA_KEY_MGMT_SAE) {
1088 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1089 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1090 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1091 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1092 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1093#endif /* CONFIG_SAE */
56586197
JM
1094#ifdef CONFIG_IEEE80211W
1095 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1096 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
f049052b 1097 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1098 "WPA: using KEY_MGMT 802.1X with SHA256");
1099 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1100 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
f049052b 1101 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1102 "WPA: using KEY_MGMT PSK with SHA256");
1103#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
1104 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1105 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
f049052b 1106 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
6fc6879b
JM
1107 } else if (sel & WPA_KEY_MGMT_PSK) {
1108 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
f049052b 1109 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
6fc6879b
JM
1110 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1111 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
f049052b 1112 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
df0f01d9
JM
1113#ifdef CONFIG_HS20
1114 } else if (sel & WPA_KEY_MGMT_OSEN) {
1115 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1116 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1117#endif /* CONFIG_HS20 */
6fc6879b 1118 } else {
f049052b
BG
1119 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1120 "authenticated key management type");
6fc6879b
JM
1121 return -1;
1122 }
1123
1124 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1125 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1126 wpa_s->pairwise_cipher);
1127 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1128
1129#ifdef CONFIG_IEEE80211W
1130 sel = ie.mgmt_group_cipher;
62d49803
JM
1131 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1132 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
0b60b0aa 1133 !(ie.capabilities & WPA_CAPABILITY_MFPC))
6fc6879b
JM
1134 sel = 0;
1135 if (sel & WPA_CIPHER_AES_128_CMAC) {
1136 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
f049052b 1137 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
6fc6879b 1138 "AES-128-CMAC");
8dd9f9cd
JM
1139 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1140 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1141 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1142 "BIP-GMAC-128");
1143 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1144 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1145 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1146 "BIP-GMAC-256");
1147 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1148 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1149 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1150 "BIP-CMAC-256");
6fc6879b
JM
1151 } else {
1152 wpa_s->mgmt_group_cipher = 0;
f049052b 1153 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
6fc6879b
JM
1154 }
1155 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1156 wpa_s->mgmt_group_cipher);
62d49803
JM
1157 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1158 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1159 wpa_s->conf->pmf : ssid->ieee80211w));
6fc6879b
JM
1160#endif /* CONFIG_IEEE80211W */
1161
1162 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
f049052b 1163 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
6fc6879b
JM
1164 return -1;
1165 }
1166
0bf927a0 1167 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
6fc6879b 1168 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
7d232e23
ZC
1169#ifndef CONFIG_NO_PBKDF2
1170 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1171 ssid->passphrase) {
1172 u8 psk[PMK_LEN];
986de33d
JM
1173 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1174 4096, psk, PMK_LEN);
7d232e23
ZC
1175 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1176 psk, PMK_LEN);
1177 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1178 }
1179#endif /* CONFIG_NO_PBKDF2 */
9173b16f
JM
1180#ifdef CONFIG_EXT_PASSWORD
1181 if (ssid->ext_psk) {
1182 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1183 ssid->ext_psk);
1184 char pw_str[64 + 1];
1185 u8 psk[PMK_LEN];
1186
1187 if (pw == NULL) {
1188 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1189 "found from external storage");
1190 return -1;
1191 }
1192
1193 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1194 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1195 "PSK length %d in external storage",
1196 (int) wpabuf_len(pw));
1197 ext_password_free(pw);
1198 return -1;
1199 }
1200
1201 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1202 pw_str[wpabuf_len(pw)] = '\0';
1203
1204#ifndef CONFIG_NO_PBKDF2
1205 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1206 {
986de33d
JM
1207 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1208 4096, psk, PMK_LEN);
9173b16f
JM
1209 os_memset(pw_str, 0, sizeof(pw_str));
1210 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1211 "external passphrase)",
1212 psk, PMK_LEN);
1213 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1214 } else
1215#endif /* CONFIG_NO_PBKDF2 */
1216 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1217 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1218 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1219 "Invalid PSK hex string");
1220 os_memset(pw_str, 0, sizeof(pw_str));
1221 ext_password_free(pw);
1222 return -1;
1223 }
1224 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1225 } else {
1226 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1227 "PSK available");
1228 os_memset(pw_str, 0, sizeof(pw_str));
1229 ext_password_free(pw);
1230 return -1;
1231 }
1232
1233 os_memset(pw_str, 0, sizeof(pw_str));
1234 ext_password_free(pw);
1235 }
1236#endif /* CONFIG_EXT_PASSWORD */
7d232e23 1237 } else
6fc6879b
JM
1238 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1239
1240 return 0;
1241}
1242
1243
8cd6b7bc 1244static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
03e47c9c 1245{
8cd6b7bc 1246 *pos = 0x00;
03e47c9c 1247
8cd6b7bc
JB
1248 switch (idx) {
1249 case 0: /* Bits 0-7 */
1250 break;
1251 case 1: /* Bits 8-15 */
1252 break;
1253 case 2: /* Bits 16-23 */
1254#ifdef CONFIG_WNM
1255 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1256 *pos |= 0x08; /* Bit 19 - BSS Transition */
1257#endif /* CONFIG_WNM */
1258 break;
1259 case 3: /* Bits 24-31 */
1260#ifdef CONFIG_WNM
1261 *pos |= 0x02; /* Bit 25 - SSID List */
1262#endif /* CONFIG_WNM */
03e47c9c 1263#ifdef CONFIG_INTERWORKING
8cd6b7bc
JB
1264 if (wpa_s->conf->interworking)
1265 *pos |= 0x80; /* Bit 31 - Interworking */
03e47c9c 1266#endif /* CONFIG_INTERWORKING */
8cd6b7bc
JB
1267 break;
1268 case 4: /* Bits 32-39 */
56f5af48 1269#ifdef CONFIG_INTERWORKING
429dd9af
JM
1270 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1271 *pos |= 0x01; /* Bit 32 - QoS Map */
56f5af48 1272#endif /* CONFIG_INTERWORKING */
8cd6b7bc
JB
1273 break;
1274 case 5: /* Bits 40-47 */
95a3ea94
JM
1275#ifdef CONFIG_HS20
1276 if (wpa_s->conf->hs20)
1277 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1278#endif /* CONFIG_HS20 */
8cd6b7bc
JB
1279 break;
1280 case 6: /* Bits 48-55 */
1281 break;
1282 }
1283}
03e47c9c 1284
03e47c9c 1285
0bbaa9b9 1286int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
8cd6b7bc
JB
1287{
1288 u8 *pos = buf;
95a3ea94 1289 u8 len = 6, i;
8cd6b7bc
JB
1290
1291 if (len < wpa_s->extended_capa_len)
1292 len = wpa_s->extended_capa_len;
0bbaa9b9
JM
1293 if (buflen < (size_t) len + 2) {
1294 wpa_printf(MSG_INFO,
1295 "Not enough room for building extended capabilities element");
1296 return -1;
1297 }
03e47c9c
JM
1298
1299 *pos++ = WLAN_EID_EXT_CAPAB;
8cd6b7bc
JB
1300 *pos++ = len;
1301 for (i = 0; i < len; i++, pos++) {
1302 wpas_ext_capab_byte(wpa_s, pos, i);
1303
1304 if (i < wpa_s->extended_capa_len) {
1305 *pos &= ~wpa_s->extended_capa_mask[i];
1306 *pos |= wpa_s->extended_capa[i];
1307 }
1308 }
03e47c9c 1309
3db5439a
JM
1310 while (len > 0 && buf[1 + len] == 0) {
1311 len--;
1312 buf[1] = len;
1313 }
1314 if (len == 0)
1315 return 0;
1316
1317 return 2 + len;
03e47c9c
JM
1318}
1319
1320
6ac4b15e
JM
1321static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1322 struct wpa_bss *test_bss)
1323{
1324 struct wpa_bss *bss;
1325
1326 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1327 if (bss == test_bss)
1328 return 1;
1329 }
1330
1331 return 0;
1332}
1333
1334
1335static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1336 struct wpa_ssid *test_ssid)
1337{
1338 struct wpa_ssid *ssid;
1339
1340 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1341 if (ssid == test_ssid)
1342 return 1;
1343 }
1344
1345 return 0;
1346}
1347
1348
1349int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1350 struct wpa_ssid *test_ssid)
1351{
1352 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1353 return 0;
1354
1355 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1356}
1357
1358
1359void wpas_connect_work_free(struct wpa_connect_work *cwork)
1360{
1361 if (cwork == NULL)
1362 return;
1363 os_free(cwork);
1364}
1365
1366
1367void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1368{
1369 struct wpa_connect_work *cwork;
1370 struct wpa_radio_work *work = wpa_s->connect_work;
1371
1372 if (!work)
1373 return;
1374
1375 wpa_s->connect_work = NULL;
1376 cwork = work->ctx;
1377 work->ctx = NULL;
1378 wpas_connect_work_free(cwork);
1379 radio_work_done(work);
1380}
1381
1382
c267753b
JM
1383int wpas_update_random_addr(struct wpa_supplicant *wpa_s)
1384{
1385 struct os_reltime now;
1386 u8 addr[ETH_ALEN];
1387
1388 os_get_reltime(&now);
1389 if (wpa_s->last_mac_addr_change.sec != 0 &&
1390 !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
1391 wpa_s->conf->rand_addr_lifetime)) {
1392 wpa_msg(wpa_s, MSG_DEBUG,
1393 "Previously selected random MAC address has not yet expired");
1394 return 0;
1395 }
1396
1397 if (random_mac_addr(addr) < 0)
1398 return -1;
1399
1400 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
1401 wpa_msg(wpa_s, MSG_INFO,
1402 "Failed to set random MAC address");
1403 return -1;
1404 }
1405
1406 os_get_reltime(&wpa_s->last_mac_addr_change);
1407 wpa_s->mac_addr_changed = 1;
1408
1409 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1410 wpa_msg(wpa_s, MSG_INFO,
1411 "Could not update MAC address information");
1412 return -1;
1413 }
1414
1415 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
1416 MAC2STR(addr));
1417
1418 return 0;
1419}
1420
1421
1422int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
1423{
1424 if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
1425 !wpa_s->conf->preassoc_mac_addr)
1426 return 0;
1427
1428 return wpas_update_random_addr(wpa_s);
1429}
1430
1431
6ac4b15e
JM
1432static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1433
6fc6879b
JM
1434/**
1435 * wpa_supplicant_associate - Request association
1436 * @wpa_s: Pointer to wpa_supplicant data
1437 * @bss: Scan results for the selected BSS, or %NULL if not available
1438 * @ssid: Configuration data for the selected network
1439 *
1440 * This function is used to request %wpa_supplicant to associate with a BSS.
1441 */
1442void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
6fa81a3b 1443 struct wpa_bss *bss, struct wpa_ssid *ssid)
6fc6879b 1444{
6ac4b15e 1445 struct wpa_connect_work *cwork;
6fc6879b 1446
c267753b
JM
1447 if (wpa_s->last_ssid == ssid) {
1448 wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
1449 } else if (ssid->mac_addr == 1 ||
1450 (ssid->mac_addr == -1 && wpa_s->conf->mac_addr == 1)) {
1451 if (wpas_update_random_addr(wpa_s) < 0)
1452 return;
1453 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1454 } else if (wpa_s->mac_addr_changed) {
1455 if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
1456 wpa_msg(wpa_s, MSG_INFO,
1457 "Could not restore permanent MAC address");
1458 return;
1459 }
1460 wpa_s->mac_addr_changed = 0;
1461 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1462 wpa_msg(wpa_s, MSG_INFO,
1463 "Could not update MAC address information");
1464 return;
1465 }
1466 wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
1467 }
1468 wpa_s->last_ssid = ssid;
1469
78177a00
JM
1470#ifdef CONFIG_IBSS_RSN
1471 ibss_rsn_deinit(wpa_s->ibss_rsn);
1472 wpa_s->ibss_rsn = NULL;
1473#endif /* CONFIG_IBSS_RSN */
1474
2c5d725c
JM
1475 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1476 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1581b38b
JM
1477#ifdef CONFIG_AP
1478 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
f049052b
BG
1479 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1480 "mode");
1581b38b
JM
1481 return;
1482 }
8c981d17
DW
1483 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1484 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
b2b688d1
VKE
1485 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1486 wpas_p2p_ap_setup_failed(wpa_s);
8c981d17
DW
1487 return;
1488 }
8f770587 1489 wpa_s->current_bss = bss;
1581b38b 1490#else /* CONFIG_AP */
f049052b
BG
1491 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1492 "the build");
1581b38b
JM
1493#endif /* CONFIG_AP */
1494 return;
1495 }
1496
52c9e6f3 1497#ifdef CONFIG_TDLS
95cb2d88
JM
1498 if (bss)
1499 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1500 bss->ie_len);
52c9e6f3
JM
1501#endif /* CONFIG_TDLS */
1502
5cc4d64b
JM
1503 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1504 ssid->mode == IEEE80211_MODE_INFRA) {
c2a04078
JM
1505 sme_authenticate(wpa_s, bss, ssid);
1506 return;
1507 }
1508
6ac4b15e
JM
1509 if (wpa_s->connect_work) {
1510 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1511 return;
1512 }
1513
f0e30c84
JM
1514 if (radio_work_pending(wpa_s, "connect")) {
1515 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1516 return;
1517 }
1518
6ac4b15e
JM
1519 cwork = os_zalloc(sizeof(*cwork));
1520 if (cwork == NULL)
1521 return;
1522
1523 cwork->bss = bss;
1524 cwork->ssid = ssid;
1525
1526 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1527 wpas_start_assoc_cb, cwork) < 0) {
1528 os_free(cwork);
1529 }
1530}
1531
1532
1533static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1534{
1535 struct wpa_connect_work *cwork = work->ctx;
1536 struct wpa_bss *bss = cwork->bss;
1537 struct wpa_ssid *ssid = cwork->ssid;
1538 struct wpa_supplicant *wpa_s = work->wpa_s;
1539 u8 wpa_ie[200];
1540 size_t wpa_ie_len;
1541 int use_crypt, ret, i, bssid_changed;
1542 int algs = WPA_AUTH_ALG_OPEN;
1543 unsigned int cipher_pairwise, cipher_group;
1544 struct wpa_driver_associate_params params;
1545 int wep_keys_set = 0;
1546 int assoc_failed = 0;
1547 struct wpa_ssid *old_ssid;
1548#ifdef CONFIG_HT_OVERRIDES
1549 struct ieee80211_ht_capabilities htcaps;
1550 struct ieee80211_ht_capabilities htcaps_mask;
1551#endif /* CONFIG_HT_OVERRIDES */
6aa1cd4e
PS
1552#ifdef CONFIG_VHT_OVERRIDES
1553 struct ieee80211_vht_capabilities vhtcaps;
1554 struct ieee80211_vht_capabilities vhtcaps_mask;
1555#endif /* CONFIG_VHT_OVERRIDES */
6ac4b15e
JM
1556
1557 if (deinit) {
b3253ebb
AO
1558 if (work->started) {
1559 wpa_s->connect_work = NULL;
1560
1561 /* cancel possible auth. timeout */
1562 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1563 NULL);
1564 }
6ac4b15e
JM
1565 wpas_connect_work_free(cwork);
1566 return;
1567 }
1568
1569 wpa_s->connect_work = work;
1570
1571 if (!wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1572 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1573 wpas_connect_work_done(wpa_s);
1574 return;
1575 }
1576
0c80427d 1577 os_memset(&params, 0, sizeof(params));
6fc6879b 1578 wpa_s->reassociate = 0;
c60ba9f7 1579 wpa_s->eap_expected_failure = 0;
22628eca 1580 if (bss && !wpas_driver_bss_selection(wpa_s)) {
6fc6879b 1581#ifdef CONFIG_IEEE80211R
6fa81a3b 1582 const u8 *ie, *md = NULL;
6fc6879b 1583#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
1584 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1585 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
6fa81a3b 1586 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
8bac466b 1587 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
1588 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1589 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
8bac466b
JM
1590 if (bssid_changed)
1591 wpas_notify_bssid_changed(wpa_s);
6fc6879b 1592#ifdef CONFIG_IEEE80211R
6fa81a3b 1593 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
6fc6879b
JM
1594 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1595 md = ie + 2;
e7846b68 1596 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
91a05482
JM
1597 if (md) {
1598 /* Prepare for the next transition */
76b7981d 1599 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
91a05482 1600 }
6fc6879b 1601#endif /* CONFIG_IEEE80211R */
24c23d1b
JM
1602#ifdef CONFIG_WPS
1603 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1604 wpa_s->conf->ap_scan == 2 &&
1605 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1606 /* Use ap_scan==1 style network selection to find the network
1607 */
4115303b 1608 wpa_s->scan_req = MANUAL_SCAN_REQ;
24c23d1b
JM
1609 wpa_s->reassociate = 1;
1610 wpa_supplicant_req_scan(wpa_s, 0, 0);
1611 return;
1612#endif /* CONFIG_WPS */
6fc6879b
JM
1613 } else {
1614 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1615 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1616 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1617 }
a4cba8f1 1618 wpa_supplicant_cancel_sched_scan(wpa_s);
6fc6879b
JM
1619 wpa_supplicant_cancel_scan(wpa_s);
1620
1621 /* Starting new association, so clear the possibly used WPA IE from the
1622 * previous association. */
1623 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1624
1625#ifdef IEEE8021X_EAPOL
1626 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1627 if (ssid->leap) {
1628 if (ssid->non_leap == 0)
abd9fafa 1629 algs = WPA_AUTH_ALG_LEAP;
6fc6879b 1630 else
abd9fafa 1631 algs |= WPA_AUTH_ALG_LEAP;
6fc6879b
JM
1632 }
1633 }
1634#endif /* IEEE8021X_EAPOL */
f049052b 1635 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
6fc6879b 1636 if (ssid->auth_alg) {
abd9fafa 1637 algs = ssid->auth_alg;
f049052b
BG
1638 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1639 "0x%x", algs);
6fc6879b 1640 }
6fc6879b 1641
6fa81a3b
JM
1642 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1643 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
0bf927a0 1644 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
6fc6879b 1645 int try_opportunistic;
6e202021
JM
1646 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1647 wpa_s->conf->okc :
1648 ssid->proactive_key_caching) &&
6fc6879b
JM
1649 (ssid->proto & WPA_PROTO_RSN);
1650 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
b2a12c4f 1651 ssid, try_opportunistic) == 0)
6fc6879b
JM
1652 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1653 wpa_ie_len = sizeof(wpa_ie);
1654 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1655 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1656 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1657 "key management and encryption suites");
6fc6879b
JM
1658 return;
1659 }
a3f7e518
JM
1660 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1661 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1662 /*
1663 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1664 * use non-WPA since the scan results did not indicate that the
1665 * AP is using WPA or WPA2.
1666 */
1667 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1668 wpa_ie_len = 0;
1669 wpa_s->wpa_proto = 0;
0bf927a0 1670 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
6fc6879b
JM
1671 wpa_ie_len = sizeof(wpa_ie);
1672 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1673 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1674 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1675 "key management and encryption suites (no "
1676 "scan results)");
6fc6879b
JM
1677 return;
1678 }
ad08c363
JM
1679#ifdef CONFIG_WPS
1680 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
b01c18a8
JM
1681 struct wpabuf *wps_ie;
1682 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
ad08c363
JM
1683 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1684 wpa_ie_len = wpabuf_len(wps_ie);
1685 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
24386985
JM
1686 } else
1687 wpa_ie_len = 0;
ad08c363
JM
1688 wpabuf_free(wps_ie);
1689 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
0c80427d
JM
1690 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1691 params.wps = WPS_MODE_PRIVACY;
1692 else
1693 params.wps = WPS_MODE_OPEN;
cf546f1a 1694 wpa_s->wpa_proto = 0;
ad08c363 1695#endif /* CONFIG_WPS */
6fc6879b
JM
1696 } else {
1697 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1698 wpa_ie_len = 0;
cf546f1a 1699 wpa_s->wpa_proto = 0;
6fc6879b
JM
1700 }
1701
5f3a6aa0
JM
1702#ifdef CONFIG_P2P
1703 if (wpa_s->global->p2p) {
1704 u8 *pos;
1705 size_t len;
1706 int res;
5f3a6aa0
JM
1707 pos = wpa_ie + wpa_ie_len;
1708 len = sizeof(wpa_ie) - wpa_ie_len;
b8a8d677
JM
1709 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1710 ssid->p2p_group);
5f3a6aa0
JM
1711 if (res >= 0)
1712 wpa_ie_len += res;
1713 }
72044390
JM
1714
1715 wpa_s->cross_connect_disallowed = 0;
1716 if (bss) {
1717 struct wpabuf *p2p;
1718 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1719 if (p2p) {
1720 wpa_s->cross_connect_disallowed =
1721 p2p_get_cross_connect_disallowed(p2p);
1722 wpabuf_free(p2p);
f049052b
BG
1723 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1724 "connection",
1725 wpa_s->cross_connect_disallowed ?
1726 "disallows" : "allows");
72044390
JM
1727 }
1728 }
25ef8529
JM
1729
1730 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
5f3a6aa0
JM
1731#endif /* CONFIG_P2P */
1732
cb418324 1733#ifdef CONFIG_HS20
55a2df43 1734 if (is_hs20_network(wpa_s, ssid, bss)) {
cb418324
JM
1735 struct wpabuf *hs20;
1736 hs20 = wpabuf_alloc(20);
1737 if (hs20) {
f9cd147d 1738 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
745ef184
JM
1739 size_t len;
1740
f9cd147d 1741 wpas_hs20_add_indication(hs20, pps_mo_id);
745ef184
JM
1742 len = sizeof(wpa_ie) - wpa_ie_len;
1743 if (wpabuf_len(hs20) <= len) {
1744 os_memcpy(wpa_ie + wpa_ie_len,
1745 wpabuf_head(hs20), wpabuf_len(hs20));
1746 wpa_ie_len += wpabuf_len(hs20);
1747 }
cb418324
JM
1748 wpabuf_free(hs20);
1749 }
1750 }
1751#endif /* CONFIG_HS20 */
1752
8b3b803a
AH
1753 /*
1754 * Workaround: Add Extended Capabilities element only if the AP
1755 * included this element in Beacon/Probe Response frames. Some older
1756 * APs seem to have interoperability issues if this element is
1757 * included, so while the standard may require us to include the
1758 * element in all cases, it is justifiable to skip it to avoid
1759 * interoperability issues.
1760 */
1761 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
0bbaa9b9 1762 u8 ext_capab[18];
8b3b803a 1763 int ext_capab_len;
0bbaa9b9
JM
1764 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
1765 sizeof(ext_capab));
8b3b803a
AH
1766 if (ext_capab_len > 0) {
1767 u8 *pos = wpa_ie;
1768 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1769 pos += 2 + pos[1];
1770 os_memmove(pos + ext_capab_len, pos,
1771 wpa_ie_len - (pos - wpa_ie));
1772 wpa_ie_len += ext_capab_len;
1773 os_memcpy(pos, ext_capab, ext_capab_len);
1774 }
92cbcf91 1775 }
92cbcf91 1776
6fc6879b
JM
1777 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1778 use_crypt = 1;
4848a38d
JM
1779 cipher_pairwise = wpa_s->pairwise_cipher;
1780 cipher_group = wpa_s->group_cipher;
6fc6879b
JM
1781 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1782 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1783 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1784 use_crypt = 0;
1785 if (wpa_set_wep_keys(wpa_s, ssid)) {
1786 use_crypt = 1;
1787 wep_keys_set = 1;
1788 }
1789 }
ad08c363
JM
1790 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1791 use_crypt = 0;
6fc6879b
JM
1792
1793#ifdef IEEE8021X_EAPOL
1794 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1795 if ((ssid->eapol_flags &
1796 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1797 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1798 !wep_keys_set) {
1799 use_crypt = 0;
1800 } else {
1801 /* Assume that dynamic WEP-104 keys will be used and
1802 * set cipher suites in order for drivers to expect
1803 * encryption. */
4848a38d 1804 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
6fc6879b
JM
1805 }
1806 }
1807#endif /* IEEE8021X_EAPOL */
1808
1809 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1810 /* Set the key before (and later after) association */
1811 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1812 }
1813
6fc6879b 1814 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
6fc6879b 1815 if (bss) {
6fa81a3b
JM
1816 params.ssid = bss->ssid;
1817 params.ssid_len = bss->ssid_len;
f15854d1
JM
1818 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1819 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1820 MACSTR " freq=%u MHz based on scan results "
1821 "(bssid_set=%d)",
1822 MAC2STR(bss->bssid), bss->freq,
1823 ssid->bssid_set);
22628eca
JM
1824 params.bssid = bss->bssid;
1825 params.freq = bss->freq;
1826 }
7ac7fd43
DS
1827 params.bssid_hint = bss->bssid;
1828 params.freq_hint = bss->freq;
6fc6879b
JM
1829 } else {
1830 params.ssid = ssid->ssid;
1831 params.ssid_len = ssid->ssid_len;
1832 }
9e2af29f
NC
1833
1834 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1835 wpa_s->conf->ap_scan == 2) {
1836 params.bssid = ssid->bssid;
1837 params.fixed_bssid = 1;
1838 }
1839
d7dcba70
JM
1840 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1841 params.freq == 0)
6fc6879b 1842 params.freq = ssid->frequency; /* Initial channel for IBSS */
8f05577d
JM
1843
1844 if (ssid->mode == WPAS_MODE_IBSS) {
1845 if (ssid->beacon_int)
1846 params.beacon_int = ssid->beacon_int;
1847 else
1848 params.beacon_int = wpa_s->conf->beacon_int;
1849 }
1850
6fc6879b
JM
1851 params.wpa_ie = wpa_ie;
1852 params.wpa_ie_len = wpa_ie_len;
1853 params.pairwise_suite = cipher_pairwise;
1854 params.group_suite = cipher_group;
4848a38d 1855 params.key_mgmt_suite = wpa_s->key_mgmt;
64fa840a 1856 params.wpa_proto = wpa_s->wpa_proto;
6fc6879b
JM
1857 params.auth_alg = algs;
1858 params.mode = ssid->mode;
1f6c0ab8 1859 params.bg_scan_period = ssid->bg_scan_period;
6fc6879b
JM
1860 for (i = 0; i < NUM_WEP_KEYS; i++) {
1861 if (ssid->wep_key_len[i])
1862 params.wep_key[i] = ssid->wep_key[i];
1863 params.wep_key_len[i] = ssid->wep_key_len[i];
1864 }
1865 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1866
c2a04078 1867 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
4848a38d
JM
1868 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1869 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
6fc6879b
JM
1870 params.passphrase = ssid->passphrase;
1871 if (ssid->psk_set)
1872 params.psk = ssid->psk;
1873 }
1874
36b15723
JM
1875 params.drop_unencrypted = use_crypt;
1876
6fc6879b 1877#ifdef CONFIG_IEEE80211W
62d49803
JM
1878 params.mgmt_frame_protection =
1879 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1880 wpa_s->conf->pmf : ssid->ieee80211w;
1881 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
6fa81a3b 1882 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
97d3497e
JM
1883 struct wpa_ie_data ie;
1884 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1885 ie.capabilities &
1886 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
f049052b
BG
1887 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1888 "MFP: require MFP");
97d3497e
JM
1889 params.mgmt_frame_protection =
1890 MGMT_FRAME_PROTECTION_REQUIRED;
1891 }
1892 }
6fc6879b
JM
1893#endif /* CONFIG_IEEE80211W */
1894
ffad8858 1895 params.p2p = ssid->p2p_group;
6e3f4b89 1896
eea2fd9e
JM
1897 if (wpa_s->parent->set_sta_uapsd)
1898 params.uapsd = wpa_s->parent->sta_uapsd;
1899 else
1900 params.uapsd = -1;
1901
80e8a5ee
BG
1902#ifdef CONFIG_HT_OVERRIDES
1903 os_memset(&htcaps, 0, sizeof(htcaps));
1904 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1905 params.htcaps = (u8 *) &htcaps;
1906 params.htcaps_mask = (u8 *) &htcaps_mask;
1907 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1908#endif /* CONFIG_HT_OVERRIDES */
6aa1cd4e
PS
1909#ifdef CONFIG_VHT_OVERRIDES
1910 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1911 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1912 params.vhtcaps = &vhtcaps;
1913 params.vhtcaps_mask = &vhtcaps_mask;
1914 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
1915#endif /* CONFIG_VHT_OVERRIDES */
80e8a5ee 1916
8567866d
JJ
1917#ifdef CONFIG_P2P
1918 /*
1919 * If multi-channel concurrency is not supported, check for any
1920 * frequency conflict. In case of any frequency conflict, remove the
1921 * least prioritized connection.
1922 */
1923 if (wpa_s->num_multichan_concurrent < 2) {
d0df6437
IP
1924 int freq, num;
1925 num = get_shared_radio_freqs(wpa_s, &freq, 1);
1926 if (num > 0 && freq > 0 && freq != params.freq) {
1927 wpa_printf(MSG_DEBUG,
1928 "Assoc conflicting freq found (%d != %d)",
8567866d
JJ
1929 freq, params.freq);
1930 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
1931 params.freq,
1932 ssid) < 0)
1933 return;
1934 }
1935 }
1936#endif /* CONFIG_P2P */
1937
17fbb751 1938 ret = wpa_drv_associate(wpa_s, &params);
6fc6879b
JM
1939 if (ret < 0) {
1940 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1941 "failed");
871f4dd0
JM
1942 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1943 /*
1944 * The driver is known to mean what is saying, so we
1945 * can stop right here; the association will not
1946 * succeed.
1947 */
1948 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
c1c02342 1949 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
871f4dd0
JM
1950 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1951 return;
1952 }
6fc6879b
JM
1953 /* try to continue anyway; new association will be tried again
1954 * after timeout */
1955 assoc_failed = 1;
1956 }
1957
1958 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1959 /* Set the key after the association just in case association
1960 * cleared the previously configured key. */
1961 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1962 /* No need to timeout authentication since there is no key
1963 * management. */
1964 wpa_supplicant_cancel_auth_timeout(wpa_s);
1965 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
53895c3b 1966#ifdef CONFIG_IBSS_RSN
d7dcba70 1967 } else if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
1968 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1969 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1970 /*
1971 * RSN IBSS authentication is per-STA and we can disable the
1972 * per-BSSID authentication.
1973 */
1974 wpa_supplicant_cancel_auth_timeout(wpa_s);
53895c3b 1975#endif /* CONFIG_IBSS_RSN */
6fc6879b
JM
1976 } else {
1977 /* Timeout for IEEE 802.11 authentication and association */
1d3c75b3
DW
1978 int timeout = 60;
1979
1980 if (assoc_failed) {
1981 /* give IBSS a bit more time */
d7dcba70 1982 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1d3c75b3
DW
1983 } else if (wpa_s->conf->ap_scan == 1) {
1984 /* give IBSS a bit more time */
d7dcba70 1985 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1d3c75b3 1986 }
6fc6879b
JM
1987 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1988 }
1989
66562e9c
JM
1990 if (wep_keys_set &&
1991 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
6fc6879b
JM
1992 /* Set static WEP keys again */
1993 wpa_set_wep_keys(wpa_s, ssid);
1994 }
1995
1996 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1997 /*
1998 * Do not allow EAP session resumption between different
1999 * network configurations.
2000 */
2001 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2002 }
8bac466b 2003 old_ssid = wpa_s->current_ssid;
6fc6879b 2004 wpa_s->current_ssid = ssid;
8f770587 2005 wpa_s->current_bss = bss;
6fc6879b
JM
2006 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
2007 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
2008 if (old_ssid != wpa_s->current_ssid)
2009 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
2010}
2011
2012
09f58c09
JM
2013static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
2014 const u8 *addr)
2015{
2016 struct wpa_ssid *old_ssid;
2017
2018 wpa_clear_keys(wpa_s, addr);
09f58c09 2019 old_ssid = wpa_s->current_ssid;
0d30cc24 2020 wpa_supplicant_mark_disassoc(wpa_s);
09f58c09
JM
2021 wpa_sm_set_config(wpa_s->wpa, NULL);
2022 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2023 if (old_ssid != wpa_s->current_ssid)
2024 wpas_notify_network_changed(wpa_s);
2025 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
2026}
2027
2028
6fc6879b
JM
2029/**
2030 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
2031 * @wpa_s: Pointer to wpa_supplicant data
2032 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
2033 *
073ab58f 2034 * This function is used to request %wpa_supplicant to deauthenticate from the
6fc6879b
JM
2035 * current AP.
2036 */
2037void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
2038 int reason_code)
2039{
2040 u8 *addr = NULL;
ef48ff94 2041 union wpa_event_data event;
42d23547 2042 int zero_addr = 0;
8bac466b 2043
42d23547
JM
2044 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
2045 " pending_bssid=" MACSTR " reason=%d state=%s",
2046 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
2047 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
2048
2049 if (!is_zero_ether_addr(wpa_s->bssid))
2050 addr = wpa_s->bssid;
2051 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
2052 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
2053 wpa_s->wpa_state == WPA_ASSOCIATING))
2054 addr = wpa_s->pending_bssid;
2055 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
2056 /*
2057 * When using driver-based BSS selection, we may not know the
2058 * BSSID with which we are currently trying to associate. We
2059 * need to notify the driver of this disconnection even in such
2060 * a case, so use the all zeros address here.
2061 */
6fc6879b 2062 addr = wpa_s->bssid;
42d23547
JM
2063 zero_addr = 1;
2064 }
2065
7b44ff2c
SD
2066#ifdef CONFIG_TDLS
2067 wpa_tdls_teardown_peers(wpa_s->wpa);
2068#endif /* CONFIG_TDLS */
2069
42d23547
JM
2070 if (addr) {
2071 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
ef48ff94
JM
2072 os_memset(&event, 0, sizeof(event));
2073 event.deauth_info.reason_code = (u16) reason_code;
2074 event.deauth_info.locally_generated = 1;
2075 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
42d23547
JM
2076 if (zero_addr)
2077 addr = NULL;
6fc6879b 2078 }
09f58c09
JM
2079
2080 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
2081}
2082
dca1a511
DS
2083static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
2084 struct wpa_ssid *ssid)
2085{
2086 if (!ssid || !ssid->disabled || ssid->disabled == 2)
2087 return;
2088
2089 ssid->disabled = 0;
2090 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2091 wpas_notify_network_enabled_changed(wpa_s, ssid);
2092
2093 /*
2094 * Try to reassociate since there is no current configuration and a new
2095 * network was made available.
2096 */
d2592497 2097 if (!wpa_s->current_ssid && !wpa_s->disconnected)
dca1a511
DS
2098 wpa_s->reassociate = 1;
2099}
2100
6fc6879b 2101
86b89452
WS
2102/**
2103 * wpa_supplicant_enable_network - Mark a configured network as enabled
2104 * @wpa_s: wpa_supplicant structure for a network interface
2105 * @ssid: wpa_ssid structure for a configured network or %NULL
2106 *
2107 * Enables the specified network or all networks if no network specified.
2108 */
2109void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2110 struct wpa_ssid *ssid)
2111{
86b89452 2112 if (ssid == NULL) {
14f79078
JM
2113 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2114 wpa_supplicant_enable_one_network(wpa_s, ssid);
dca1a511
DS
2115 } else
2116 wpa_supplicant_enable_one_network(wpa_s, ssid);
86b89452 2117
d2592497 2118 if (wpa_s->reassociate && !wpa_s->disconnected) {
dca1a511
DS
2119 if (wpa_s->sched_scanning) {
2120 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2121 "new network to scan filters");
2122 wpa_supplicant_cancel_sched_scan(wpa_s);
2123 }
86b89452 2124
dad153d1
JM
2125 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2126 wpa_supplicant_req_scan(wpa_s, 0, 0);
86b89452
WS
2127 }
2128}
2129
2130
2131/**
2132 * wpa_supplicant_disable_network - Mark a configured network as disabled
2133 * @wpa_s: wpa_supplicant structure for a network interface
2134 * @ssid: wpa_ssid structure for a configured network or %NULL
2135 *
2136 * Disables the specified network or all networks if no network specified.
2137 */
2138void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2139 struct wpa_ssid *ssid)
2140{
2141 struct wpa_ssid *other_ssid;
2142 int was_disabled;
2143
2144 if (ssid == NULL) {
725fc39e
DS
2145 if (wpa_s->sched_scanning)
2146 wpa_supplicant_cancel_sched_scan(wpa_s);
2147
4dac0245
JM
2148 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2149 other_ssid = other_ssid->next) {
86b89452 2150 was_disabled = other_ssid->disabled;
4dac0245
JM
2151 if (was_disabled == 2)
2152 continue; /* do not change persistent P2P group
2153 * data */
86b89452
WS
2154
2155 other_ssid->disabled = 1;
2156
2157 if (was_disabled != other_ssid->disabled)
2158 wpas_notify_network_enabled_changed(
2159 wpa_s, other_ssid);
86b89452
WS
2160 }
2161 if (wpa_s->current_ssid)
07783eaa 2162 wpa_supplicant_deauthenticate(
86b89452 2163 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4dac0245 2164 } else if (ssid->disabled != 2) {
86b89452 2165 if (ssid == wpa_s->current_ssid)
07783eaa 2166 wpa_supplicant_deauthenticate(
86b89452
WS
2167 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2168
2169 was_disabled = ssid->disabled;
2170
2171 ssid->disabled = 1;
2172
725fc39e 2173 if (was_disabled != ssid->disabled) {
86b89452 2174 wpas_notify_network_enabled_changed(wpa_s, ssid);
725fc39e
DS
2175 if (wpa_s->sched_scanning) {
2176 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2177 "to remove network from filters");
2178 wpa_supplicant_cancel_sched_scan(wpa_s);
2179 wpa_supplicant_req_scan(wpa_s, 0, 0);
2180 }
2181 }
86b89452
WS
2182 }
2183}
2184
2185
2186/**
2187 * wpa_supplicant_select_network - Attempt association with a network
2188 * @wpa_s: wpa_supplicant structure for a network interface
2189 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2190 */
2191void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2192 struct wpa_ssid *ssid)
2193{
2194
2195 struct wpa_ssid *other_ssid;
d93dfbd5 2196 int disconnected = 0;
86b89452 2197
d93dfbd5 2198 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
07783eaa 2199 wpa_supplicant_deauthenticate(
86b89452 2200 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
d93dfbd5
JM
2201 disconnected = 1;
2202 }
86b89452 2203
00e5e3d5
JM
2204 if (ssid)
2205 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2206
86b89452
WS
2207 /*
2208 * Mark all other networks disabled or mark all networks enabled if no
2209 * network specified.
2210 */
4dac0245
JM
2211 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2212 other_ssid = other_ssid->next) {
86b89452 2213 int was_disabled = other_ssid->disabled;
4dac0245
JM
2214 if (was_disabled == 2)
2215 continue; /* do not change persistent P2P group data */
86b89452
WS
2216
2217 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
00e5e3d5
JM
2218 if (was_disabled && !other_ssid->disabled)
2219 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
86b89452
WS
2220
2221 if (was_disabled != other_ssid->disabled)
2222 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
86b89452 2223 }
2a6f78fb
JJ
2224
2225 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2226 /* We are already associated with the selected network */
2227 wpa_printf(MSG_DEBUG, "Already associated with the "
2228 "selected network - do nothing");
2229 return;
2230 }
2231
25a8f9e3 2232 if (ssid) {
96efeeb6 2233 wpa_s->current_ssid = ssid;
25a8f9e3
JM
2234 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2235 }
7dcdcfd6 2236 wpa_s->connect_without_scan = NULL;
86b89452
WS
2237 wpa_s->disconnected = 0;
2238 wpa_s->reassociate = 1;
cecdddc1
PS
2239
2240 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2241 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
86b89452 2242
a1641d26
JM
2243 if (ssid)
2244 wpas_notify_network_selected(wpa_s, ssid);
86b89452
WS
2245}
2246
2247
bdec7ee5
MS
2248/**
2249 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2250 * @wpa_s: wpa_supplicant structure for a network interface
2251 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2252 * @pkcs11_module_path: PKCS #11 module path or NULL
2253 * Returns: 0 on success; -1 on failure
2254 *
2255 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2256 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2257 * module path fails the paths will be reset to the default value (NULL).
2258 */
2259int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2260 const char *pkcs11_engine_path,
2261 const char *pkcs11_module_path)
2262{
2263 char *pkcs11_engine_path_copy = NULL;
2264 char *pkcs11_module_path_copy = NULL;
2265
2266 if (pkcs11_engine_path != NULL) {
2267 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2268 if (pkcs11_engine_path_copy == NULL)
2269 return -1;
2270 }
2271 if (pkcs11_module_path != NULL) {
2272 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
04c366cb 2273 if (pkcs11_module_path_copy == NULL) {
bdec7ee5
MS
2274 os_free(pkcs11_engine_path_copy);
2275 return -1;
2276 }
2277 }
2278
2279 os_free(wpa_s->conf->pkcs11_engine_path);
2280 os_free(wpa_s->conf->pkcs11_module_path);
2281 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2282 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2283
2284 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2285 eapol_sm_deinit(wpa_s->eapol);
2286 wpa_s->eapol = NULL;
2287 if (wpa_supplicant_init_eapol(wpa_s)) {
2288 /* Error -> Reset paths to the default value (NULL) once. */
2289 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2290 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2291 NULL);
2292
2293 return -1;
2294 }
2295 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2296
2297 return 0;
2298}
2299
2300
86b89452
WS
2301/**
2302 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2303 * @wpa_s: wpa_supplicant structure for a network interface
2304 * @ap_scan: AP scan mode
2305 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2306 *
2307 */
2308int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2309{
2310
2311 int old_ap_scan;
2312
2313 if (ap_scan < 0 || ap_scan > 2)
2314 return -1;
2315
48f8e036
DS
2316#ifdef ANDROID
2317 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2318 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2319 wpa_s->wpa_state < WPA_COMPLETED) {
2320 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2321 "associating", wpa_s->conf->ap_scan, ap_scan);
2322 return 0;
2323 }
2324#endif /* ANDROID */
2325
86b89452
WS
2326 old_ap_scan = wpa_s->conf->ap_scan;
2327 wpa_s->conf->ap_scan = ap_scan;
2328
2329 if (old_ap_scan != wpa_s->conf->ap_scan)
2330 wpas_notify_ap_scan_changed(wpa_s);
2331
2332 return 0;
2333}
2334
2335
78633c37
SL
2336/**
2337 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2338 * @wpa_s: wpa_supplicant structure for a network interface
2339 * @expire_age: Expiration age in seconds
2340 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2341 *
2342 */
2343int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2344 unsigned int bss_expire_age)
2345{
2346 if (bss_expire_age < 10) {
2347 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2348 bss_expire_age);
2349 return -1;
2350 }
2351 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2352 bss_expire_age);
2353 wpa_s->conf->bss_expiration_age = bss_expire_age;
2354
2355 return 0;
2356}
2357
2358
2359/**
2360 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2361 * @wpa_s: wpa_supplicant structure for a network interface
2362 * @expire_count: number of scans after which an unseen BSS is reclaimed
2363 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2364 *
2365 */
2366int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2367 unsigned int bss_expire_count)
2368{
2369 if (bss_expire_count < 1) {
2370 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2371 bss_expire_count);
2372 return -1;
2373 }
2374 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2375 bss_expire_count);
2376 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2377
2378 return 0;
2379}
2380
2381
c6e86b63
MA
2382/**
2383 * wpa_supplicant_set_scan_interval - Set scan interval
2384 * @wpa_s: wpa_supplicant structure for a network interface
2385 * @scan_interval: scan interval in seconds
2386 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2387 *
2388 */
2389int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2390 int scan_interval)
2391{
2392 if (scan_interval < 0) {
2393 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2394 scan_interval);
2395 return -1;
2396 }
2397 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2398 scan_interval);
9e737f08 2399 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
c6e86b63
MA
2400
2401 return 0;
2402}
2403
2404
86b89452
WS
2405/**
2406 * wpa_supplicant_set_debug_params - Set global debug params
2407 * @global: wpa_global structure
2408 * @debug_level: debug level
2409 * @debug_timestamp: determines if show timestamp in debug data
2410 * @debug_show_keys: determines if show keys in debug data
2411 * Returns: 0 if succeed or -1 if debug_level has wrong value
2412 */
2413int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2414 int debug_timestamp, int debug_show_keys)
2415{
2416
2417 int old_level, old_timestamp, old_show_keys;
2418
2419 /* check for allowed debuglevels */
14dc0011
PS
2420 if (debug_level != MSG_EXCESSIVE &&
2421 debug_level != MSG_MSGDUMP &&
86b89452
WS
2422 debug_level != MSG_DEBUG &&
2423 debug_level != MSG_INFO &&
2424 debug_level != MSG_WARNING &&
2425 debug_level != MSG_ERROR)
2426 return -1;
2427
2428 old_level = wpa_debug_level;
2429 old_timestamp = wpa_debug_timestamp;
2430 old_show_keys = wpa_debug_show_keys;
2431
2432 wpa_debug_level = debug_level;
2433 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2434 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2435
db9133ac
WS
2436 if (wpa_debug_level != old_level)
2437 wpas_notify_debug_level_changed(global);
2438 if (wpa_debug_timestamp != old_timestamp)
2439 wpas_notify_debug_timestamp_changed(global);
2440 if (wpa_debug_show_keys != old_show_keys)
2441 wpas_notify_debug_show_keys_changed(global);
86b89452
WS
2442
2443 return 0;
2444}
2445
2446
6fc6879b
JM
2447/**
2448 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2449 * @wpa_s: Pointer to wpa_supplicant data
2450 * Returns: A pointer to the current network structure or %NULL on failure
2451 */
2452struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2453{
2454 struct wpa_ssid *entry;
2455 u8 ssid[MAX_SSID_LEN];
2456 int res;
2457 size_t ssid_len;
2458 u8 bssid[ETH_ALEN];
2459 int wired;
2460
17fbb751
JM
2461 res = wpa_drv_get_ssid(wpa_s, ssid);
2462 if (res < 0) {
2463 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2464 "driver");
2465 return NULL;
6fc6879b 2466 }
17fbb751 2467 ssid_len = res;
6fc6879b 2468
17fbb751 2469 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
f049052b
BG
2470 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2471 "driver");
6fc6879b
JM
2472 return NULL;
2473 }
2474
c2a04078
JM
2475 wired = wpa_s->conf->ap_scan == 0 &&
2476 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
6fc6879b
JM
2477
2478 entry = wpa_s->conf->ssid;
2479 while (entry) {
349493bd 2480 if (!wpas_network_disabled(wpa_s, entry) &&
6fc6879b
JM
2481 ((ssid_len == entry->ssid_len &&
2482 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2483 (!entry->bssid_set ||
2484 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2485 return entry;
24c23d1b 2486#ifdef CONFIG_WPS
349493bd 2487 if (!wpas_network_disabled(wpa_s, entry) &&
24c23d1b
JM
2488 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2489 (entry->ssid == NULL || entry->ssid_len == 0) &&
2490 (!entry->bssid_set ||
2491 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2492 return entry;
2493#endif /* CONFIG_WPS */
7d232e23 2494
349493bd 2495 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
7d232e23
ZC
2496 entry->ssid_len == 0 &&
2497 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2498 return entry;
2499
6fc6879b
JM
2500 entry = entry->next;
2501 }
2502
2503 return NULL;
2504}
2505
2506
7756114f
JM
2507static int select_driver(struct wpa_supplicant *wpa_s, int i)
2508{
2509 struct wpa_global *global = wpa_s->global;
2510
2511 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2512 global->drv_priv[i] = wpa_drivers[i]->global_init();
2513 if (global->drv_priv[i] == NULL) {
2514 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2515 "'%s'", wpa_drivers[i]->name);
2516 return -1;
2517 }
2518 }
2519
2520 wpa_s->driver = wpa_drivers[i];
2521 wpa_s->global_drv_priv = global->drv_priv[i];
2522
2523 return 0;
2524}
2525
2526
6fc6879b
JM
2527static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2528 const char *name)
2529{
2530 int i;
362f781e 2531 size_t len;
74b1c84a 2532 const char *pos, *driver = name;
6fc6879b
JM
2533
2534 if (wpa_s == NULL)
2535 return -1;
2536
c5121837 2537 if (wpa_drivers[0] == NULL) {
f049052b
BG
2538 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2539 "wpa_supplicant");
6fc6879b
JM
2540 return -1;
2541 }
2542
2543 if (name == NULL) {
2544 /* default to first driver in the list */
7756114f 2545 return select_driver(wpa_s, 0);
6fc6879b
JM
2546 }
2547
74b1c84a
SO
2548 do {
2549 pos = os_strchr(driver, ',');
2550 if (pos)
2551 len = pos - driver;
2552 else
2553 len = os_strlen(driver);
2554
2555 for (i = 0; wpa_drivers[i]; i++) {
2556 if (os_strlen(wpa_drivers[i]->name) == len &&
2557 os_strncmp(driver, wpa_drivers[i]->name, len) ==
0f4668ce
DW
2558 0) {
2559 /* First driver that succeeds wins */
2560 if (select_driver(wpa_s, i) == 0)
2561 return 0;
2562 }
6fc6879b 2563 }
74b1c84a
SO
2564
2565 driver = pos + 1;
2566 } while (pos);
6fc6879b 2567
f049052b 2568 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
6fc6879b
JM
2569 return -1;
2570}
2571
2572
a8e0505b
JM
2573/**
2574 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2575 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2576 * with struct wpa_driver_ops::init()
2577 * @src_addr: Source address of the EAPOL frame
2578 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2579 * @len: Length of the EAPOL data
2580 *
2581 * This function is called for each received EAPOL frame. Most driver
2582 * interfaces rely on more generic OS mechanism for receiving frames through
2583 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2584 * take care of received EAPOL frames and deliver them to the core supplicant
2585 * code by calling this function.
2586 */
6fc6879b
JM
2587void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2588 const u8 *buf, size_t len)
2589{
2590 struct wpa_supplicant *wpa_s = ctx;
2591
f049052b 2592 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
6fc6879b
JM
2593 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2594
db76aa64
JM
2595#ifdef CONFIG_PEERKEY
2596 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2597 wpa_s->current_ssid->peerkey &&
2598 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2599 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2600 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2601 return;
2602 }
2603#endif /* CONFIG_PEERKEY */
2604
3ab35a66
JM
2605 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2606 (wpa_s->last_eapol_matches_bssid &&
2607#ifdef CONFIG_AP
2608 !wpa_s->ap_iface &&
2609#endif /* CONFIG_AP */
2610 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
1ff73338
JM
2611 /*
2612 * There is possible race condition between receiving the
2613 * association event and the EAPOL frame since they are coming
2614 * through different paths from the driver. In order to avoid
2615 * issues in trying to process the EAPOL frame before receiving
2616 * association information, lets queue it for processing until
3ab35a66
JM
2617 * the association event is received. This may also be needed in
2618 * driver-based roaming case, so also use src_addr != BSSID as a
2619 * trigger if we have previously confirmed that the
2620 * Authenticator uses BSSID as the src_addr (which is not the
2621 * case with wired IEEE 802.1X).
1ff73338 2622 */
f049052b 2623 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
3ab35a66
JM
2624 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2625 wpa_supplicant_state_txt(wpa_s->wpa_state),
2626 MAC2STR(wpa_s->bssid));
1ff73338
JM
2627 wpabuf_free(wpa_s->pending_eapol_rx);
2628 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2629 if (wpa_s->pending_eapol_rx) {
c2be937c 2630 os_get_reltime(&wpa_s->pending_eapol_rx_time);
1ff73338
JM
2631 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2632 ETH_ALEN);
2633 }
2634 return;
2635 }
2636
3ab35a66
JM
2637 wpa_s->last_eapol_matches_bssid =
2638 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2639
db149ac9
JM
2640#ifdef CONFIG_AP
2641 if (wpa_s->ap_iface) {
2642 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2643 return;
2644 }
2645#endif /* CONFIG_AP */
2646
6fc6879b 2647 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
f049052b
BG
2648 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2649 "no key management is configured");
6fc6879b
JM
2650 return;
2651 }
2652
2653 if (wpa_s->eapol_received == 0 &&
c2a04078 2654 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
56586197 2655 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
9c972abb
JM
2656 wpa_s->wpa_state != WPA_COMPLETED) &&
2657 (wpa_s->current_ssid == NULL ||
2658 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
6fc6879b
JM
2659 /* Timeout for completing IEEE 802.1X and WPA authentication */
2660 wpa_supplicant_req_auth_timeout(
2661 wpa_s,
56586197 2662 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
a6f06dab
AT
2663 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2664 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
6fc6879b
JM
2665 70 : 10, 0);
2666 }
2667 wpa_s->eapol_received++;
2668
2669 if (wpa_s->countermeasures) {
f049052b
BG
2670 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2671 "EAPOL packet");
6fc6879b
JM
2672 return;
2673 }
2674
8be18440
JM
2675#ifdef CONFIG_IBSS_RSN
2676 if (wpa_s->current_ssid &&
d7dcba70 2677 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
8be18440
JM
2678 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2679 return;
2680 }
2681#endif /* CONFIG_IBSS_RSN */
2682
6fc6879b
JM
2683 /* Source address of the incoming EAPOL frame could be compared to the
2684 * current BSSID. However, it is possible that a centralized
2685 * Authenticator could be using another MAC address than the BSSID of
2686 * an AP, so just allow any address to be used for now. The replies are
2687 * still sent to the current BSSID (if available), though. */
2688
2689 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
56586197 2690 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
6fc6879b
JM
2691 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2692 return;
2693 wpa_drv_poll(wpa_s);
c2a04078 2694 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
6fc6879b 2695 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
56586197 2696 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
2697 /*
2698 * Set portValid = TRUE here since we are going to skip 4-way
2699 * handshake processing which would normally set portValid. We
2700 * need this to allow the EAPOL state machines to be completed
2701 * without going through EAPOL-Key handshake.
2702 */
2703 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2704 }
2705}
2706
2707
bfba8deb 2708int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
6fc6879b 2709{
6fc6879b
JM
2710 if (wpa_s->driver->send_eapol) {
2711 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2712 if (addr)
2713 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1c42b42f
JM
2714 } else if ((!wpa_s->p2p_mgmt ||
2715 !(wpa_s->drv_flags &
2716 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
c68f6200 2717 !(wpa_s->drv_flags &
fdadd5fe 2718 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
bfba8deb 2719 l2_packet_deinit(wpa_s->l2);
6fc6879b
JM
2720 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2721 wpa_drv_get_mac_addr(wpa_s),
2722 ETH_P_EAPOL,
2723 wpa_supplicant_rx_eapol, wpa_s, 0);
2724 if (wpa_s->l2 == NULL)
2725 return -1;
fdadd5fe
JM
2726 } else {
2727 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2728 if (addr)
2729 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
6fc6879b
JM
2730 }
2731
2732 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
f049052b 2733 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
6fc6879b
JM
2734 return -1;
2735 }
2736
c267753b
JM
2737 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2738
bfba8deb
JM
2739 return 0;
2740}
2741
2742
25f839c6
JM
2743static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2744 const u8 *buf, size_t len)
2745{
2746 struct wpa_supplicant *wpa_s = ctx;
2747 const struct l2_ethhdr *eth;
2748
2749 if (len < sizeof(*eth))
2750 return;
2751 eth = (const struct l2_ethhdr *) buf;
2752
2753 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2754 !(eth->h_dest[0] & 0x01)) {
2755 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2756 " (bridge - not for this interface - ignore)",
2757 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2758 return;
2759 }
2760
2761 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2762 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2763 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2764 len - sizeof(*eth));
2765}
2766
2767
bfba8deb
JM
2768/**
2769 * wpa_supplicant_driver_init - Initialize driver interface parameters
2770 * @wpa_s: Pointer to wpa_supplicant data
2771 * Returns: 0 on success, -1 on failure
2772 *
2773 * This function is called to initialize driver interface parameters.
2774 * wpa_drv_init() must have been called before this function to initialize the
2775 * driver interface.
2776 */
2777int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2778{
2779 static int interface_count = 0;
2780
2781 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2782 return -1;
2783
c68f6200
AS
2784 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2785 MAC2STR(wpa_s->own_addr));
2786 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2787
6fc6879b 2788 if (wpa_s->bridge_ifname[0]) {
f049052b
BG
2789 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2790 "interface '%s'", wpa_s->bridge_ifname);
6fc6879b
JM
2791 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2792 wpa_s->own_addr,
2793 ETH_P_EAPOL,
25f839c6
JM
2794 wpa_supplicant_rx_eapol_bridge,
2795 wpa_s, 1);
6fc6879b 2796 if (wpa_s->l2_br == NULL) {
f049052b
BG
2797 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2798 "connection for the bridge interface '%s'",
2799 wpa_s->bridge_ifname);
6fc6879b
JM
2800 return -1;
2801 }
2802 }
2803
6fc6879b
JM
2804 wpa_clear_keys(wpa_s, NULL);
2805
2806 /* Make sure that TKIP countermeasures are not left enabled (could
2807 * happen if wpa_supplicant is killed during countermeasures. */
2808 wpa_drv_set_countermeasures(wpa_s, 0);
2809
f049052b 2810 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
6fc6879b
JM
2811 wpa_drv_flush_pmkid(wpa_s);
2812
ba2a573c 2813 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
b3aa456b
ES
2814 wpa_s->prev_scan_wildcard = 0;
2815
349493bd 2816 if (wpa_supplicant_enabled_networks(wpa_s)) {
a0e9d892
AS
2817 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2818 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2819 interface_count = 0;
2820 }
3a94adbf 2821 if (!wpa_s->p2p_mgmt &&
5d0d72a3
BG
2822 wpa_supplicant_delayed_sched_scan(wpa_s,
2823 interface_count % 3,
6a90053c 2824 100000))
5d0d72a3 2825 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
a4cba8f1 2826 100000);
74e259ec
JM
2827 interface_count++;
2828 } else
2829 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
6fc6879b
JM
2830
2831 return 0;
2832}
2833
2834
2835static int wpa_supplicant_daemon(const char *pid_file)
2836{
2837 wpa_printf(MSG_DEBUG, "Daemonize..");
2838 return os_daemonize(pid_file);
2839}
2840
2841
2842static struct wpa_supplicant * wpa_supplicant_alloc(void)
2843{
2844 struct wpa_supplicant *wpa_s;
2845
2846 wpa_s = os_zalloc(sizeof(*wpa_s));
2847 if (wpa_s == NULL)
2848 return NULL;
4115303b 2849 wpa_s->scan_req = INITIAL_SCAN_REQ;
67b9bd08 2850 wpa_s->scan_interval = 5;
c302f207 2851 wpa_s->new_connection = 1;
b22128ef 2852 wpa_s->parent = wpa_s;
cbdf3507 2853 wpa_s->sched_scanning = 0;
6fc6879b
JM
2854
2855 return wpa_s;
2856}
2857
2858
80e8a5ee
BG
2859#ifdef CONFIG_HT_OVERRIDES
2860
2861static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2862 struct ieee80211_ht_capabilities *htcaps,
2863 struct ieee80211_ht_capabilities *htcaps_mask,
2864 const char *ht_mcs)
2865{
2866 /* parse ht_mcs into hex array */
2867 int i;
2868 const char *tmp = ht_mcs;
2869 char *end = NULL;
2870
2871 /* If ht_mcs is null, do not set anything */
2872 if (!ht_mcs)
2873 return 0;
2874
2875 /* This is what we are setting in the kernel */
2876 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2877
2878 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2879
2880 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2881 errno = 0;
2882 long v = strtol(tmp, &end, 16);
2883 if (errno == 0) {
2884 wpa_msg(wpa_s, MSG_DEBUG,
2885 "htcap value[%i]: %ld end: %p tmp: %p",
2886 i, v, end, tmp);
2887 if (end == tmp)
2888 break;
2889
2890 htcaps->supported_mcs_set[i] = v;
2891 tmp = end;
2892 } else {
2893 wpa_msg(wpa_s, MSG_ERROR,
2894 "Failed to parse ht-mcs: %s, error: %s\n",
2895 ht_mcs, strerror(errno));
2896 return -1;
2897 }
2898 }
2899
2900 /*
2901 * If we were able to parse any values, then set mask for the MCS set.
2902 */
2903 if (i) {
2904 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2905 IEEE80211_HT_MCS_MASK_LEN - 1);
2906 /* skip the 3 reserved bits */
2907 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2908 0x1f;
2909 }
2910
2911 return 0;
2912}
2913
2914
2915static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2916 struct ieee80211_ht_capabilities *htcaps,
2917 struct ieee80211_ht_capabilities *htcaps_mask,
2918 int disabled)
2919{
5bc28571 2920 le16 msk;
80e8a5ee
BG
2921
2922 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2923
2924 if (disabled == -1)
2925 return 0;
2926
2927 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2928 htcaps_mask->ht_capabilities_info |= msk;
2929 if (disabled)
2930 htcaps->ht_capabilities_info &= msk;
2931 else
2932 htcaps->ht_capabilities_info |= msk;
2933
2934 return 0;
2935}
2936
2937
2938static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2939 struct ieee80211_ht_capabilities *htcaps,
2940 struct ieee80211_ht_capabilities *htcaps_mask,
2941 int factor)
2942{
2943 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2944
2945 if (factor == -1)
2946 return 0;
2947
2948 if (factor < 0 || factor > 3) {
2949 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2950 "Must be 0-3 or -1", factor);
2951 return -EINVAL;
2952 }
2953
2954 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2955 htcaps->a_mpdu_params &= ~0x3;
2956 htcaps->a_mpdu_params |= factor & 0x3;
2957
2958 return 0;
2959}
2960
2961
2962static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2963 struct ieee80211_ht_capabilities *htcaps,
2964 struct ieee80211_ht_capabilities *htcaps_mask,
2965 int density)
2966{
2967 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2968
2969 if (density == -1)
2970 return 0;
2971
2972 if (density < 0 || density > 7) {
2973 wpa_msg(wpa_s, MSG_ERROR,
2974 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2975 density);
2976 return -EINVAL;
2977 }
2978
2979 htcaps_mask->a_mpdu_params |= 0x1C;
2980 htcaps->a_mpdu_params &= ~(0x1C);
2981 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2982
2983 return 0;
2984}
2985
2986
2987static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2988 struct ieee80211_ht_capabilities *htcaps,
2989 struct ieee80211_ht_capabilities *htcaps_mask,
2990 int disabled)
2991{
2992 /* Masking these out disables HT40 */
5bc28571
JM
2993 le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2994 HT_CAP_INFO_SHORT_GI40MHZ);
80e8a5ee
BG
2995
2996 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2997
2998 if (disabled)
2999 htcaps->ht_capabilities_info &= ~msk;
3000 else
3001 htcaps->ht_capabilities_info |= msk;
3002
3003 htcaps_mask->ht_capabilities_info |= msk;
3004
3005 return 0;
3006}
3007
3008
a90497f8
BG
3009static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
3010 struct ieee80211_ht_capabilities *htcaps,
3011 struct ieee80211_ht_capabilities *htcaps_mask,
3012 int disabled)
3013{
3014 /* Masking these out disables SGI */
5bc28571
JM
3015 le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
3016 HT_CAP_INFO_SHORT_GI40MHZ);
a90497f8
BG
3017
3018 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
3019
3020 if (disabled)
3021 htcaps->ht_capabilities_info &= ~msk;
3022 else
3023 htcaps->ht_capabilities_info |= msk;
3024
3025 htcaps_mask->ht_capabilities_info |= msk;
3026
3027 return 0;
3028}
3029
3030
39a5800f
PK
3031static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
3032 struct ieee80211_ht_capabilities *htcaps,
3033 struct ieee80211_ht_capabilities *htcaps_mask,
3034 int disabled)
3035{
3036 /* Masking these out disables LDPC */
5bc28571 3037 le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
39a5800f
PK
3038
3039 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
3040
3041 if (disabled)
3042 htcaps->ht_capabilities_info &= ~msk;
3043 else
3044 htcaps->ht_capabilities_info |= msk;
3045
3046 htcaps_mask->ht_capabilities_info |= msk;
3047
3048 return 0;
3049}
3050
3051
80e8a5ee
BG
3052void wpa_supplicant_apply_ht_overrides(
3053 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3054 struct wpa_driver_associate_params *params)
3055{
3056 struct ieee80211_ht_capabilities *htcaps;
3057 struct ieee80211_ht_capabilities *htcaps_mask;
3058
3059 if (!ssid)
3060 return;
3061
3062 params->disable_ht = ssid->disable_ht;
3063 if (!params->htcaps || !params->htcaps_mask)
3064 return;
3065
3066 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
3067 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
3068 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
3069 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
3070 ssid->disable_max_amsdu);
3071 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
3072 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
3073 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
a90497f8 3074 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
39a5800f 3075 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
d41cc8cc
JM
3076
3077 if (ssid->ht40_intolerant) {
5bc28571 3078 le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
d41cc8cc
JM
3079 htcaps->ht_capabilities_info |= bit;
3080 htcaps_mask->ht_capabilities_info |= bit;
3081 }
80e8a5ee
BG
3082}
3083
3084#endif /* CONFIG_HT_OVERRIDES */
3085
3086
e9ee8dc3
JB
3087#ifdef CONFIG_VHT_OVERRIDES
3088void wpa_supplicant_apply_vht_overrides(
3089 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3090 struct wpa_driver_associate_params *params)
3091{
3092 struct ieee80211_vht_capabilities *vhtcaps;
3093 struct ieee80211_vht_capabilities *vhtcaps_mask;
4f560cde
EP
3094#ifdef CONFIG_HT_OVERRIDES
3095 int max_ampdu;
3096 const u32 max_ampdu_mask = VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX;
3097#endif /* CONFIG_HT_OVERRIDES */
e9ee8dc3
JB
3098
3099 if (!ssid)
3100 return;
3101
3102 params->disable_vht = ssid->disable_vht;
3103
3104 vhtcaps = (void *) params->vhtcaps;
3105 vhtcaps_mask = (void *) params->vhtcaps_mask;
3106
3107 if (!vhtcaps || !vhtcaps_mask)
3108 return;
3109
3110 vhtcaps->vht_capabilities_info = ssid->vht_capa;
3111 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
3112
4f560cde
EP
3113#ifdef CONFIG_HT_OVERRIDES
3114 /* if max ampdu is <= 3, we have to make the HT cap the same */
3115 if (ssid->vht_capa_mask & max_ampdu_mask) {
3116 max_ampdu = (ssid->vht_capa & max_ampdu_mask) >>
3117 find_first_bit(max_ampdu_mask);
3118
3119 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
3120 wpa_set_ampdu_factor(wpa_s,
3121 (void *) params->htcaps,
3122 (void *) params->htcaps_mask,
3123 max_ampdu);
3124 }
3125#endif /* CONFIG_HT_OVERRIDES */
3126
e9ee8dc3
JB
3127#define OVERRIDE_MCS(i) \
3128 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
3129 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
3130 3 << 2 * (i - 1); \
3131 vhtcaps->vht_supported_mcs_set.tx_map |= \
3132 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
3133 } \
3134 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
3135 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
3136 3 << 2 * (i - 1); \
3137 vhtcaps->vht_supported_mcs_set.rx_map |= \
3138 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
3139 }
3140
3141 OVERRIDE_MCS(1);
3142 OVERRIDE_MCS(2);
3143 OVERRIDE_MCS(3);
3144 OVERRIDE_MCS(4);
3145 OVERRIDE_MCS(5);
3146 OVERRIDE_MCS(6);
3147 OVERRIDE_MCS(7);
3148 OVERRIDE_MCS(8);
3149}
3150#endif /* CONFIG_VHT_OVERRIDES */
3151
3152
f64adcd7
JM
3153static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
3154{
3155#ifdef PCSC_FUNCS
3156 size_t len;
3157
3158 if (!wpa_s->conf->pcsc_reader)
3159 return 0;
3160
22cf7d73 3161 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
f64adcd7
JM
3162 if (!wpa_s->scard)
3163 return 1;
3164
3165 if (wpa_s->conf->pcsc_pin &&
3166 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
3167 scard_deinit(wpa_s->scard);
3168 wpa_s->scard = NULL;
3169 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
3170 return -1;
3171 }
3172
3173 len = sizeof(wpa_s->imsi) - 1;
3174 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
3175 scard_deinit(wpa_s->scard);
3176 wpa_s->scard = NULL;
3177 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
3178 return -1;
3179 }
3180 wpa_s->imsi[len] = '\0';
3181
3182 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3183
3184 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3185 wpa_s->imsi, wpa_s->mnc_len);
3186
3187 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3188 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3189#endif /* PCSC_FUNCS */
3190
3191 return 0;
3192}
3193
3194
306ae225
JM
3195int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3196{
3197 char *val, *pos;
3198
3199 ext_password_deinit(wpa_s->ext_pw);
3200 wpa_s->ext_pw = NULL;
3201 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3202
3203 if (!wpa_s->conf->ext_password_backend)
3204 return 0;
3205
3206 val = os_strdup(wpa_s->conf->ext_password_backend);
3207 if (val == NULL)
3208 return -1;
3209 pos = os_strchr(val, ':');
3210 if (pos)
3211 *pos++ = '\0';
3212
3213 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3214
3215 wpa_s->ext_pw = ext_password_init(val, pos);
3216 os_free(val);
3217 if (wpa_s->ext_pw == NULL) {
3218 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3219 return -1;
3220 }
3221 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3222
3223 return 0;
3224}
3225
3226
e4fa8b12
EP
3227static int wpas_check_wowlan_trigger(const char *start, const char *trigger,
3228 int capa_trigger, u8 *param_trigger)
3229{
3230 if (os_strcmp(start, trigger) != 0)
3231 return 0;
3232 if (!capa_trigger)
3233 return 0;
3234
3235 *param_trigger = 1;
3236 return 1;
3237}
3238
3239
a520bf4a
JM
3240static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
3241 struct wpa_driver_capa *capa)
e4fa8b12
EP
3242{
3243 struct wowlan_triggers triggers;
3244 char *start, *end, *buf;
3245 int last, ret;
3246
3247 if (!wpa_s->conf->wowlan_triggers)
3248 return 0;
3249
3250 buf = os_strdup(wpa_s->conf->wowlan_triggers);
3251 if (buf == NULL)
3252 return -1;
3253
3254 os_memset(&triggers, 0, sizeof(triggers));
3255
3256#define CHECK_TRIGGER(trigger) \
3257 wpas_check_wowlan_trigger(start, #trigger, \
3258 capa->wowlan_triggers.trigger, \
3259 &triggers.trigger)
3260
3261 start = buf;
3262 while (*start != '\0') {
3263 while (isblank(*start))
3264 start++;
3265 if (*start == '\0')
3266 break;
3267 end = start;
3268 while (!isblank(*end) && *end != '\0')
3269 end++;
3270 last = *end == '\0';
3271 *end = '\0';
3272
3273 if (!CHECK_TRIGGER(any) &&
3274 !CHECK_TRIGGER(disconnect) &&
3275 !CHECK_TRIGGER(magic_pkt) &&
3276 !CHECK_TRIGGER(gtk_rekey_failure) &&
3277 !CHECK_TRIGGER(eap_identity_req) &&
3278 !CHECK_TRIGGER(four_way_handshake) &&
3279 !CHECK_TRIGGER(rfkill_release)) {
3280 wpa_printf(MSG_DEBUG,
3281 "Unknown/unsupported wowlan trigger '%s'",
3282 start);
3283 ret = -1;
3284 goto out;
3285 }
3286
3287 if (last)
3288 break;
3289 start = end + 1;
3290 }
3291#undef CHECK_TRIGGER
3292
3293 ret = wpa_drv_wowlan(wpa_s, &triggers);
3294out:
3295 os_free(buf);
3296 return ret;
3297}
3298
3299
202dec2a
JM
3300static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3301 const char *rn)
3302{
3303 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3304 struct wpa_radio *radio;
3305
3306 while (rn && iface) {
3307 radio = iface->radio;
3308 if (radio && os_strcmp(rn, radio->name) == 0) {
3309 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3310 wpa_s->ifname, rn);
3311 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3312 return radio;
3313 }
b154a24e
TB
3314
3315 iface = iface->next;
202dec2a
JM
3316 }
3317
3318 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3319 wpa_s->ifname, rn ? rn : "N/A");
3320 radio = os_zalloc(sizeof(*radio));
3321 if (radio == NULL)
3322 return NULL;
3323
3324 if (rn)
3325 os_strlcpy(radio->name, rn, sizeof(radio->name));
3326 dl_list_init(&radio->ifaces);
b1ae396f 3327 dl_list_init(&radio->work);
202dec2a
JM
3328 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3329
3330 return radio;
3331}
3332
3333
b1ae396f
JM
3334static void radio_work_free(struct wpa_radio_work *work)
3335{
d12a51b5
JM
3336 if (work->wpa_s->scan_work == work) {
3337 /* This should not really happen. */
3338 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3339 work->type, work, work->started);
3340 work->wpa_s->scan_work = NULL;
3341 }
3342
1b5d4714
JM
3343#ifdef CONFIG_P2P
3344 if (work->wpa_s->p2p_scan_work == work) {
3345 /* This should not really happen. */
3346 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3347 work->type, work, work->started);
3348 work->wpa_s->p2p_scan_work = NULL;
3349 }
3350#endif /* CONFIG_P2P */
3351
b1ae396f
JM
3352 dl_list_del(&work->list);
3353 os_free(work);
3354}
3355
3356
3357static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3358{
3359 struct wpa_radio *radio = eloop_ctx;
3360 struct wpa_radio_work *work;
3361 struct os_reltime now, diff;
6428d0a7 3362 struct wpa_supplicant *wpa_s;
b1ae396f
JM
3363
3364 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3365 if (work == NULL)
3366 return;
3367
3368 if (work->started)
3369 return; /* already started and still in progress */
3370
6428d0a7
JM
3371 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3372 radio_list);
3373 if (wpa_s && wpa_s->external_scan_running) {
3374 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3375 return;
3376 }
3377
b1ae396f
JM
3378 os_get_reltime(&now);
3379 os_reltime_sub(&now, &work->time, &diff);
3380 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3381 work->type, work, diff.sec, diff.usec);
3382 work->started = 1;
3383 work->time = now;
3384 work->cb(work, 0);
3385}
3386
3387
b3253ebb
AO
3388/*
3389 * This function removes both started and pending radio works running on
3390 * the provided interface's radio.
3391 * Prior to the removal of the radio work, its callback (cb) is called with
3392 * deinit set to be 1. Each work's callback is responsible for clearing its
3393 * internal data and restoring to a correct state.
3394 * @wpa_s: wpa_supplicant data
3395 * @type: type of works to be removed
3396 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3397 * this interface's works.
3398 */
3399void radio_remove_works(struct wpa_supplicant *wpa_s,
3400 const char *type, int remove_all)
b1ae396f
JM
3401{
3402 struct wpa_radio_work *work, *tmp;
3403 struct wpa_radio *radio = wpa_s->radio;
3404
3405 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3406 list) {
b3253ebb 3407 if (type && os_strcmp(type, work->type) != 0)
b1ae396f 3408 continue;
b3253ebb
AO
3409
3410 /* skip other ifaces' works */
3411 if (!remove_all && work->wpa_s != wpa_s)
b1ae396f 3412 continue;
b3253ebb
AO
3413
3414 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3415 work->type, work, work->started ? " (started)" : "");
b1ae396f
JM
3416 work->cb(work, 1);
3417 radio_work_free(work);
3418 }
b3253ebb
AO
3419
3420 /* in case we removed the started work */
3421 radio_work_check_next(wpa_s);
b1ae396f
JM
3422}
3423
3424
202dec2a
JM
3425static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3426{
3427 struct wpa_radio *radio = wpa_s->radio;
3428
3429 if (!radio)
3430 return;
3431
3432 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3433 wpa_s->ifname, radio->name);
3434 dl_list_del(&wpa_s->radio_list);
c46235aa
AO
3435 radio_remove_works(wpa_s, NULL, 0);
3436 wpa_s->radio = NULL;
3437 if (!dl_list_empty(&radio->ifaces))
202dec2a
JM
3438 return; /* Interfaces remain for this radio */
3439
3440 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
b1ae396f 3441 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
202dec2a
JM
3442 os_free(radio);
3443}
3444
3445
6428d0a7 3446void radio_work_check_next(struct wpa_supplicant *wpa_s)
b1ae396f
JM
3447{
3448 struct wpa_radio *radio = wpa_s->radio;
3449
3450 if (dl_list_empty(&radio->work))
3451 return;
3452 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3453 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3454}
3455
3456
3457/**
3458 * radio_add_work - Add a radio work item
3459 * @wpa_s: Pointer to wpa_supplicant data
3460 * @freq: Frequency of the offchannel operation in MHz or 0
3461 * @type: Unique identifier for each type of work
3462 * @next: Force as the next work to be executed
3463 * @cb: Callback function for indicating when radio is available
3464 * @ctx: Context pointer for the work (work->ctx in cb())
3465 * Returns: 0 on success, -1 on failure
3466 *
3467 * This function is used to request time for an operation that requires
3468 * exclusive radio control. Once the radio is available, the registered callback
3469 * function will be called. radio_work_done() must be called once the exclusive
3470 * radio operation has been completed, so that the radio is freed for other
3471 * operations. The special case of deinit=1 is used to free the context data
3472 * during interface removal. That does not allow the callback function to start
3473 * the radio operation, i.e., it must free any resources allocated for the radio
3474 * work and return.
3475 *
3476 * The @freq parameter can be used to indicate a single channel on which the
3477 * offchannel operation will occur. This may allow multiple radio work
3478 * operations to be performed in parallel if they apply for the same channel.
3479 * Setting this to 0 indicates that the work item may use multiple channels or
3480 * requires exclusive control of the radio.
3481 */
3482int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3483 const char *type, int next,
3484 void (*cb)(struct wpa_radio_work *work, int deinit),
3485 void *ctx)
3486{
3487 struct wpa_radio_work *work;
3488 int was_empty;
3489
3490 work = os_zalloc(sizeof(*work));
3491 if (work == NULL)
3492 return -1;
3493 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3494 os_get_reltime(&work->time);
3495 work->freq = freq;
3496 work->type = type;
3497 work->wpa_s = wpa_s;
3498 work->cb = cb;
3499 work->ctx = ctx;
3500
3501 was_empty = dl_list_empty(&wpa_s->radio->work);
3502 if (next)
3503 dl_list_add(&wpa_s->radio->work, &work->list);
3504 else
3505 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3506 if (was_empty) {
3507 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3508 radio_work_check_next(wpa_s);
3509 }
3510
3511 return 0;
3512}
3513
3514
3515/**
3516 * radio_work_done - Indicate that a radio work item has been completed
3517 * @work: Completed work
3518 *
3519 * This function is called once the callback function registered with
3520 * radio_add_work() has completed its work.
3521 */
3522void radio_work_done(struct wpa_radio_work *work)
3523{
3524 struct wpa_supplicant *wpa_s = work->wpa_s;
3525 struct os_reltime now, diff;
1f965e62 3526 unsigned int started = work->started;
b1ae396f
JM
3527
3528 os_get_reltime(&now);
3529 os_reltime_sub(&now, &work->time, &diff);
1f965e62
JM
3530 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3531 work->type, work, started ? "done" : "canceled",
3532 diff.sec, diff.usec);
b1ae396f 3533 radio_work_free(work);
1f965e62
JM
3534 if (started)
3535 radio_work_check_next(wpa_s);
b1ae396f
JM
3536}
3537
3538
f0e30c84
JM
3539int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3540{
3541 struct wpa_radio_work *work;
3542 struct wpa_radio *radio = wpa_s->radio;
3543
3544 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3545 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3546 return 1;
3547 }
3548
3549 return 0;
3550}
3551
3552
73c00fd7
JM
3553static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3554 struct wpa_interface *iface)
3555{
202dec2a 3556 const char *ifname, *driver, *rn;
73c00fd7
JM
3557
3558 driver = iface->driver;
3559next_driver:
3560 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3561 return -1;
3562
3563 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3564 if (wpa_s->drv_priv == NULL) {
3565 const char *pos;
3566 pos = driver ? os_strchr(driver, ',') : NULL;
3567 if (pos) {
3568 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3569 "driver interface - try next driver wrapper");
3570 driver = pos + 1;
3571 goto next_driver;
3572 }
3573 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3574 "interface");
3575 return -1;
3576 }
3577 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3578 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3579 "driver_param '%s'", wpa_s->conf->driver_param);
3580 return -1;
3581 }
3582
3583 ifname = wpa_drv_get_ifname(wpa_s);
3584 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3585 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3586 "interface name with '%s'", ifname);
3587 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3588 }
3589
95bf699f 3590 rn = wpa_driver_get_radio_name(wpa_s);
202dec2a
JM
3591 if (rn && rn[0] == '\0')
3592 rn = NULL;
3593
3594 wpa_s->radio = radio_add_interface(wpa_s, rn);
3595 if (wpa_s->radio == NULL)
3596 return -1;
3597
73c00fd7
JM
3598 return 0;
3599}
3600
3601
6fc6879b
JM
3602static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3603 struct wpa_interface *iface)
3604{
362f781e
JM
3605 struct wpa_driver_capa capa;
3606
6fc6879b
JM
3607 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3608 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3609 iface->confname ? iface->confname : "N/A",
3610 iface->driver ? iface->driver : "default",
3611 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3612 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3613
6fc6879b
JM
3614 if (iface->confname) {
3615#ifdef CONFIG_BACKEND_FILE
3616 wpa_s->confname = os_rel2abs_path(iface->confname);
3617 if (wpa_s->confname == NULL) {
3618 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3619 "for configuration file '%s'.",
3620 iface->confname);
3621 return -1;
3622 }
3623 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3624 iface->confname, wpa_s->confname);
3625#else /* CONFIG_BACKEND_FILE */
3626 wpa_s->confname = os_strdup(iface->confname);
3627#endif /* CONFIG_BACKEND_FILE */
e6304cad 3628 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
6fc6879b
JM
3629 if (wpa_s->conf == NULL) {
3630 wpa_printf(MSG_ERROR, "Failed to read or parse "
3631 "configuration '%s'.", wpa_s->confname);
3632 return -1;
3633 }
e6304cad
DS
3634 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3635 wpa_config_read(wpa_s->confanother, wpa_s->conf);
6fc6879b 3636
c16a7590
IP
3637#ifdef CONFIG_P2P
3638 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3639 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3640#endif /* CONFIG_P2P */
3641
6fc6879b
JM
3642 /*
3643 * Override ctrl_interface and driver_param if set on command
3644 * line.
3645 */
3646 if (iface->ctrl_interface) {
3647 os_free(wpa_s->conf->ctrl_interface);
3648 wpa_s->conf->ctrl_interface =
3649 os_strdup(iface->ctrl_interface);
3650 }
3651
3652 if (iface->driver_param) {
3653 os_free(wpa_s->conf->driver_param);
3654 wpa_s->conf->driver_param =
3655 os_strdup(iface->driver_param);
3656 }
78f79fe5
JM
3657
3658 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3659 os_free(wpa_s->conf->ctrl_interface);
3660 wpa_s->conf->ctrl_interface = NULL;
3661 }
6fc6879b
JM
3662 } else
3663 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3664 iface->driver_param);
3665
3666 if (wpa_s->conf == NULL) {
3667 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3668 return -1;
3669 }
3670
3671 if (iface->ifname == NULL) {
3672 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3673 return -1;
3674 }
3675 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3676 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3677 iface->ifname);
3678 return -1;
3679 }
3680 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3681
3682 if (iface->bridge_ifname) {
3683 if (os_strlen(iface->bridge_ifname) >=
3684 sizeof(wpa_s->bridge_ifname)) {
3685 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3686 "name '%s'.", iface->bridge_ifname);
3687 return -1;
3688 }
3689 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3690 sizeof(wpa_s->bridge_ifname));
3691 }
3692
6fc6879b
JM
3693 /* RSNA Supplicant Key Management - INITIALIZE */
3694 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3695 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3696
3697 /* Initialize driver interface and register driver event handler before
3698 * L2 receive handler so that association events are processed before
3699 * EAPOL-Key packets if both become available for the same select()
3700 * call. */
73c00fd7 3701 if (wpas_init_driver(wpa_s, iface) < 0)
362f781e
JM
3702 return -1;
3703
6fc6879b
JM
3704 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3705 return -1;
3706
3707 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3708 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3709 NULL);
3710 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3711
3712 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3713 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3714 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
f049052b
BG
3715 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3716 "dot11RSNAConfigPMKLifetime");
6fc6879b
JM
3717 return -1;
3718 }
3719
3720 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3721 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3722 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
f049052b 3723 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
6fc6879b
JM
3724 "dot11RSNAConfigPMKReauthThreshold");
3725 return -1;
3726 }
3727
3728 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3729 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3730 wpa_s->conf->dot11RSNAConfigSATimeout)) {
f049052b
BG
3731 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3732 "dot11RSNAConfigSATimeout");
6fc6879b
JM
3733 return -1;
3734 }
3735
6bf731e8
CL
3736 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3737 &wpa_s->hw.num_modes,
3738 &wpa_s->hw.flags);
3739
814782b9 3740 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
c58ab8f2 3741 wpa_s->drv_capa_known = 1;
814782b9 3742 wpa_s->drv_flags = capa.flags;
349493bd 3743 wpa_s->drv_enc = capa.enc;
4f73d88a 3744 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
814782b9 3745 wpa_s->max_scan_ssids = capa.max_scan_ssids;
cbdf3507
LC
3746 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3747 wpa_s->sched_scan_supported = capa.sched_scan_supported;
b59e6f26 3748 wpa_s->max_match_sets = capa.max_match_sets;
814782b9 3749 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
c4ea4c5c 3750 wpa_s->max_stations = capa.max_stations;
8cd6b7bc
JB
3751 wpa_s->extended_capa = capa.extended_capa;
3752 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3753 wpa_s->extended_capa_len = capa.extended_capa_len;
4752147d
IP
3754 wpa_s->num_multichan_concurrent =
3755 capa.num_multichan_concurrent;
814782b9
JM
3756 }
3757 if (wpa_s->max_remain_on_chan == 0)
3758 wpa_s->max_remain_on_chan = 1000;
3759
c68f6200
AS
3760 /*
3761 * Only take p2p_mgmt parameters when P2P Device is supported.
3762 * Doing it here as it determines whether l2_packet_init() will be done
3763 * during wpa_supplicant_driver_init().
3764 */
3765 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3766 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3767 else
3768 iface->p2p_mgmt = 1;
3769
4752147d
IP
3770 if (wpa_s->num_multichan_concurrent == 0)
3771 wpa_s->num_multichan_concurrent = 1;
3772
6fc6879b
JM
3773 if (wpa_supplicant_driver_init(wpa_s) < 0)
3774 return -1;
3775
281ff0aa 3776#ifdef CONFIG_TDLS
1c42b42f
JM
3777 if ((!iface->p2p_mgmt ||
3778 !(wpa_s->drv_flags &
3779 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3780 wpa_tdls_init(wpa_s->wpa))
281ff0aa
GP
3781 return -1;
3782#endif /* CONFIG_TDLS */
3783
315ce40a
JM
3784 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3785 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
f049052b 3786 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
6d158490
LR
3787 return -1;
3788 }
3789
116654ce
JM
3790 if (wpas_wps_init(wpa_s))
3791 return -1;
3792
6fc6879b
JM
3793 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3794 return -1;
3795 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3796
3797 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3798 if (wpa_s->ctrl_iface == NULL) {
3799 wpa_printf(MSG_ERROR,
3800 "Failed to initialize control interface '%s'.\n"
3801 "You may have another wpa_supplicant process "
3802 "already running or the file was\n"
3803 "left by an unclean termination of wpa_supplicant "
3804 "in which case you will need\n"
3805 "to manually remove this file before starting "
3806 "wpa_supplicant again.\n",
3807 wpa_s->conf->ctrl_interface);
3808 return -1;
3809 }
3810
04ea7b79
JM
3811 wpa_s->gas = gas_query_init(wpa_s);
3812 if (wpa_s->gas == NULL) {
3813 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3814 return -1;
3815 }
3816
c68f6200 3817 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
f049052b 3818 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
b22128ef
JM
3819 return -1;
3820 }
b22128ef 3821
83922c2d
JM
3822 if (wpa_bss_init(wpa_s) < 0)
3823 return -1;
83922c2d 3824
e4fa8b12
EP
3825 /*
3826 * Set Wake-on-WLAN triggers, if configured.
3827 * Note: We don't restore/remove the triggers on shutdown (it doesn't
3828 * have effect anyway when the interface is down).
3829 */
3830 if (wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
3831 return -1;
3832
ec7b97ab
JM
3833#ifdef CONFIG_EAP_PROXY
3834{
3835 size_t len;
07041c6f
NJ
3836 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3837 &len);
ec7b97ab
JM
3838 if (wpa_s->mnc_len > 0) {
3839 wpa_s->imsi[len] = '\0';
3840 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3841 wpa_s->imsi, wpa_s->mnc_len);
3842 } else {
3843 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3844 }
3845}
3846#endif /* CONFIG_EAP_PROXY */
3847
f64adcd7
JM
3848 if (pcsc_reader_init(wpa_s) < 0)
3849 return -1;
3850
306ae225
JM
3851 if (wpas_init_ext_pw(wpa_s) < 0)
3852 return -1;
3853
6fc6879b
JM
3854 return 0;
3855}
3856
3857
2ee055b3 3858static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
df509539 3859 int notify, int terminate)
6fc6879b 3860{
e679f140 3861 wpa_s->disconnected = 1;
6fc6879b
JM
3862 if (wpa_s->drv_priv) {
3863 wpa_supplicant_deauthenticate(wpa_s,
3864 WLAN_REASON_DEAUTH_LEAVING);
3865
6fc6879b
JM
3866 wpa_drv_set_countermeasures(wpa_s, 0);
3867 wpa_clear_keys(wpa_s, NULL);
3868 }
3869
8e56d189 3870 wpa_supplicant_cleanup(wpa_s);
bd10d938 3871 wpas_p2p_deinit_iface(wpa_s);
ab28911d 3872
1f965e62 3873 wpas_ctrl_radio_work_flush(wpa_s);
202dec2a
JM
3874 radio_remove_interface(wpa_s);
3875
6fc6879b
JM
3876 if (wpa_s->drv_priv)
3877 wpa_drv_deinit(wpa_s);
2523ff6e
DS
3878
3879 if (notify)
3880 wpas_notify_iface_removed(wpa_s);
f0811516
DS
3881
3882 if (terminate)
3883 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
3884
3885 if (wpa_s->ctrl_iface) {
3886 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3887 wpa_s->ctrl_iface = NULL;
3888 }
3889
3890 if (wpa_s->conf != NULL) {
3891 wpa_config_free(wpa_s->conf);
3892 wpa_s->conf = NULL;
3893 }
18e00b5e
JM
3894
3895 os_free(wpa_s);
6fc6879b
JM
3896}
3897
3898
3899/**
3900 * wpa_supplicant_add_iface - Add a new network interface
3901 * @global: Pointer to global data from wpa_supplicant_init()
3902 * @iface: Interface configuration options
3903 * Returns: Pointer to the created interface or %NULL on failure
3904 *
3905 * This function is used to add new network interfaces for %wpa_supplicant.
3906 * This can be called before wpa_supplicant_run() to add interfaces before the
3907 * main event loop has been started. In addition, new interfaces can be added
3908 * dynamically while %wpa_supplicant is already running. This could happen,
3909 * e.g., when a hotplug network adapter is inserted.
3910 */
3911struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3912 struct wpa_interface *iface)
3913{
3914 struct wpa_supplicant *wpa_s;
d27df100 3915 struct wpa_interface t_iface;
8e56d189 3916 struct wpa_ssid *ssid;
6fc6879b
JM
3917
3918 if (global == NULL || iface == NULL)
3919 return NULL;
3920
3921 wpa_s = wpa_supplicant_alloc();
3922 if (wpa_s == NULL)
3923 return NULL;
3924
d8222ae3
JM
3925 wpa_s->global = global;
3926
d27df100
JM
3927 t_iface = *iface;
3928 if (global->params.override_driver) {
3929 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3930 "('%s' -> '%s')",
3931 iface->driver, global->params.override_driver);
3932 t_iface.driver = global->params.override_driver;
3933 }
3934 if (global->params.override_ctrl_interface) {
3935 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3936 "ctrl_interface ('%s' -> '%s')",
3937 iface->ctrl_interface,
3938 global->params.override_ctrl_interface);
3939 t_iface.ctrl_interface =
3940 global->params.override_ctrl_interface;
3941 }
3942 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
6fc6879b
JM
3943 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3944 iface->ifname);
df509539 3945 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
6fc6879b
JM
3946 return NULL;
3947 }
3948
dc461de4
WS
3949 /* Notify the control interfaces about new iface */
3950 if (wpas_notify_iface_added(wpa_s)) {
df509539 3951 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
6fc6879b
JM
3952 return NULL;
3953 }
1bd3f426 3954
8e56d189
JM
3955 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3956 wpas_notify_network_added(wpa_s, ssid);
3957
6fc6879b
JM
3958 wpa_s->next = global->ifaces;
3959 global->ifaces = wpa_s;
3960
f049052b 3961 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
99218999 3962 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b
JM
3963
3964 return wpa_s;
3965}
3966
3967
3968/**
3969 * wpa_supplicant_remove_iface - Remove a network interface
3970 * @global: Pointer to global data from wpa_supplicant_init()
3971 * @wpa_s: Pointer to the network interface to be removed
3972 * Returns: 0 if interface was removed, -1 if interface was not found
3973 *
3974 * This function can be used to dynamically remove network interfaces from
3975 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3976 * addition, this function is used to remove all remaining interfaces when
3977 * %wpa_supplicant is terminated.
3978 */
3979int wpa_supplicant_remove_iface(struct wpa_global *global,
df509539
DS
3980 struct wpa_supplicant *wpa_s,
3981 int terminate)
6fc6879b
JM
3982{
3983 struct wpa_supplicant *prev;
3984
3985 /* Remove interface from the global list of interfaces */
3986 prev = global->ifaces;
3987 if (prev == wpa_s) {
3988 global->ifaces = wpa_s->next;
3989 } else {
3990 while (prev && prev->next != wpa_s)
3991 prev = prev->next;
3992 if (prev == NULL)
3993 return -1;
3994 prev->next = wpa_s->next;
3995 }
3996
f049052b 3997 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
6fc6879b 3998
b22128ef
JM
3999 if (global->p2p_group_formation == wpa_s)
4000 global->p2p_group_formation = NULL;
dbca75f8
JM
4001 if (global->p2p_invite_group == wpa_s)
4002 global->p2p_invite_group = NULL;
df509539 4003 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
6fc6879b
JM
4004
4005 return 0;
4006}
4007
4008
cf83fb0b
PS
4009/**
4010 * wpa_supplicant_get_eap_mode - Get the current EAP mode
4011 * @wpa_s: Pointer to the network interface
4012 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
4013 */
4014const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
4015{
4016 const char *eapol_method;
4017
4018 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
4019 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
4020 return "NO-EAP";
4021 }
4022
4023 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
4024 if (eapol_method == NULL)
4025 return "UNKNOWN-EAP";
4026
4027 return eapol_method;
4028}
4029
4030
6fc6879b
JM
4031/**
4032 * wpa_supplicant_get_iface - Get a new network interface
4033 * @global: Pointer to global data from wpa_supplicant_init()
4034 * @ifname: Interface name
4035 * Returns: Pointer to the interface or %NULL if not found
4036 */
4037struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
4038 const char *ifname)
4039{
4040 struct wpa_supplicant *wpa_s;
4041
4042 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4043 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4044 return wpa_s;
4045 }
4046 return NULL;
4047}
4048
4049
50b16da1 4050#ifndef CONFIG_NO_WPA_MSG
4f1495ae
BG
4051static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
4052{
4053 struct wpa_supplicant *wpa_s = ctx;
4054 if (wpa_s == NULL)
4055 return NULL;
4056 return wpa_s->ifname;
4057}
50b16da1 4058#endif /* CONFIG_NO_WPA_MSG */
4f1495ae
BG
4059
4060
6fc6879b
JM
4061/**
4062 * wpa_supplicant_init - Initialize %wpa_supplicant
4063 * @params: Parameters for %wpa_supplicant
4064 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
4065 *
4066 * This function is used to initialize %wpa_supplicant. After successful
4067 * initialization, the returned data pointer can be used to add and remove
4068 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
4069 */
4070struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
4071{
4072 struct wpa_global *global;
ac305589 4073 int ret, i;
6fc6879b
JM
4074
4075 if (params == NULL)
4076 return NULL;
4077
39e7d718
JM
4078#ifdef CONFIG_DRIVER_NDIS
4079 {
4080 void driver_ndis_init_ops(void);
4081 driver_ndis_init_ops();
4082 }
4083#endif /* CONFIG_DRIVER_NDIS */
4084
50b16da1 4085#ifndef CONFIG_NO_WPA_MSG
4f1495ae 4086 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
50b16da1 4087#endif /* CONFIG_NO_WPA_MSG */
4f1495ae 4088
6fc6879b 4089 wpa_debug_open_file(params->wpa_debug_file_path);
daa70d49
SL
4090 if (params->wpa_debug_syslog)
4091 wpa_debug_open_syslog();
4f68895e
JB
4092 if (params->wpa_debug_tracing) {
4093 ret = wpa_debug_open_linux_tracing();
4094 if (ret) {
4095 wpa_printf(MSG_ERROR,
4096 "Failed to enable trace logging");
4097 return NULL;
4098 }
4099 }
6fc6879b 4100
12760815 4101 ret = eap_register_methods();
6fc6879b
JM
4102 if (ret) {
4103 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
4104 if (ret == -2)
4105 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
4106 "the same EAP type.");
4107 return NULL;
4108 }
4109
4110 global = os_zalloc(sizeof(*global));
4111 if (global == NULL)
4112 return NULL;
b22128ef
JM
4113 dl_list_init(&global->p2p_srv_bonjour);
4114 dl_list_init(&global->p2p_srv_upnp);
6fc6879b
JM
4115 global->params.daemonize = params->daemonize;
4116 global->params.wait_for_monitor = params->wait_for_monitor;
4117 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
4118 if (params->pid_file)
4119 global->params.pid_file = os_strdup(params->pid_file);
4120 if (params->ctrl_interface)
4121 global->params.ctrl_interface =
4122 os_strdup(params->ctrl_interface);
29257565
JM
4123 if (params->ctrl_interface_group)
4124 global->params.ctrl_interface_group =
4125 os_strdup(params->ctrl_interface_group);
d27df100
JM
4126 if (params->override_driver)
4127 global->params.override_driver =
4128 os_strdup(params->override_driver);
4129 if (params->override_ctrl_interface)
4130 global->params.override_ctrl_interface =
4131 os_strdup(params->override_ctrl_interface);
6fc6879b
JM
4132 wpa_debug_level = global->params.wpa_debug_level =
4133 params->wpa_debug_level;
4134 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
4135 params->wpa_debug_show_keys;
4136 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
4137 params->wpa_debug_timestamp;
4138
f19858f5
JM
4139 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
4140
0456ea16 4141 if (eloop_init()) {
6fc6879b
JM
4142 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
4143 wpa_supplicant_deinit(global);
4144 return NULL;
4145 }
4146
38e24575 4147 random_init(params->entropy_file);
d47fa330 4148
6fc6879b
JM
4149 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
4150 if (global->ctrl_iface == NULL) {
4151 wpa_supplicant_deinit(global);
4152 return NULL;
4153 }
4154
dc461de4
WS
4155 if (wpas_notify_supplicant_initialized(global)) {
4156 wpa_supplicant_deinit(global);
4157 return NULL;
6fc6879b
JM
4158 }
4159
c5121837 4160 for (i = 0; wpa_drivers[i]; i++)
ac305589
JM
4161 global->drv_count++;
4162 if (global->drv_count == 0) {
4163 wpa_printf(MSG_ERROR, "No drivers enabled");
4164 wpa_supplicant_deinit(global);
4165 return NULL;
4166 }
4167 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
4168 if (global->drv_priv == NULL) {
4169 wpa_supplicant_deinit(global);
4170 return NULL;
4171 }
ac305589 4172
9675ce35
JM
4173#ifdef CONFIG_WIFI_DISPLAY
4174 if (wifi_display_init(global) < 0) {
4175 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
4176 wpa_supplicant_deinit(global);
4177 return NULL;
4178 }
4179#endif /* CONFIG_WIFI_DISPLAY */
4180
6fc6879b
JM
4181 return global;
4182}
4183
4184
4185/**
4186 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4187 * @global: Pointer to global data from wpa_supplicant_init()
4188 * Returns: 0 after successful event loop run, -1 on failure
4189 *
4190 * This function starts the main event loop and continues running as long as
4191 * there are any remaining events. In most cases, this function is running as
4192 * long as the %wpa_supplicant process in still in use.
4193 */
4194int wpa_supplicant_run(struct wpa_global *global)
4195{
4196 struct wpa_supplicant *wpa_s;
4197
4198 if (global->params.daemonize &&
4199 wpa_supplicant_daemon(global->params.pid_file))
4200 return -1;
4201
4202 if (global->params.wait_for_monitor) {
4203 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4204 if (wpa_s->ctrl_iface)
4205 wpa_supplicant_ctrl_iface_wait(
4206 wpa_s->ctrl_iface);
4207 }
4208
0456ea16
JM
4209 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4210 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
6fc6879b
JM
4211
4212 eloop_run();
4213
4214 return 0;
4215}
4216
4217
4218/**
4219 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4220 * @global: Pointer to global data from wpa_supplicant_init()
4221 *
4222 * This function is called to deinitialize %wpa_supplicant and to free all
4223 * allocated resources. Remaining network interfaces will also be removed.
4224 */
4225void wpa_supplicant_deinit(struct wpa_global *global)
4226{
ac305589
JM
4227 int i;
4228
6fc6879b
JM
4229 if (global == NULL)
4230 return;
4231
9675ce35
JM
4232#ifdef CONFIG_WIFI_DISPLAY
4233 wifi_display_deinit(global);
4234#endif /* CONFIG_WIFI_DISPLAY */
b22128ef 4235
6fc6879b 4236 while (global->ifaces)
df509539 4237 wpa_supplicant_remove_iface(global, global->ifaces, 1);
6fc6879b
JM
4238
4239 if (global->ctrl_iface)
4240 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
dc461de4
WS
4241
4242 wpas_notify_supplicant_deinitialized(global);
6fc6879b
JM
4243
4244 eap_peer_unregister_methods();
3ec97afe
JM
4245#ifdef CONFIG_AP
4246 eap_server_unregister_methods();
4247#endif /* CONFIG_AP */
6fc6879b 4248
c5121837 4249 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
ac305589
JM
4250 if (!global->drv_priv[i])
4251 continue;
c5121837 4252 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
ac305589
JM
4253 }
4254 os_free(global->drv_priv);
4255
d47fa330
JM
4256 random_deinit();
4257
6fc6879b
JM
4258 eloop_destroy();
4259
4260 if (global->params.pid_file) {
4261 os_daemonize_terminate(global->params.pid_file);
4262 os_free(global->params.pid_file);
4263 }
4264 os_free(global->params.ctrl_interface);
29257565 4265 os_free(global->params.ctrl_interface_group);
d27df100
JM
4266 os_free(global->params.override_driver);
4267 os_free(global->params.override_ctrl_interface);
6fc6879b 4268
af8a827b 4269 os_free(global->p2p_disallow_freq.range);
253f2e37 4270 os_free(global->p2p_go_avoid_freq.range);
01a57fe4 4271 os_free(global->add_psk);
6f3bc72b 4272
6fc6879b 4273 os_free(global);
daa70d49 4274 wpa_debug_close_syslog();
6fc6879b 4275 wpa_debug_close_file();
4f68895e 4276 wpa_debug_close_linux_tracing();
6fc6879b 4277}
611aea7d
JM
4278
4279
4280void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4281{
849b5dc7
JM
4282 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4283 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4284 char country[3];
4285 country[0] = wpa_s->conf->country[0];
4286 country[1] = wpa_s->conf->country[1];
4287 country[2] = '\0';
4288 if (wpa_drv_set_country(wpa_s, country) < 0) {
4289 wpa_printf(MSG_ERROR, "Failed to set country code "
4290 "'%s'", country);
4291 }
4292 }
4293
306ae225
JM
4294 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4295 wpas_init_ext_pw(wpa_s);
4296
611aea7d
JM
4297#ifdef CONFIG_WPS
4298 wpas_wps_update_config(wpa_s);
4299#endif /* CONFIG_WPS */
b22128ef 4300 wpas_p2p_update_config(wpa_s);
611aea7d
JM
4301 wpa_s->conf->changed_parameters = 0;
4302}
2f9c6aa6
JM
4303
4304
0fb337c1
JM
4305static void add_freq(int *freqs, int *num_freqs, int freq)
4306{
4307 int i;
4308
4309 for (i = 0; i < *num_freqs; i++) {
4310 if (freqs[i] == freq)
4311 return;
4312 }
4313
4314 freqs[*num_freqs] = freq;
4315 (*num_freqs)++;
4316}
4317
4318
4319static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4320{
4321 struct wpa_bss *bss, *cbss;
4322 const int max_freqs = 10;
4323 int *freqs;
4324 int num_freqs = 0;
4325
4326 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4327 if (freqs == NULL)
4328 return NULL;
4329
4330 cbss = wpa_s->current_bss;
4331
4332 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4333 if (bss == cbss)
4334 continue;
4335 if (bss->ssid_len == cbss->ssid_len &&
4336 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4337 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4338 add_freq(freqs, &num_freqs, bss->freq);
4339 if (num_freqs == max_freqs)
4340 break;
4341 }
4342 }
4343
4344 if (num_freqs == 0) {
4345 os_free(freqs);
4346 freqs = NULL;
4347 }
4348
4349 return freqs;
4350}
4351
4352
4353void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4354{
4355 int timeout;
4356 int count;
4357 int *freqs = NULL;
4358
6ac4b15e
JM
4359 wpas_connect_work_done(wpa_s);
4360
5fd9fb27
JM
4361 /*
4362 * Remove possible authentication timeout since the connection failed.
4363 */
4364 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4365
0cdb93fe
JM
4366 if (wpa_s->disconnected) {
4367 /*
4368 * There is no point in blacklisting the AP if this event is
4369 * generated based on local request to disconnect.
4370 */
4371 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4372 "indication since interface has been put into "
4373 "disconnected state");
4374 return;
4375 }
4376
0fb337c1
JM
4377 /*
4378 * Add the failed BSSID into the blacklist and speed up next scan
4379 * attempt if there could be other APs that could accept association.
4380 * The current blacklist count indicates how many times we have tried
4381 * connecting to this AP and multiple attempts mean that other APs are
4382 * either not available or has already been tried, so that we can start
4383 * increasing the delay here to avoid constant scanning.
4384 */
4385 count = wpa_blacklist_add(wpa_s, bssid);
4386 if (count == 1 && wpa_s->current_bss) {
4387 /*
4388 * This BSS was not in the blacklist before. If there is
4389 * another BSS available for the same ESS, we should try that
4390 * next. Otherwise, we may as well try this one once more
4391 * before allowing other, likely worse, ESSes to be considered.
4392 */
4393 freqs = get_bss_freqs_in_ess(wpa_s);
4394 if (freqs) {
f049052b
BG
4395 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4396 "has been seen; try it next");
0fb337c1
JM
4397 wpa_blacklist_add(wpa_s, bssid);
4398 /*
4399 * On the next scan, go through only the known channels
4400 * used in this ESS based on previous scans to speed up
4401 * common load balancing use case.
4402 */
4403 os_free(wpa_s->next_scan_freqs);
4404 wpa_s->next_scan_freqs = freqs;
4405 }
4406 }
4407
f1a52633
JM
4408 /*
4409 * Add previous failure count in case the temporary blacklist was
4410 * cleared due to no other BSSes being available.
4411 */
4412 count += wpa_s->extra_blacklist_count;
4413
dd579704
JM
4414 if (count > 3 && wpa_s->current_ssid) {
4415 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4416 "consider temporary network disabling");
b19c098e 4417 wpas_auth_failed(wpa_s, "CONN_FAILED");
dd579704
JM
4418 }
4419
0fb337c1
JM
4420 switch (count) {
4421 case 1:
4422 timeout = 100;
4423 break;
4424 case 2:
4425 timeout = 500;
4426 break;
4427 case 3:
4428 timeout = 1000;
4429 break;
f1a52633 4430 case 4:
0fb337c1 4431 timeout = 5000;
f1a52633
JM
4432 break;
4433 default:
4434 timeout = 10000;
4435 break;
0fb337c1
JM
4436 }
4437
f1a52633
JM
4438 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4439 "ms", count, timeout);
4440
0fb337c1
JM
4441 /*
4442 * TODO: if more than one possible AP is available in scan results,
4443 * could try the other ones before requesting a new scan.
4444 */
4445 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4446 1000 * (timeout % 1000));
4447}
22628eca
JM
4448
4449
4450int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4451{
4452 return wpa_s->conf->ap_scan == 2 ||
4453 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4454}
d2118814
JM
4455
4456
4457#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4458int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4459 struct wpa_ssid *ssid,
4460 const char *field,
4461 const char *value)
4462{
4463#ifdef IEEE8021X_EAPOL
4464 struct eap_peer_config *eap = &ssid->eap;
4465
4466 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4467 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4468 (const u8 *) value, os_strlen(value));
4469
4470 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4471 case WPA_CTRL_REQ_EAP_IDENTITY:
4472 os_free(eap->identity);
4473 eap->identity = (u8 *) os_strdup(value);
4474 eap->identity_len = os_strlen(value);
4475 eap->pending_req_identity = 0;
4476 if (ssid == wpa_s->current_ssid)
4477 wpa_s->reassociate = 1;
4478 break;
4479 case WPA_CTRL_REQ_EAP_PASSWORD:
19c48da0 4480 bin_clear_free(eap->password, eap->password_len);
d2118814
JM
4481 eap->password = (u8 *) os_strdup(value);
4482 eap->password_len = os_strlen(value);
4483 eap->pending_req_password = 0;
4484 if (ssid == wpa_s->current_ssid)
4485 wpa_s->reassociate = 1;
4486 break;
4487 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
19c48da0 4488 bin_clear_free(eap->new_password, eap->new_password_len);
d2118814
JM
4489 eap->new_password = (u8 *) os_strdup(value);
4490 eap->new_password_len = os_strlen(value);
4491 eap->pending_req_new_password = 0;
4492 if (ssid == wpa_s->current_ssid)
4493 wpa_s->reassociate = 1;
4494 break;
4495 case WPA_CTRL_REQ_EAP_PIN:
19c48da0 4496 str_clear_free(eap->pin);
d2118814
JM
4497 eap->pin = os_strdup(value);
4498 eap->pending_req_pin = 0;
4499 if (ssid == wpa_s->current_ssid)
4500 wpa_s->reassociate = 1;
4501 break;
4502 case WPA_CTRL_REQ_EAP_OTP:
19c48da0 4503 bin_clear_free(eap->otp, eap->otp_len);
d2118814
JM
4504 eap->otp = (u8 *) os_strdup(value);
4505 eap->otp_len = os_strlen(value);
4506 os_free(eap->pending_req_otp);
4507 eap->pending_req_otp = NULL;
4508 eap->pending_req_otp_len = 0;
4509 break;
4510 case WPA_CTRL_REQ_EAP_PASSPHRASE:
19c48da0
JM
4511 str_clear_free(eap->private_key_passwd);
4512 eap->private_key_passwd = os_strdup(value);
d2118814
JM
4513 eap->pending_req_passphrase = 0;
4514 if (ssid == wpa_s->current_ssid)
4515 wpa_s->reassociate = 1;
4516 break;
a5d44ac0 4517 case WPA_CTRL_REQ_SIM:
19c48da0 4518 str_clear_free(eap->external_sim_resp);
a5d44ac0
JM
4519 eap->external_sim_resp = os_strdup(value);
4520 break;
d2118814
JM
4521 default:
4522 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4523 return -1;
4524 }
4525
4526 return 0;
4527#else /* IEEE8021X_EAPOL */
4528 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4529 return -1;
4530#endif /* IEEE8021X_EAPOL */
4531}
4532#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
349493bd
JM
4533
4534
4535int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4536{
4537 int i;
4538 unsigned int drv_enc;
4539
4540 if (ssid == NULL)
4541 return 1;
4542
4543 if (ssid->disabled)
4544 return 1;
4545
4546 if (wpa_s && wpa_s->drv_capa_known)
4547 drv_enc = wpa_s->drv_enc;
4548 else
4549 drv_enc = (unsigned int) -1;
4550
4551 for (i = 0; i < NUM_WEP_KEYS; i++) {
4552 size_t len = ssid->wep_key_len[i];
4553 if (len == 0)
4554 continue;
4555 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4556 continue;
4557 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4558 continue;
4559 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4560 continue;
4561 return 1; /* invalid WEP key */
4562 }
4563
9173b16f 4564 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
759ff2f0 4565 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
2518aad3
JM
4566 return 1;
4567
349493bd
JM
4568 return 0;
4569}
b9cfc09a
JJ
4570
4571
4572int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
4573{
4574 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
4575 return 1;
4576 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
4577 return 0;
4578 return -1;
4579}
00e5e3d5
JM
4580
4581
b19c098e 4582void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
00e5e3d5
JM
4583{
4584 struct wpa_ssid *ssid = wpa_s->current_ssid;
4585 int dur;
4e1eae1d 4586 struct os_reltime now;
00e5e3d5
JM
4587
4588 if (ssid == NULL) {
4589 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4590 "SSID block");
4591 return;
4592 }
4593
4594 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4595 return;
4596
4597 ssid->auth_failures++;
cbf41ca7
SL
4598
4599#ifdef CONFIG_P2P
4600 if (ssid->p2p_group &&
4601 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4602 /*
4603 * Skip the wait time since there is a short timeout on the
4604 * connection to a P2P group.
4605 */
4606 return;
4607 }
4608#endif /* CONFIG_P2P */
4609
00e5e3d5
JM
4610 if (ssid->auth_failures > 50)
4611 dur = 300;
00e5e3d5 4612 else if (ssid->auth_failures > 10)
8a77f1be 4613 dur = 120;
00e5e3d5 4614 else if (ssid->auth_failures > 5)
8a77f1be
JM
4615 dur = 90;
4616 else if (ssid->auth_failures > 3)
4617 dur = 60;
4618 else if (ssid->auth_failures > 2)
00e5e3d5
JM
4619 dur = 30;
4620 else if (ssid->auth_failures > 1)
4621 dur = 20;
4622 else
4623 dur = 10;
4624
8a77f1be
JM
4625 if (ssid->auth_failures > 1 &&
4626 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4627 dur += os_random() % (ssid->auth_failures * 10);
4628
4e1eae1d 4629 os_get_reltime(&now);
00e5e3d5
JM
4630 if (now.sec + dur <= ssid->disabled_until.sec)
4631 return;
4632
4633 ssid->disabled_until.sec = now.sec + dur;
4634
4635 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
b19c098e 4636 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
00e5e3d5 4637 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
b19c098e 4638 ssid->auth_failures, dur, reason);
00e5e3d5
JM
4639}
4640
4641
4642void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4643 struct wpa_ssid *ssid, int clear_failures)
4644{
4645 if (ssid == NULL)
4646 return;
4647
4648 if (ssid->disabled_until.sec) {
4649 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4650 "id=%d ssid=\"%s\"",
4651 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4652 }
4653 ssid->disabled_until.sec = 0;
4654 ssid->disabled_until.usec = 0;
4655 if (clear_failures)
4656 ssid->auth_failures = 0;
4657}
6407f413
JM
4658
4659
4660int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4661{
4662 size_t i;
4663
4664 if (wpa_s->disallow_aps_bssid == NULL)
4665 return 0;
4666
4667 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4668 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4669 bssid, ETH_ALEN) == 0)
4670 return 1;
4671 }
4672
4673 return 0;
4674}
4675
4676
4677int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4678 size_t ssid_len)
4679{
4680 size_t i;
4681
4682 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4683 return 0;
4684
4685 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4686 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4687 if (ssid_len == s->ssid_len &&
4688 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4689 return 1;
4690 }
4691
4692 return 0;
4693}
9796a86c
JM
4694
4695
4696/**
4697 * wpas_request_connection - Request a new connection
4698 * @wpa_s: Pointer to the network interface
4699 *
4700 * This function is used to request a new connection to be found. It will mark
4701 * the interface to allow reassociation and request a new scan to find a
4702 * suitable network to connect to.
4703 */
4704void wpas_request_connection(struct wpa_supplicant *wpa_s)
4705{
4706 wpa_s->normal_scans = 0;
4707 wpa_supplicant_reinit_autoscan(wpa_s);
4708 wpa_s->extra_blacklist_count = 0;
4709 wpa_s->disconnected = 0;
4710 wpa_s->reassociate = 1;
5e24beae
MH
4711
4712 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4713 wpa_supplicant_req_scan(wpa_s, 0, 0);
9796a86c 4714}
36b9883d
DG
4715
4716
a0c90bb0
IP
4717void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
4718 struct wpa_used_freq_data *freqs_data,
4719 unsigned int len)
4720{
4721 unsigned int i;
4722
4723 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4724 len, title);
4725 for (i = 0; i < len; i++) {
4726 struct wpa_used_freq_data *cur = &freqs_data[i];
4727 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
4728 i, cur->freq, cur->flags);
4729 }
4730}
4731
4732
53c5dfc2
IP
4733/*
4734 * Find the operating frequencies of any of the virtual interfaces that
a0c90bb0
IP
4735 * are using the same radio as the current interface, and in addition, get
4736 * information about the interface types that are using the frequency.
53c5dfc2 4737 */
a0c90bb0
IP
4738int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
4739 struct wpa_used_freq_data *freqs_data,
4740 unsigned int len)
53c5dfc2 4741{
53c5dfc2
IP
4742 struct wpa_supplicant *ifs;
4743 u8 bssid[ETH_ALEN];
4744 int freq;
4745 unsigned int idx = 0, i;
4746
217cf499
JM
4747 wpa_dbg(wpa_s, MSG_DEBUG,
4748 "Determining shared radio frequencies (max len %u)", len);
a0c90bb0 4749 os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
53c5dfc2 4750
0ad3b9c4
JM
4751 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4752 radio_list) {
a0c90bb0
IP
4753 if (idx == len)
4754 break;
4755
53c5dfc2
IP
4756 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4757 continue;
4758
4759 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4760 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4761 freq = ifs->current_ssid->frequency;
4762 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4763 freq = ifs->assoc_freq;
4764 else
4765 continue;
4766
4767 /* Hold only distinct freqs */
4768 for (i = 0; i < idx; i++)
a0c90bb0 4769 if (freqs_data[i].freq == freq)
53c5dfc2
IP
4770 break;
4771
4772 if (i == idx)
a0c90bb0
IP
4773 freqs_data[idx++].freq = freq;
4774
4775 if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
4776 freqs_data[i].flags = ifs->current_ssid->p2p_group ?
4777 WPA_FREQ_USED_BY_P2P_CLIENT :
4778 WPA_FREQ_USED_BY_INFRA_STATION;
4779 }
53c5dfc2 4780 }
217cf499 4781
a0c90bb0 4782 dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
53c5dfc2
IP
4783 return idx;
4784}
a0c90bb0
IP
4785
4786
4787/*
4788 * Find the operating frequencies of any of the virtual interfaces that
4789 * are using the same radio as the current interface.
4790 */
4791int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4792 int *freq_array, unsigned int len)
4793{
4794 struct wpa_used_freq_data *freqs_data;
4795 int num, i;
4796
4797 os_memset(freq_array, 0, sizeof(int) * len);
4798
4799 freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
4800 if (!freqs_data)
4801 return -1;
4802
4803 num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
4804 for (i = 0; i < num; i++)
4805 freq_array[i] = freqs_data[i].freq;
4806
4807 os_free(freqs_data);
4808
4809 return num;
4810}