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