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