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