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