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