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