]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpa_supplicant.c
Add parameter for vendor specific elements into Beacon/Probe Response
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant
d93dfbd5 3 * Copyright (c) 2003-2012, 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"
3ec97afe 20#include "eap_server/eap_methods.h"
3acb5005 21#include "rsn_supp/wpa.h"
6fc6879b 22#include "eloop.h"
6fc6879b 23#include "config.h"
306ae225 24#include "utils/ext_password.h"
6fc6879b
JM
25#include "l2_packet/l2_packet.h"
26#include "wpa_supplicant_i.h"
2d5b792d 27#include "driver_i.h"
6fc6879b 28#include "ctrl_iface.h"
6fc6879b 29#include "pcsc_funcs.h"
90973fb2 30#include "common/version.h"
3acb5005
JM
31#include "rsn_supp/preauth.h"
32#include "rsn_supp/pmksa_cache.h"
90973fb2 33#include "common/wpa_ctrl.h"
90973fb2 34#include "common/ieee802_11_defs.h"
72044390 35#include "p2p/p2p.h"
6fc6879b
JM
36#include "blacklist.h"
37#include "wpas_glue.h"
116654ce 38#include "wps_supplicant.h"
11ef8d35 39#include "ibss_rsn.h"
c2a04078 40#include "sme.h"
04ea7b79 41#include "gas_query.h"
1f1b62a0 42#include "ap.h"
b22128ef 43#include "p2p_supplicant.h"
8bac466b 44#include "notify.h"
60b94c98 45#include "bgscan.h"
7c865c68 46#include "autoscan.h"
83922c2d 47#include "bss.h"
9ba9fa07 48#include "scan.h"
24f6497c 49#include "offchannel.h"
cb418324 50#include "hs20_supplicant.h"
6fc6879b
JM
51
52const char *wpa_supplicant_version =
53"wpa_supplicant v" VERSION_STR "\n"
57d38ddf 54"Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
6fc6879b
JM
55
56const char *wpa_supplicant_license =
331f89ff
JM
57"This software may be distributed under the terms of the BSD license.\n"
58"See README for more details.\n"
6fc6879b
JM
59#ifdef EAP_TLS_OPENSSL
60"\nThis product includes software developed by the OpenSSL Project\n"
61"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
62#endif /* EAP_TLS_OPENSSL */
63;
64
65#ifndef CONFIG_NO_STDOUT_DEBUG
66/* Long text divided into parts in order to fit in C89 strings size limits. */
67const char *wpa_supplicant_full_license1 =
331f89ff 68"";
6fc6879b 69const char *wpa_supplicant_full_license2 =
331f89ff 70"This software may be distributed under the terms of the BSD license.\n"
6fc6879b
JM
71"\n"
72"Redistribution and use in source and binary forms, with or without\n"
73"modification, are permitted provided that the following conditions are\n"
74"met:\n"
75"\n";
76const char *wpa_supplicant_full_license3 =
77"1. Redistributions of source code must retain the above copyright\n"
78" notice, this list of conditions and the following disclaimer.\n"
79"\n"
80"2. Redistributions in binary form must reproduce the above copyright\n"
81" notice, this list of conditions and the following disclaimer in the\n"
82" documentation and/or other materials provided with the distribution.\n"
83"\n";
84const char *wpa_supplicant_full_license4 =
85"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
86" names of its contributors may be used to endorse or promote products\n"
87" derived from this software without specific prior written permission.\n"
88"\n"
89"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
90"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
91"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
92"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
93const char *wpa_supplicant_full_license5 =
94"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
95"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
96"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
97"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
98"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
99"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
100"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
101"\n";
102#endif /* CONFIG_NO_STDOUT_DEBUG */
103
6fc6879b
JM
104extern int wpa_debug_level;
105extern int wpa_debug_show_keys;
106extern int wpa_debug_timestamp;
c5121837 107extern struct wpa_driver_ops *wpa_drivers[];
6fc6879b
JM
108
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
128static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
129 struct wpa_ssid *ssid)
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;
157 case WPA_CIPHER_TKIP:
158 /* WPA-None uses the same Michael MIC key for both TX and RX */
159 os_memcpy(key, ssid->psk, 16 + 8);
160 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
161 keylen = 32;
162 alg = WPA_ALG_TKIP;
163 break;
164 default:
f049052b
BG
165 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
166 "WPA-None", wpa_s->group_cipher);
6fc6879b
JM
167 return -1;
168 }
169
170 /* TODO: should actually remember the previously used seq#, both for TX
171 * and RX from each STA.. */
172
0382097e 173 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
6fc6879b
JM
174}
175
176
177static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
178{
179 struct wpa_supplicant *wpa_s = eloop_ctx;
180 const u8 *bssid = wpa_s->bssid;
a8e16edc 181 if (is_zero_ether_addr(bssid))
6fc6879b
JM
182 bssid = wpa_s->pending_bssid;
183 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
184 MAC2STR(bssid));
185 wpa_blacklist_add(wpa_s, bssid);
186 wpa_sm_notify_disassoc(wpa_s->wpa);
187 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
188 wpa_s->reassociate = 1;
48b84f18
BG
189
190 /*
191 * If we timed out, the AP or the local radio may be busy.
192 * So, wait a second until scanning again.
193 */
194 wpa_supplicant_req_scan(wpa_s, 1, 0);
99fcd404
JM
195
196#ifdef CONFIG_P2P
197 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
198 wpa_s->global->p2p != NULL) {
199 wpa_s->p2p_cb_on_scan_complete = 0;
200 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
201 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
202 "continued after timed out authentication");
99fcd404
JM
203 }
204 }
205#endif /* CONFIG_P2P */
6fc6879b
JM
206}
207
208
209/**
210 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
211 * @wpa_s: Pointer to wpa_supplicant data
212 * @sec: Number of seconds after which to time out authentication
213 * @usec: Number of microseconds after which to time out authentication
214 *
215 * This function is used to schedule a timeout for the current authentication
216 * attempt.
217 */
218void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
219 int sec, int usec)
220{
c2a04078
JM
221 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
222 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b
JM
223 return;
224
f049052b 225 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
6fc6879b
JM
226 "%d usec", sec, usec);
227 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
228 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
229}
230
231
232/**
233 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
234 * @wpa_s: Pointer to wpa_supplicant data
235 *
236 * This function is used to cancel authentication timeout scheduled with
237 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
238 * been completed.
239 */
240void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
241{
f049052b 242 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
6fc6879b
JM
243 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
244 wpa_blacklist_del(wpa_s, wpa_s->bssid);
245}
246
247
248/**
249 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
250 * @wpa_s: Pointer to wpa_supplicant data
251 *
252 * This function is used to configure EAPOL state machine based on the selected
253 * authentication mode.
254 */
255void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
256{
257#ifdef IEEE8021X_EAPOL
258 struct eapol_config eapol_conf;
259 struct wpa_ssid *ssid = wpa_s->current_ssid;
260
53895c3b 261#ifdef CONFIG_IBSS_RSN
d7dcba70 262 if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
263 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
264 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
265 /*
266 * RSN IBSS authentication is per-STA and we can disable the
267 * per-BSSID EAPOL authentication.
268 */
269 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
270 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
271 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
272 return;
273 }
274#endif /* CONFIG_IBSS_RSN */
275
0a40ec6a
JM
276 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
277 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
278
6fc6879b
JM
279 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
280 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
281 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
282 else
283 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
284
285 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
286 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
287 eapol_conf.accept_802_1x_keys = 1;
288 eapol_conf.required_keys = 0;
289 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
290 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
291 }
292 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
293 eapol_conf.required_keys |=
294 EAPOL_REQUIRE_KEY_BROADCAST;
295 }
296
c2a04078 297 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
6fc6879b 298 eapol_conf.required_keys = 0;
6fc6879b
JM
299 }
300 if (wpa_s->conf)
301 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
302 eapol_conf.workaround = ssid->eap_workaround;
56586197
JM
303 eapol_conf.eap_disabled =
304 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
ad08c363
JM
305 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
306 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
6fc6879b
JM
307 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
308#endif /* IEEE8021X_EAPOL */
309}
310
311
312/**
313 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
314 * @wpa_s: Pointer to wpa_supplicant data
315 * @ssid: Configuration data for the network
316 *
317 * This function is used to configure WPA state machine and related parameters
318 * to a mode where WPA is not enabled. This is called as part of the
319 * authentication configuration when the selected network does not use WPA.
320 */
321void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
322 struct wpa_ssid *ssid)
323{
324 int i;
325
ad08c363
JM
326 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
327 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
328 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
6fc6879b
JM
329 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
330 else
331 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
332 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
333 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
334 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
335 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
336 wpa_s->group_cipher = WPA_CIPHER_NONE;
337 wpa_s->mgmt_group_cipher = 0;
338
339 for (i = 0; i < NUM_WEP_KEYS; i++) {
340 if (ssid->wep_key_len[i] > 5) {
341 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
342 wpa_s->group_cipher = WPA_CIPHER_WEP104;
343 break;
344 } else if (ssid->wep_key_len[i] > 0) {
345 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
346 wpa_s->group_cipher = WPA_CIPHER_WEP40;
347 break;
348 }
349 }
350
351 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
353 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
354 wpa_s->pairwise_cipher);
355 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
356#ifdef CONFIG_IEEE80211W
357 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
358 wpa_s->mgmt_group_cipher);
359#endif /* CONFIG_IEEE80211W */
360
361 pmksa_cache_clear_current(wpa_s->wpa);
362}
363
364
6979582c 365void free_hw_features(struct wpa_supplicant *wpa_s)
6bf731e8
CL
366{
367 int i;
368 if (wpa_s->hw.modes == NULL)
369 return;
370
371 for (i = 0; i < wpa_s->hw.num_modes; i++) {
372 os_free(wpa_s->hw.modes[i].channels);
373 os_free(wpa_s->hw.modes[i].rates);
374 }
375
376 os_free(wpa_s->hw.modes);
377 wpa_s->hw.modes = NULL;
378}
379
380
6fc6879b
JM
381static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
382{
60b94c98 383 bgscan_deinit(wpa_s);
7c865c68 384 autoscan_deinit(wpa_s);
6fc6879b
JM
385 scard_deinit(wpa_s->scard);
386 wpa_s->scard = NULL;
387 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
388 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
389 l2_packet_deinit(wpa_s->l2);
390 wpa_s->l2 = NULL;
391 if (wpa_s->l2_br) {
392 l2_packet_deinit(wpa_s->l2_br);
393 wpa_s->l2_br = NULL;
394 }
395
6fc6879b 396 if (wpa_s->conf != NULL) {
8e56d189
JM
397 struct wpa_ssid *ssid;
398 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
399 wpas_notify_network_removed(wpa_s, ssid);
6fc6879b
JM
400 }
401
402 os_free(wpa_s->confname);
403 wpa_s->confname = NULL;
404
405 wpa_sm_set_eapol(wpa_s->wpa, NULL);
406 eapol_sm_deinit(wpa_s->eapol);
407 wpa_s->eapol = NULL;
408
409 rsn_preauth_deinit(wpa_s->wpa);
410
281ff0aa
GP
411#ifdef CONFIG_TDLS
412 wpa_tdls_deinit(wpa_s->wpa);
413#endif /* CONFIG_TDLS */
414
6fc6879b
JM
415 pmksa_candidate_free(wpa_s->wpa);
416 wpa_sm_deinit(wpa_s->wpa);
417 wpa_s->wpa = NULL;
418 wpa_blacklist_clear(wpa_s);
419
83922c2d 420 wpa_bss_deinit(wpa_s);
6fc6879b
JM
421
422 wpa_supplicant_cancel_scan(wpa_s);
423 wpa_supplicant_cancel_auth_timeout(wpa_s);
01a17491
JM
424 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
425#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
426 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
427 wpa_s, NULL);
428#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
6fc6879b 429
116654ce 430 wpas_wps_deinit(wpa_s);
11ef8d35 431
1ff73338
JM
432 wpabuf_free(wpa_s->pending_eapol_rx);
433 wpa_s->pending_eapol_rx = NULL;
434
11ef8d35
JM
435#ifdef CONFIG_IBSS_RSN
436 ibss_rsn_deinit(wpa_s->ibss_rsn);
437 wpa_s->ibss_rsn = NULL;
438#endif /* CONFIG_IBSS_RSN */
c2a04078 439
e29853bb 440 sme_deinit(wpa_s);
2d5b792d
JM
441
442#ifdef CONFIG_AP
443 wpa_supplicant_ap_deinit(wpa_s);
444#endif /* CONFIG_AP */
b22128ef
JM
445
446#ifdef CONFIG_P2P
447 wpas_p2p_deinit(wpa_s);
448#endif /* CONFIG_P2P */
f47d639d 449
24f6497c
JM
450#ifdef CONFIG_OFFCHANNEL
451 offchannel_deinit(wpa_s);
452#endif /* CONFIG_OFFCHANNEL */
453
a4cba8f1
LC
454 wpa_supplicant_cancel_sched_scan(wpa_s);
455
f47d639d
JM
456 os_free(wpa_s->next_scan_freqs);
457 wpa_s->next_scan_freqs = NULL;
04ea7b79
JM
458
459 gas_query_deinit(wpa_s->gas);
460 wpa_s->gas = NULL;
6bf731e8
CL
461
462 free_hw_features(wpa_s);
d445a5cd
JM
463
464 os_free(wpa_s->bssid_filter);
465 wpa_s->bssid_filter = NULL;
b6668734
JM
466
467 wnm_bss_keep_alive_deinit(wpa_s);
306ae225
JM
468
469 ext_password_deinit(wpa_s->ext_pw);
470 wpa_s->ext_pw = NULL;
6fc6879b
JM
471}
472
473
474/**
475 * wpa_clear_keys - Clear keys configured for the driver
476 * @wpa_s: Pointer to wpa_supplicant data
477 * @addr: Previously used BSSID or %NULL if not available
478 *
479 * This function clears the encryption keys that has been previously configured
480 * for the driver.
481 */
482void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
483{
6fc6879b
JM
484 if (wpa_s->keys_cleared) {
485 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
486 * timing issues with keys being cleared just before new keys
487 * are set or just after association or something similar. This
488 * shows up in group key handshake failing often because of the
489 * client not receiving the first encrypted packets correctly.
490 * Skipping some of the extra key clearing steps seems to help
491 * in completing group key handshake more reliably. */
f049052b
BG
492 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
493 "skip key clearing");
6fc6879b
JM
494 return;
495 }
496
497 /* MLME-DELETEKEYS.request */
0382097e
JM
498 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
499 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
500 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
501 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
0e27f655 502#ifdef CONFIG_IEEE80211W
0382097e
JM
503 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
504 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
0e27f655 505#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
506 if (addr) {
507 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
508 0);
509 /* MLME-SETPROTECTION.request(None) */
510 wpa_drv_mlme_setprotection(
511 wpa_s, addr,
512 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
513 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
514 }
515 wpa_s->keys_cleared = 1;
516}
517
518
519/**
520 * wpa_supplicant_state_txt - Get the connection state name as a text string
521 * @state: State (wpa_state; WPA_*)
522 * Returns: The state name as a printable text string
523 */
71934751 524const char * wpa_supplicant_state_txt(enum wpa_states state)
6fc6879b
JM
525{
526 switch (state) {
527 case WPA_DISCONNECTED:
528 return "DISCONNECTED";
529 case WPA_INACTIVE:
530 return "INACTIVE";
8401a6b0
JM
531 case WPA_INTERFACE_DISABLED:
532 return "INTERFACE_DISABLED";
6fc6879b
JM
533 case WPA_SCANNING:
534 return "SCANNING";
c2a04078
JM
535 case WPA_AUTHENTICATING:
536 return "AUTHENTICATING";
6fc6879b
JM
537 case WPA_ASSOCIATING:
538 return "ASSOCIATING";
539 case WPA_ASSOCIATED:
540 return "ASSOCIATED";
541 case WPA_4WAY_HANDSHAKE:
542 return "4WAY_HANDSHAKE";
543 case WPA_GROUP_HANDSHAKE:
544 return "GROUP_HANDSHAKE";
545 case WPA_COMPLETED:
546 return "COMPLETED";
547 default:
548 return "UNKNOWN";
549 }
550}
551
552
cfe53c9a
PS
553#ifdef CONFIG_BGSCAN
554
555static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
556{
0096c427
JM
557 if (wpas_driver_bss_selection(wpa_s))
558 return;
cfe53c9a
PS
559 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
560 return;
561
562 bgscan_deinit(wpa_s);
563 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
564 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
565 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
566 "bgscan");
567 /*
568 * Live without bgscan; it is only used as a roaming
569 * optimization, so the initial connection is not
570 * affected.
571 */
6409b7a7
YD
572 } else {
573 struct wpa_scan_results *scan_res;
cfe53c9a 574 wpa_s->bgscan_ssid = wpa_s->current_ssid;
6409b7a7
YD
575 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
576 0);
577 if (scan_res) {
578 bgscan_notify_scan(wpa_s, scan_res);
579 wpa_scan_results_free(scan_res);
580 }
581 }
cfe53c9a
PS
582 } else
583 wpa_s->bgscan_ssid = NULL;
584}
585
586
587static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
588{
589 if (wpa_s->bgscan_ssid != NULL) {
590 bgscan_deinit(wpa_s);
591 wpa_s->bgscan_ssid = NULL;
592 }
593}
594
595#endif /* CONFIG_BGSCAN */
596
597
7c865c68
TB
598static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
599{
99218999 600 if (autoscan_init(wpa_s, 0))
7c865c68
TB
601 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
602}
603
604
605static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
606{
607 autoscan_deinit(wpa_s);
608}
609
610
c3d12238
JM
611void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
612{
613 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
614 wpa_s->wpa_state == WPA_SCANNING) {
615 autoscan_deinit(wpa_s);
616 wpa_supplicant_start_autoscan(wpa_s);
617 }
618}
619
620
6fc6879b
JM
621/**
622 * wpa_supplicant_set_state - Set current connection state
623 * @wpa_s: Pointer to wpa_supplicant data
624 * @state: The new connection state
625 *
626 * This function is called whenever the connection state changes, e.g.,
627 * association is completed for WPA/WPA2 4-Way Handshake is started.
628 */
71934751
JM
629void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
630 enum wpa_states state)
6fc6879b 631{
27f43d8d
MH
632 enum wpa_states old_state = wpa_s->wpa_state;
633
f049052b
BG
634 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
635 wpa_supplicant_state_txt(wpa_s->wpa_state),
636 wpa_supplicant_state_txt(state));
6fc6879b 637
cb8564b1
DW
638 if (state != WPA_SCANNING)
639 wpa_supplicant_notify_scanning(wpa_s, 0);
640
6fc6879b
JM
641 if (state == WPA_COMPLETED && wpa_s->new_connection) {
642#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
643 struct wpa_ssid *ssid = wpa_s->current_ssid;
644 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
645 MACSTR " completed %s [id=%d id_str=%s]",
646 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
647 "(reauth)" : "(auth)",
648 ssid ? ssid->id : -1,
649 ssid && ssid->id_str ? ssid->id_str : "");
650#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
00e5e3d5 651 wpas_clear_temp_disabled(wpa_s, ssid, 1);
6fc6879b
JM
652 wpa_s->new_connection = 0;
653 wpa_s->reassociated_connection = 1;
654 wpa_drv_set_operstate(wpa_s, 1);
99ac2913
FF
655#ifndef IEEE8021X_EAPOL
656 wpa_drv_set_supp_port(wpa_s, 1);
657#endif /* IEEE8021X_EAPOL */
17a4734d 658 wpa_s->after_wps = 0;
b22128ef
JM
659#ifdef CONFIG_P2P
660 wpas_p2p_completed(wpa_s);
661#endif /* CONFIG_P2P */
c3701c66
RM
662
663 sme_sched_obss_scan(wpa_s, 1);
6fc6879b
JM
664 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
665 state == WPA_ASSOCIATED) {
666 wpa_s->new_connection = 1;
667 wpa_drv_set_operstate(wpa_s, 0);
99ac2913
FF
668#ifndef IEEE8021X_EAPOL
669 wpa_drv_set_supp_port(wpa_s, 0);
670#endif /* IEEE8021X_EAPOL */
c3701c66 671 sme_sched_obss_scan(wpa_s, 0);
6fc6879b
JM
672 }
673 wpa_s->wpa_state = state;
27f43d8d 674
cfe53c9a
PS
675#ifdef CONFIG_BGSCAN
676 if (state == WPA_COMPLETED)
677 wpa_supplicant_start_bgscan(wpa_s);
678 else
679 wpa_supplicant_stop_bgscan(wpa_s);
680#endif /* CONFIG_BGSCAN */
681
7c865c68
TB
682 if (state == WPA_AUTHENTICATING)
683 wpa_supplicant_stop_autoscan(wpa_s);
684
685 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
686 wpa_supplicant_start_autoscan(wpa_s);
687
5bbf9f10 688 if (wpa_s->wpa_state != old_state) {
27f43d8d 689 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
5bbf9f10
PS
690
691 if (wpa_s->wpa_state == WPA_COMPLETED ||
692 old_state == WPA_COMPLETED)
693 wpas_notify_auth_changed(wpa_s);
694 }
6fc6879b
JM
695}
696
697
1a1bf008
JM
698void wpa_supplicant_terminate_proc(struct wpa_global *global)
699{
700 int pending = 0;
701#ifdef CONFIG_WPS
702 struct wpa_supplicant *wpa_s = global->ifaces;
703 while (wpa_s) {
704 if (wpas_wps_terminate_pending(wpa_s) == 1)
705 pending = 1;
706 wpa_s = wpa_s->next;
707 }
708#endif /* CONFIG_WPS */
709 if (pending)
710 return;
711 eloop_terminate();
712}
713
714
0456ea16 715static void wpa_supplicant_terminate(int sig, void *signal_ctx)
6fc6879b 716{
0456ea16 717 struct wpa_global *global = signal_ctx;
1a1bf008 718 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
719}
720
721
b22128ef 722void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
6fc6879b 723{
71934751 724 enum wpa_states old_state = wpa_s->wpa_state;
27f43d8d 725
6fc6879b
JM
726 wpa_s->pairwise_cipher = 0;
727 wpa_s->group_cipher = 0;
728 wpa_s->mgmt_group_cipher = 0;
729 wpa_s->key_mgmt = 0;
8401a6b0 730 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
99218999 731 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
27f43d8d
MH
732
733 if (wpa_s->wpa_state != old_state)
734 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
6fc6879b
JM
735}
736
737
738/**
739 * wpa_supplicant_reload_configuration - Reload configuration data
740 * @wpa_s: Pointer to wpa_supplicant data
741 * Returns: 0 on success or -1 if configuration parsing failed
742 *
743 * This function can be used to request that the configuration data is reloaded
744 * (e.g., after configuration file change). This function is reloading
745 * configuration only for one interface, so this may need to be called multiple
746 * times if %wpa_supplicant is controlling multiple interfaces and all
747 * interfaces need reconfiguration.
748 */
749int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
750{
751 struct wpa_config *conf;
752 int reconf_ctrl;
8bac466b
JM
753 int old_ap_scan;
754
6fc6879b
JM
755 if (wpa_s->confname == NULL)
756 return -1;
757 conf = wpa_config_read(wpa_s->confname);
758 if (conf == NULL) {
759 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
760 "file '%s' - exiting", wpa_s->confname);
761 return -1;
762 }
611aea7d 763 conf->changed_parameters = (unsigned int) -1;
6fc6879b
JM
764
765 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
766 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
767 os_strcmp(conf->ctrl_interface,
768 wpa_s->conf->ctrl_interface) != 0);
769
770 if (reconf_ctrl && wpa_s->ctrl_iface) {
771 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
772 wpa_s->ctrl_iface = NULL;
773 }
774
775 eapol_sm_invalidate_cached_session(wpa_s->eapol);
7b7ce8aa
JM
776 if (wpa_s->current_ssid) {
777 wpa_supplicant_deauthenticate(wpa_s,
778 WLAN_REASON_DEAUTH_LEAVING);
779 }
8bac466b 780
6fc6879b
JM
781 /*
782 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
783 * pkcs11_engine_path, pkcs11_module_path.
784 */
56586197 785 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
6fc6879b
JM
786 /*
787 * Clear forced success to clear EAP state for next
788 * authentication.
789 */
790 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
791 }
792 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
793 wpa_sm_set_config(wpa_s->wpa, NULL);
d8a790b9 794 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
6fc6879b
JM
795 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
796 rsn_preauth_deinit(wpa_s->wpa);
8bac466b
JM
797
798 old_ap_scan = wpa_s->conf->ap_scan;
6fc6879b
JM
799 wpa_config_free(wpa_s->conf);
800 wpa_s->conf = conf;
8bac466b
JM
801 if (old_ap_scan != wpa_s->conf->ap_scan)
802 wpas_notify_ap_scan_changed(wpa_s);
803
6fc6879b
JM
804 if (reconf_ctrl)
805 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
806
611aea7d
JM
807 wpa_supplicant_update_config(wpa_s);
808
6fc6879b 809 wpa_supplicant_clear_status(wpa_s);
349493bd 810 if (wpa_supplicant_enabled_networks(wpa_s)) {
43a38635
JM
811 wpa_s->reassociate = 1;
812 wpa_supplicant_req_scan(wpa_s, 0, 0);
813 }
f049052b 814 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
6fc6879b
JM
815 return 0;
816}
817
818
0456ea16 819static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
6fc6879b 820{
0456ea16 821 struct wpa_global *global = signal_ctx;
6fc6879b 822 struct wpa_supplicant *wpa_s;
6fc6879b 823 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
f049052b
BG
824 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
825 sig);
6fc6879b 826 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1a1bf008 827 wpa_supplicant_terminate_proc(global);
6fc6879b
JM
828 }
829 }
830}
831
832
508545f3 833enum wpa_cipher cipher_suite2driver(int cipher)
6fc6879b
JM
834{
835 switch (cipher) {
836 case WPA_CIPHER_NONE:
837 return CIPHER_NONE;
838 case WPA_CIPHER_WEP40:
839 return CIPHER_WEP40;
840 case WPA_CIPHER_WEP104:
841 return CIPHER_WEP104;
842 case WPA_CIPHER_CCMP:
843 return CIPHER_CCMP;
844 case WPA_CIPHER_TKIP:
845 default:
846 return CIPHER_TKIP;
847 }
848}
849
850
508545f3 851enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
6fc6879b
JM
852{
853 switch (key_mgmt) {
854 case WPA_KEY_MGMT_NONE:
855 return KEY_MGMT_NONE;
856 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
857 return KEY_MGMT_802_1X_NO_WPA;
858 case WPA_KEY_MGMT_IEEE8021X:
859 return KEY_MGMT_802_1X;
860 case WPA_KEY_MGMT_WPA_NONE:
861 return KEY_MGMT_WPA_NONE;
862 case WPA_KEY_MGMT_FT_IEEE8021X:
863 return KEY_MGMT_FT_802_1X;
864 case WPA_KEY_MGMT_FT_PSK:
865 return KEY_MGMT_FT_PSK;
56586197
JM
866 case WPA_KEY_MGMT_IEEE8021X_SHA256:
867 return KEY_MGMT_802_1X_SHA256;
868 case WPA_KEY_MGMT_PSK_SHA256:
869 return KEY_MGMT_PSK_SHA256;
ad08c363
JM
870 case WPA_KEY_MGMT_WPS:
871 return KEY_MGMT_WPS;
6fc6879b
JM
872 case WPA_KEY_MGMT_PSK:
873 default:
874 return KEY_MGMT_PSK;
875 }
876}
877
878
879static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
880 struct wpa_ssid *ssid,
881 struct wpa_ie_data *ie)
882{
883 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
884 if (ret) {
885 if (ret == -2) {
886 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
887 "from association info");
888 }
889 return -1;
890 }
891
f049052b
BG
892 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
893 "cipher suites");
6fc6879b
JM
894 if (!(ie->group_cipher & ssid->group_cipher)) {
895 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
896 "cipher 0x%x (mask 0x%x) - reject",
897 ie->group_cipher, ssid->group_cipher);
898 return -1;
899 }
900 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
901 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
902 "cipher 0x%x (mask 0x%x) - reject",
903 ie->pairwise_cipher, ssid->pairwise_cipher);
904 return -1;
905 }
906 if (!(ie->key_mgmt & ssid->key_mgmt)) {
907 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
908 "management 0x%x (mask 0x%x) - reject",
909 ie->key_mgmt, ssid->key_mgmt);
910 return -1;
911 }
912
913#ifdef CONFIG_IEEE80211W
0b60b0aa 914 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
70f8cc8e 915 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
6fc6879b
JM
916 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
917 "that does not support management frame protection - "
918 "reject");
919 return -1;
920 }
921#endif /* CONFIG_IEEE80211W */
922
923 return 0;
924}
925
926
927/**
928 * wpa_supplicant_set_suites - Set authentication and encryption parameters
929 * @wpa_s: Pointer to wpa_supplicant data
930 * @bss: Scan results for the selected BSS, or %NULL if not available
931 * @ssid: Configuration data for the selected network
932 * @wpa_ie: Buffer for the WPA/RSN IE
933 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
934 * used buffer length in case the functions returns success.
935 * Returns: 0 on success or -1 on failure
936 *
937 * This function is used to configure authentication and encryption parameters
938 * based on the network configuration and scan result for the selected BSS (if
939 * available).
940 */
941int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
6fa81a3b 942 struct wpa_bss *bss, struct wpa_ssid *ssid,
6fc6879b
JM
943 u8 *wpa_ie, size_t *wpa_ie_len)
944{
945 struct wpa_ie_data ie;
946 int sel, proto;
947 const u8 *bss_wpa, *bss_rsn;
948
949 if (bss) {
6fa81a3b
JM
950 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
951 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
6fc6879b
JM
952 } else
953 bss_wpa = bss_rsn = NULL;
954
955 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
956 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
957 (ie.group_cipher & ssid->group_cipher) &&
958 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
959 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 960 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
6fc6879b
JM
961 proto = WPA_PROTO_RSN;
962 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
963 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
964 (ie.group_cipher & ssid->group_cipher) &&
965 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
966 (ie.key_mgmt & ssid->key_mgmt)) {
f049052b 967 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
6fc6879b
JM
968 proto = WPA_PROTO_WPA;
969 } else if (bss) {
970 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
971 return -1;
972 } else {
973 if (ssid->proto & WPA_PROTO_RSN)
974 proto = WPA_PROTO_RSN;
975 else
976 proto = WPA_PROTO_WPA;
977 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
978 os_memset(&ie, 0, sizeof(ie));
979 ie.group_cipher = ssid->group_cipher;
980 ie.pairwise_cipher = ssid->pairwise_cipher;
981 ie.key_mgmt = ssid->key_mgmt;
982#ifdef CONFIG_IEEE80211W
983 ie.mgmt_group_cipher =
70f8cc8e 984 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
6fc6879b
JM
985 WPA_CIPHER_AES_128_CMAC : 0;
986#endif /* CONFIG_IEEE80211W */
f049052b
BG
987 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
988 "based on configuration");
6fc6879b
JM
989 } else
990 proto = ie.proto;
991 }
992
f049052b
BG
993 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
994 "pairwise %d key_mgmt %d proto %d",
995 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
6fc6879b
JM
996#ifdef CONFIG_IEEE80211W
997 if (ssid->ieee80211w) {
f049052b
BG
998 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
999 ie.mgmt_group_cipher);
6fc6879b
JM
1000 }
1001#endif /* CONFIG_IEEE80211W */
1002
64fa840a 1003 wpa_s->wpa_proto = proto;
6fc6879b
JM
1004 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1005 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
1006 !!(ssid->proto & WPA_PROTO_RSN));
1007
1008 if (bss || !wpa_s->ap_ies_from_associnfo) {
1009 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1010 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1011 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1012 bss_rsn ? 2 + bss_rsn[1] : 0))
1013 return -1;
1014 }
1015
1016 sel = ie.group_cipher & ssid->group_cipher;
1017 if (sel & WPA_CIPHER_CCMP) {
1018 wpa_s->group_cipher = WPA_CIPHER_CCMP;
f049052b 1019 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
6fc6879b
JM
1020 } else if (sel & WPA_CIPHER_TKIP) {
1021 wpa_s->group_cipher = WPA_CIPHER_TKIP;
f049052b 1022 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
6fc6879b
JM
1023 } else if (sel & WPA_CIPHER_WEP104) {
1024 wpa_s->group_cipher = WPA_CIPHER_WEP104;
f049052b 1025 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
6fc6879b
JM
1026 } else if (sel & WPA_CIPHER_WEP40) {
1027 wpa_s->group_cipher = WPA_CIPHER_WEP40;
f049052b 1028 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
6fc6879b 1029 } else {
f049052b
BG
1030 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1031 "cipher");
6fc6879b
JM
1032 return -1;
1033 }
1034
1035 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1036 if (sel & WPA_CIPHER_CCMP) {
1037 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
f049052b 1038 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
6fc6879b
JM
1039 } else if (sel & WPA_CIPHER_TKIP) {
1040 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
f049052b 1041 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
6fc6879b
JM
1042 } else if (sel & WPA_CIPHER_NONE) {
1043 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
f049052b 1044 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
6fc6879b 1045 } else {
f049052b
BG
1046 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1047 "cipher");
6fc6879b
JM
1048 return -1;
1049 }
1050
1051 sel = ie.key_mgmt & ssid->key_mgmt;
1052 if (0) {
1053#ifdef CONFIG_IEEE80211R
1054 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1055 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
f049052b 1056 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
6fc6879b
JM
1057 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1058 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
f049052b 1059 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
6fc6879b 1060#endif /* CONFIG_IEEE80211R */
56586197
JM
1061#ifdef CONFIG_IEEE80211W
1062 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1063 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
f049052b 1064 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1065 "WPA: using KEY_MGMT 802.1X with SHA256");
1066 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1067 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
f049052b 1068 wpa_dbg(wpa_s, MSG_DEBUG,
56586197
JM
1069 "WPA: using KEY_MGMT PSK with SHA256");
1070#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
1071 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1072 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
f049052b 1073 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
6fc6879b
JM
1074 } else if (sel & WPA_KEY_MGMT_PSK) {
1075 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
f049052b 1076 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
6fc6879b
JM
1077 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1078 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
f049052b 1079 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
6fc6879b 1080 } else {
f049052b
BG
1081 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1082 "authenticated key management type");
6fc6879b
JM
1083 return -1;
1084 }
1085
1086 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1087 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1088 wpa_s->pairwise_cipher);
1089 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1090
1091#ifdef CONFIG_IEEE80211W
1092 sel = ie.mgmt_group_cipher;
70f8cc8e 1093 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
0b60b0aa 1094 !(ie.capabilities & WPA_CAPABILITY_MFPC))
6fc6879b
JM
1095 sel = 0;
1096 if (sel & WPA_CIPHER_AES_128_CMAC) {
1097 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
f049052b 1098 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
6fc6879b
JM
1099 "AES-128-CMAC");
1100 } else {
1101 wpa_s->mgmt_group_cipher = 0;
f049052b 1102 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
6fc6879b
JM
1103 }
1104 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1105 wpa_s->mgmt_group_cipher);
e820cf95 1106 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
6fc6879b
JM
1107#endif /* CONFIG_IEEE80211W */
1108
1109 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
f049052b 1110 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
6fc6879b
JM
1111 return -1;
1112 }
1113
0bf927a0 1114 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
6fc6879b 1115 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
7d232e23
ZC
1116#ifndef CONFIG_NO_PBKDF2
1117 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1118 ssid->passphrase) {
1119 u8 psk[PMK_LEN];
986de33d
JM
1120 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1121 4096, psk, PMK_LEN);
7d232e23
ZC
1122 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1123 psk, PMK_LEN);
1124 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1125 }
1126#endif /* CONFIG_NO_PBKDF2 */
9173b16f
JM
1127#ifdef CONFIG_EXT_PASSWORD
1128 if (ssid->ext_psk) {
1129 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1130 ssid->ext_psk);
1131 char pw_str[64 + 1];
1132 u8 psk[PMK_LEN];
1133
1134 if (pw == NULL) {
1135 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1136 "found from external storage");
1137 return -1;
1138 }
1139
1140 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1141 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1142 "PSK length %d in external storage",
1143 (int) wpabuf_len(pw));
1144 ext_password_free(pw);
1145 return -1;
1146 }
1147
1148 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1149 pw_str[wpabuf_len(pw)] = '\0';
1150
1151#ifndef CONFIG_NO_PBKDF2
1152 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1153 {
986de33d
JM
1154 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1155 4096, psk, PMK_LEN);
9173b16f
JM
1156 os_memset(pw_str, 0, sizeof(pw_str));
1157 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1158 "external passphrase)",
1159 psk, PMK_LEN);
1160 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1161 } else
1162#endif /* CONFIG_NO_PBKDF2 */
1163 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1164 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1165 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1166 "Invalid PSK hex string");
1167 os_memset(pw_str, 0, sizeof(pw_str));
1168 ext_password_free(pw);
1169 return -1;
1170 }
1171 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1172 } else {
1173 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1174 "PSK available");
1175 os_memset(pw_str, 0, sizeof(pw_str));
1176 ext_password_free(pw);
1177 return -1;
1178 }
1179
1180 os_memset(pw_str, 0, sizeof(pw_str));
1181 ext_password_free(pw);
1182 }
1183#endif /* CONFIG_EXT_PASSWORD */
7d232e23 1184 } else
6fc6879b
JM
1185 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1186
1187 return 0;
1188}
1189
1190
1191/**
1192 * wpa_supplicant_associate - Request association
1193 * @wpa_s: Pointer to wpa_supplicant data
1194 * @bss: Scan results for the selected BSS, or %NULL if not available
1195 * @ssid: Configuration data for the selected network
1196 *
1197 * This function is used to request %wpa_supplicant to associate with a BSS.
1198 */
1199void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
6fa81a3b 1200 struct wpa_bss *bss, struct wpa_ssid *ssid)
6fc6879b 1201{
5f3a6aa0 1202 u8 wpa_ie[200];
6fc6879b 1203 size_t wpa_ie_len;
8bac466b 1204 int use_crypt, ret, i, bssid_changed;
abd9fafa 1205 int algs = WPA_AUTH_ALG_OPEN;
71934751 1206 enum wpa_cipher cipher_pairwise, cipher_group;
6fc6879b
JM
1207 struct wpa_driver_associate_params params;
1208 int wep_keys_set = 0;
1209 struct wpa_driver_capa capa;
1210 int assoc_failed = 0;
8bac466b 1211 struct wpa_ssid *old_ssid;
80e8a5ee
BG
1212#ifdef CONFIG_HT_OVERRIDES
1213 struct ieee80211_ht_capabilities htcaps;
1214 struct ieee80211_ht_capabilities htcaps_mask;
1215#endif /* CONFIG_HT_OVERRIDES */
6fc6879b 1216
78177a00
JM
1217#ifdef CONFIG_IBSS_RSN
1218 ibss_rsn_deinit(wpa_s->ibss_rsn);
1219 wpa_s->ibss_rsn = NULL;
1220#endif /* CONFIG_IBSS_RSN */
1221
2c5d725c
JM
1222 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1223 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1581b38b
JM
1224#ifdef CONFIG_AP
1225 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
f049052b
BG
1226 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1227 "mode");
1581b38b
JM
1228 return;
1229 }
1230 wpa_supplicant_create_ap(wpa_s, ssid);
8f770587 1231 wpa_s->current_bss = bss;
1581b38b 1232#else /* CONFIG_AP */
f049052b
BG
1233 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1234 "the build");
1581b38b
JM
1235#endif /* CONFIG_AP */
1236 return;
1237 }
1238
52c9e6f3 1239#ifdef CONFIG_TDLS
95cb2d88
JM
1240 if (bss)
1241 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1242 bss->ie_len);
52c9e6f3
JM
1243#endif /* CONFIG_TDLS */
1244
5cc4d64b
JM
1245 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1246 ssid->mode == IEEE80211_MODE_INFRA) {
c2a04078
JM
1247 sme_authenticate(wpa_s, bss, ssid);
1248 return;
1249 }
1250
0c80427d 1251 os_memset(&params, 0, sizeof(params));
6fc6879b 1252 wpa_s->reassociate = 0;
22628eca 1253 if (bss && !wpas_driver_bss_selection(wpa_s)) {
6fc6879b 1254#ifdef CONFIG_IEEE80211R
6fa81a3b 1255 const u8 *ie, *md = NULL;
6fc6879b 1256#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
1257 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1258 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
6fa81a3b 1259 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
8bac466b 1260 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
6fc6879b
JM
1261 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1262 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
8bac466b
JM
1263 if (bssid_changed)
1264 wpas_notify_bssid_changed(wpa_s);
6fc6879b 1265#ifdef CONFIG_IEEE80211R
6fa81a3b 1266 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
6fc6879b
JM
1267 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1268 md = ie + 2;
e7846b68 1269 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
91a05482
JM
1270 if (md) {
1271 /* Prepare for the next transition */
76b7981d 1272 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
91a05482 1273 }
6fc6879b 1274#endif /* CONFIG_IEEE80211R */
24c23d1b
JM
1275#ifdef CONFIG_WPS
1276 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1277 wpa_s->conf->ap_scan == 2 &&
1278 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1279 /* Use ap_scan==1 style network selection to find the network
1280 */
1281 wpa_s->scan_req = 2;
1282 wpa_s->reassociate = 1;
1283 wpa_supplicant_req_scan(wpa_s, 0, 0);
1284 return;
1285#endif /* CONFIG_WPS */
6fc6879b
JM
1286 } else {
1287 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1288 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1289 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1290 }
a4cba8f1 1291 wpa_supplicant_cancel_sched_scan(wpa_s);
6fc6879b
JM
1292 wpa_supplicant_cancel_scan(wpa_s);
1293
1294 /* Starting new association, so clear the possibly used WPA IE from the
1295 * previous association. */
1296 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1297
1298#ifdef IEEE8021X_EAPOL
1299 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1300 if (ssid->leap) {
1301 if (ssid->non_leap == 0)
abd9fafa 1302 algs = WPA_AUTH_ALG_LEAP;
6fc6879b 1303 else
abd9fafa 1304 algs |= WPA_AUTH_ALG_LEAP;
6fc6879b
JM
1305 }
1306 }
1307#endif /* IEEE8021X_EAPOL */
f049052b 1308 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
6fc6879b 1309 if (ssid->auth_alg) {
abd9fafa 1310 algs = ssid->auth_alg;
f049052b
BG
1311 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1312 "0x%x", algs);
6fc6879b 1313 }
6fc6879b 1314
6fa81a3b
JM
1315 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1316 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
0bf927a0 1317 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
6fc6879b
JM
1318 int try_opportunistic;
1319 try_opportunistic = ssid->proactive_key_caching &&
1320 (ssid->proto & WPA_PROTO_RSN);
1321 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1322 wpa_s->current_ssid,
1323 try_opportunistic) == 0)
1324 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1325 wpa_ie_len = sizeof(wpa_ie);
1326 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1327 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1328 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1329 "key management and encryption suites");
6fc6879b
JM
1330 return;
1331 }
a3f7e518
JM
1332 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1333 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1334 /*
1335 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1336 * use non-WPA since the scan results did not indicate that the
1337 * AP is using WPA or WPA2.
1338 */
1339 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1340 wpa_ie_len = 0;
1341 wpa_s->wpa_proto = 0;
0bf927a0 1342 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
6fc6879b
JM
1343 wpa_ie_len = sizeof(wpa_ie);
1344 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1345 wpa_ie, &wpa_ie_len)) {
f049052b
BG
1346 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1347 "key management and encryption suites (no "
1348 "scan results)");
6fc6879b
JM
1349 return;
1350 }
ad08c363
JM
1351#ifdef CONFIG_WPS
1352 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
b01c18a8
JM
1353 struct wpabuf *wps_ie;
1354 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
ad08c363
JM
1355 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1356 wpa_ie_len = wpabuf_len(wps_ie);
1357 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
24386985
JM
1358 } else
1359 wpa_ie_len = 0;
ad08c363
JM
1360 wpabuf_free(wps_ie);
1361 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
0c80427d
JM
1362 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1363 params.wps = WPS_MODE_PRIVACY;
1364 else
1365 params.wps = WPS_MODE_OPEN;
cf546f1a 1366 wpa_s->wpa_proto = 0;
ad08c363 1367#endif /* CONFIG_WPS */
6fc6879b
JM
1368 } else {
1369 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1370 wpa_ie_len = 0;
cf546f1a 1371 wpa_s->wpa_proto = 0;
6fc6879b
JM
1372 }
1373
5f3a6aa0
JM
1374#ifdef CONFIG_P2P
1375 if (wpa_s->global->p2p) {
1376 u8 *pos;
1377 size_t len;
1378 int res;
5f3a6aa0
JM
1379 pos = wpa_ie + wpa_ie_len;
1380 len = sizeof(wpa_ie) - wpa_ie_len;
b8a8d677
JM
1381 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1382 ssid->p2p_group);
5f3a6aa0
JM
1383 if (res >= 0)
1384 wpa_ie_len += res;
1385 }
72044390
JM
1386
1387 wpa_s->cross_connect_disallowed = 0;
1388 if (bss) {
1389 struct wpabuf *p2p;
1390 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1391 if (p2p) {
1392 wpa_s->cross_connect_disallowed =
1393 p2p_get_cross_connect_disallowed(p2p);
1394 wpabuf_free(p2p);
f049052b
BG
1395 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1396 "connection",
1397 wpa_s->cross_connect_disallowed ?
1398 "disallows" : "allows");
72044390
JM
1399 }
1400 }
5f3a6aa0
JM
1401#endif /* CONFIG_P2P */
1402
cb418324
JM
1403#ifdef CONFIG_HS20
1404 if (wpa_s->conf->hs20) {
1405 struct wpabuf *hs20;
1406 hs20 = wpabuf_alloc(20);
1407 if (hs20) {
1408 wpas_hs20_add_indication(hs20);
1409 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1410 wpabuf_len(hs20));
1411 wpa_ie_len += wpabuf_len(hs20);
1412 wpabuf_free(hs20);
1413 }
1414 }
1415#endif /* CONFIG_HS20 */
1416
92cbcf91
JM
1417#ifdef CONFIG_INTERWORKING
1418 if (wpa_s->conf->interworking) {
1419 u8 *pos = wpa_ie;
1420 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1421 pos += 2 + pos[1];
1422 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1423 wpa_ie_len += 6;
1424 *pos++ = WLAN_EID_EXT_CAPAB;
1425 *pos++ = 4;
1426 *pos++ = 0x00;
1427 *pos++ = 0x00;
1428 *pos++ = 0x00;
1429 *pos++ = 0x80; /* Bit 31 - Interworking */
1430 }
1431#endif /* CONFIG_INTERWORKING */
1432
6fc6879b
JM
1433 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1434 use_crypt = 1;
1435 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1436 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1437 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1438 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1439 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1440 use_crypt = 0;
1441 if (wpa_set_wep_keys(wpa_s, ssid)) {
1442 use_crypt = 1;
1443 wep_keys_set = 1;
1444 }
1445 }
ad08c363
JM
1446 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1447 use_crypt = 0;
6fc6879b
JM
1448
1449#ifdef IEEE8021X_EAPOL
1450 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1451 if ((ssid->eapol_flags &
1452 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1453 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1454 !wep_keys_set) {
1455 use_crypt = 0;
1456 } else {
1457 /* Assume that dynamic WEP-104 keys will be used and
1458 * set cipher suites in order for drivers to expect
1459 * encryption. */
1460 cipher_pairwise = cipher_group = CIPHER_WEP104;
1461 }
1462 }
1463#endif /* IEEE8021X_EAPOL */
1464
1465 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1466 /* Set the key before (and later after) association */
1467 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1468 }
1469
6fc6879b 1470 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
6fc6879b 1471 if (bss) {
6fa81a3b
JM
1472 params.ssid = bss->ssid;
1473 params.ssid_len = bss->ssid_len;
f15854d1
JM
1474 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1475 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1476 MACSTR " freq=%u MHz based on scan results "
1477 "(bssid_set=%d)",
1478 MAC2STR(bss->bssid), bss->freq,
1479 ssid->bssid_set);
22628eca
JM
1480 params.bssid = bss->bssid;
1481 params.freq = bss->freq;
1482 }
6fc6879b
JM
1483 } else {
1484 params.ssid = ssid->ssid;
1485 params.ssid_len = ssid->ssid_len;
1486 }
9e2af29f
NC
1487
1488 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1489 wpa_s->conf->ap_scan == 2) {
1490 params.bssid = ssid->bssid;
1491 params.fixed_bssid = 1;
1492 }
1493
d7dcba70
JM
1494 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1495 params.freq == 0)
6fc6879b
JM
1496 params.freq = ssid->frequency; /* Initial channel for IBSS */
1497 params.wpa_ie = wpa_ie;
1498 params.wpa_ie_len = wpa_ie_len;
1499 params.pairwise_suite = cipher_pairwise;
1500 params.group_suite = cipher_group;
1501 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
64fa840a 1502 params.wpa_proto = wpa_s->wpa_proto;
6fc6879b
JM
1503 params.auth_alg = algs;
1504 params.mode = ssid->mode;
1f6c0ab8 1505 params.bg_scan_period = ssid->bg_scan_period;
6fc6879b
JM
1506 for (i = 0; i < NUM_WEP_KEYS; i++) {
1507 if (ssid->wep_key_len[i])
1508 params.wep_key[i] = ssid->wep_key[i];
1509 params.wep_key_len[i] = ssid->wep_key_len[i];
1510 }
1511 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1512
c2a04078 1513 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
6fc6879b
JM
1514 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1515 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1516 params.passphrase = ssid->passphrase;
1517 if (ssid->psk_set)
1518 params.psk = ssid->psk;
1519 }
1520
36b15723
JM
1521 params.drop_unencrypted = use_crypt;
1522
6fc6879b 1523#ifdef CONFIG_IEEE80211W
70f8cc8e
JM
1524 params.mgmt_frame_protection = ssid->ieee80211w;
1525 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
6fa81a3b 1526 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
97d3497e
JM
1527 struct wpa_ie_data ie;
1528 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1529 ie.capabilities &
1530 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
f049052b
BG
1531 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1532 "MFP: require MFP");
97d3497e
JM
1533 params.mgmt_frame_protection =
1534 MGMT_FRAME_PROTECTION_REQUIRED;
1535 }
1536 }
6fc6879b
JM
1537#endif /* CONFIG_IEEE80211W */
1538
ffad8858 1539 params.p2p = ssid->p2p_group;
6e3f4b89 1540
eea2fd9e
JM
1541 if (wpa_s->parent->set_sta_uapsd)
1542 params.uapsd = wpa_s->parent->sta_uapsd;
1543 else
1544 params.uapsd = -1;
1545
80e8a5ee
BG
1546#ifdef CONFIG_HT_OVERRIDES
1547 os_memset(&htcaps, 0, sizeof(htcaps));
1548 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1549 params.htcaps = (u8 *) &htcaps;
1550 params.htcaps_mask = (u8 *) &htcaps_mask;
1551 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1552#endif /* CONFIG_HT_OVERRIDES */
1553
17fbb751 1554 ret = wpa_drv_associate(wpa_s, &params);
6fc6879b
JM
1555 if (ret < 0) {
1556 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1557 "failed");
871f4dd0
JM
1558 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1559 /*
1560 * The driver is known to mean what is saying, so we
1561 * can stop right here; the association will not
1562 * succeed.
1563 */
1564 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
c1c02342 1565 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
871f4dd0
JM
1566 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1567 return;
1568 }
6fc6879b
JM
1569 /* try to continue anyway; new association will be tried again
1570 * after timeout */
1571 assoc_failed = 1;
1572 }
1573
1574 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1575 /* Set the key after the association just in case association
1576 * cleared the previously configured key. */
1577 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1578 /* No need to timeout authentication since there is no key
1579 * management. */
1580 wpa_supplicant_cancel_auth_timeout(wpa_s);
1581 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
53895c3b 1582#ifdef CONFIG_IBSS_RSN
d7dcba70 1583 } else if (ssid->mode == WPAS_MODE_IBSS &&
53895c3b
JM
1584 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1585 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1586 /*
1587 * RSN IBSS authentication is per-STA and we can disable the
1588 * per-BSSID authentication.
1589 */
1590 wpa_supplicant_cancel_auth_timeout(wpa_s);
53895c3b 1591#endif /* CONFIG_IBSS_RSN */
6fc6879b
JM
1592 } else {
1593 /* Timeout for IEEE 802.11 authentication and association */
1d3c75b3
DW
1594 int timeout = 60;
1595
1596 if (assoc_failed) {
1597 /* give IBSS a bit more time */
d7dcba70 1598 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1d3c75b3
DW
1599 } else if (wpa_s->conf->ap_scan == 1) {
1600 /* give IBSS a bit more time */
d7dcba70 1601 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1d3c75b3 1602 }
6fc6879b
JM
1603 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1604 }
1605
1606 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1607 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1608 /* Set static WEP keys again */
1609 wpa_set_wep_keys(wpa_s, ssid);
1610 }
1611
1612 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1613 /*
1614 * Do not allow EAP session resumption between different
1615 * network configurations.
1616 */
1617 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1618 }
8bac466b 1619 old_ssid = wpa_s->current_ssid;
6fc6879b 1620 wpa_s->current_ssid = ssid;
8f770587 1621 wpa_s->current_bss = bss;
6fc6879b
JM
1622 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1623 wpa_supplicant_initiate_eapol(wpa_s);
8bac466b
JM
1624 if (old_ssid != wpa_s->current_ssid)
1625 wpas_notify_network_changed(wpa_s);
6fc6879b
JM
1626}
1627
1628
09f58c09
JM
1629static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1630 const u8 *addr)
1631{
1632 struct wpa_ssid *old_ssid;
1633
1634 wpa_clear_keys(wpa_s, addr);
09f58c09 1635 old_ssid = wpa_s->current_ssid;
0d30cc24 1636 wpa_supplicant_mark_disassoc(wpa_s);
09f58c09
JM
1637 wpa_sm_set_config(wpa_s->wpa, NULL);
1638 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1639 if (old_ssid != wpa_s->current_ssid)
1640 wpas_notify_network_changed(wpa_s);
1641 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1642}
1643
1644
6fc6879b
JM
1645/**
1646 * wpa_supplicant_disassociate - Disassociate the current connection
1647 * @wpa_s: Pointer to wpa_supplicant data
1648 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1649 *
1650 * This function is used to request %wpa_supplicant to disassociate with the
1651 * current AP.
1652 */
1653void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1654 int reason_code)
1655{
1656 u8 *addr = NULL;
ef48ff94 1657 union wpa_event_data event;
8bac466b 1658
a8e16edc 1659 if (!is_zero_ether_addr(wpa_s->bssid)) {
17fbb751 1660 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
6fc6879b 1661 addr = wpa_s->bssid;
ef48ff94
JM
1662 os_memset(&event, 0, sizeof(event));
1663 event.disassoc_info.reason_code = (u16) reason_code;
1664 event.disassoc_info.locally_generated = 1;
1665 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
6fc6879b 1666 }
09f58c09
JM
1667
1668 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
1669}
1670
1671
1672/**
1673 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1674 * @wpa_s: Pointer to wpa_supplicant data
1675 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1676 *
073ab58f 1677 * This function is used to request %wpa_supplicant to deauthenticate from the
6fc6879b
JM
1678 * current AP.
1679 */
1680void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1681 int reason_code)
1682{
1683 u8 *addr = NULL;
ef48ff94 1684 union wpa_event_data event;
8bac466b 1685
a8e16edc 1686 if (!is_zero_ether_addr(wpa_s->bssid)) {
17fbb751 1687 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
6fc6879b 1688 addr = wpa_s->bssid;
ef48ff94
JM
1689 os_memset(&event, 0, sizeof(event));
1690 event.deauth_info.reason_code = (u16) reason_code;
1691 event.deauth_info.locally_generated = 1;
1692 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
6fc6879b 1693 }
09f58c09
JM
1694
1695 wpa_supplicant_clear_connection(wpa_s, addr);
6fc6879b
JM
1696}
1697
1698
86b89452
WS
1699/**
1700 * wpa_supplicant_enable_network - Mark a configured network as enabled
1701 * @wpa_s: wpa_supplicant structure for a network interface
1702 * @ssid: wpa_ssid structure for a configured network or %NULL
1703 *
1704 * Enables the specified network or all networks if no network specified.
1705 */
1706void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1707 struct wpa_ssid *ssid)
1708{
1709 struct wpa_ssid *other_ssid;
1710 int was_disabled;
1711
1712 if (ssid == NULL) {
4dac0245
JM
1713 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1714 other_ssid = other_ssid->next) {
1715 if (other_ssid->disabled == 2)
1716 continue; /* do not change persistent P2P group
1717 * data */
86b89452
WS
1718 if (other_ssid == wpa_s->current_ssid &&
1719 other_ssid->disabled)
1720 wpa_s->reassociate = 1;
1721
1722 was_disabled = other_ssid->disabled;
1723
1724 other_ssid->disabled = 0;
00e5e3d5
JM
1725 if (was_disabled)
1726 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
86b89452
WS
1727
1728 if (was_disabled != other_ssid->disabled)
1729 wpas_notify_network_enabled_changed(
1730 wpa_s, other_ssid);
86b89452
WS
1731 }
1732 if (wpa_s->reassociate)
1733 wpa_supplicant_req_scan(wpa_s, 0, 0);
4dac0245 1734 } else if (ssid->disabled && ssid->disabled != 2) {
adc8d4a7
KM
1735 if (wpa_s->current_ssid == NULL) {
1736 /*
1737 * Try to reassociate since there is no current
1738 * configuration and a new network was made available.
1739 */
1740 wpa_s->reassociate = 1;
1741 wpa_supplicant_req_scan(wpa_s, 0, 0);
1742 }
86b89452
WS
1743
1744 was_disabled = ssid->disabled;
1745
1746 ssid->disabled = 0;
00e5e3d5 1747 wpas_clear_temp_disabled(wpa_s, ssid, 1);
86b89452
WS
1748
1749 if (was_disabled != ssid->disabled)
1750 wpas_notify_network_enabled_changed(wpa_s, ssid);
1751 }
1752}
1753
1754
1755/**
1756 * wpa_supplicant_disable_network - Mark a configured network as disabled
1757 * @wpa_s: wpa_supplicant structure for a network interface
1758 * @ssid: wpa_ssid structure for a configured network or %NULL
1759 *
1760 * Disables the specified network or all networks if no network specified.
1761 */
1762void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1763 struct wpa_ssid *ssid)
1764{
1765 struct wpa_ssid *other_ssid;
1766 int was_disabled;
1767
1768 if (ssid == NULL) {
4dac0245
JM
1769 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1770 other_ssid = other_ssid->next) {
86b89452 1771 was_disabled = other_ssid->disabled;
4dac0245
JM
1772 if (was_disabled == 2)
1773 continue; /* do not change persistent P2P group
1774 * data */
86b89452
WS
1775
1776 other_ssid->disabled = 1;
1777
1778 if (was_disabled != other_ssid->disabled)
1779 wpas_notify_network_enabled_changed(
1780 wpa_s, other_ssid);
86b89452
WS
1781 }
1782 if (wpa_s->current_ssid)
1783 wpa_supplicant_disassociate(
1784 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
4dac0245 1785 } else if (ssid->disabled != 2) {
86b89452
WS
1786 if (ssid == wpa_s->current_ssid)
1787 wpa_supplicant_disassociate(
1788 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1789
1790 was_disabled = ssid->disabled;
1791
1792 ssid->disabled = 1;
1793
1794 if (was_disabled != ssid->disabled)
1795 wpas_notify_network_enabled_changed(wpa_s, ssid);
1796 }
1797}
1798
1799
1800/**
1801 * wpa_supplicant_select_network - Attempt association with a network
1802 * @wpa_s: wpa_supplicant structure for a network interface
1803 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1804 */
1805void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1806 struct wpa_ssid *ssid)
1807{
1808
1809 struct wpa_ssid *other_ssid;
d93dfbd5 1810 int disconnected = 0;
86b89452 1811
d93dfbd5 1812 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
86b89452
WS
1813 wpa_supplicant_disassociate(
1814 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
d93dfbd5
JM
1815 disconnected = 1;
1816 }
86b89452 1817
00e5e3d5
JM
1818 if (ssid)
1819 wpas_clear_temp_disabled(wpa_s, ssid, 1);
1820
86b89452
WS
1821 /*
1822 * Mark all other networks disabled or mark all networks enabled if no
1823 * network specified.
1824 */
4dac0245
JM
1825 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1826 other_ssid = other_ssid->next) {
86b89452 1827 int was_disabled = other_ssid->disabled;
4dac0245
JM
1828 if (was_disabled == 2)
1829 continue; /* do not change persistent P2P group data */
86b89452
WS
1830
1831 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
00e5e3d5
JM
1832 if (was_disabled && !other_ssid->disabled)
1833 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
86b89452
WS
1834
1835 if (was_disabled != other_ssid->disabled)
1836 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
86b89452 1837 }
2a6f78fb
JJ
1838
1839 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1840 /* We are already associated with the selected network */
1841 wpa_printf(MSG_DEBUG, "Already associated with the "
1842 "selected network - do nothing");
1843 return;
1844 }
1845
96efeeb6
JM
1846 if (ssid)
1847 wpa_s->current_ssid = ssid;
7dcdcfd6 1848 wpa_s->connect_without_scan = NULL;
86b89452
WS
1849 wpa_s->disconnected = 0;
1850 wpa_s->reassociate = 1;
d93dfbd5 1851 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
86b89452 1852
a1641d26
JM
1853 if (ssid)
1854 wpas_notify_network_selected(wpa_s, ssid);
86b89452
WS
1855}
1856
1857
1858/**
1859 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1860 * @wpa_s: wpa_supplicant structure for a network interface
1861 * @ap_scan: AP scan mode
1862 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1863 *
1864 */
1865int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1866{
1867
1868 int old_ap_scan;
1869
1870 if (ap_scan < 0 || ap_scan > 2)
1871 return -1;
1872
48f8e036
DS
1873#ifdef ANDROID
1874 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1875 wpa_s->wpa_state >= WPA_ASSOCIATING &&
1876 wpa_s->wpa_state < WPA_COMPLETED) {
1877 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1878 "associating", wpa_s->conf->ap_scan, ap_scan);
1879 return 0;
1880 }
1881#endif /* ANDROID */
1882
86b89452
WS
1883 old_ap_scan = wpa_s->conf->ap_scan;
1884 wpa_s->conf->ap_scan = ap_scan;
1885
1886 if (old_ap_scan != wpa_s->conf->ap_scan)
1887 wpas_notify_ap_scan_changed(wpa_s);
1888
1889 return 0;
1890}
1891
1892
78633c37
SL
1893/**
1894 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1895 * @wpa_s: wpa_supplicant structure for a network interface
1896 * @expire_age: Expiration age in seconds
1897 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1898 *
1899 */
1900int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1901 unsigned int bss_expire_age)
1902{
1903 if (bss_expire_age < 10) {
1904 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1905 bss_expire_age);
1906 return -1;
1907 }
1908 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1909 bss_expire_age);
1910 wpa_s->conf->bss_expiration_age = bss_expire_age;
1911
1912 return 0;
1913}
1914
1915
1916/**
1917 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1918 * @wpa_s: wpa_supplicant structure for a network interface
1919 * @expire_count: number of scans after which an unseen BSS is reclaimed
1920 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1921 *
1922 */
1923int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1924 unsigned int bss_expire_count)
1925{
1926 if (bss_expire_count < 1) {
1927 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1928 bss_expire_count);
1929 return -1;
1930 }
1931 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1932 bss_expire_count);
1933 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1934
1935 return 0;
1936}
1937
1938
c6e86b63
MA
1939/**
1940 * wpa_supplicant_set_scan_interval - Set scan interval
1941 * @wpa_s: wpa_supplicant structure for a network interface
1942 * @scan_interval: scan interval in seconds
1943 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
1944 *
1945 */
1946int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
1947 int scan_interval)
1948{
1949 if (scan_interval < 0) {
1950 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
1951 scan_interval);
1952 return -1;
1953 }
1954 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
1955 scan_interval);
1956 wpa_s->scan_interval = scan_interval;
1957
1958 return 0;
1959}
1960
1961
86b89452
WS
1962/**
1963 * wpa_supplicant_set_debug_params - Set global debug params
1964 * @global: wpa_global structure
1965 * @debug_level: debug level
1966 * @debug_timestamp: determines if show timestamp in debug data
1967 * @debug_show_keys: determines if show keys in debug data
1968 * Returns: 0 if succeed or -1 if debug_level has wrong value
1969 */
1970int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1971 int debug_timestamp, int debug_show_keys)
1972{
1973
1974 int old_level, old_timestamp, old_show_keys;
1975
1976 /* check for allowed debuglevels */
14dc0011
PS
1977 if (debug_level != MSG_EXCESSIVE &&
1978 debug_level != MSG_MSGDUMP &&
86b89452
WS
1979 debug_level != MSG_DEBUG &&
1980 debug_level != MSG_INFO &&
1981 debug_level != MSG_WARNING &&
1982 debug_level != MSG_ERROR)
1983 return -1;
1984
1985 old_level = wpa_debug_level;
1986 old_timestamp = wpa_debug_timestamp;
1987 old_show_keys = wpa_debug_show_keys;
1988
1989 wpa_debug_level = debug_level;
1990 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1991 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1992
db9133ac
WS
1993 if (wpa_debug_level != old_level)
1994 wpas_notify_debug_level_changed(global);
1995 if (wpa_debug_timestamp != old_timestamp)
1996 wpas_notify_debug_timestamp_changed(global);
1997 if (wpa_debug_show_keys != old_show_keys)
1998 wpas_notify_debug_show_keys_changed(global);
86b89452
WS
1999
2000 return 0;
2001}
2002
2003
6fc6879b
JM
2004/**
2005 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2006 * @wpa_s: Pointer to wpa_supplicant data
2007 * Returns: A pointer to the current network structure or %NULL on failure
2008 */
2009struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2010{
2011 struct wpa_ssid *entry;
2012 u8 ssid[MAX_SSID_LEN];
2013 int res;
2014 size_t ssid_len;
2015 u8 bssid[ETH_ALEN];
2016 int wired;
2017
17fbb751
JM
2018 res = wpa_drv_get_ssid(wpa_s, ssid);
2019 if (res < 0) {
2020 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2021 "driver");
2022 return NULL;
6fc6879b 2023 }
17fbb751 2024 ssid_len = res;
6fc6879b 2025
17fbb751 2026 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
f049052b
BG
2027 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2028 "driver");
6fc6879b
JM
2029 return NULL;
2030 }
2031
c2a04078
JM
2032 wired = wpa_s->conf->ap_scan == 0 &&
2033 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
6fc6879b
JM
2034
2035 entry = wpa_s->conf->ssid;
2036 while (entry) {
349493bd 2037 if (!wpas_network_disabled(wpa_s, entry) &&
6fc6879b
JM
2038 ((ssid_len == entry->ssid_len &&
2039 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2040 (!entry->bssid_set ||
2041 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2042 return entry;
24c23d1b 2043#ifdef CONFIG_WPS
349493bd 2044 if (!wpas_network_disabled(wpa_s, entry) &&
24c23d1b
JM
2045 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2046 (entry->ssid == NULL || entry->ssid_len == 0) &&
2047 (!entry->bssid_set ||
2048 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2049 return entry;
2050#endif /* CONFIG_WPS */
7d232e23 2051
349493bd 2052 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
7d232e23
ZC
2053 entry->ssid_len == 0 &&
2054 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2055 return entry;
2056
6fc6879b
JM
2057 entry = entry->next;
2058 }
2059
2060 return NULL;
2061}
2062
2063
7756114f
JM
2064static int select_driver(struct wpa_supplicant *wpa_s, int i)
2065{
2066 struct wpa_global *global = wpa_s->global;
2067
2068 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2069 global->drv_priv[i] = wpa_drivers[i]->global_init();
2070 if (global->drv_priv[i] == NULL) {
2071 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2072 "'%s'", wpa_drivers[i]->name);
2073 return -1;
2074 }
2075 }
2076
2077 wpa_s->driver = wpa_drivers[i];
2078 wpa_s->global_drv_priv = global->drv_priv[i];
2079
2080 return 0;
2081}
2082
2083
6fc6879b
JM
2084static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2085 const char *name)
2086{
2087 int i;
362f781e 2088 size_t len;
74b1c84a 2089 const char *pos, *driver = name;
6fc6879b
JM
2090
2091 if (wpa_s == NULL)
2092 return -1;
2093
c5121837 2094 if (wpa_drivers[0] == NULL) {
f049052b
BG
2095 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2096 "wpa_supplicant");
6fc6879b
JM
2097 return -1;
2098 }
2099
2100 if (name == NULL) {
2101 /* default to first driver in the list */
7756114f 2102 return select_driver(wpa_s, 0);
6fc6879b
JM
2103 }
2104
74b1c84a
SO
2105 do {
2106 pos = os_strchr(driver, ',');
2107 if (pos)
2108 len = pos - driver;
2109 else
2110 len = os_strlen(driver);
2111
2112 for (i = 0; wpa_drivers[i]; i++) {
2113 if (os_strlen(wpa_drivers[i]->name) == len &&
2114 os_strncmp(driver, wpa_drivers[i]->name, len) ==
0f4668ce
DW
2115 0) {
2116 /* First driver that succeeds wins */
2117 if (select_driver(wpa_s, i) == 0)
2118 return 0;
2119 }
6fc6879b 2120 }
74b1c84a
SO
2121
2122 driver = pos + 1;
2123 } while (pos);
6fc6879b 2124
f049052b 2125 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
6fc6879b
JM
2126 return -1;
2127}
2128
2129
a8e0505b
JM
2130/**
2131 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2132 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2133 * with struct wpa_driver_ops::init()
2134 * @src_addr: Source address of the EAPOL frame
2135 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2136 * @len: Length of the EAPOL data
2137 *
2138 * This function is called for each received EAPOL frame. Most driver
2139 * interfaces rely on more generic OS mechanism for receiving frames through
2140 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2141 * take care of received EAPOL frames and deliver them to the core supplicant
2142 * code by calling this function.
2143 */
6fc6879b
JM
2144void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2145 const u8 *buf, size_t len)
2146{
2147 struct wpa_supplicant *wpa_s = ctx;
2148
f049052b 2149 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
6fc6879b
JM
2150 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2151
1ff73338
JM
2152 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
2153 /*
2154 * There is possible race condition between receiving the
2155 * association event and the EAPOL frame since they are coming
2156 * through different paths from the driver. In order to avoid
2157 * issues in trying to process the EAPOL frame before receiving
2158 * association information, lets queue it for processing until
2159 * the association event is received.
2160 */
f049052b
BG
2161 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
2162 "of received EAPOL frame");
1ff73338
JM
2163 wpabuf_free(wpa_s->pending_eapol_rx);
2164 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2165 if (wpa_s->pending_eapol_rx) {
2166 os_get_time(&wpa_s->pending_eapol_rx_time);
2167 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2168 ETH_ALEN);
2169 }
2170 return;
2171 }
2172
db149ac9
JM
2173#ifdef CONFIG_AP
2174 if (wpa_s->ap_iface) {
2175 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2176 return;
2177 }
2178#endif /* CONFIG_AP */
2179
6fc6879b 2180 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
f049052b
BG
2181 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2182 "no key management is configured");
6fc6879b
JM
2183 return;
2184 }
2185
2186 if (wpa_s->eapol_received == 0 &&
c2a04078 2187 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
56586197 2188 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
9c972abb
JM
2189 wpa_s->wpa_state != WPA_COMPLETED) &&
2190 (wpa_s->current_ssid == NULL ||
2191 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
6fc6879b
JM
2192 /* Timeout for completing IEEE 802.1X and WPA authentication */
2193 wpa_supplicant_req_auth_timeout(
2194 wpa_s,
56586197 2195 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
a6f06dab
AT
2196 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2197 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
6fc6879b
JM
2198 70 : 10, 0);
2199 }
2200 wpa_s->eapol_received++;
2201
2202 if (wpa_s->countermeasures) {
f049052b
BG
2203 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2204 "EAPOL packet");
6fc6879b
JM
2205 return;
2206 }
2207
8be18440
JM
2208#ifdef CONFIG_IBSS_RSN
2209 if (wpa_s->current_ssid &&
d7dcba70 2210 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
8be18440
JM
2211 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2212 return;
2213 }
2214#endif /* CONFIG_IBSS_RSN */
2215
6fc6879b
JM
2216 /* Source address of the incoming EAPOL frame could be compared to the
2217 * current BSSID. However, it is possible that a centralized
2218 * Authenticator could be using another MAC address than the BSSID of
2219 * an AP, so just allow any address to be used for now. The replies are
2220 * still sent to the current BSSID (if available), though. */
2221
2222 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
56586197 2223 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
6fc6879b
JM
2224 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2225 return;
2226 wpa_drv_poll(wpa_s);
c2a04078 2227 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
6fc6879b 2228 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
56586197 2229 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
2230 /*
2231 * Set portValid = TRUE here since we are going to skip 4-way
2232 * handshake processing which would normally set portValid. We
2233 * need this to allow the EAPOL state machines to be completed
2234 * without going through EAPOL-Key handshake.
2235 */
2236 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2237 }
2238}
2239
2240
bfba8deb 2241int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
6fc6879b 2242{
6fc6879b
JM
2243 if (wpa_s->driver->send_eapol) {
2244 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2245 if (addr)
2246 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
fdadd5fe
JM
2247 } else if (!(wpa_s->drv_flags &
2248 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
bfba8deb 2249 l2_packet_deinit(wpa_s->l2);
6fc6879b
JM
2250 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2251 wpa_drv_get_mac_addr(wpa_s),
2252 ETH_P_EAPOL,
2253 wpa_supplicant_rx_eapol, wpa_s, 0);
2254 if (wpa_s->l2 == NULL)
2255 return -1;
fdadd5fe
JM
2256 } else {
2257 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2258 if (addr)
2259 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
6fc6879b
JM
2260 }
2261
2262 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
f049052b 2263 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
6fc6879b
JM
2264 return -1;
2265 }
2266
f049052b
BG
2267 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2268 MAC2STR(wpa_s->own_addr));
f98eb880 2269 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
6fc6879b 2270
bfba8deb
JM
2271 return 0;
2272}
2273
2274
25f839c6
JM
2275static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2276 const u8 *buf, size_t len)
2277{
2278 struct wpa_supplicant *wpa_s = ctx;
2279 const struct l2_ethhdr *eth;
2280
2281 if (len < sizeof(*eth))
2282 return;
2283 eth = (const struct l2_ethhdr *) buf;
2284
2285 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2286 !(eth->h_dest[0] & 0x01)) {
2287 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2288 " (bridge - not for this interface - ignore)",
2289 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2290 return;
2291 }
2292
2293 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2294 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2295 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2296 len - sizeof(*eth));
2297}
2298
2299
bfba8deb
JM
2300/**
2301 * wpa_supplicant_driver_init - Initialize driver interface parameters
2302 * @wpa_s: Pointer to wpa_supplicant data
2303 * Returns: 0 on success, -1 on failure
2304 *
2305 * This function is called to initialize driver interface parameters.
2306 * wpa_drv_init() must have been called before this function to initialize the
2307 * driver interface.
2308 */
2309int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2310{
2311 static int interface_count = 0;
2312
2313 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2314 return -1;
2315
6fc6879b 2316 if (wpa_s->bridge_ifname[0]) {
f049052b
BG
2317 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2318 "interface '%s'", wpa_s->bridge_ifname);
6fc6879b
JM
2319 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2320 wpa_s->own_addr,
2321 ETH_P_EAPOL,
25f839c6
JM
2322 wpa_supplicant_rx_eapol_bridge,
2323 wpa_s, 1);
6fc6879b 2324 if (wpa_s->l2_br == NULL) {
f049052b
BG
2325 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2326 "connection for the bridge interface '%s'",
2327 wpa_s->bridge_ifname);
6fc6879b
JM
2328 return -1;
2329 }
2330 }
2331
6fc6879b
JM
2332 wpa_clear_keys(wpa_s, NULL);
2333
2334 /* Make sure that TKIP countermeasures are not left enabled (could
2335 * happen if wpa_supplicant is killed during countermeasures. */
2336 wpa_drv_set_countermeasures(wpa_s, 0);
2337
f049052b 2338 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
6fc6879b
JM
2339 wpa_drv_flush_pmkid(wpa_s);
2340
ba2a573c 2341 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
b3aa456b
ES
2342 wpa_s->prev_scan_wildcard = 0;
2343
349493bd 2344 if (wpa_supplicant_enabled_networks(wpa_s)) {
6a90053c
LC
2345 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2346 100000))
a4cba8f1
LC
2347 wpa_supplicant_req_scan(wpa_s, interface_count,
2348 100000);
74e259ec
JM
2349 interface_count++;
2350 } else
2351 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
6fc6879b
JM
2352
2353 return 0;
2354}
2355
2356
2357static int wpa_supplicant_daemon(const char *pid_file)
2358{
2359 wpa_printf(MSG_DEBUG, "Daemonize..");
2360 return os_daemonize(pid_file);
2361}
2362
2363
2364static struct wpa_supplicant * wpa_supplicant_alloc(void)
2365{
2366 struct wpa_supplicant *wpa_s;
2367
2368 wpa_s = os_zalloc(sizeof(*wpa_s));
2369 if (wpa_s == NULL)
2370 return NULL;
2371 wpa_s->scan_req = 1;
67b9bd08 2372 wpa_s->scan_interval = 5;
c302f207 2373 wpa_s->new_connection = 1;
b22128ef 2374 wpa_s->parent = wpa_s;
cbdf3507 2375 wpa_s->sched_scanning = 0;
6fc6879b
JM
2376
2377 return wpa_s;
2378}
2379
2380
80e8a5ee
BG
2381#ifdef CONFIG_HT_OVERRIDES
2382
2383static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2384 struct ieee80211_ht_capabilities *htcaps,
2385 struct ieee80211_ht_capabilities *htcaps_mask,
2386 const char *ht_mcs)
2387{
2388 /* parse ht_mcs into hex array */
2389 int i;
2390 const char *tmp = ht_mcs;
2391 char *end = NULL;
2392
2393 /* If ht_mcs is null, do not set anything */
2394 if (!ht_mcs)
2395 return 0;
2396
2397 /* This is what we are setting in the kernel */
2398 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2399
2400 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2401
2402 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2403 errno = 0;
2404 long v = strtol(tmp, &end, 16);
2405 if (errno == 0) {
2406 wpa_msg(wpa_s, MSG_DEBUG,
2407 "htcap value[%i]: %ld end: %p tmp: %p",
2408 i, v, end, tmp);
2409 if (end == tmp)
2410 break;
2411
2412 htcaps->supported_mcs_set[i] = v;
2413 tmp = end;
2414 } else {
2415 wpa_msg(wpa_s, MSG_ERROR,
2416 "Failed to parse ht-mcs: %s, error: %s\n",
2417 ht_mcs, strerror(errno));
2418 return -1;
2419 }
2420 }
2421
2422 /*
2423 * If we were able to parse any values, then set mask for the MCS set.
2424 */
2425 if (i) {
2426 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2427 IEEE80211_HT_MCS_MASK_LEN - 1);
2428 /* skip the 3 reserved bits */
2429 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2430 0x1f;
2431 }
2432
2433 return 0;
2434}
2435
2436
2437static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2438 struct ieee80211_ht_capabilities *htcaps,
2439 struct ieee80211_ht_capabilities *htcaps_mask,
2440 int disabled)
2441{
2442 u16 msk;
2443
2444 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2445
2446 if (disabled == -1)
2447 return 0;
2448
2449 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2450 htcaps_mask->ht_capabilities_info |= msk;
2451 if (disabled)
2452 htcaps->ht_capabilities_info &= msk;
2453 else
2454 htcaps->ht_capabilities_info |= msk;
2455
2456 return 0;
2457}
2458
2459
2460static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2461 struct ieee80211_ht_capabilities *htcaps,
2462 struct ieee80211_ht_capabilities *htcaps_mask,
2463 int factor)
2464{
2465 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2466
2467 if (factor == -1)
2468 return 0;
2469
2470 if (factor < 0 || factor > 3) {
2471 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2472 "Must be 0-3 or -1", factor);
2473 return -EINVAL;
2474 }
2475
2476 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2477 htcaps->a_mpdu_params &= ~0x3;
2478 htcaps->a_mpdu_params |= factor & 0x3;
2479
2480 return 0;
2481}
2482
2483
2484static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2485 struct ieee80211_ht_capabilities *htcaps,
2486 struct ieee80211_ht_capabilities *htcaps_mask,
2487 int density)
2488{
2489 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2490
2491 if (density == -1)
2492 return 0;
2493
2494 if (density < 0 || density > 7) {
2495 wpa_msg(wpa_s, MSG_ERROR,
2496 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2497 density);
2498 return -EINVAL;
2499 }
2500
2501 htcaps_mask->a_mpdu_params |= 0x1C;
2502 htcaps->a_mpdu_params &= ~(0x1C);
2503 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2504
2505 return 0;
2506}
2507
2508
2509static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2510 struct ieee80211_ht_capabilities *htcaps,
2511 struct ieee80211_ht_capabilities *htcaps_mask,
2512 int disabled)
2513{
2514 /* Masking these out disables HT40 */
2515 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2516 HT_CAP_INFO_SHORT_GI40MHZ);
2517
2518 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2519
2520 if (disabled)
2521 htcaps->ht_capabilities_info &= ~msk;
2522 else
2523 htcaps->ht_capabilities_info |= msk;
2524
2525 htcaps_mask->ht_capabilities_info |= msk;
2526
2527 return 0;
2528}
2529
2530
2531void wpa_supplicant_apply_ht_overrides(
2532 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2533 struct wpa_driver_associate_params *params)
2534{
2535 struct ieee80211_ht_capabilities *htcaps;
2536 struct ieee80211_ht_capabilities *htcaps_mask;
2537
2538 if (!ssid)
2539 return;
2540
2541 params->disable_ht = ssid->disable_ht;
2542 if (!params->htcaps || !params->htcaps_mask)
2543 return;
2544
2545 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2546 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2547 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2548 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2549 ssid->disable_max_amsdu);
2550 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2551 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2552 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
2553}
2554
2555#endif /* CONFIG_HT_OVERRIDES */
2556
2557
f64adcd7
JM
2558static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
2559{
2560#ifdef PCSC_FUNCS
2561 size_t len;
2562
2563 if (!wpa_s->conf->pcsc_reader)
2564 return 0;
2565
2566 wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
2567 if (!wpa_s->scard)
2568 return 1;
2569
2570 if (wpa_s->conf->pcsc_pin &&
2571 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
2572 scard_deinit(wpa_s->scard);
2573 wpa_s->scard = NULL;
2574 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
2575 return -1;
2576 }
2577
2578 len = sizeof(wpa_s->imsi) - 1;
2579 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
2580 scard_deinit(wpa_s->scard);
2581 wpa_s->scard = NULL;
2582 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
2583 return -1;
2584 }
2585 wpa_s->imsi[len] = '\0';
2586
2587 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
2588
2589 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
2590 wpa_s->imsi, wpa_s->mnc_len);
2591
2592 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
2593 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
2594#endif /* PCSC_FUNCS */
2595
2596 return 0;
2597}
2598
2599
306ae225
JM
2600int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
2601{
2602 char *val, *pos;
2603
2604 ext_password_deinit(wpa_s->ext_pw);
2605 wpa_s->ext_pw = NULL;
2606 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
2607
2608 if (!wpa_s->conf->ext_password_backend)
2609 return 0;
2610
2611 val = os_strdup(wpa_s->conf->ext_password_backend);
2612 if (val == NULL)
2613 return -1;
2614 pos = os_strchr(val, ':');
2615 if (pos)
2616 *pos++ = '\0';
2617
2618 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
2619
2620 wpa_s->ext_pw = ext_password_init(val, pos);
2621 os_free(val);
2622 if (wpa_s->ext_pw == NULL) {
2623 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
2624 return -1;
2625 }
2626 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
2627
2628 return 0;
2629}
2630
2631
6fc6879b
JM
2632static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2633 struct wpa_interface *iface)
2634{
362f781e
JM
2635 const char *ifname, *driver;
2636 struct wpa_driver_capa capa;
2637
6fc6879b
JM
2638 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2639 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2640 iface->confname ? iface->confname : "N/A",
2641 iface->driver ? iface->driver : "default",
2642 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2643 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2644
6fc6879b
JM
2645 if (iface->confname) {
2646#ifdef CONFIG_BACKEND_FILE
2647 wpa_s->confname = os_rel2abs_path(iface->confname);
2648 if (wpa_s->confname == NULL) {
2649 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2650 "for configuration file '%s'.",
2651 iface->confname);
2652 return -1;
2653 }
2654 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2655 iface->confname, wpa_s->confname);
2656#else /* CONFIG_BACKEND_FILE */
2657 wpa_s->confname = os_strdup(iface->confname);
2658#endif /* CONFIG_BACKEND_FILE */
2659 wpa_s->conf = wpa_config_read(wpa_s->confname);
2660 if (wpa_s->conf == NULL) {
2661 wpa_printf(MSG_ERROR, "Failed to read or parse "
2662 "configuration '%s'.", wpa_s->confname);
2663 return -1;
2664 }
2665
2666 /*
2667 * Override ctrl_interface and driver_param if set on command
2668 * line.
2669 */
2670 if (iface->ctrl_interface) {
2671 os_free(wpa_s->conf->ctrl_interface);
2672 wpa_s->conf->ctrl_interface =
2673 os_strdup(iface->ctrl_interface);
2674 }
2675
2676 if (iface->driver_param) {
2677 os_free(wpa_s->conf->driver_param);
2678 wpa_s->conf->driver_param =
2679 os_strdup(iface->driver_param);
2680 }
2681 } else
2682 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2683 iface->driver_param);
2684
2685 if (wpa_s->conf == NULL) {
2686 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2687 return -1;
2688 }
2689
2690 if (iface->ifname == NULL) {
2691 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2692 return -1;
2693 }
2694 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2695 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2696 iface->ifname);
2697 return -1;
2698 }
2699 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2700
2701 if (iface->bridge_ifname) {
2702 if (os_strlen(iface->bridge_ifname) >=
2703 sizeof(wpa_s->bridge_ifname)) {
2704 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2705 "name '%s'.", iface->bridge_ifname);
2706 return -1;
2707 }
2708 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2709 sizeof(wpa_s->bridge_ifname));
2710 }
2711
6fc6879b
JM
2712 /* RSNA Supplicant Key Management - INITIALIZE */
2713 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2714 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2715
2716 /* Initialize driver interface and register driver event handler before
2717 * L2 receive handler so that association events are processed before
2718 * EAPOL-Key packets if both become available for the same select()
2719 * call. */
362f781e
JM
2720 driver = iface->driver;
2721next_driver:
2722 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2723 return -1;
2724
6fc6879b
JM
2725 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2726 if (wpa_s->drv_priv == NULL) {
362f781e 2727 const char *pos;
a5b9337f 2728 pos = driver ? os_strchr(driver, ',') : NULL;
362f781e 2729 if (pos) {
f049052b
BG
2730 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2731 "driver interface - try next driver wrapper");
362f781e
JM
2732 driver = pos + 1;
2733 goto next_driver;
2734 }
f049052b
BG
2735 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2736 "interface");
6fc6879b
JM
2737 return -1;
2738 }
2739 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
f049052b
BG
2740 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2741 "driver_param '%s'", wpa_s->conf->driver_param);
6fc6879b
JM
2742 return -1;
2743 }
2744
2745 ifname = wpa_drv_get_ifname(wpa_s);
2746 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
f049052b
BG
2747 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2748 "interface name with '%s'", ifname);
6fc6879b
JM
2749 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2750 }
2751
2752 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2753 return -1;
2754
2755 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2756 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2757 NULL);
2758 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2759
2760 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2761 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2762 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
f049052b
BG
2763 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2764 "dot11RSNAConfigPMKLifetime");
6fc6879b
JM
2765 return -1;
2766 }
2767
2768 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2769 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2770 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
f049052b 2771 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
6fc6879b
JM
2772 "dot11RSNAConfigPMKReauthThreshold");
2773 return -1;
2774 }
2775
2776 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2777 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2778 wpa_s->conf->dot11RSNAConfigSATimeout)) {
f049052b
BG
2779 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2780 "dot11RSNAConfigSATimeout");
6fc6879b
JM
2781 return -1;
2782 }
2783
6bf731e8
CL
2784 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2785 &wpa_s->hw.num_modes,
2786 &wpa_s->hw.flags);
2787
814782b9 2788 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
c58ab8f2 2789 wpa_s->drv_capa_known = 1;
814782b9 2790 wpa_s->drv_flags = capa.flags;
349493bd 2791 wpa_s->drv_enc = capa.enc;
4f73d88a 2792 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
814782b9 2793 wpa_s->max_scan_ssids = capa.max_scan_ssids;
cbdf3507
LC
2794 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2795 wpa_s->sched_scan_supported = capa.sched_scan_supported;
b59e6f26 2796 wpa_s->max_match_sets = capa.max_match_sets;
814782b9 2797 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
c4ea4c5c 2798 wpa_s->max_stations = capa.max_stations;
814782b9
JM
2799 }
2800 if (wpa_s->max_remain_on_chan == 0)
2801 wpa_s->max_remain_on_chan = 1000;
2802
6fc6879b
JM
2803 if (wpa_supplicant_driver_init(wpa_s) < 0)
2804 return -1;
2805
281ff0aa
GP
2806#ifdef CONFIG_TDLS
2807 if (wpa_tdls_init(wpa_s->wpa))
2808 return -1;
2809#endif /* CONFIG_TDLS */
2810
315ce40a
JM
2811 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2812 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
f049052b 2813 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
6d158490
LR
2814 return -1;
2815 }
2816
116654ce
JM
2817 if (wpas_wps_init(wpa_s))
2818 return -1;
2819
6fc6879b
JM
2820 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2821 return -1;
2822 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2823
2824 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2825 if (wpa_s->ctrl_iface == NULL) {
2826 wpa_printf(MSG_ERROR,
2827 "Failed to initialize control interface '%s'.\n"
2828 "You may have another wpa_supplicant process "
2829 "already running or the file was\n"
2830 "left by an unclean termination of wpa_supplicant "
2831 "in which case you will need\n"
2832 "to manually remove this file before starting "
2833 "wpa_supplicant again.\n",
2834 wpa_s->conf->ctrl_interface);
2835 return -1;
2836 }
2837
04ea7b79
JM
2838 wpa_s->gas = gas_query_init(wpa_s);
2839 if (wpa_s->gas == NULL) {
2840 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2841 return -1;
2842 }
2843
b22128ef
JM
2844#ifdef CONFIG_P2P
2845 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
f049052b 2846 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
b22128ef
JM
2847 return -1;
2848 }
2849#endif /* CONFIG_P2P */
2850
83922c2d
JM
2851 if (wpa_bss_init(wpa_s) < 0)
2852 return -1;
83922c2d 2853
f64adcd7
JM
2854 if (pcsc_reader_init(wpa_s) < 0)
2855 return -1;
2856
306ae225
JM
2857 if (wpas_init_ext_pw(wpa_s) < 0)
2858 return -1;
2859
6fc6879b
JM
2860 return 0;
2861}
2862
2863
2ee055b3 2864static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
df509539 2865 int notify, int terminate)
6fc6879b
JM
2866{
2867 if (wpa_s->drv_priv) {
2868 wpa_supplicant_deauthenticate(wpa_s,
2869 WLAN_REASON_DEAUTH_LEAVING);
2870
6fc6879b
JM
2871 wpa_drv_set_countermeasures(wpa_s, 0);
2872 wpa_clear_keys(wpa_s, NULL);
2873 }
2874
8e56d189
JM
2875 wpa_supplicant_cleanup(wpa_s);
2876
ab28911d
JM
2877#ifdef CONFIG_P2P
2878 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
2879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
2880 "the management interface is being removed");
2881 wpas_p2p_deinit_global(wpa_s->global);
2882 }
2883#endif /* CONFIG_P2P */
2884
6fc6879b
JM
2885 if (wpa_s->drv_priv)
2886 wpa_drv_deinit(wpa_s);
2523ff6e
DS
2887
2888 if (notify)
2889 wpas_notify_iface_removed(wpa_s);
f0811516
DS
2890
2891 if (terminate)
2892 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2893
2894 if (wpa_s->ctrl_iface) {
2895 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2896 wpa_s->ctrl_iface = NULL;
2897 }
2898
2899 if (wpa_s->conf != NULL) {
2900 wpa_config_free(wpa_s->conf);
2901 wpa_s->conf = NULL;
2902 }
6fc6879b
JM
2903}
2904
2905
2906/**
2907 * wpa_supplicant_add_iface - Add a new network interface
2908 * @global: Pointer to global data from wpa_supplicant_init()
2909 * @iface: Interface configuration options
2910 * Returns: Pointer to the created interface or %NULL on failure
2911 *
2912 * This function is used to add new network interfaces for %wpa_supplicant.
2913 * This can be called before wpa_supplicant_run() to add interfaces before the
2914 * main event loop has been started. In addition, new interfaces can be added
2915 * dynamically while %wpa_supplicant is already running. This could happen,
2916 * e.g., when a hotplug network adapter is inserted.
2917 */
2918struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2919 struct wpa_interface *iface)
2920{
2921 struct wpa_supplicant *wpa_s;
d27df100 2922 struct wpa_interface t_iface;
8e56d189 2923 struct wpa_ssid *ssid;
6fc6879b
JM
2924
2925 if (global == NULL || iface == NULL)
2926 return NULL;
2927
2928 wpa_s = wpa_supplicant_alloc();
2929 if (wpa_s == NULL)
2930 return NULL;
2931
d8222ae3
JM
2932 wpa_s->global = global;
2933
d27df100
JM
2934 t_iface = *iface;
2935 if (global->params.override_driver) {
2936 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2937 "('%s' -> '%s')",
2938 iface->driver, global->params.override_driver);
2939 t_iface.driver = global->params.override_driver;
2940 }
2941 if (global->params.override_ctrl_interface) {
2942 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2943 "ctrl_interface ('%s' -> '%s')",
2944 iface->ctrl_interface,
2945 global->params.override_ctrl_interface);
2946 t_iface.ctrl_interface =
2947 global->params.override_ctrl_interface;
2948 }
2949 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
6fc6879b
JM
2950 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2951 iface->ifname);
df509539 2952 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
6fc6879b
JM
2953 os_free(wpa_s);
2954 return NULL;
2955 }
2956
dc461de4
WS
2957 /* Notify the control interfaces about new iface */
2958 if (wpas_notify_iface_added(wpa_s)) {
df509539 2959 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
6fc6879b
JM
2960 os_free(wpa_s);
2961 return NULL;
2962 }
1bd3f426 2963
8e56d189
JM
2964 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2965 wpas_notify_network_added(wpa_s, ssid);
2966
6fc6879b
JM
2967 wpa_s->next = global->ifaces;
2968 global->ifaces = wpa_s;
2969
f049052b 2970 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
99218999 2971 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
6fc6879b
JM
2972
2973 return wpa_s;
2974}
2975
2976
2977/**
2978 * wpa_supplicant_remove_iface - Remove a network interface
2979 * @global: Pointer to global data from wpa_supplicant_init()
2980 * @wpa_s: Pointer to the network interface to be removed
2981 * Returns: 0 if interface was removed, -1 if interface was not found
2982 *
2983 * This function can be used to dynamically remove network interfaces from
2984 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2985 * addition, this function is used to remove all remaining interfaces when
2986 * %wpa_supplicant is terminated.
2987 */
2988int wpa_supplicant_remove_iface(struct wpa_global *global,
df509539
DS
2989 struct wpa_supplicant *wpa_s,
2990 int terminate)
6fc6879b
JM
2991{
2992 struct wpa_supplicant *prev;
2993
2994 /* Remove interface from the global list of interfaces */
2995 prev = global->ifaces;
2996 if (prev == wpa_s) {
2997 global->ifaces = wpa_s->next;
2998 } else {
2999 while (prev && prev->next != wpa_s)
3000 prev = prev->next;
3001 if (prev == NULL)
3002 return -1;
3003 prev->next = wpa_s->next;
3004 }
3005
f049052b 3006 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
6fc6879b 3007
b22128ef
JM
3008 if (global->p2p_group_formation == wpa_s)
3009 global->p2p_group_formation = NULL;
df509539 3010 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
6fc6879b
JM
3011 os_free(wpa_s);
3012
3013 return 0;
3014}
3015
3016
cf83fb0b
PS
3017/**
3018 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3019 * @wpa_s: Pointer to the network interface
3020 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3021 */
3022const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3023{
3024 const char *eapol_method;
3025
3026 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3027 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3028 return "NO-EAP";
3029 }
3030
3031 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3032 if (eapol_method == NULL)
3033 return "UNKNOWN-EAP";
3034
3035 return eapol_method;
3036}
3037
3038
6fc6879b
JM
3039/**
3040 * wpa_supplicant_get_iface - Get a new network interface
3041 * @global: Pointer to global data from wpa_supplicant_init()
3042 * @ifname: Interface name
3043 * Returns: Pointer to the interface or %NULL if not found
3044 */
3045struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3046 const char *ifname)
3047{
3048 struct wpa_supplicant *wpa_s;
3049
3050 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3051 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3052 return wpa_s;
3053 }
3054 return NULL;
3055}
3056
3057
50b16da1 3058#ifndef CONFIG_NO_WPA_MSG
4f1495ae
BG
3059static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3060{
3061 struct wpa_supplicant *wpa_s = ctx;
3062 if (wpa_s == NULL)
3063 return NULL;
3064 return wpa_s->ifname;
3065}
50b16da1 3066#endif /* CONFIG_NO_WPA_MSG */
4f1495ae
BG
3067
3068
6fc6879b
JM
3069/**
3070 * wpa_supplicant_init - Initialize %wpa_supplicant
3071 * @params: Parameters for %wpa_supplicant
3072 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3073 *
3074 * This function is used to initialize %wpa_supplicant. After successful
3075 * initialization, the returned data pointer can be used to add and remove
3076 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3077 */
3078struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3079{
3080 struct wpa_global *global;
ac305589 3081 int ret, i;
6fc6879b
JM
3082
3083 if (params == NULL)
3084 return NULL;
3085
39e7d718
JM
3086#ifdef CONFIG_DRIVER_NDIS
3087 {
3088 void driver_ndis_init_ops(void);
3089 driver_ndis_init_ops();
3090 }
3091#endif /* CONFIG_DRIVER_NDIS */
3092
50b16da1 3093#ifndef CONFIG_NO_WPA_MSG
4f1495ae 3094 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
50b16da1 3095#endif /* CONFIG_NO_WPA_MSG */
4f1495ae 3096
6fc6879b 3097 wpa_debug_open_file(params->wpa_debug_file_path);
daa70d49
SL
3098 if (params->wpa_debug_syslog)
3099 wpa_debug_open_syslog();
4f68895e
JB
3100 if (params->wpa_debug_tracing) {
3101 ret = wpa_debug_open_linux_tracing();
3102 if (ret) {
3103 wpa_printf(MSG_ERROR,
3104 "Failed to enable trace logging");
3105 return NULL;
3106 }
3107 }
6fc6879b 3108
12760815 3109 ret = eap_register_methods();
6fc6879b
JM
3110 if (ret) {
3111 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
3112 if (ret == -2)
3113 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
3114 "the same EAP type.");
3115 return NULL;
3116 }
3117
3118 global = os_zalloc(sizeof(*global));
3119 if (global == NULL)
3120 return NULL;
b22128ef
JM
3121 dl_list_init(&global->p2p_srv_bonjour);
3122 dl_list_init(&global->p2p_srv_upnp);
6fc6879b
JM
3123 global->params.daemonize = params->daemonize;
3124 global->params.wait_for_monitor = params->wait_for_monitor;
3125 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
3126 if (params->pid_file)
3127 global->params.pid_file = os_strdup(params->pid_file);
3128 if (params->ctrl_interface)
3129 global->params.ctrl_interface =
3130 os_strdup(params->ctrl_interface);
d27df100
JM
3131 if (params->override_driver)
3132 global->params.override_driver =
3133 os_strdup(params->override_driver);
3134 if (params->override_ctrl_interface)
3135 global->params.override_ctrl_interface =
3136 os_strdup(params->override_ctrl_interface);
6fc6879b
JM
3137 wpa_debug_level = global->params.wpa_debug_level =
3138 params->wpa_debug_level;
3139 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
3140 params->wpa_debug_show_keys;
3141 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
3142 params->wpa_debug_timestamp;
3143
f19858f5
JM
3144 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
3145
0456ea16 3146 if (eloop_init()) {
6fc6879b
JM
3147 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
3148 wpa_supplicant_deinit(global);
3149 return NULL;
3150 }
3151
38e24575 3152 random_init(params->entropy_file);
d47fa330 3153
6fc6879b
JM
3154 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
3155 if (global->ctrl_iface == NULL) {
3156 wpa_supplicant_deinit(global);
3157 return NULL;
3158 }
3159
dc461de4
WS
3160 if (wpas_notify_supplicant_initialized(global)) {
3161 wpa_supplicant_deinit(global);
3162 return NULL;
6fc6879b
JM
3163 }
3164
c5121837 3165 for (i = 0; wpa_drivers[i]; i++)
ac305589
JM
3166 global->drv_count++;
3167 if (global->drv_count == 0) {
3168 wpa_printf(MSG_ERROR, "No drivers enabled");
3169 wpa_supplicant_deinit(global);
3170 return NULL;
3171 }
3172 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
3173 if (global->drv_priv == NULL) {
3174 wpa_supplicant_deinit(global);
3175 return NULL;
3176 }
ac305589 3177
6fc6879b
JM
3178 return global;
3179}
3180
3181
3182/**
3183 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
3184 * @global: Pointer to global data from wpa_supplicant_init()
3185 * Returns: 0 after successful event loop run, -1 on failure
3186 *
3187 * This function starts the main event loop and continues running as long as
3188 * there are any remaining events. In most cases, this function is running as
3189 * long as the %wpa_supplicant process in still in use.
3190 */
3191int wpa_supplicant_run(struct wpa_global *global)
3192{
3193 struct wpa_supplicant *wpa_s;
3194
3195 if (global->params.daemonize &&
3196 wpa_supplicant_daemon(global->params.pid_file))
3197 return -1;
3198
3199 if (global->params.wait_for_monitor) {
3200 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
3201 if (wpa_s->ctrl_iface)
3202 wpa_supplicant_ctrl_iface_wait(
3203 wpa_s->ctrl_iface);
3204 }
3205
0456ea16
JM
3206 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
3207 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
6fc6879b
JM
3208
3209 eloop_run();
3210
3211 return 0;
3212}
3213
3214
3215/**
3216 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
3217 * @global: Pointer to global data from wpa_supplicant_init()
3218 *
3219 * This function is called to deinitialize %wpa_supplicant and to free all
3220 * allocated resources. Remaining network interfaces will also be removed.
3221 */
3222void wpa_supplicant_deinit(struct wpa_global *global)
3223{
ac305589
JM
3224 int i;
3225
6fc6879b
JM
3226 if (global == NULL)
3227 return;
3228
b22128ef
JM
3229#ifdef CONFIG_P2P
3230 wpas_p2p_deinit_global(global);
3231#endif /* CONFIG_P2P */
3232
6fc6879b 3233 while (global->ifaces)
df509539 3234 wpa_supplicant_remove_iface(global, global->ifaces, 1);
6fc6879b
JM
3235
3236 if (global->ctrl_iface)
3237 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
dc461de4
WS
3238
3239 wpas_notify_supplicant_deinitialized(global);
6fc6879b
JM
3240
3241 eap_peer_unregister_methods();
3ec97afe
JM
3242#ifdef CONFIG_AP
3243 eap_server_unregister_methods();
3244#endif /* CONFIG_AP */
6fc6879b 3245
c5121837 3246 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
ac305589
JM
3247 if (!global->drv_priv[i])
3248 continue;
c5121837 3249 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
ac305589
JM
3250 }
3251 os_free(global->drv_priv);
3252
d47fa330
JM
3253 random_deinit();
3254
6fc6879b
JM
3255 eloop_destroy();
3256
3257 if (global->params.pid_file) {
3258 os_daemonize_terminate(global->params.pid_file);
3259 os_free(global->params.pid_file);
3260 }
3261 os_free(global->params.ctrl_interface);
d27df100
JM
3262 os_free(global->params.override_driver);
3263 os_free(global->params.override_ctrl_interface);
6fc6879b 3264
6f3bc72b
JM
3265 os_free(global->p2p_disallow_freq);
3266
6fc6879b 3267 os_free(global);
daa70d49 3268 wpa_debug_close_syslog();
6fc6879b 3269 wpa_debug_close_file();
4f68895e 3270 wpa_debug_close_linux_tracing();
6fc6879b 3271}
611aea7d
JM
3272
3273
3274void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
3275{
849b5dc7
JM
3276 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3277 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3278 char country[3];
3279 country[0] = wpa_s->conf->country[0];
3280 country[1] = wpa_s->conf->country[1];
3281 country[2] = '\0';
3282 if (wpa_drv_set_country(wpa_s, country) < 0) {
3283 wpa_printf(MSG_ERROR, "Failed to set country code "
3284 "'%s'", country);
3285 }
3286 }
3287
306ae225
JM
3288 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
3289 wpas_init_ext_pw(wpa_s);
3290
611aea7d
JM
3291#ifdef CONFIG_WPS
3292 wpas_wps_update_config(wpa_s);
3293#endif /* CONFIG_WPS */
3294
b22128ef
JM
3295#ifdef CONFIG_P2P
3296 wpas_p2p_update_config(wpa_s);
3297#endif /* CONFIG_P2P */
3298
611aea7d
JM
3299 wpa_s->conf->changed_parameters = 0;
3300}
2f9c6aa6
JM
3301
3302
0fb337c1
JM
3303static void add_freq(int *freqs, int *num_freqs, int freq)
3304{
3305 int i;
3306
3307 for (i = 0; i < *num_freqs; i++) {
3308 if (freqs[i] == freq)
3309 return;
3310 }
3311
3312 freqs[*num_freqs] = freq;
3313 (*num_freqs)++;
3314}
3315
3316
3317static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
3318{
3319 struct wpa_bss *bss, *cbss;
3320 const int max_freqs = 10;
3321 int *freqs;
3322 int num_freqs = 0;
3323
3324 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
3325 if (freqs == NULL)
3326 return NULL;
3327
3328 cbss = wpa_s->current_bss;
3329
3330 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
3331 if (bss == cbss)
3332 continue;
3333 if (bss->ssid_len == cbss->ssid_len &&
3334 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
3335 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
3336 add_freq(freqs, &num_freqs, bss->freq);
3337 if (num_freqs == max_freqs)
3338 break;
3339 }
3340 }
3341
3342 if (num_freqs == 0) {
3343 os_free(freqs);
3344 freqs = NULL;
3345 }
3346
3347 return freqs;
3348}
3349
3350
3351void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
3352{
3353 int timeout;
3354 int count;
3355 int *freqs = NULL;
3356
5fd9fb27
JM
3357 /*
3358 * Remove possible authentication timeout since the connection failed.
3359 */
3360 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
3361
0fb337c1
JM
3362 /*
3363 * Add the failed BSSID into the blacklist and speed up next scan
3364 * attempt if there could be other APs that could accept association.
3365 * The current blacklist count indicates how many times we have tried
3366 * connecting to this AP and multiple attempts mean that other APs are
3367 * either not available or has already been tried, so that we can start
3368 * increasing the delay here to avoid constant scanning.
3369 */
3370 count = wpa_blacklist_add(wpa_s, bssid);
3371 if (count == 1 && wpa_s->current_bss) {
3372 /*
3373 * This BSS was not in the blacklist before. If there is
3374 * another BSS available for the same ESS, we should try that
3375 * next. Otherwise, we may as well try this one once more
3376 * before allowing other, likely worse, ESSes to be considered.
3377 */
3378 freqs = get_bss_freqs_in_ess(wpa_s);
3379 if (freqs) {
f049052b
BG
3380 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
3381 "has been seen; try it next");
0fb337c1
JM
3382 wpa_blacklist_add(wpa_s, bssid);
3383 /*
3384 * On the next scan, go through only the known channels
3385 * used in this ESS based on previous scans to speed up
3386 * common load balancing use case.
3387 */
3388 os_free(wpa_s->next_scan_freqs);
3389 wpa_s->next_scan_freqs = freqs;
3390 }
3391 }
3392
3393 switch (count) {
3394 case 1:
3395 timeout = 100;
3396 break;
3397 case 2:
3398 timeout = 500;
3399 break;
3400 case 3:
3401 timeout = 1000;
3402 break;
3403 default:
3404 timeout = 5000;
3405 }
3406
3407 /*
3408 * TODO: if more than one possible AP is available in scan results,
3409 * could try the other ones before requesting a new scan.
3410 */
3411 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
3412 1000 * (timeout % 1000));
99fcd404
JM
3413
3414#ifdef CONFIG_P2P
3415 if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
3416 wpa_s->global->p2p != NULL) {
3417 wpa_s->p2p_cb_on_scan_complete = 0;
3418 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
3419 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
3420 "continued after failed association");
3421 }
3422 }
3423#endif /* CONFIG_P2P */
0fb337c1 3424}
22628eca
JM
3425
3426
3427int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
3428{
3429 return wpa_s->conf->ap_scan == 2 ||
3430 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
3431}
d2118814
JM
3432
3433
3434#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
3435int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
3436 struct wpa_ssid *ssid,
3437 const char *field,
3438 const char *value)
3439{
3440#ifdef IEEE8021X_EAPOL
3441 struct eap_peer_config *eap = &ssid->eap;
3442
3443 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
3444 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
3445 (const u8 *) value, os_strlen(value));
3446
3447 switch (wpa_supplicant_ctrl_req_from_string(field)) {
3448 case WPA_CTRL_REQ_EAP_IDENTITY:
3449 os_free(eap->identity);
3450 eap->identity = (u8 *) os_strdup(value);
3451 eap->identity_len = os_strlen(value);
3452 eap->pending_req_identity = 0;
3453 if (ssid == wpa_s->current_ssid)
3454 wpa_s->reassociate = 1;
3455 break;
3456 case WPA_CTRL_REQ_EAP_PASSWORD:
3457 os_free(eap->password);
3458 eap->password = (u8 *) os_strdup(value);
3459 eap->password_len = os_strlen(value);
3460 eap->pending_req_password = 0;
3461 if (ssid == wpa_s->current_ssid)
3462 wpa_s->reassociate = 1;
3463 break;
3464 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
3465 os_free(eap->new_password);
3466 eap->new_password = (u8 *) os_strdup(value);
3467 eap->new_password_len = os_strlen(value);
3468 eap->pending_req_new_password = 0;
3469 if (ssid == wpa_s->current_ssid)
3470 wpa_s->reassociate = 1;
3471 break;
3472 case WPA_CTRL_REQ_EAP_PIN:
3473 os_free(eap->pin);
3474 eap->pin = os_strdup(value);
3475 eap->pending_req_pin = 0;
3476 if (ssid == wpa_s->current_ssid)
3477 wpa_s->reassociate = 1;
3478 break;
3479 case WPA_CTRL_REQ_EAP_OTP:
3480 os_free(eap->otp);
3481 eap->otp = (u8 *) os_strdup(value);
3482 eap->otp_len = os_strlen(value);
3483 os_free(eap->pending_req_otp);
3484 eap->pending_req_otp = NULL;
3485 eap->pending_req_otp_len = 0;
3486 break;
3487 case WPA_CTRL_REQ_EAP_PASSPHRASE:
3488 os_free(eap->private_key_passwd);
3489 eap->private_key_passwd = (u8 *) os_strdup(value);
3490 eap->pending_req_passphrase = 0;
3491 if (ssid == wpa_s->current_ssid)
3492 wpa_s->reassociate = 1;
3493 break;
3494 default:
3495 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
3496 return -1;
3497 }
3498
3499 return 0;
3500#else /* IEEE8021X_EAPOL */
3501 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
3502 return -1;
3503#endif /* IEEE8021X_EAPOL */
3504}
3505#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
349493bd
JM
3506
3507
3508int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
3509{
3510 int i;
3511 unsigned int drv_enc;
3512
3513 if (ssid == NULL)
3514 return 1;
3515
3516 if (ssid->disabled)
3517 return 1;
3518
3519 if (wpa_s && wpa_s->drv_capa_known)
3520 drv_enc = wpa_s->drv_enc;
3521 else
3522 drv_enc = (unsigned int) -1;
3523
3524 for (i = 0; i < NUM_WEP_KEYS; i++) {
3525 size_t len = ssid->wep_key_len[i];
3526 if (len == 0)
3527 continue;
3528 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
3529 continue;
3530 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
3531 continue;
3532 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
3533 continue;
3534 return 1; /* invalid WEP key */
3535 }
3536
9173b16f
JM
3537 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
3538 !ssid->ext_psk)
2518aad3
JM
3539 return 1;
3540
349493bd
JM
3541 return 0;
3542}
b9cfc09a
JJ
3543
3544
3545int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
3546{
3547 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
3548 return 1;
3549 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
3550 return 0;
3551 return -1;
3552}
00e5e3d5
JM
3553
3554
3555void wpas_auth_failed(struct wpa_supplicant *wpa_s)
3556{
3557 struct wpa_ssid *ssid = wpa_s->current_ssid;
3558 int dur;
3559 struct os_time now;
3560
3561 if (ssid == NULL) {
3562 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
3563 "SSID block");
3564 return;
3565 }
3566
3567 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
3568 return;
3569
3570 ssid->auth_failures++;
3571 if (ssid->auth_failures > 50)
3572 dur = 300;
3573 else if (ssid->auth_failures > 20)
3574 dur = 120;
3575 else if (ssid->auth_failures > 10)
3576 dur = 60;
3577 else if (ssid->auth_failures > 5)
3578 dur = 30;
3579 else if (ssid->auth_failures > 1)
3580 dur = 20;
3581 else
3582 dur = 10;
3583
3584 os_get_time(&now);
3585 if (now.sec + dur <= ssid->disabled_until.sec)
3586 return;
3587
3588 ssid->disabled_until.sec = now.sec + dur;
3589
3590 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
3591 "id=%d ssid=\"%s\" auth_failures=%u duration=%d",
3592 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
3593 ssid->auth_failures, dur);
3594}
3595
3596
3597void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
3598 struct wpa_ssid *ssid, int clear_failures)
3599{
3600 if (ssid == NULL)
3601 return;
3602
3603 if (ssid->disabled_until.sec) {
3604 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
3605 "id=%d ssid=\"%s\"",
3606 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3607 }
3608 ssid->disabled_until.sec = 0;
3609 ssid->disabled_until.usec = 0;
3610 if (clear_failures)
3611 ssid->auth_failures = 0;
3612}