blob: ffba0f5e7803f6237f1635160d2d8dfe1d10de4d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 *
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"
16#include "crypto/random.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080017#include "crypto/sha1.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "eapol_supp/eapol_supp_sm.h"
19#include "eap_peer/eap.h"
Dmitry Shmidt34af3062013-07-11 10:46:32 -070020#include "eap_peer/eap_proxy.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "eap_server/eap_methods.h"
22#include "rsn_supp/wpa.h"
23#include "eloop.h"
24#include "config.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070025#include "utils/ext_password.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "l2_packet/l2_packet.h"
27#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ctrl_iface.h"
30#include "pcsc_funcs.h"
31#include "common/version.h"
32#include "rsn_supp/preauth.h"
33#include "rsn_supp/pmksa_cache.h"
34#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "common/ieee802_11_defs.h"
36#include "p2p/p2p.h"
37#include "blacklist.h"
38#include "wpas_glue.h"
39#include "wps_supplicant.h"
40#include "ibss_rsn.h"
41#include "sme.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042#include "gas_query.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043#include "ap.h"
44#include "p2p_supplicant.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046#include "notify.h"
47#include "bgscan.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#include "autoscan.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049#include "bss.h"
50#include "scan.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051#include "offchannel.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070052#include "hs20_supplicant.h"
Dmitry Shmidt44c95782013-05-17 09:51:35 -070053#include "wnm_sta.h"
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -070054#include "wpas_kay.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055
56const char *wpa_supplicant_version =
57"wpa_supplicant v" VERSION_STR "\n"
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080058"Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> and contributors";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059
60const char *wpa_supplicant_license =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080061"This software may be distributed under the terms of the BSD license.\n"
62"See README for more details.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063#ifdef EAP_TLS_OPENSSL
64"\nThis product includes software developed by the OpenSSL Project\n"
65"for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
66#endif /* EAP_TLS_OPENSSL */
67;
68
69#ifndef CONFIG_NO_STDOUT_DEBUG
70/* Long text divided into parts in order to fit in C89 strings size limits. */
71const char *wpa_supplicant_full_license1 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080072"";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073const char *wpa_supplicant_full_license2 =
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080074"This software may be distributed under the terms of the BSD license.\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075"\n"
76"Redistribution and use in source and binary forms, with or without\n"
77"modification, are permitted provided that the following conditions are\n"
78"met:\n"
79"\n";
80const char *wpa_supplicant_full_license3 =
81"1. Redistributions of source code must retain the above copyright\n"
82" notice, this list of conditions and the following disclaimer.\n"
83"\n"
84"2. Redistributions in binary form must reproduce the above copyright\n"
85" notice, this list of conditions and the following disclaimer in the\n"
86" documentation and/or other materials provided with the distribution.\n"
87"\n";
88const char *wpa_supplicant_full_license4 =
89"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
90" names of its contributors may be used to endorse or promote products\n"
91" derived from this software without specific prior written permission.\n"
92"\n"
93"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
94"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
95"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
96"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
97const char *wpa_supplicant_full_license5 =
98"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
99"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
100"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
101"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
102"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
103"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
104"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
105"\n";
106#endif /* CONFIG_NO_STDOUT_DEBUG */
107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108/* Configure default/group WEP keys for static WEP */
109int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
110{
111 int i, set = 0;
112
113 for (i = 0; i < NUM_WEP_KEYS; i++) {
114 if (ssid->wep_key_len[i] == 0)
115 continue;
116
117 set = 1;
118 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
119 i, i == ssid->wep_tx_keyidx, NULL, 0,
120 ssid->wep_key[i], ssid->wep_key_len[i]);
121 }
122
123 return set;
124}
125
126
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700127int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
128 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129{
130 u8 key[32];
131 size_t keylen;
132 enum wpa_alg alg;
133 u8 seq[6] = { 0 };
134
135 /* IBSS/WPA-None uses only one key (Group) for both receiving and
136 * sending unicast and multicast packets. */
137
138 if (ssid->mode != WPAS_MODE_IBSS) {
139 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
140 "IBSS/ad-hoc) for WPA-None", ssid->mode);
141 return -1;
142 }
143
144 if (!ssid->psk_set) {
145 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
146 "WPA-None");
147 return -1;
148 }
149
150 switch (wpa_s->group_cipher) {
151 case WPA_CIPHER_CCMP:
152 os_memcpy(key, ssid->psk, 16);
153 keylen = 16;
154 alg = WPA_ALG_CCMP;
155 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700156 case WPA_CIPHER_GCMP:
157 os_memcpy(key, ssid->psk, 16);
158 keylen = 16;
159 alg = WPA_ALG_GCMP;
160 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 case WPA_CIPHER_TKIP:
162 /* WPA-None uses the same Michael MIC key for both TX and RX */
163 os_memcpy(key, ssid->psk, 16 + 8);
164 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
165 keylen = 32;
166 alg = WPA_ALG_TKIP;
167 break;
168 default:
169 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
170 "WPA-None", wpa_s->group_cipher);
171 return -1;
172 }
173
174 /* TODO: should actually remember the previously used seq#, both for TX
175 * and RX from each STA.. */
176
177 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
178}
179
180
181static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
182{
183 struct wpa_supplicant *wpa_s = eloop_ctx;
184 const u8 *bssid = wpa_s->bssid;
185 if (is_zero_ether_addr(bssid))
186 bssid = wpa_s->pending_bssid;
187 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
188 MAC2STR(bssid));
189 wpa_blacklist_add(wpa_s, bssid);
190 wpa_sm_notify_disassoc(wpa_s->wpa);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800191 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 wpa_s->reassociate = 1;
193
194 /*
195 * If we timed out, the AP or the local radio may be busy.
196 * So, wait a second until scanning again.
197 */
198 wpa_supplicant_req_scan(wpa_s, 1, 0);
199}
200
201
202/**
203 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
204 * @wpa_s: Pointer to wpa_supplicant data
205 * @sec: Number of seconds after which to time out authentication
206 * @usec: Number of microseconds after which to time out authentication
207 *
208 * This function is used to schedule a timeout for the current authentication
209 * attempt.
210 */
211void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
212 int sec, int usec)
213{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700214 if (wpa_s->conf->ap_scan == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
216 return;
217
218 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
219 "%d usec", sec, usec);
220 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
221 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
222}
223
224
225/**
226 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
227 * @wpa_s: Pointer to wpa_supplicant data
228 *
229 * This function is used to cancel authentication timeout scheduled with
230 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
231 * been completed.
232 */
233void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
234{
235 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
236 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
237 wpa_blacklist_del(wpa_s, wpa_s->bssid);
238}
239
240
241/**
242 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
243 * @wpa_s: Pointer to wpa_supplicant data
244 *
245 * This function is used to configure EAPOL state machine based on the selected
246 * authentication mode.
247 */
248void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
249{
250#ifdef IEEE8021X_EAPOL
251 struct eapol_config eapol_conf;
252 struct wpa_ssid *ssid = wpa_s->current_ssid;
253
254#ifdef CONFIG_IBSS_RSN
255 if (ssid->mode == WPAS_MODE_IBSS &&
256 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
257 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
258 /*
259 * RSN IBSS authentication is per-STA and we can disable the
260 * per-BSSID EAPOL authentication.
261 */
262 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
263 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
264 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
265 return;
266 }
267#endif /* CONFIG_IBSS_RSN */
268
269 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
270 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
271
272 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
273 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
274 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
275 else
276 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
277
278 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
279 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
280 eapol_conf.accept_802_1x_keys = 1;
281 eapol_conf.required_keys = 0;
282 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
283 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
284 }
285 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
286 eapol_conf.required_keys |=
287 EAPOL_REQUIRE_KEY_BROADCAST;
288 }
289
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700290 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 eapol_conf.required_keys = 0;
292 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700293 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 eapol_conf.workaround = ssid->eap_workaround;
295 eapol_conf.eap_disabled =
296 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
297 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
298 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
Dmitry Shmidt051af732013-10-22 13:52:46 -0700299 eapol_conf.external_sim = wpa_s->conf->external_sim;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
301#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700302
303 ieee802_1x_alloc_kay_sm(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304}
305
306
307/**
308 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
309 * @wpa_s: Pointer to wpa_supplicant data
310 * @ssid: Configuration data for the network
311 *
312 * This function is used to configure WPA state machine and related parameters
313 * to a mode where WPA is not enabled. This is called as part of the
314 * authentication configuration when the selected network does not use WPA.
315 */
316void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
317 struct wpa_ssid *ssid)
318{
319 int i;
320
321 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
322 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
323 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
324 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
325 else
326 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
327 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
328 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
329 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
330 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
331 wpa_s->group_cipher = WPA_CIPHER_NONE;
332 wpa_s->mgmt_group_cipher = 0;
333
334 for (i = 0; i < NUM_WEP_KEYS; i++) {
335 if (ssid->wep_key_len[i] > 5) {
336 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
337 wpa_s->group_cipher = WPA_CIPHER_WEP104;
338 break;
339 } else if (ssid->wep_key_len[i] > 0) {
340 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
341 wpa_s->group_cipher = WPA_CIPHER_WEP40;
342 break;
343 }
344 }
345
346 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
347 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
348 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
349 wpa_s->pairwise_cipher);
350 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
351#ifdef CONFIG_IEEE80211W
352 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
353 wpa_s->mgmt_group_cipher);
354#endif /* CONFIG_IEEE80211W */
355
356 pmksa_cache_clear_current(wpa_s->wpa);
357}
358
359
Dmitry Shmidt04949592012-07-19 12:16:46 -0700360void free_hw_features(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361{
362 int i;
363 if (wpa_s->hw.modes == NULL)
364 return;
365
366 for (i = 0; i < wpa_s->hw.num_modes; i++) {
367 os_free(wpa_s->hw.modes[i].channels);
368 os_free(wpa_s->hw.modes[i].rates);
369 }
370
371 os_free(wpa_s->hw.modes);
372 wpa_s->hw.modes = NULL;
373}
374
375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
377{
378 bgscan_deinit(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700379 autoscan_deinit(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 scard_deinit(wpa_s->scard);
381 wpa_s->scard = NULL;
382 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
383 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
384 l2_packet_deinit(wpa_s->l2);
385 wpa_s->l2 = NULL;
386 if (wpa_s->l2_br) {
387 l2_packet_deinit(wpa_s->l2_br);
388 wpa_s->l2_br = NULL;
389 }
390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 if (wpa_s->conf != NULL) {
392 struct wpa_ssid *ssid;
393 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
394 wpas_notify_network_removed(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396
397 os_free(wpa_s->confname);
398 wpa_s->confname = NULL;
399
Jouni Malinen5d1c8ad2013-04-23 12:34:56 -0700400 os_free(wpa_s->confanother);
401 wpa_s->confanother = NULL;
402
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -0800403#ifdef CONFIG_P2P
404 os_free(wpa_s->conf_p2p_dev);
405 wpa_s->conf_p2p_dev = NULL;
406#endif /* CONFIG_P2P */
407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 wpa_sm_set_eapol(wpa_s->wpa, NULL);
409 eapol_sm_deinit(wpa_s->eapol);
410 wpa_s->eapol = NULL;
411
412 rsn_preauth_deinit(wpa_s->wpa);
413
414#ifdef CONFIG_TDLS
415 wpa_tdls_deinit(wpa_s->wpa);
416#endif /* CONFIG_TDLS */
417
418 pmksa_candidate_free(wpa_s->wpa);
419 wpa_sm_deinit(wpa_s->wpa);
420 wpa_s->wpa = NULL;
421 wpa_blacklist_clear(wpa_s);
422
423 wpa_bss_deinit(wpa_s);
424
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700425 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 wpa_supplicant_cancel_scan(wpa_s);
427 wpa_supplicant_cancel_auth_timeout(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
429#ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
430 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
431 wpa_s, NULL);
432#endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433
434 wpas_wps_deinit(wpa_s);
435
436 wpabuf_free(wpa_s->pending_eapol_rx);
437 wpa_s->pending_eapol_rx = NULL;
438
439#ifdef CONFIG_IBSS_RSN
440 ibss_rsn_deinit(wpa_s->ibss_rsn);
441 wpa_s->ibss_rsn = NULL;
442#endif /* CONFIG_IBSS_RSN */
443
444 sme_deinit(wpa_s);
445
446#ifdef CONFIG_AP
447 wpa_supplicant_ap_deinit(wpa_s);
448#endif /* CONFIG_AP */
449
450#ifdef CONFIG_P2P
451 wpas_p2p_deinit(wpa_s);
452#endif /* CONFIG_P2P */
453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454#ifdef CONFIG_OFFCHANNEL
455 offchannel_deinit(wpa_s);
456#endif /* CONFIG_OFFCHANNEL */
457
458 wpa_supplicant_cancel_sched_scan(wpa_s);
459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 os_free(wpa_s->next_scan_freqs);
461 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800462
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800463 os_free(wpa_s->manual_scan_freqs);
464 wpa_s->manual_scan_freqs = NULL;
465
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700466 os_free(wpa_s->manual_sched_scan_freqs);
467 wpa_s->manual_sched_scan_freqs = NULL;
468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800469 gas_query_deinit(wpa_s->gas);
470 wpa_s->gas = NULL;
471
472 free_hw_features(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700473
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -0700474 ieee802_1x_dealloc_kay_sm(wpa_s);
475
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 os_free(wpa_s->bssid_filter);
477 wpa_s->bssid_filter = NULL;
478
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800479 os_free(wpa_s->disallow_aps_bssid);
480 wpa_s->disallow_aps_bssid = NULL;
481 os_free(wpa_s->disallow_aps_ssid);
482 wpa_s->disallow_aps_ssid = NULL;
483
Dmitry Shmidt04949592012-07-19 12:16:46 -0700484 wnm_bss_keep_alive_deinit(wpa_s);
Dmitry Shmidt44c95782013-05-17 09:51:35 -0700485#ifdef CONFIG_WNM
486 wnm_deallocate_memory(wpa_s);
487#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700488
489 ext_password_deinit(wpa_s->ext_pw);
490 wpa_s->ext_pw = NULL;
491
492 wpabuf_free(wpa_s->last_gas_resp);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800493 wpa_s->last_gas_resp = NULL;
494 wpabuf_free(wpa_s->prev_gas_resp);
495 wpa_s->prev_gas_resp = NULL;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700496
497 os_free(wpa_s->last_scan_res);
498 wpa_s->last_scan_res = NULL;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800499
500#ifdef CONFIG_HS20
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700501 hs20_deinit(wpa_s);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800502#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503}
504
505
506/**
507 * wpa_clear_keys - Clear keys configured for the driver
508 * @wpa_s: Pointer to wpa_supplicant data
509 * @addr: Previously used BSSID or %NULL if not available
510 *
511 * This function clears the encryption keys that has been previously configured
512 * for the driver.
513 */
514void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
515{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800516 int i, max;
517
518#ifdef CONFIG_IEEE80211W
519 max = 6;
520#else /* CONFIG_IEEE80211W */
521 max = 4;
522#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523
524 /* MLME-DELETEKEYS.request */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800525 for (i = 0; i < max; i++) {
526 if (wpa_s->keys_cleared & BIT(i))
527 continue;
528 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
529 NULL, 0);
530 }
531 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
532 !is_zero_ether_addr(addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
534 0);
535 /* MLME-SETPROTECTION.request(None) */
536 wpa_drv_mlme_setprotection(
537 wpa_s, addr,
538 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
539 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
540 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800541 wpa_s->keys_cleared = (u32) -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542}
543
544
545/**
546 * wpa_supplicant_state_txt - Get the connection state name as a text string
547 * @state: State (wpa_state; WPA_*)
548 * Returns: The state name as a printable text string
549 */
550const char * wpa_supplicant_state_txt(enum wpa_states state)
551{
552 switch (state) {
553 case WPA_DISCONNECTED:
554 return "DISCONNECTED";
555 case WPA_INACTIVE:
556 return "INACTIVE";
557 case WPA_INTERFACE_DISABLED:
558 return "INTERFACE_DISABLED";
559 case WPA_SCANNING:
560 return "SCANNING";
561 case WPA_AUTHENTICATING:
562 return "AUTHENTICATING";
563 case WPA_ASSOCIATING:
564 return "ASSOCIATING";
565 case WPA_ASSOCIATED:
566 return "ASSOCIATED";
567 case WPA_4WAY_HANDSHAKE:
568 return "4WAY_HANDSHAKE";
569 case WPA_GROUP_HANDSHAKE:
570 return "GROUP_HANDSHAKE";
571 case WPA_COMPLETED:
572 return "COMPLETED";
573 default:
574 return "UNKNOWN";
575 }
576}
577
578
579#ifdef CONFIG_BGSCAN
580
581static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
582{
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800583 const char *name;
584
585 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
586 name = wpa_s->current_ssid->bgscan;
587 else
588 name = wpa_s->conf->bgscan;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800589 if (name == NULL || name[0] == '\0')
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800590 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 if (wpas_driver_bss_selection(wpa_s))
592 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
594 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800595#ifdef CONFIG_P2P
596 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
597 return;
598#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599
600 bgscan_deinit(wpa_s);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800601 if (wpa_s->current_ssid) {
602 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
604 "bgscan");
605 /*
606 * Live without bgscan; it is only used as a roaming
607 * optimization, so the initial connection is not
608 * affected.
609 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700610 } else {
611 struct wpa_scan_results *scan_res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 wpa_s->bgscan_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700613 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
614 0);
615 if (scan_res) {
616 bgscan_notify_scan(wpa_s, scan_res);
617 wpa_scan_results_free(scan_res);
618 }
619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 } else
621 wpa_s->bgscan_ssid = NULL;
622}
623
624
625static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
626{
627 if (wpa_s->bgscan_ssid != NULL) {
628 bgscan_deinit(wpa_s);
629 wpa_s->bgscan_ssid = NULL;
630 }
631}
632
633#endif /* CONFIG_BGSCAN */
634
635
Dmitry Shmidt04949592012-07-19 12:16:46 -0700636static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
637{
638 if (autoscan_init(wpa_s, 0))
639 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
640}
641
642
643static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
644{
645 autoscan_deinit(wpa_s);
646}
647
648
649void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
650{
651 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
652 wpa_s->wpa_state == WPA_SCANNING) {
653 autoscan_deinit(wpa_s);
654 wpa_supplicant_start_autoscan(wpa_s);
655 }
656}
657
658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659/**
660 * wpa_supplicant_set_state - Set current connection state
661 * @wpa_s: Pointer to wpa_supplicant data
662 * @state: The new connection state
663 *
664 * This function is called whenever the connection state changes, e.g.,
665 * association is completed for WPA/WPA2 4-Way Handshake is started.
666 */
667void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
668 enum wpa_states state)
669{
670 enum wpa_states old_state = wpa_s->wpa_state;
671
672 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
673 wpa_supplicant_state_txt(wpa_s->wpa_state),
674 wpa_supplicant_state_txt(state));
675
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -0800676 if (state == WPA_INTERFACE_DISABLED) {
677 /* Assure normal scan when interface is restored */
678 wpa_s->normal_scans = 0;
679 }
680
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700681 if (state == WPA_COMPLETED) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800682 wpas_connect_work_done(wpa_s);
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -0700683 /* Reinitialize normal_scan counter */
684 wpa_s->normal_scans = 0;
685 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 if (state != WPA_SCANNING)
688 wpa_supplicant_notify_scanning(wpa_s, 0);
689
690 if (state == WPA_COMPLETED && wpa_s->new_connection) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700692#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
Dmitry Shmidt8f0dbf42013-11-08 13:35:41 -0800694 MACSTR " completed [id=%d id_str=%s]",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800695 MAC2STR(wpa_s->bssid),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 ssid ? ssid->id : -1,
697 ssid && ssid->id_str ? ssid->id_str : "");
698#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700699 wpas_clear_temp_disabled(wpa_s, ssid, 1);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800700 wpa_s->extra_blacklist_count = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 wpa_s->new_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 wpa_drv_set_operstate(wpa_s, 1);
703#ifndef IEEE8021X_EAPOL
704 wpa_drv_set_supp_port(wpa_s, 1);
705#endif /* IEEE8021X_EAPOL */
706 wpa_s->after_wps = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700707 wpa_s->known_wps_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708#ifdef CONFIG_P2P
709 wpas_p2p_completed(wpa_s);
710#endif /* CONFIG_P2P */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700711
712 sme_sched_obss_scan(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
714 state == WPA_ASSOCIATED) {
715 wpa_s->new_connection = 1;
716 wpa_drv_set_operstate(wpa_s, 0);
717#ifndef IEEE8021X_EAPOL
718 wpa_drv_set_supp_port(wpa_s, 0);
719#endif /* IEEE8021X_EAPOL */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700720 sme_sched_obss_scan(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 }
722 wpa_s->wpa_state = state;
723
724#ifdef CONFIG_BGSCAN
725 if (state == WPA_COMPLETED)
726 wpa_supplicant_start_bgscan(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800727 else if (state < WPA_ASSOCIATED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 wpa_supplicant_stop_bgscan(wpa_s);
729#endif /* CONFIG_BGSCAN */
730
Dmitry Shmidt04949592012-07-19 12:16:46 -0700731 if (state == WPA_AUTHENTICATING)
732 wpa_supplicant_stop_autoscan(wpa_s);
733
734 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
735 wpa_supplicant_start_autoscan(wpa_s);
736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 if (wpa_s->wpa_state != old_state) {
738 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
739
740 if (wpa_s->wpa_state == WPA_COMPLETED ||
741 old_state == WPA_COMPLETED)
742 wpas_notify_auth_changed(wpa_s);
743 }
744}
745
746
747void wpa_supplicant_terminate_proc(struct wpa_global *global)
748{
749 int pending = 0;
750#ifdef CONFIG_WPS
751 struct wpa_supplicant *wpa_s = global->ifaces;
752 while (wpa_s) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800753 struct wpa_supplicant *next = wpa_s->next;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700754 if (wpas_wps_terminate_pending(wpa_s) == 1)
755 pending = 1;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700756#ifdef CONFIG_P2P
757 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
758 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
759 wpas_p2p_disconnect(wpa_s);
760#endif /* CONFIG_P2P */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800761 wpa_s = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 }
763#endif /* CONFIG_WPS */
764 if (pending)
765 return;
766 eloop_terminate();
767}
768
769
770static void wpa_supplicant_terminate(int sig, void *signal_ctx)
771{
772 struct wpa_global *global = signal_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773 wpa_supplicant_terminate_proc(global);
774}
775
776
777void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
778{
779 enum wpa_states old_state = wpa_s->wpa_state;
780
781 wpa_s->pairwise_cipher = 0;
782 wpa_s->group_cipher = 0;
783 wpa_s->mgmt_group_cipher = 0;
784 wpa_s->key_mgmt = 0;
785 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787
788 if (wpa_s->wpa_state != old_state)
789 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
790}
791
792
793/**
794 * wpa_supplicant_reload_configuration - Reload configuration data
795 * @wpa_s: Pointer to wpa_supplicant data
796 * Returns: 0 on success or -1 if configuration parsing failed
797 *
798 * This function can be used to request that the configuration data is reloaded
799 * (e.g., after configuration file change). This function is reloading
800 * configuration only for one interface, so this may need to be called multiple
801 * times if %wpa_supplicant is controlling multiple interfaces and all
802 * interfaces need reconfiguration.
803 */
804int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
805{
806 struct wpa_config *conf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 int reconf_ctrl;
808 int old_ap_scan;
809
810 if (wpa_s->confname == NULL)
811 return -1;
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700812 conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813 if (conf == NULL) {
814 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
815 "file '%s' - exiting", wpa_s->confname);
816 return -1;
817 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -0700818 wpa_config_read(wpa_s->confanother, conf);
819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 conf->changed_parameters = (unsigned int) -1;
821
822 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
823 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
824 os_strcmp(conf->ctrl_interface,
825 wpa_s->conf->ctrl_interface) != 0);
826
827 if (reconf_ctrl && wpa_s->ctrl_iface) {
828 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
829 wpa_s->ctrl_iface = NULL;
830 }
831
832 eapol_sm_invalidate_cached_session(wpa_s->eapol);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800833 if (wpa_s->current_ssid) {
834 wpa_supplicant_deauthenticate(wpa_s,
835 WLAN_REASON_DEAUTH_LEAVING);
836 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837
838 /*
839 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
840 * pkcs11_engine_path, pkcs11_module_path.
841 */
842 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
843 /*
844 * Clear forced success to clear EAP state for next
845 * authentication.
846 */
847 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
848 }
849 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
850 wpa_sm_set_config(wpa_s->wpa, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800851 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
853 rsn_preauth_deinit(wpa_s->wpa);
854
855 old_ap_scan = wpa_s->conf->ap_scan;
856 wpa_config_free(wpa_s->conf);
857 wpa_s->conf = conf;
858 if (old_ap_scan != wpa_s->conf->ap_scan)
859 wpas_notify_ap_scan_changed(wpa_s);
860
861 if (reconf_ctrl)
862 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
863
864 wpa_supplicant_update_config(wpa_s);
865
866 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 wpa_s->reassociate = 1;
869 wpa_supplicant_req_scan(wpa_s, 0, 0);
870 }
871 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
872 return 0;
873}
874
875
876static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
877{
878 struct wpa_global *global = signal_ctx;
879 struct wpa_supplicant *wpa_s;
880 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
881 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
882 sig);
883 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
884 wpa_supplicant_terminate_proc(global);
885 }
886 }
887}
888
889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
891 struct wpa_ssid *ssid,
892 struct wpa_ie_data *ie)
893{
894 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
895 if (ret) {
896 if (ret == -2) {
897 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
898 "from association info");
899 }
900 return -1;
901 }
902
903 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
904 "cipher suites");
905 if (!(ie->group_cipher & ssid->group_cipher)) {
906 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
907 "cipher 0x%x (mask 0x%x) - reject",
908 ie->group_cipher, ssid->group_cipher);
909 return -1;
910 }
911 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
912 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
913 "cipher 0x%x (mask 0x%x) - reject",
914 ie->pairwise_cipher, ssid->pairwise_cipher);
915 return -1;
916 }
917 if (!(ie->key_mgmt & ssid->key_mgmt)) {
918 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
919 "management 0x%x (mask 0x%x) - reject",
920 ie->key_mgmt, ssid->key_mgmt);
921 return -1;
922 }
923
924#ifdef CONFIG_IEEE80211W
925 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800926 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
927 wpa_s->conf->pmf : ssid->ieee80211w) ==
928 MGMT_FRAME_PROTECTION_REQUIRED) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
930 "that does not support management frame protection - "
931 "reject");
932 return -1;
933 }
934#endif /* CONFIG_IEEE80211W */
935
936 return 0;
937}
938
939
940/**
941 * wpa_supplicant_set_suites - Set authentication and encryption parameters
942 * @wpa_s: Pointer to wpa_supplicant data
943 * @bss: Scan results for the selected BSS, or %NULL if not available
944 * @ssid: Configuration data for the selected network
945 * @wpa_ie: Buffer for the WPA/RSN IE
946 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
947 * used buffer length in case the functions returns success.
948 * Returns: 0 on success or -1 on failure
949 *
950 * This function is used to configure authentication and encryption parameters
951 * based on the network configuration and scan result for the selected BSS (if
952 * available).
953 */
954int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
955 struct wpa_bss *bss, struct wpa_ssid *ssid,
956 u8 *wpa_ie, size_t *wpa_ie_len)
957{
958 struct wpa_ie_data ie;
959 int sel, proto;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800960 const u8 *bss_wpa, *bss_rsn, *bss_osen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961
962 if (bss) {
963 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
964 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800965 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 } else
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800967 bss_wpa = bss_rsn = bss_osen = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968
969 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
970 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
971 (ie.group_cipher & ssid->group_cipher) &&
972 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
973 (ie.key_mgmt & ssid->key_mgmt)) {
974 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
975 proto = WPA_PROTO_RSN;
976 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
977 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
978 (ie.group_cipher & ssid->group_cipher) &&
979 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
980 (ie.key_mgmt & ssid->key_mgmt)) {
981 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
982 proto = WPA_PROTO_WPA;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800983#ifdef CONFIG_HS20
984 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
985 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
986 /* TODO: parse OSEN element */
987 ie.group_cipher = WPA_CIPHER_CCMP;
988 ie.pairwise_cipher = WPA_CIPHER_CCMP;
989 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
990 proto = WPA_PROTO_OSEN;
991#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 } else if (bss) {
993 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
994 return -1;
995 } else {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800996 if (ssid->proto & WPA_PROTO_OSEN)
997 proto = WPA_PROTO_OSEN;
998 else if (ssid->proto & WPA_PROTO_RSN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999 proto = WPA_PROTO_RSN;
1000 else
1001 proto = WPA_PROTO_WPA;
1002 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1003 os_memset(&ie, 0, sizeof(ie));
1004 ie.group_cipher = ssid->group_cipher;
1005 ie.pairwise_cipher = ssid->pairwise_cipher;
1006 ie.key_mgmt = ssid->key_mgmt;
1007#ifdef CONFIG_IEEE80211W
1008 ie.mgmt_group_cipher =
1009 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
1010 WPA_CIPHER_AES_128_CMAC : 0;
1011#endif /* CONFIG_IEEE80211W */
1012 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1013 "based on configuration");
1014 } else
1015 proto = ie.proto;
1016 }
1017
1018 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1019 "pairwise %d key_mgmt %d proto %d",
1020 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1021#ifdef CONFIG_IEEE80211W
1022 if (ssid->ieee80211w) {
1023 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1024 ie.mgmt_group_cipher);
1025 }
1026#endif /* CONFIG_IEEE80211W */
1027
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 wpa_s->wpa_proto = proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1030 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001031 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032
1033 if (bss || !wpa_s->ap_ies_from_associnfo) {
1034 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1035 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1036 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1037 bss_rsn ? 2 + bss_rsn[1] : 0))
1038 return -1;
1039 }
1040
1041 sel = ie.group_cipher & ssid->group_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001042 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1043 if (wpa_s->group_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1045 "cipher");
1046 return -1;
1047 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001048 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1049 wpa_cipher_txt(wpa_s->group_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050
1051 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001052 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1053 if (wpa_s->pairwise_cipher < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1055 "cipher");
1056 return -1;
1057 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001058 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1059 wpa_cipher_txt(wpa_s->pairwise_cipher));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060
1061 sel = ie.key_mgmt & ssid->key_mgmt;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001062#ifdef CONFIG_SAE
1063 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1064 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1065#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 if (0) {
1067#ifdef CONFIG_IEEE80211R
1068 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1069 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1070 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1071 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1072 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1073 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1074#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001075#ifdef CONFIG_SAE
1076 } else if (sel & WPA_KEY_MGMT_SAE) {
1077 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1078 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1079 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1080 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1081 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1082#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083#ifdef CONFIG_IEEE80211W
1084 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1085 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1086 wpa_dbg(wpa_s, MSG_DEBUG,
1087 "WPA: using KEY_MGMT 802.1X with SHA256");
1088 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1089 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1090 wpa_dbg(wpa_s, MSG_DEBUG,
1091 "WPA: using KEY_MGMT PSK with SHA256");
1092#endif /* CONFIG_IEEE80211W */
1093 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1094 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1095 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1096 } else if (sel & WPA_KEY_MGMT_PSK) {
1097 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1098 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1099 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1100 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1101 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001102#ifdef CONFIG_HS20
1103 } else if (sel & WPA_KEY_MGMT_OSEN) {
1104 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1105 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1106#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 } else {
1108 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1109 "authenticated key management type");
1110 return -1;
1111 }
1112
1113 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1114 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1115 wpa_s->pairwise_cipher);
1116 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1117
1118#ifdef CONFIG_IEEE80211W
1119 sel = ie.mgmt_group_cipher;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001120 if ((ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1121 wpa_s->conf->pmf : ssid->ieee80211w) == NO_MGMT_FRAME_PROTECTION ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1123 sel = 0;
1124 if (sel & WPA_CIPHER_AES_128_CMAC) {
1125 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1126 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1127 "AES-128-CMAC");
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001128 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1129 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1130 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1131 "BIP-GMAC-128");
1132 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1133 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1134 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1135 "BIP-GMAC-256");
1136 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1137 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1138 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1139 "BIP-CMAC-256");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 } else {
1141 wpa_s->mgmt_group_cipher = 0;
1142 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1143 }
1144 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1145 wpa_s->mgmt_group_cipher);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001146 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1147 (ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1148 wpa_s->conf->pmf : ssid->ieee80211w));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149#endif /* CONFIG_IEEE80211W */
1150
1151 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1152 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1153 return -1;
1154 }
1155
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001156 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001158#ifndef CONFIG_NO_PBKDF2
1159 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1160 ssid->passphrase) {
1161 u8 psk[PMK_LEN];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001162 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1163 4096, psk, PMK_LEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001164 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1165 psk, PMK_LEN);
1166 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1167 }
1168#endif /* CONFIG_NO_PBKDF2 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001169#ifdef CONFIG_EXT_PASSWORD
1170 if (ssid->ext_psk) {
1171 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1172 ssid->ext_psk);
1173 char pw_str[64 + 1];
1174 u8 psk[PMK_LEN];
1175
1176 if (pw == NULL) {
1177 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1178 "found from external storage");
1179 return -1;
1180 }
1181
1182 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1183 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1184 "PSK length %d in external storage",
1185 (int) wpabuf_len(pw));
1186 ext_password_free(pw);
1187 return -1;
1188 }
1189
1190 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1191 pw_str[wpabuf_len(pw)] = '\0';
1192
1193#ifndef CONFIG_NO_PBKDF2
1194 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1195 {
1196 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1197 4096, psk, PMK_LEN);
1198 os_memset(pw_str, 0, sizeof(pw_str));
1199 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1200 "external passphrase)",
1201 psk, PMK_LEN);
1202 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1203 } else
1204#endif /* CONFIG_NO_PBKDF2 */
1205 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1206 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1207 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1208 "Invalid PSK hex string");
1209 os_memset(pw_str, 0, sizeof(pw_str));
1210 ext_password_free(pw);
1211 return -1;
1212 }
1213 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1214 } else {
1215 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1216 "PSK available");
1217 os_memset(pw_str, 0, sizeof(pw_str));
1218 ext_password_free(pw);
1219 return -1;
1220 }
1221
1222 os_memset(pw_str, 0, sizeof(pw_str));
1223 ext_password_free(pw);
1224 }
1225#endif /* CONFIG_EXT_PASSWORD */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001226 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1228
1229 return 0;
1230}
1231
1232
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001233static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1234{
1235 *pos = 0x00;
1236
1237 switch (idx) {
1238 case 0: /* Bits 0-7 */
1239 break;
1240 case 1: /* Bits 8-15 */
1241 break;
1242 case 2: /* Bits 16-23 */
1243#ifdef CONFIG_WNM
1244 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1245 *pos |= 0x08; /* Bit 19 - BSS Transition */
1246#endif /* CONFIG_WNM */
1247 break;
1248 case 3: /* Bits 24-31 */
1249#ifdef CONFIG_WNM
1250 *pos |= 0x02; /* Bit 25 - SSID List */
1251#endif /* CONFIG_WNM */
1252#ifdef CONFIG_INTERWORKING
1253 if (wpa_s->conf->interworking)
1254 *pos |= 0x80; /* Bit 31 - Interworking */
1255#endif /* CONFIG_INTERWORKING */
1256 break;
1257 case 4: /* Bits 32-39 */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001258#ifdef CONFIG_INTERWORKING
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001259 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1260 *pos |= 0x01; /* Bit 32 - QoS Map */
Dmitry Shmidt051af732013-10-22 13:52:46 -07001261#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001262 break;
1263 case 5: /* Bits 40-47 */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001264#ifdef CONFIG_HS20
1265 if (wpa_s->conf->hs20)
1266 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1267#endif /* CONFIG_HS20 */
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001268 break;
1269 case 6: /* Bits 48-55 */
1270 break;
1271 }
1272}
1273
1274
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001275int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001276{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001277 u8 *pos = buf;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001278 u8 len = 6, i;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001279
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001280 if (len < wpa_s->extended_capa_len)
1281 len = wpa_s->extended_capa_len;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001282 if (buflen < (size_t) len + 2) {
1283 wpa_printf(MSG_INFO,
1284 "Not enough room for building extended capabilities element");
1285 return -1;
1286 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001287
1288 *pos++ = WLAN_EID_EXT_CAPAB;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001289 *pos++ = len;
1290 for (i = 0; i < len; i++, pos++) {
1291 wpas_ext_capab_byte(wpa_s, pos, i);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001292
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001293 if (i < wpa_s->extended_capa_len) {
1294 *pos &= ~wpa_s->extended_capa_mask[i];
1295 *pos |= wpa_s->extended_capa[i];
1296 }
1297 }
1298
1299 while (len > 0 && buf[1 + len] == 0) {
1300 len--;
1301 buf[1] = len;
1302 }
1303 if (len == 0)
1304 return 0;
1305
1306 return 2 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001307}
1308
1309
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001310static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1311 struct wpa_bss *test_bss)
1312{
1313 struct wpa_bss *bss;
1314
1315 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1316 if (bss == test_bss)
1317 return 1;
1318 }
1319
1320 return 0;
1321}
1322
1323
1324static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1325 struct wpa_ssid *test_ssid)
1326{
1327 struct wpa_ssid *ssid;
1328
1329 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1330 if (ssid == test_ssid)
1331 return 1;
1332 }
1333
1334 return 0;
1335}
1336
1337
1338int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1339 struct wpa_ssid *test_ssid)
1340{
1341 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1342 return 0;
1343
1344 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1345}
1346
1347
1348void wpas_connect_work_free(struct wpa_connect_work *cwork)
1349{
1350 if (cwork == NULL)
1351 return;
1352 os_free(cwork);
1353}
1354
1355
1356void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1357{
1358 struct wpa_connect_work *cwork;
1359 struct wpa_radio_work *work = wpa_s->connect_work;
1360
1361 if (!work)
1362 return;
1363
1364 wpa_s->connect_work = NULL;
1365 cwork = work->ctx;
1366 work->ctx = NULL;
1367 wpas_connect_work_free(cwork);
1368 radio_work_done(work);
1369}
1370
1371
1372static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374/**
1375 * wpa_supplicant_associate - Request association
1376 * @wpa_s: Pointer to wpa_supplicant data
1377 * @bss: Scan results for the selected BSS, or %NULL if not available
1378 * @ssid: Configuration data for the selected network
1379 *
1380 * This function is used to request %wpa_supplicant to associate with a BSS.
1381 */
1382void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1383 struct wpa_bss *bss, struct wpa_ssid *ssid)
1384{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001385 struct wpa_connect_work *cwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386
1387#ifdef CONFIG_IBSS_RSN
1388 ibss_rsn_deinit(wpa_s->ibss_rsn);
1389 wpa_s->ibss_rsn = NULL;
1390#endif /* CONFIG_IBSS_RSN */
1391
1392 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1393 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1394#ifdef CONFIG_AP
1395 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1396 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1397 "mode");
1398 return;
1399 }
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001400 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1401 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001402 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1403 wpas_p2p_ap_setup_failed(wpa_s);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001404 return;
1405 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406 wpa_s->current_bss = bss;
1407#else /* CONFIG_AP */
1408 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1409 "the build");
1410#endif /* CONFIG_AP */
1411 return;
1412 }
1413
1414#ifdef CONFIG_TDLS
1415 if (bss)
1416 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1417 bss->ie_len);
1418#endif /* CONFIG_TDLS */
1419
1420 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1421 ssid->mode == IEEE80211_MODE_INFRA) {
1422 sme_authenticate(wpa_s, bss, ssid);
1423 return;
1424 }
1425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001426 if (wpa_s->connect_work) {
1427 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1428 return;
1429 }
1430
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001431 if (radio_work_pending(wpa_s, "connect")) {
1432 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1433 return;
1434 }
1435
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001436 cwork = os_zalloc(sizeof(*cwork));
1437 if (cwork == NULL)
1438 return;
1439
1440 cwork->bss = bss;
1441 cwork->ssid = ssid;
1442
1443 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1444 wpas_start_assoc_cb, cwork) < 0) {
1445 os_free(cwork);
1446 }
1447}
1448
1449
1450static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
1451{
1452 struct wpa_connect_work *cwork = work->ctx;
1453 struct wpa_bss *bss = cwork->bss;
1454 struct wpa_ssid *ssid = cwork->ssid;
1455 struct wpa_supplicant *wpa_s = work->wpa_s;
1456 u8 wpa_ie[200];
1457 size_t wpa_ie_len;
1458 int use_crypt, ret, i, bssid_changed;
1459 int algs = WPA_AUTH_ALG_OPEN;
1460 unsigned int cipher_pairwise, cipher_group;
1461 struct wpa_driver_associate_params params;
1462 int wep_keys_set = 0;
1463 int assoc_failed = 0;
1464 struct wpa_ssid *old_ssid;
1465#ifdef CONFIG_HT_OVERRIDES
1466 struct ieee80211_ht_capabilities htcaps;
1467 struct ieee80211_ht_capabilities htcaps_mask;
1468#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001469#ifdef CONFIG_VHT_OVERRIDES
1470 struct ieee80211_vht_capabilities vhtcaps;
1471 struct ieee80211_vht_capabilities vhtcaps_mask;
1472#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001473
1474 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001475 if (work->started) {
1476 wpa_s->connect_work = NULL;
1477
1478 /* cancel possible auth. timeout */
1479 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
1480 NULL);
1481 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001482 wpas_connect_work_free(cwork);
1483 return;
1484 }
1485
1486 wpa_s->connect_work = work;
1487
1488 if (!wpas_valid_bss_ssid(wpa_s, bss, ssid)) {
1489 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
1490 wpas_connect_work_done(wpa_s);
1491 return;
1492 }
1493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 os_memset(&params, 0, sizeof(params));
1495 wpa_s->reassociate = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08001496 wpa_s->eap_expected_failure = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 if (bss && !wpas_driver_bss_selection(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498#ifdef CONFIG_IEEE80211R
1499 const u8 *ie, *md = NULL;
1500#endif /* CONFIG_IEEE80211R */
1501 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1502 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1503 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1504 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1505 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1506 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1507 if (bssid_changed)
1508 wpas_notify_bssid_changed(wpa_s);
1509#ifdef CONFIG_IEEE80211R
1510 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1511 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1512 md = ie + 2;
1513 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1514 if (md) {
1515 /* Prepare for the next transition */
1516 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1517 }
1518#endif /* CONFIG_IEEE80211R */
1519#ifdef CONFIG_WPS
1520 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1521 wpa_s->conf->ap_scan == 2 &&
1522 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1523 /* Use ap_scan==1 style network selection to find the network
1524 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001525 wpa_s->scan_req = MANUAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 wpa_s->reassociate = 1;
1527 wpa_supplicant_req_scan(wpa_s, 0, 0);
1528 return;
1529#endif /* CONFIG_WPS */
1530 } else {
1531 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1532 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1533 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1534 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001535 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 wpa_supplicant_cancel_scan(wpa_s);
1537
1538 /* Starting new association, so clear the possibly used WPA IE from the
1539 * previous association. */
1540 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1541
1542#ifdef IEEE8021X_EAPOL
1543 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1544 if (ssid->leap) {
1545 if (ssid->non_leap == 0)
1546 algs = WPA_AUTH_ALG_LEAP;
1547 else
1548 algs |= WPA_AUTH_ALG_LEAP;
1549 }
1550 }
1551#endif /* IEEE8021X_EAPOL */
1552 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1553 if (ssid->auth_alg) {
1554 algs = ssid->auth_alg;
1555 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1556 "0x%x", algs);
1557 }
1558
1559 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1560 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 int try_opportunistic;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001563 try_opportunistic = (ssid->proactive_key_caching < 0 ?
1564 wpa_s->conf->okc :
1565 ssid->proactive_key_caching) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 (ssid->proto & WPA_PROTO_RSN);
1567 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07001568 ssid, try_opportunistic) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1570 wpa_ie_len = sizeof(wpa_ie);
1571 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1572 wpa_ie, &wpa_ie_len)) {
1573 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1574 "key management and encryption suites");
1575 return;
1576 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001577 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
1578 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
1579 /*
1580 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
1581 * use non-WPA since the scan results did not indicate that the
1582 * AP is using WPA or WPA2.
1583 */
1584 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1585 wpa_ie_len = 0;
1586 wpa_s->wpa_proto = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001587 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 wpa_ie_len = sizeof(wpa_ie);
1589 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1590 wpa_ie, &wpa_ie_len)) {
1591 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1592 "key management and encryption suites (no "
1593 "scan results)");
1594 return;
1595 }
1596#ifdef CONFIG_WPS
1597 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1598 struct wpabuf *wps_ie;
1599 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1600 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1601 wpa_ie_len = wpabuf_len(wps_ie);
1602 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1603 } else
1604 wpa_ie_len = 0;
1605 wpabuf_free(wps_ie);
1606 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1607 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1608 params.wps = WPS_MODE_PRIVACY;
1609 else
1610 params.wps = WPS_MODE_OPEN;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612#endif /* CONFIG_WPS */
1613 } else {
1614 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1615 wpa_ie_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001616 wpa_s->wpa_proto = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 }
1618
1619#ifdef CONFIG_P2P
1620 if (wpa_s->global->p2p) {
1621 u8 *pos;
1622 size_t len;
1623 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 pos = wpa_ie + wpa_ie_len;
1625 len = sizeof(wpa_ie) - wpa_ie_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001626 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1627 ssid->p2p_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 if (res >= 0)
1629 wpa_ie_len += res;
1630 }
1631
1632 wpa_s->cross_connect_disallowed = 0;
1633 if (bss) {
1634 struct wpabuf *p2p;
1635 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1636 if (p2p) {
1637 wpa_s->cross_connect_disallowed =
1638 p2p_get_cross_connect_disallowed(p2p);
1639 wpabuf_free(p2p);
1640 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1641 "connection",
1642 wpa_s->cross_connect_disallowed ?
1643 "disallows" : "allows");
1644 }
1645 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001646
1647 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648#endif /* CONFIG_P2P */
1649
Dmitry Shmidt04949592012-07-19 12:16:46 -07001650#ifdef CONFIG_HS20
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001651 if (is_hs20_network(wpa_s, ssid, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001652 struct wpabuf *hs20;
1653 hs20 = wpabuf_alloc(20);
1654 if (hs20) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001655 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
1656 wpas_hs20_add_indication(hs20, pps_mo_id);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001657 os_memcpy(wpa_ie + wpa_ie_len, wpabuf_head(hs20),
1658 wpabuf_len(hs20));
1659 wpa_ie_len += wpabuf_len(hs20);
1660 wpabuf_free(hs20);
1661 }
1662 }
1663#endif /* CONFIG_HS20 */
1664
Dmitry Shmidt56052862013-10-04 10:23:25 -07001665 /*
1666 * Workaround: Add Extended Capabilities element only if the AP
1667 * included this element in Beacon/Probe Response frames. Some older
1668 * APs seem to have interoperability issues if this element is
1669 * included, so while the standard may require us to include the
1670 * element in all cases, it is justifiable to skip it to avoid
1671 * interoperability issues.
1672 */
1673 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001674 u8 ext_capab[18];
Dmitry Shmidt56052862013-10-04 10:23:25 -07001675 int ext_capab_len;
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07001676 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
1677 sizeof(ext_capab));
Dmitry Shmidt56052862013-10-04 10:23:25 -07001678 if (ext_capab_len > 0) {
1679 u8 *pos = wpa_ie;
1680 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1681 pos += 2 + pos[1];
1682 os_memmove(pos + ext_capab_len, pos,
1683 wpa_ie_len - (pos - wpa_ie));
1684 wpa_ie_len += ext_capab_len;
1685 os_memcpy(pos, ext_capab, ext_capab_len);
1686 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001687 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1690 use_crypt = 1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001691 cipher_pairwise = wpa_s->pairwise_cipher;
1692 cipher_group = wpa_s->group_cipher;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001693 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1694 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1695 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1696 use_crypt = 0;
1697 if (wpa_set_wep_keys(wpa_s, ssid)) {
1698 use_crypt = 1;
1699 wep_keys_set = 1;
1700 }
1701 }
1702 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1703 use_crypt = 0;
1704
1705#ifdef IEEE8021X_EAPOL
1706 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1707 if ((ssid->eapol_flags &
1708 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1709 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1710 !wep_keys_set) {
1711 use_crypt = 0;
1712 } else {
1713 /* Assume that dynamic WEP-104 keys will be used and
1714 * set cipher suites in order for drivers to expect
1715 * encryption. */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001716 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 }
1718 }
1719#endif /* IEEE8021X_EAPOL */
1720
1721 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1722 /* Set the key before (and later after) association */
1723 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1724 }
1725
1726 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1727 if (bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 params.ssid = bss->ssid;
1729 params.ssid_len = bss->ssid_len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001730 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
1731 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
1732 MACSTR " freq=%u MHz based on scan results "
1733 "(bssid_set=%d)",
1734 MAC2STR(bss->bssid), bss->freq,
1735 ssid->bssid_set);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001736 params.bssid = bss->bssid;
1737 params.freq = bss->freq;
1738 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001739 params.bssid_hint = bss->bssid;
1740 params.freq_hint = bss->freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741 } else {
1742 params.ssid = ssid->ssid;
1743 params.ssid_len = ssid->ssid_len;
1744 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001745
1746 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1747 wpa_s->conf->ap_scan == 2) {
1748 params.bssid = ssid->bssid;
1749 params.fixed_bssid = 1;
1750 }
1751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1753 params.freq == 0)
1754 params.freq = ssid->frequency; /* Initial channel for IBSS */
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08001755
1756 if (ssid->mode == WPAS_MODE_IBSS) {
1757 if (ssid->beacon_int)
1758 params.beacon_int = ssid->beacon_int;
1759 else
1760 params.beacon_int = wpa_s->conf->beacon_int;
1761 }
1762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 params.wpa_ie = wpa_ie;
1764 params.wpa_ie_len = wpa_ie_len;
1765 params.pairwise_suite = cipher_pairwise;
1766 params.group_suite = cipher_group;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001767 params.key_mgmt_suite = wpa_s->key_mgmt;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001768 params.wpa_proto = wpa_s->wpa_proto;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 params.auth_alg = algs;
1770 params.mode = ssid->mode;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001771 params.bg_scan_period = ssid->bg_scan_period;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772 for (i = 0; i < NUM_WEP_KEYS; i++) {
1773 if (ssid->wep_key_len[i])
1774 params.wep_key[i] = ssid->wep_key[i];
1775 params.wep_key_len[i] = ssid->wep_key_len[i];
1776 }
1777 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1778
1779 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001780 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
1781 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 params.passphrase = ssid->passphrase;
1783 if (ssid->psk_set)
1784 params.psk = ssid->psk;
1785 }
1786
1787 params.drop_unencrypted = use_crypt;
1788
1789#ifdef CONFIG_IEEE80211W
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001790 params.mgmt_frame_protection =
1791 ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT ?
1792 wpa_s->conf->pmf : ssid->ieee80211w;
1793 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1795 struct wpa_ie_data ie;
1796 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1797 ie.capabilities &
1798 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1799 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1800 "MFP: require MFP");
1801 params.mgmt_frame_protection =
1802 MGMT_FRAME_PROTECTION_REQUIRED;
1803 }
1804 }
1805#endif /* CONFIG_IEEE80211W */
1806
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001807 params.p2p = ssid->p2p_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808
1809 if (wpa_s->parent->set_sta_uapsd)
1810 params.uapsd = wpa_s->parent->sta_uapsd;
1811 else
1812 params.uapsd = -1;
1813
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001814#ifdef CONFIG_HT_OVERRIDES
1815 os_memset(&htcaps, 0, sizeof(htcaps));
1816 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1817 params.htcaps = (u8 *) &htcaps;
1818 params.htcaps_mask = (u8 *) &htcaps_mask;
1819 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1820#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001821#ifdef CONFIG_VHT_OVERRIDES
1822 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
1823 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
1824 params.vhtcaps = &vhtcaps;
1825 params.vhtcaps_mask = &vhtcaps_mask;
1826 wpa_supplicant_apply_vht_overrides(wpa_s, wpa_s->current_ssid, &params);
1827#endif /* CONFIG_VHT_OVERRIDES */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001828
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001829#ifdef CONFIG_P2P
1830 /*
1831 * If multi-channel concurrency is not supported, check for any
1832 * frequency conflict. In case of any frequency conflict, remove the
1833 * least prioritized connection.
1834 */
1835 if (wpa_s->num_multichan_concurrent < 2) {
Dmitry Shmidtf9bdef92014-04-25 10:46:36 -07001836 int freq, num;
1837 num = get_shared_radio_freqs(wpa_s, &freq, 1);
1838 if (num > 0 && freq > 0 && freq != params.freq) {
1839 wpa_printf(MSG_DEBUG,
1840 "Assoc conflicting freq found (%d != %d)",
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001841 freq, params.freq);
1842 if (wpas_p2p_handle_frequency_conflicts(wpa_s,
1843 params.freq,
1844 ssid) < 0)
1845 return;
1846 }
1847 }
1848#endif /* CONFIG_P2P */
1849
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001850 ret = wpa_drv_associate(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851 if (ret < 0) {
1852 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1853 "failed");
1854 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1855 /*
1856 * The driver is known to mean what is saying, so we
1857 * can stop right here; the association will not
1858 * succeed.
1859 */
1860 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001861 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1863 return;
1864 }
1865 /* try to continue anyway; new association will be tried again
1866 * after timeout */
1867 assoc_failed = 1;
1868 }
1869
1870 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1871 /* Set the key after the association just in case association
1872 * cleared the previously configured key. */
1873 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1874 /* No need to timeout authentication since there is no key
1875 * management. */
1876 wpa_supplicant_cancel_auth_timeout(wpa_s);
1877 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1878#ifdef CONFIG_IBSS_RSN
1879 } else if (ssid->mode == WPAS_MODE_IBSS &&
1880 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1881 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1882 /*
1883 * RSN IBSS authentication is per-STA and we can disable the
1884 * per-BSSID authentication.
1885 */
1886 wpa_supplicant_cancel_auth_timeout(wpa_s);
1887#endif /* CONFIG_IBSS_RSN */
1888 } else {
1889 /* Timeout for IEEE 802.11 authentication and association */
1890 int timeout = 60;
1891
1892 if (assoc_failed) {
1893 /* give IBSS a bit more time */
1894 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1895 } else if (wpa_s->conf->ap_scan == 1) {
1896 /* give IBSS a bit more time */
1897 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1898 }
1899 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1900 }
1901
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001902 if (wep_keys_set &&
1903 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 /* Set static WEP keys again */
1905 wpa_set_wep_keys(wpa_s, ssid);
1906 }
1907
1908 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1909 /*
1910 * Do not allow EAP session resumption between different
1911 * network configurations.
1912 */
1913 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1914 }
1915 old_ssid = wpa_s->current_ssid;
1916 wpa_s->current_ssid = ssid;
1917 wpa_s->current_bss = bss;
1918 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1919 wpa_supplicant_initiate_eapol(wpa_s);
1920 if (old_ssid != wpa_s->current_ssid)
1921 wpas_notify_network_changed(wpa_s);
1922}
1923
1924
1925static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1926 const u8 *addr)
1927{
1928 struct wpa_ssid *old_ssid;
1929
1930 wpa_clear_keys(wpa_s, addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 old_ssid = wpa_s->current_ssid;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001932 wpa_supplicant_mark_disassoc(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001933 wpa_sm_set_config(wpa_s->wpa, NULL);
1934 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1935 if (old_ssid != wpa_s->current_ssid)
1936 wpas_notify_network_changed(wpa_s);
1937 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1938}
1939
1940
1941/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1943 * @wpa_s: Pointer to wpa_supplicant data
1944 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1945 *
1946 * This function is used to request %wpa_supplicant to deauthenticate from the
1947 * current AP.
1948 */
1949void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1950 int reason_code)
1951{
1952 u8 *addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001953 union wpa_event_data event;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001954 int zero_addr = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001956 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
1957 " pending_bssid=" MACSTR " reason=%d state=%s",
1958 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
1959 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
1960
1961 if (!is_zero_ether_addr(wpa_s->bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 addr = wpa_s->bssid;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001963 else if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
1964 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
1965 wpa_s->wpa_state == WPA_ASSOCIATING))
1966 addr = wpa_s->pending_bssid;
1967 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
1968 /*
1969 * When using driver-based BSS selection, we may not know the
1970 * BSSID with which we are currently trying to associate. We
1971 * need to notify the driver of this disconnection even in such
1972 * a case, so use the all zeros address here.
1973 */
1974 addr = wpa_s->bssid;
1975 zero_addr = 1;
1976 }
1977
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001978#ifdef CONFIG_TDLS
1979 wpa_tdls_teardown_peers(wpa_s->wpa);
1980#endif /* CONFIG_TDLS */
1981
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001982 if (addr) {
1983 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001984 os_memset(&event, 0, sizeof(event));
1985 event.deauth_info.reason_code = (u16) reason_code;
1986 event.deauth_info.locally_generated = 1;
1987 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001988 if (zero_addr)
1989 addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 }
1991
1992 wpa_supplicant_clear_connection(wpa_s, addr);
1993}
1994
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001995static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
1996 struct wpa_ssid *ssid)
1997{
1998 if (!ssid || !ssid->disabled || ssid->disabled == 2)
1999 return;
2000
2001 ssid->disabled = 0;
2002 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2003 wpas_notify_network_enabled_changed(wpa_s, ssid);
2004
2005 /*
2006 * Try to reassociate since there is no current configuration and a new
2007 * network was made available.
2008 */
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002009 if (!wpa_s->current_ssid && !wpa_s->disconnected)
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002010 wpa_s->reassociate = 1;
2011}
2012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013
2014/**
2015 * wpa_supplicant_enable_network - Mark a configured network as enabled
2016 * @wpa_s: wpa_supplicant structure for a network interface
2017 * @ssid: wpa_ssid structure for a configured network or %NULL
2018 *
2019 * Enables the specified network or all networks if no network specified.
2020 */
2021void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2022 struct wpa_ssid *ssid)
2023{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002025 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2026 wpa_supplicant_enable_one_network(wpa_s, ssid);
2027 } else
2028 wpa_supplicant_enable_one_network(wpa_s, ssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002030 if (wpa_s->reassociate && !wpa_s->disconnected) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002031 if (wpa_s->sched_scanning) {
2032 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2033 "new network to scan filters");
2034 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 }
2036
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002037 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2038 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 }
2040}
2041
2042
2043/**
2044 * wpa_supplicant_disable_network - Mark a configured network as disabled
2045 * @wpa_s: wpa_supplicant structure for a network interface
2046 * @ssid: wpa_ssid structure for a configured network or %NULL
2047 *
2048 * Disables the specified network or all networks if no network specified.
2049 */
2050void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2051 struct wpa_ssid *ssid)
2052{
2053 struct wpa_ssid *other_ssid;
2054 int was_disabled;
2055
2056 if (ssid == NULL) {
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002057 if (wpa_s->sched_scanning)
2058 wpa_supplicant_cancel_sched_scan(wpa_s);
2059
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2061 other_ssid = other_ssid->next) {
2062 was_disabled = other_ssid->disabled;
2063 if (was_disabled == 2)
2064 continue; /* do not change persistent P2P group
2065 * data */
2066
2067 other_ssid->disabled = 1;
2068
2069 if (was_disabled != other_ssid->disabled)
2070 wpas_notify_network_enabled_changed(
2071 wpa_s, other_ssid);
2072 }
2073 if (wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002074 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2076 } else if (ssid->disabled != 2) {
2077 if (ssid == wpa_s->current_ssid)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002078 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2080
2081 was_disabled = ssid->disabled;
2082
2083 ssid->disabled = 1;
2084
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002085 if (was_disabled != ssid->disabled) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 wpas_notify_network_enabled_changed(wpa_s, ssid);
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002087 if (wpa_s->sched_scanning) {
2088 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
2089 "to remove network from filters");
2090 wpa_supplicant_cancel_sched_scan(wpa_s);
2091 wpa_supplicant_req_scan(wpa_s, 0, 0);
2092 }
2093 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 }
2095}
2096
2097
2098/**
2099 * wpa_supplicant_select_network - Attempt association with a network
2100 * @wpa_s: wpa_supplicant structure for a network interface
2101 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
2102 */
2103void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
2104 struct wpa_ssid *ssid)
2105{
2106
2107 struct wpa_ssid *other_ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002108 int disconnected = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002110 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002111 wpa_supplicant_deauthenticate(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002113 disconnected = 1;
2114 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002115
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002116 if (ssid)
2117 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 /*
2120 * Mark all other networks disabled or mark all networks enabled if no
2121 * network specified.
2122 */
2123 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2124 other_ssid = other_ssid->next) {
2125 int was_disabled = other_ssid->disabled;
2126 if (was_disabled == 2)
2127 continue; /* do not change persistent P2P group data */
2128
2129 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002130 if (was_disabled && !other_ssid->disabled)
2131 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132
2133 if (was_disabled != other_ssid->disabled)
2134 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
2135 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136
2137 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
2138 /* We are already associated with the selected network */
2139 wpa_printf(MSG_DEBUG, "Already associated with the "
2140 "selected network - do nothing");
2141 return;
2142 }
2143
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002144 if (ssid) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002145 wpa_s->current_ssid = ssid;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002146 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2147 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07002148 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 wpa_s->disconnected = 0;
2150 wpa_s->reassociate = 1;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002151
2152 if (wpa_supplicant_fast_associate(wpa_s) != 1)
2153 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154
2155 if (ssid)
2156 wpas_notify_network_selected(wpa_s, ssid);
2157}
2158
2159
2160/**
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002161 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
2162 * @wpa_s: wpa_supplicant structure for a network interface
2163 * @pkcs11_engine_path: PKCS #11 engine path or NULL
2164 * @pkcs11_module_path: PKCS #11 module path or NULL
2165 * Returns: 0 on success; -1 on failure
2166 *
2167 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
2168 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
2169 * module path fails the paths will be reset to the default value (NULL).
2170 */
2171int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
2172 const char *pkcs11_engine_path,
2173 const char *pkcs11_module_path)
2174{
2175 char *pkcs11_engine_path_copy = NULL;
2176 char *pkcs11_module_path_copy = NULL;
2177
2178 if (pkcs11_engine_path != NULL) {
2179 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
2180 if (pkcs11_engine_path_copy == NULL)
2181 return -1;
2182 }
2183 if (pkcs11_module_path != NULL) {
2184 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002185 if (pkcs11_module_path_copy == NULL) {
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08002186 os_free(pkcs11_engine_path_copy);
2187 return -1;
2188 }
2189 }
2190
2191 os_free(wpa_s->conf->pkcs11_engine_path);
2192 os_free(wpa_s->conf->pkcs11_module_path);
2193 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
2194 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
2195
2196 wpa_sm_set_eapol(wpa_s->wpa, NULL);
2197 eapol_sm_deinit(wpa_s->eapol);
2198 wpa_s->eapol = NULL;
2199 if (wpa_supplicant_init_eapol(wpa_s)) {
2200 /* Error -> Reset paths to the default value (NULL) once. */
2201 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
2202 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
2203 NULL);
2204
2205 return -1;
2206 }
2207 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2208
2209 return 0;
2210}
2211
2212
2213/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
2215 * @wpa_s: wpa_supplicant structure for a network interface
2216 * @ap_scan: AP scan mode
2217 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
2218 *
2219 */
2220int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
2221{
2222
2223 int old_ap_scan;
2224
2225 if (ap_scan < 0 || ap_scan > 2)
2226 return -1;
2227
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002228#ifdef ANDROID
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002229 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
2230 wpa_s->wpa_state >= WPA_ASSOCIATING &&
2231 wpa_s->wpa_state < WPA_COMPLETED) {
2232 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
2233 "associating", wpa_s->conf->ap_scan, ap_scan);
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002234 return 0;
2235 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002236#endif /* ANDROID */
Dmitry Shmidt114c3862011-08-16 11:52:06 -07002237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 old_ap_scan = wpa_s->conf->ap_scan;
2239 wpa_s->conf->ap_scan = ap_scan;
2240
2241 if (old_ap_scan != wpa_s->conf->ap_scan)
2242 wpas_notify_ap_scan_changed(wpa_s);
2243
2244 return 0;
2245}
2246
2247
2248/**
2249 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
2250 * @wpa_s: wpa_supplicant structure for a network interface
2251 * @expire_age: Expiration age in seconds
2252 * Returns: 0 if succeed or -1 if expire_age has an invalid value
2253 *
2254 */
2255int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
2256 unsigned int bss_expire_age)
2257{
2258 if (bss_expire_age < 10) {
2259 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
2260 bss_expire_age);
2261 return -1;
2262 }
2263 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
2264 bss_expire_age);
2265 wpa_s->conf->bss_expiration_age = bss_expire_age;
2266
2267 return 0;
2268}
2269
2270
2271/**
2272 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
2273 * @wpa_s: wpa_supplicant structure for a network interface
2274 * @expire_count: number of scans after which an unseen BSS is reclaimed
2275 * Returns: 0 if succeed or -1 if expire_count has an invalid value
2276 *
2277 */
2278int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
2279 unsigned int bss_expire_count)
2280{
2281 if (bss_expire_count < 1) {
2282 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
2283 bss_expire_count);
2284 return -1;
2285 }
2286 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
2287 bss_expire_count);
2288 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
2289
2290 return 0;
2291}
2292
2293
2294/**
Dmitry Shmidt04949592012-07-19 12:16:46 -07002295 * wpa_supplicant_set_scan_interval - Set scan interval
2296 * @wpa_s: wpa_supplicant structure for a network interface
2297 * @scan_interval: scan interval in seconds
2298 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
2299 *
2300 */
2301int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
2302 int scan_interval)
2303{
2304 if (scan_interval < 0) {
2305 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
2306 scan_interval);
2307 return -1;
2308 }
2309 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
2310 scan_interval);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08002311 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002312
2313 return 0;
2314}
2315
2316
2317/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 * wpa_supplicant_set_debug_params - Set global debug params
2319 * @global: wpa_global structure
2320 * @debug_level: debug level
2321 * @debug_timestamp: determines if show timestamp in debug data
2322 * @debug_show_keys: determines if show keys in debug data
2323 * Returns: 0 if succeed or -1 if debug_level has wrong value
2324 */
2325int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
2326 int debug_timestamp, int debug_show_keys)
2327{
2328
2329 int old_level, old_timestamp, old_show_keys;
2330
2331 /* check for allowed debuglevels */
2332 if (debug_level != MSG_EXCESSIVE &&
2333 debug_level != MSG_MSGDUMP &&
2334 debug_level != MSG_DEBUG &&
2335 debug_level != MSG_INFO &&
2336 debug_level != MSG_WARNING &&
2337 debug_level != MSG_ERROR)
2338 return -1;
2339
2340 old_level = wpa_debug_level;
2341 old_timestamp = wpa_debug_timestamp;
2342 old_show_keys = wpa_debug_show_keys;
2343
2344 wpa_debug_level = debug_level;
2345 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
2346 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
2347
2348 if (wpa_debug_level != old_level)
2349 wpas_notify_debug_level_changed(global);
2350 if (wpa_debug_timestamp != old_timestamp)
2351 wpas_notify_debug_timestamp_changed(global);
2352 if (wpa_debug_show_keys != old_show_keys)
2353 wpas_notify_debug_show_keys_changed(global);
2354
2355 return 0;
2356}
2357
2358
2359/**
2360 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
2361 * @wpa_s: Pointer to wpa_supplicant data
2362 * Returns: A pointer to the current network structure or %NULL on failure
2363 */
2364struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
2365{
2366 struct wpa_ssid *entry;
2367 u8 ssid[MAX_SSID_LEN];
2368 int res;
2369 size_t ssid_len;
2370 u8 bssid[ETH_ALEN];
2371 int wired;
2372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 res = wpa_drv_get_ssid(wpa_s, ssid);
2374 if (res < 0) {
2375 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
2376 "driver");
2377 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002379 ssid_len = res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002381 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
2383 "driver");
2384 return NULL;
2385 }
2386
2387 wired = wpa_s->conf->ap_scan == 0 &&
2388 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
2389
2390 entry = wpa_s->conf->ssid;
2391 while (entry) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07002392 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 ((ssid_len == entry->ssid_len &&
2394 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
2395 (!entry->bssid_set ||
2396 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2397 return entry;
2398#ifdef CONFIG_WPS
Dmitry Shmidt04949592012-07-19 12:16:46 -07002399 if (!wpas_network_disabled(wpa_s, entry) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
2401 (entry->ssid == NULL || entry->ssid_len == 0) &&
2402 (!entry->bssid_set ||
2403 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
2404 return entry;
2405#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002406
Dmitry Shmidt04949592012-07-19 12:16:46 -07002407 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408 entry->ssid_len == 0 &&
2409 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
2410 return entry;
2411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 entry = entry->next;
2413 }
2414
2415 return NULL;
2416}
2417
2418
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002419static int select_driver(struct wpa_supplicant *wpa_s, int i)
2420{
2421 struct wpa_global *global = wpa_s->global;
2422
2423 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
2424 global->drv_priv[i] = wpa_drivers[i]->global_init();
2425 if (global->drv_priv[i] == NULL) {
2426 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2427 "'%s'", wpa_drivers[i]->name);
2428 return -1;
2429 }
2430 }
2431
2432 wpa_s->driver = wpa_drivers[i];
2433 wpa_s->global_drv_priv = global->drv_priv[i];
2434
2435 return 0;
2436}
2437
2438
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
2440 const char *name)
2441{
2442 int i;
2443 size_t len;
2444 const char *pos, *driver = name;
2445
2446 if (wpa_s == NULL)
2447 return -1;
2448
2449 if (wpa_drivers[0] == NULL) {
2450 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
2451 "wpa_supplicant");
2452 return -1;
2453 }
2454
2455 if (name == NULL) {
2456 /* default to first driver in the list */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002457 return select_driver(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 }
2459
2460 do {
2461 pos = os_strchr(driver, ',');
2462 if (pos)
2463 len = pos - driver;
2464 else
2465 len = os_strlen(driver);
2466
2467 for (i = 0; wpa_drivers[i]; i++) {
2468 if (os_strlen(wpa_drivers[i]->name) == len &&
2469 os_strncmp(driver, wpa_drivers[i]->name, len) ==
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002470 0) {
2471 /* First driver that succeeds wins */
2472 if (select_driver(wpa_s, i) == 0)
2473 return 0;
2474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 }
2476
2477 driver = pos + 1;
2478 } while (pos);
2479
2480 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
2481 return -1;
2482}
2483
2484
2485/**
2486 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
2487 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
2488 * with struct wpa_driver_ops::init()
2489 * @src_addr: Source address of the EAPOL frame
2490 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
2491 * @len: Length of the EAPOL data
2492 *
2493 * This function is called for each received EAPOL frame. Most driver
2494 * interfaces rely on more generic OS mechanism for receiving frames through
2495 * l2_packet, but if such a mechanism is not available, the driver wrapper may
2496 * take care of received EAPOL frames and deliver them to the core supplicant
2497 * code by calling this function.
2498 */
2499void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
2500 const u8 *buf, size_t len)
2501{
2502 struct wpa_supplicant *wpa_s = ctx;
2503
2504 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
2505 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
2506
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002507#ifdef CONFIG_PEERKEY
2508 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
2509 wpa_s->current_ssid->peerkey &&
2510 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2511 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
2512 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
2513 return;
2514 }
2515#endif /* CONFIG_PEERKEY */
2516
Jouni Malinena05074c2012-12-21 21:35:35 +02002517 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
2518 (wpa_s->last_eapol_matches_bssid &&
2519#ifdef CONFIG_AP
2520 !wpa_s->ap_iface &&
2521#endif /* CONFIG_AP */
2522 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 /*
2524 * There is possible race condition between receiving the
2525 * association event and the EAPOL frame since they are coming
2526 * through different paths from the driver. In order to avoid
2527 * issues in trying to process the EAPOL frame before receiving
2528 * association information, lets queue it for processing until
Jouni Malinena05074c2012-12-21 21:35:35 +02002529 * the association event is received. This may also be needed in
2530 * driver-based roaming case, so also use src_addr != BSSID as a
2531 * trigger if we have previously confirmed that the
2532 * Authenticator uses BSSID as the src_addr (which is not the
2533 * case with wired IEEE 802.1X).
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 */
2535 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
Jouni Malinena05074c2012-12-21 21:35:35 +02002536 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
2537 wpa_supplicant_state_txt(wpa_s->wpa_state),
2538 MAC2STR(wpa_s->bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 wpabuf_free(wpa_s->pending_eapol_rx);
2540 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
2541 if (wpa_s->pending_eapol_rx) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002542 os_get_reltime(&wpa_s->pending_eapol_rx_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
2544 ETH_ALEN);
2545 }
2546 return;
2547 }
2548
Jouni Malinena05074c2012-12-21 21:35:35 +02002549 wpa_s->last_eapol_matches_bssid =
2550 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
2551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552#ifdef CONFIG_AP
2553 if (wpa_s->ap_iface) {
2554 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
2555 return;
2556 }
2557#endif /* CONFIG_AP */
2558
2559 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2560 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2561 "no key management is configured");
2562 return;
2563 }
2564
2565 if (wpa_s->eapol_received == 0 &&
2566 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2567 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2568 wpa_s->wpa_state != WPA_COMPLETED) &&
2569 (wpa_s->current_ssid == NULL ||
2570 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2571 /* Timeout for completing IEEE 802.1X and WPA authentication */
2572 wpa_supplicant_req_auth_timeout(
2573 wpa_s,
2574 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2575 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2576 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2577 70 : 10, 0);
2578 }
2579 wpa_s->eapol_received++;
2580
2581 if (wpa_s->countermeasures) {
2582 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2583 "EAPOL packet");
2584 return;
2585 }
2586
2587#ifdef CONFIG_IBSS_RSN
2588 if (wpa_s->current_ssid &&
2589 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2590 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2591 return;
2592 }
2593#endif /* CONFIG_IBSS_RSN */
2594
2595 /* Source address of the incoming EAPOL frame could be compared to the
2596 * current BSSID. However, it is possible that a centralized
2597 * Authenticator could be using another MAC address than the BSSID of
2598 * an AP, so just allow any address to be used for now. The replies are
2599 * still sent to the current BSSID (if available), though. */
2600
2601 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2602 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2603 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2604 return;
2605 wpa_drv_poll(wpa_s);
2606 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2607 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2608 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2609 /*
2610 * Set portValid = TRUE here since we are going to skip 4-way
2611 * handshake processing which would normally set portValid. We
2612 * need this to allow the EAPOL state machines to be completed
2613 * without going through EAPOL-Key handshake.
2614 */
2615 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2616 }
2617}
2618
2619
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002620int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002621{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 if (wpa_s->driver->send_eapol) {
2623 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2624 if (addr)
2625 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002626 } else if ((!wpa_s->p2p_mgmt ||
2627 !(wpa_s->drv_flags &
2628 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
2629 !(wpa_s->drv_flags &
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002631 l2_packet_deinit(wpa_s->l2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2633 wpa_drv_get_mac_addr(wpa_s),
2634 ETH_P_EAPOL,
2635 wpa_supplicant_rx_eapol, wpa_s, 0);
2636 if (wpa_s->l2 == NULL)
2637 return -1;
2638 } else {
2639 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2640 if (addr)
2641 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2642 }
2643
2644 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2645 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2646 return -1;
2647 }
2648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002649 return 0;
2650}
2651
2652
Dmitry Shmidt04949592012-07-19 12:16:46 -07002653static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2654 const u8 *buf, size_t len)
2655{
2656 struct wpa_supplicant *wpa_s = ctx;
2657 const struct l2_ethhdr *eth;
2658
2659 if (len < sizeof(*eth))
2660 return;
2661 eth = (const struct l2_ethhdr *) buf;
2662
2663 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2664 !(eth->h_dest[0] & 0x01)) {
2665 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2666 " (bridge - not for this interface - ignore)",
2667 MAC2STR(src_addr), MAC2STR(eth->h_dest));
2668 return;
2669 }
2670
2671 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2672 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2673 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2674 len - sizeof(*eth));
2675}
2676
2677
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002678/**
2679 * wpa_supplicant_driver_init - Initialize driver interface parameters
2680 * @wpa_s: Pointer to wpa_supplicant data
2681 * Returns: 0 on success, -1 on failure
2682 *
2683 * This function is called to initialize driver interface parameters.
2684 * wpa_drv_init() must have been called before this function to initialize the
2685 * driver interface.
2686 */
2687int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2688{
2689 static int interface_count = 0;
2690
2691 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2692 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002694 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2695 MAC2STR(wpa_s->own_addr));
2696 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002698 if (wpa_s->bridge_ifname[0]) {
2699 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2700 "interface '%s'", wpa_s->bridge_ifname);
2701 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2702 wpa_s->own_addr,
2703 ETH_P_EAPOL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002704 wpa_supplicant_rx_eapol_bridge,
2705 wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 if (wpa_s->l2_br == NULL) {
2707 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2708 "connection for the bridge interface '%s'",
2709 wpa_s->bridge_ifname);
2710 return -1;
2711 }
2712 }
2713
2714 wpa_clear_keys(wpa_s, NULL);
2715
2716 /* Make sure that TKIP countermeasures are not left enabled (could
2717 * happen if wpa_supplicant is killed during countermeasures. */
2718 wpa_drv_set_countermeasures(wpa_s, 0);
2719
2720 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2721 wpa_drv_flush_pmkid(wpa_s);
2722
2723 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002724 wpa_s->prev_scan_wildcard = 0;
2725
Dmitry Shmidt04949592012-07-19 12:16:46 -07002726 if (wpa_supplicant_enabled_networks(wpa_s)) {
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08002727 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
2728 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2729 interface_count = 0;
2730 }
Dmitry Shmidta38abf92014-03-06 13:38:44 -08002731 if (!wpa_s->p2p_mgmt &&
Dmitry Shmidt98660862014-03-11 17:26:21 -07002732 wpa_supplicant_delayed_sched_scan(wpa_s,
2733 interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002734 100000))
Dmitry Shmidt98660862014-03-11 17:26:21 -07002735 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 100000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 interface_count++;
2738 } else
2739 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2740
2741 return 0;
2742}
2743
2744
2745static int wpa_supplicant_daemon(const char *pid_file)
2746{
2747 wpa_printf(MSG_DEBUG, "Daemonize..");
2748 return os_daemonize(pid_file);
2749}
2750
2751
2752static struct wpa_supplicant * wpa_supplicant_alloc(void)
2753{
2754 struct wpa_supplicant *wpa_s;
2755
2756 wpa_s = os_zalloc(sizeof(*wpa_s));
2757 if (wpa_s == NULL)
2758 return NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002759 wpa_s->scan_req = INITIAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 wpa_s->scan_interval = 5;
2761 wpa_s->new_connection = 1;
2762 wpa_s->parent = wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002763 wpa_s->sched_scanning = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764
2765 return wpa_s;
2766}
2767
2768
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002769#ifdef CONFIG_HT_OVERRIDES
2770
2771static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2772 struct ieee80211_ht_capabilities *htcaps,
2773 struct ieee80211_ht_capabilities *htcaps_mask,
2774 const char *ht_mcs)
2775{
2776 /* parse ht_mcs into hex array */
2777 int i;
2778 const char *tmp = ht_mcs;
2779 char *end = NULL;
2780
2781 /* If ht_mcs is null, do not set anything */
2782 if (!ht_mcs)
2783 return 0;
2784
2785 /* This is what we are setting in the kernel */
2786 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2787
2788 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2789
2790 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2791 errno = 0;
2792 long v = strtol(tmp, &end, 16);
2793 if (errno == 0) {
2794 wpa_msg(wpa_s, MSG_DEBUG,
2795 "htcap value[%i]: %ld end: %p tmp: %p",
2796 i, v, end, tmp);
2797 if (end == tmp)
2798 break;
2799
2800 htcaps->supported_mcs_set[i] = v;
2801 tmp = end;
2802 } else {
2803 wpa_msg(wpa_s, MSG_ERROR,
2804 "Failed to parse ht-mcs: %s, error: %s\n",
2805 ht_mcs, strerror(errno));
2806 return -1;
2807 }
2808 }
2809
2810 /*
2811 * If we were able to parse any values, then set mask for the MCS set.
2812 */
2813 if (i) {
2814 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2815 IEEE80211_HT_MCS_MASK_LEN - 1);
2816 /* skip the 3 reserved bits */
2817 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2818 0x1f;
2819 }
2820
2821 return 0;
2822}
2823
2824
2825static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2826 struct ieee80211_ht_capabilities *htcaps,
2827 struct ieee80211_ht_capabilities *htcaps_mask,
2828 int disabled)
2829{
2830 u16 msk;
2831
2832 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2833
2834 if (disabled == -1)
2835 return 0;
2836
2837 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2838 htcaps_mask->ht_capabilities_info |= msk;
2839 if (disabled)
2840 htcaps->ht_capabilities_info &= msk;
2841 else
2842 htcaps->ht_capabilities_info |= msk;
2843
2844 return 0;
2845}
2846
2847
2848static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2849 struct ieee80211_ht_capabilities *htcaps,
2850 struct ieee80211_ht_capabilities *htcaps_mask,
2851 int factor)
2852{
2853 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2854
2855 if (factor == -1)
2856 return 0;
2857
2858 if (factor < 0 || factor > 3) {
2859 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2860 "Must be 0-3 or -1", factor);
2861 return -EINVAL;
2862 }
2863
2864 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2865 htcaps->a_mpdu_params &= ~0x3;
2866 htcaps->a_mpdu_params |= factor & 0x3;
2867
2868 return 0;
2869}
2870
2871
2872static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2873 struct ieee80211_ht_capabilities *htcaps,
2874 struct ieee80211_ht_capabilities *htcaps_mask,
2875 int density)
2876{
2877 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2878
2879 if (density == -1)
2880 return 0;
2881
2882 if (density < 0 || density > 7) {
2883 wpa_msg(wpa_s, MSG_ERROR,
2884 "ampdu_density: %d out of range. Must be 0-7 or -1.",
2885 density);
2886 return -EINVAL;
2887 }
2888
2889 htcaps_mask->a_mpdu_params |= 0x1C;
2890 htcaps->a_mpdu_params &= ~(0x1C);
2891 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2892
2893 return 0;
2894}
2895
2896
2897static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2898 struct ieee80211_ht_capabilities *htcaps,
2899 struct ieee80211_ht_capabilities *htcaps_mask,
2900 int disabled)
2901{
2902 /* Masking these out disables HT40 */
2903 u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2904 HT_CAP_INFO_SHORT_GI40MHZ);
2905
2906 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2907
2908 if (disabled)
2909 htcaps->ht_capabilities_info &= ~msk;
2910 else
2911 htcaps->ht_capabilities_info |= msk;
2912
2913 htcaps_mask->ht_capabilities_info |= msk;
2914
2915 return 0;
2916}
2917
2918
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002919static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
2920 struct ieee80211_ht_capabilities *htcaps,
2921 struct ieee80211_ht_capabilities *htcaps_mask,
2922 int disabled)
2923{
2924 /* Masking these out disables SGI */
2925 u16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
2926 HT_CAP_INFO_SHORT_GI40MHZ);
2927
2928 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
2929
2930 if (disabled)
2931 htcaps->ht_capabilities_info &= ~msk;
2932 else
2933 htcaps->ht_capabilities_info |= msk;
2934
2935 htcaps_mask->ht_capabilities_info |= msk;
2936
2937 return 0;
2938}
2939
2940
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002941static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
2942 struct ieee80211_ht_capabilities *htcaps,
2943 struct ieee80211_ht_capabilities *htcaps_mask,
2944 int disabled)
2945{
2946 /* Masking these out disables LDPC */
2947 u16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
2948
2949 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
2950
2951 if (disabled)
2952 htcaps->ht_capabilities_info &= ~msk;
2953 else
2954 htcaps->ht_capabilities_info |= msk;
2955
2956 htcaps_mask->ht_capabilities_info |= msk;
2957
2958 return 0;
2959}
2960
2961
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002962void wpa_supplicant_apply_ht_overrides(
2963 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2964 struct wpa_driver_associate_params *params)
2965{
2966 struct ieee80211_ht_capabilities *htcaps;
2967 struct ieee80211_ht_capabilities *htcaps_mask;
2968
2969 if (!ssid)
2970 return;
2971
2972 params->disable_ht = ssid->disable_ht;
2973 if (!params->htcaps || !params->htcaps_mask)
2974 return;
2975
2976 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2977 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2978 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2979 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2980 ssid->disable_max_amsdu);
2981 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2982 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2983 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002984 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07002985 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002986
2987 if (ssid->ht40_intolerant) {
2988 u16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
2989 htcaps->ht_capabilities_info |= bit;
2990 htcaps_mask->ht_capabilities_info |= bit;
2991 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002992}
2993
2994#endif /* CONFIG_HT_OVERRIDES */
2995
2996
Dmitry Shmidt2f023192013-03-12 12:44:17 -07002997#ifdef CONFIG_VHT_OVERRIDES
2998void wpa_supplicant_apply_vht_overrides(
2999 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3000 struct wpa_driver_associate_params *params)
3001{
3002 struct ieee80211_vht_capabilities *vhtcaps;
3003 struct ieee80211_vht_capabilities *vhtcaps_mask;
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003004#ifdef CONFIG_HT_OVERRIDES
3005 int max_ampdu;
3006 const u32 max_ampdu_mask = VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX;
3007#endif /* CONFIG_HT_OVERRIDES */
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003008
3009 if (!ssid)
3010 return;
3011
3012 params->disable_vht = ssid->disable_vht;
3013
3014 vhtcaps = (void *) params->vhtcaps;
3015 vhtcaps_mask = (void *) params->vhtcaps_mask;
3016
3017 if (!vhtcaps || !vhtcaps_mask)
3018 return;
3019
3020 vhtcaps->vht_capabilities_info = ssid->vht_capa;
3021 vhtcaps_mask->vht_capabilities_info = ssid->vht_capa_mask;
3022
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07003023#ifdef CONFIG_HT_OVERRIDES
3024 /* if max ampdu is <= 3, we have to make the HT cap the same */
3025 if (ssid->vht_capa_mask & max_ampdu_mask) {
3026 max_ampdu = (ssid->vht_capa & max_ampdu_mask) >>
3027 find_first_bit(max_ampdu_mask);
3028
3029 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
3030 wpa_set_ampdu_factor(wpa_s,
3031 (void *) params->htcaps,
3032 (void *) params->htcaps_mask,
3033 max_ampdu);
3034 }
3035#endif /* CONFIG_HT_OVERRIDES */
3036
Dmitry Shmidt2f023192013-03-12 12:44:17 -07003037#define OVERRIDE_MCS(i) \
3038 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
3039 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
3040 3 << 2 * (i - 1); \
3041 vhtcaps->vht_supported_mcs_set.tx_map |= \
3042 ssid->vht_tx_mcs_nss_ ##i << 2 * (i - 1); \
3043 } \
3044 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
3045 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
3046 3 << 2 * (i - 1); \
3047 vhtcaps->vht_supported_mcs_set.rx_map |= \
3048 ssid->vht_rx_mcs_nss_ ##i << 2 * (i - 1); \
3049 }
3050
3051 OVERRIDE_MCS(1);
3052 OVERRIDE_MCS(2);
3053 OVERRIDE_MCS(3);
3054 OVERRIDE_MCS(4);
3055 OVERRIDE_MCS(5);
3056 OVERRIDE_MCS(6);
3057 OVERRIDE_MCS(7);
3058 OVERRIDE_MCS(8);
3059}
3060#endif /* CONFIG_VHT_OVERRIDES */
3061
3062
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
3064{
3065#ifdef PCSC_FUNCS
3066 size_t len;
3067
3068 if (!wpa_s->conf->pcsc_reader)
3069 return 0;
3070
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003071 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003072 if (!wpa_s->scard)
3073 return 1;
3074
3075 if (wpa_s->conf->pcsc_pin &&
3076 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
3077 scard_deinit(wpa_s->scard);
3078 wpa_s->scard = NULL;
3079 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
3080 return -1;
3081 }
3082
3083 len = sizeof(wpa_s->imsi) - 1;
3084 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
3085 scard_deinit(wpa_s->scard);
3086 wpa_s->scard = NULL;
3087 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
3088 return -1;
3089 }
3090 wpa_s->imsi[len] = '\0';
3091
3092 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
3093
3094 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
3095 wpa_s->imsi, wpa_s->mnc_len);
3096
3097 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
3098 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
3099#endif /* PCSC_FUNCS */
3100
3101 return 0;
3102}
3103
3104
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003105int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
3106{
3107 char *val, *pos;
3108
3109 ext_password_deinit(wpa_s->ext_pw);
3110 wpa_s->ext_pw = NULL;
3111 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
3112
3113 if (!wpa_s->conf->ext_password_backend)
3114 return 0;
3115
3116 val = os_strdup(wpa_s->conf->ext_password_backend);
3117 if (val == NULL)
3118 return -1;
3119 pos = os_strchr(val, ':');
3120 if (pos)
3121 *pos++ = '\0';
3122
3123 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
3124
3125 wpa_s->ext_pw = ext_password_init(val, pos);
3126 os_free(val);
3127 if (wpa_s->ext_pw == NULL) {
3128 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
3129 return -1;
3130 }
3131 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
3132
3133 return 0;
3134}
3135
3136
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003137static int wpas_check_wowlan_trigger(const char *start, const char *trigger,
3138 int capa_trigger, u8 *param_trigger)
3139{
3140 if (os_strcmp(start, trigger) != 0)
3141 return 0;
3142 if (!capa_trigger)
3143 return 0;
3144
3145 *param_trigger = 1;
3146 return 1;
3147}
3148
3149
3150int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
3151 struct wpa_driver_capa *capa)
3152{
3153 struct wowlan_triggers triggers;
3154 char *start, *end, *buf;
3155 int last, ret;
3156
3157 if (!wpa_s->conf->wowlan_triggers)
3158 return 0;
3159
3160 buf = os_strdup(wpa_s->conf->wowlan_triggers);
3161 if (buf == NULL)
3162 return -1;
3163
3164 os_memset(&triggers, 0, sizeof(triggers));
3165
3166#define CHECK_TRIGGER(trigger) \
3167 wpas_check_wowlan_trigger(start, #trigger, \
3168 capa->wowlan_triggers.trigger, \
3169 &triggers.trigger)
3170
3171 start = buf;
3172 while (*start != '\0') {
3173 while (isblank(*start))
3174 start++;
3175 if (*start == '\0')
3176 break;
3177 end = start;
3178 while (!isblank(*end) && *end != '\0')
3179 end++;
3180 last = *end == '\0';
3181 *end = '\0';
3182
3183 if (!CHECK_TRIGGER(any) &&
3184 !CHECK_TRIGGER(disconnect) &&
3185 !CHECK_TRIGGER(magic_pkt) &&
3186 !CHECK_TRIGGER(gtk_rekey_failure) &&
3187 !CHECK_TRIGGER(eap_identity_req) &&
3188 !CHECK_TRIGGER(four_way_handshake) &&
3189 !CHECK_TRIGGER(rfkill_release)) {
3190 wpa_printf(MSG_DEBUG,
3191 "Unknown/unsupported wowlan trigger '%s'",
3192 start);
3193 ret = -1;
3194 goto out;
3195 }
3196
3197 if (last)
3198 break;
3199 start = end + 1;
3200 }
3201#undef CHECK_TRIGGER
3202
3203 ret = wpa_drv_wowlan(wpa_s, &triggers);
3204out:
3205 os_free(buf);
3206 return ret;
3207}
3208
3209
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003210static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
3211 const char *rn)
3212{
3213 struct wpa_supplicant *iface = wpa_s->global->ifaces;
3214 struct wpa_radio *radio;
3215
3216 while (rn && iface) {
3217 radio = iface->radio;
3218 if (radio && os_strcmp(rn, radio->name) == 0) {
3219 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
3220 wpa_s->ifname, rn);
3221 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3222 return radio;
3223 }
Dmitry Shmidt3cf6f792013-12-18 13:12:19 -08003224
3225 iface = iface->next;
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003226 }
3227
3228 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
3229 wpa_s->ifname, rn ? rn : "N/A");
3230 radio = os_zalloc(sizeof(*radio));
3231 if (radio == NULL)
3232 return NULL;
3233
3234 if (rn)
3235 os_strlcpy(radio->name, rn, sizeof(radio->name));
3236 dl_list_init(&radio->ifaces);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003237 dl_list_init(&radio->work);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003238 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
3239
3240 return radio;
3241}
3242
3243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003244static void radio_work_free(struct wpa_radio_work *work)
3245{
3246 if (work->wpa_s->scan_work == work) {
3247 /* This should not really happen. */
3248 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
3249 work->type, work, work->started);
3250 work->wpa_s->scan_work = NULL;
3251 }
3252
3253#ifdef CONFIG_P2P
3254 if (work->wpa_s->p2p_scan_work == work) {
3255 /* This should not really happen. */
3256 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
3257 work->type, work, work->started);
3258 work->wpa_s->p2p_scan_work = NULL;
3259 }
3260#endif /* CONFIG_P2P */
3261
3262 dl_list_del(&work->list);
3263 os_free(work);
3264}
3265
3266
3267static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
3268{
3269 struct wpa_radio *radio = eloop_ctx;
3270 struct wpa_radio_work *work;
3271 struct os_reltime now, diff;
3272 struct wpa_supplicant *wpa_s;
3273
3274 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
3275 if (work == NULL)
3276 return;
3277
3278 if (work->started)
3279 return; /* already started and still in progress */
3280
3281 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
3282 radio_list);
3283 if (wpa_s && wpa_s->external_scan_running) {
3284 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
3285 return;
3286 }
3287
3288 os_get_reltime(&now);
3289 os_reltime_sub(&now, &work->time, &diff);
3290 wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting radio work '%s'@%p after %ld.%06ld second wait",
3291 work->type, work, diff.sec, diff.usec);
3292 work->started = 1;
3293 work->time = now;
3294 work->cb(work, 0);
3295}
3296
3297
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003298/*
3299 * This function removes both started and pending radio works running on
3300 * the provided interface's radio.
3301 * Prior to the removal of the radio work, its callback (cb) is called with
3302 * deinit set to be 1. Each work's callback is responsible for clearing its
3303 * internal data and restoring to a correct state.
3304 * @wpa_s: wpa_supplicant data
3305 * @type: type of works to be removed
3306 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
3307 * this interface's works.
3308 */
3309void radio_remove_works(struct wpa_supplicant *wpa_s,
3310 const char *type, int remove_all)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003311{
3312 struct wpa_radio_work *work, *tmp;
3313 struct wpa_radio *radio = wpa_s->radio;
3314
3315 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
3316 list) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003317 if (type && os_strcmp(type, work->type) != 0)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003318 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003319
3320 /* skip other ifaces' works */
3321 if (!remove_all && work->wpa_s != wpa_s)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003322 continue;
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003323
3324 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
3325 work->type, work, work->started ? " (started)" : "");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003326 work->cb(work, 1);
3327 radio_work_free(work);
3328 }
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08003329
3330 /* in case we removed the started work */
3331 radio_work_check_next(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003332}
3333
3334
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003335static void radio_remove_interface(struct wpa_supplicant *wpa_s)
3336{
3337 struct wpa_radio *radio = wpa_s->radio;
3338
3339 if (!radio)
3340 return;
3341
3342 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
3343 wpa_s->ifname, radio->name);
3344 dl_list_del(&wpa_s->radio_list);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003345 radio_remove_works(wpa_s, NULL, 0);
3346 wpa_s->radio = NULL;
3347 if (!dl_list_empty(&radio->ifaces))
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003348 return; /* Interfaces remain for this radio */
3349
3350 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003351 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003352 os_free(radio);
3353}
3354
3355
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003356void radio_work_check_next(struct wpa_supplicant *wpa_s)
3357{
3358 struct wpa_radio *radio = wpa_s->radio;
3359
3360 if (dl_list_empty(&radio->work))
3361 return;
3362 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
3363 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
3364}
3365
3366
3367/**
3368 * radio_add_work - Add a radio work item
3369 * @wpa_s: Pointer to wpa_supplicant data
3370 * @freq: Frequency of the offchannel operation in MHz or 0
3371 * @type: Unique identifier for each type of work
3372 * @next: Force as the next work to be executed
3373 * @cb: Callback function for indicating when radio is available
3374 * @ctx: Context pointer for the work (work->ctx in cb())
3375 * Returns: 0 on success, -1 on failure
3376 *
3377 * This function is used to request time for an operation that requires
3378 * exclusive radio control. Once the radio is available, the registered callback
3379 * function will be called. radio_work_done() must be called once the exclusive
3380 * radio operation has been completed, so that the radio is freed for other
3381 * operations. The special case of deinit=1 is used to free the context data
3382 * during interface removal. That does not allow the callback function to start
3383 * the radio operation, i.e., it must free any resources allocated for the radio
3384 * work and return.
3385 *
3386 * The @freq parameter can be used to indicate a single channel on which the
3387 * offchannel operation will occur. This may allow multiple radio work
3388 * operations to be performed in parallel if they apply for the same channel.
3389 * Setting this to 0 indicates that the work item may use multiple channels or
3390 * requires exclusive control of the radio.
3391 */
3392int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
3393 const char *type, int next,
3394 void (*cb)(struct wpa_radio_work *work, int deinit),
3395 void *ctx)
3396{
3397 struct wpa_radio_work *work;
3398 int was_empty;
3399
3400 work = os_zalloc(sizeof(*work));
3401 if (work == NULL)
3402 return -1;
3403 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
3404 os_get_reltime(&work->time);
3405 work->freq = freq;
3406 work->type = type;
3407 work->wpa_s = wpa_s;
3408 work->cb = cb;
3409 work->ctx = ctx;
3410
3411 was_empty = dl_list_empty(&wpa_s->radio->work);
3412 if (next)
3413 dl_list_add(&wpa_s->radio->work, &work->list);
3414 else
3415 dl_list_add_tail(&wpa_s->radio->work, &work->list);
3416 if (was_empty) {
3417 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
3418 radio_work_check_next(wpa_s);
3419 }
3420
3421 return 0;
3422}
3423
3424
3425/**
3426 * radio_work_done - Indicate that a radio work item has been completed
3427 * @work: Completed work
3428 *
3429 * This function is called once the callback function registered with
3430 * radio_add_work() has completed its work.
3431 */
3432void radio_work_done(struct wpa_radio_work *work)
3433{
3434 struct wpa_supplicant *wpa_s = work->wpa_s;
3435 struct os_reltime now, diff;
3436 unsigned int started = work->started;
3437
3438 os_get_reltime(&now);
3439 os_reltime_sub(&now, &work->time, &diff);
3440 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
3441 work->type, work, started ? "done" : "canceled",
3442 diff.sec, diff.usec);
3443 radio_work_free(work);
3444 if (started)
3445 radio_work_check_next(wpa_s);
3446}
3447
3448
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003449int radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
3450{
3451 struct wpa_radio_work *work;
3452 struct wpa_radio *radio = wpa_s->radio;
3453
3454 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
3455 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
3456 return 1;
3457 }
3458
3459 return 0;
3460}
3461
3462
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003463static int wpas_init_driver(struct wpa_supplicant *wpa_s,
3464 struct wpa_interface *iface)
3465{
3466 const char *ifname, *driver, *rn;
3467
3468 driver = iface->driver;
3469next_driver:
3470 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
3471 return -1;
3472
3473 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
3474 if (wpa_s->drv_priv == NULL) {
3475 const char *pos;
3476 pos = driver ? os_strchr(driver, ',') : NULL;
3477 if (pos) {
3478 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
3479 "driver interface - try next driver wrapper");
3480 driver = pos + 1;
3481 goto next_driver;
3482 }
3483 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
3484 "interface");
3485 return -1;
3486 }
3487 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
3488 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
3489 "driver_param '%s'", wpa_s->conf->driver_param);
3490 return -1;
3491 }
3492
3493 ifname = wpa_drv_get_ifname(wpa_s);
3494 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
3495 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
3496 "interface name with '%s'", ifname);
3497 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
3498 }
3499
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07003500 rn = wpa_driver_get_radio_name(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003501 if (rn && rn[0] == '\0')
3502 rn = NULL;
3503
3504 wpa_s->radio = radio_add_interface(wpa_s, rn);
3505 if (wpa_s->radio == NULL)
3506 return -1;
3507
3508 return 0;
3509}
3510
3511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
3513 struct wpa_interface *iface)
3514{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 struct wpa_driver_capa capa;
3516
3517 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
3518 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
3519 iface->confname ? iface->confname : "N/A",
3520 iface->driver ? iface->driver : "default",
3521 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
3522 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
3523
3524 if (iface->confname) {
3525#ifdef CONFIG_BACKEND_FILE
3526 wpa_s->confname = os_rel2abs_path(iface->confname);
3527 if (wpa_s->confname == NULL) {
3528 wpa_printf(MSG_ERROR, "Failed to get absolute path "
3529 "for configuration file '%s'.",
3530 iface->confname);
3531 return -1;
3532 }
3533 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
3534 iface->confname, wpa_s->confname);
3535#else /* CONFIG_BACKEND_FILE */
3536 wpa_s->confname = os_strdup(iface->confname);
3537#endif /* CONFIG_BACKEND_FILE */
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003538 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 if (wpa_s->conf == NULL) {
3540 wpa_printf(MSG_ERROR, "Failed to read or parse "
3541 "configuration '%s'.", wpa_s->confname);
3542 return -1;
3543 }
Dmitry Shmidt64f47c52013-04-16 10:41:54 -07003544 wpa_s->confanother = os_rel2abs_path(iface->confanother);
3545 wpa_config_read(wpa_s->confanother, wpa_s->conf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003547#ifdef CONFIG_P2P
3548 wpa_s->conf_p2p_dev = os_rel2abs_path(iface->conf_p2p_dev);
3549 wpa_config_read(wpa_s->conf_p2p_dev, wpa_s->conf);
3550#endif /* CONFIG_P2P */
3551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 /*
3553 * Override ctrl_interface and driver_param if set on command
3554 * line.
3555 */
3556 if (iface->ctrl_interface) {
3557 os_free(wpa_s->conf->ctrl_interface);
3558 wpa_s->conf->ctrl_interface =
3559 os_strdup(iface->ctrl_interface);
3560 }
3561
3562 if (iface->driver_param) {
3563 os_free(wpa_s->conf->driver_param);
3564 wpa_s->conf->driver_param =
3565 os_strdup(iface->driver_param);
3566 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003567
3568 if (iface->p2p_mgmt && !iface->ctrl_interface) {
3569 os_free(wpa_s->conf->ctrl_interface);
3570 wpa_s->conf->ctrl_interface = NULL;
3571 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003572 } else
3573 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
3574 iface->driver_param);
3575
3576 if (wpa_s->conf == NULL) {
3577 wpa_printf(MSG_ERROR, "\nNo configuration found.");
3578 return -1;
3579 }
3580
3581 if (iface->ifname == NULL) {
3582 wpa_printf(MSG_ERROR, "\nInterface name is required.");
3583 return -1;
3584 }
3585 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
3586 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
3587 iface->ifname);
3588 return -1;
3589 }
3590 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
3591
3592 if (iface->bridge_ifname) {
3593 if (os_strlen(iface->bridge_ifname) >=
3594 sizeof(wpa_s->bridge_ifname)) {
3595 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
3596 "name '%s'.", iface->bridge_ifname);
3597 return -1;
3598 }
3599 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
3600 sizeof(wpa_s->bridge_ifname));
3601 }
3602
3603 /* RSNA Supplicant Key Management - INITIALIZE */
3604 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
3605 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
3606
3607 /* Initialize driver interface and register driver event handler before
3608 * L2 receive handler so that association events are processed before
3609 * EAPOL-Key packets if both become available for the same select()
3610 * call. */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003611 if (wpas_init_driver(wpa_s, iface) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 return -1;
3613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 if (wpa_supplicant_init_wpa(wpa_s) < 0)
3615 return -1;
3616
3617 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
3618 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
3619 NULL);
3620 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
3621
3622 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
3623 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
3624 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
3625 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3626 "dot11RSNAConfigPMKLifetime");
3627 return -1;
3628 }
3629
3630 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
3631 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
3632 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
3633 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3634 "dot11RSNAConfigPMKReauthThreshold");
3635 return -1;
3636 }
3637
3638 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
3639 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
3640 wpa_s->conf->dot11RSNAConfigSATimeout)) {
3641 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
3642 "dot11RSNAConfigSATimeout");
3643 return -1;
3644 }
3645
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003646 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
3647 &wpa_s->hw.num_modes,
3648 &wpa_s->hw.flags);
3649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003651 wpa_s->drv_capa_known = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652 wpa_s->drv_flags = capa.flags;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003653 wpa_s->drv_enc = capa.enc;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003654 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 wpa_s->max_scan_ssids = capa.max_scan_ssids;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003656 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
3657 wpa_s->sched_scan_supported = capa.sched_scan_supported;
3658 wpa_s->max_match_sets = capa.max_match_sets;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
3660 wpa_s->max_stations = capa.max_stations;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003661 wpa_s->extended_capa = capa.extended_capa;
3662 wpa_s->extended_capa_mask = capa.extended_capa_mask;
3663 wpa_s->extended_capa_len = capa.extended_capa_len;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003664 wpa_s->num_multichan_concurrent =
3665 capa.num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 }
3667 if (wpa_s->max_remain_on_chan == 0)
3668 wpa_s->max_remain_on_chan = 1000;
3669
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003670 /*
3671 * Only take p2p_mgmt parameters when P2P Device is supported.
3672 * Doing it here as it determines whether l2_packet_init() will be done
3673 * during wpa_supplicant_driver_init().
3674 */
3675 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
3676 wpa_s->p2p_mgmt = iface->p2p_mgmt;
3677 else
3678 iface->p2p_mgmt = 1;
3679
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003680 if (wpa_s->num_multichan_concurrent == 0)
3681 wpa_s->num_multichan_concurrent = 1;
3682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683 if (wpa_supplicant_driver_init(wpa_s) < 0)
3684 return -1;
3685
3686#ifdef CONFIG_TDLS
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003687 if ((!iface->p2p_mgmt ||
3688 !(wpa_s->drv_flags &
3689 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3690 wpa_tdls_init(wpa_s->wpa))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 return -1;
3692#endif /* CONFIG_TDLS */
3693
3694 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
3695 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
3696 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
3697 return -1;
3698 }
3699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (wpas_wps_init(wpa_s))
3701 return -1;
3702
3703 if (wpa_supplicant_init_eapol(wpa_s) < 0)
3704 return -1;
3705 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3706
3707 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
3708 if (wpa_s->ctrl_iface == NULL) {
3709 wpa_printf(MSG_ERROR,
3710 "Failed to initialize control interface '%s'.\n"
3711 "You may have another wpa_supplicant process "
3712 "already running or the file was\n"
3713 "left by an unclean termination of wpa_supplicant "
3714 "in which case you will need\n"
3715 "to manually remove this file before starting "
3716 "wpa_supplicant again.\n",
3717 wpa_s->conf->ctrl_interface);
3718 return -1;
3719 }
3720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003721 wpa_s->gas = gas_query_init(wpa_s);
3722 if (wpa_s->gas == NULL) {
3723 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
3724 return -1;
3725 }
3726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003727#ifdef CONFIG_P2P
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003728 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003729 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
3730 return -1;
3731 }
3732#endif /* CONFIG_P2P */
3733
3734 if (wpa_bss_init(wpa_s) < 0)
3735 return -1;
3736
Dmitry Shmidtb58836e2014-04-29 14:35:56 -07003737 /*
3738 * Set Wake-on-WLAN triggers, if configured.
3739 * Note: We don't restore/remove the triggers on shutdown (it doesn't
3740 * have effect anyway when the interface is down).
3741 */
3742 if (wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
3743 return -1;
3744
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003745#ifdef CONFIG_EAP_PROXY
3746{
3747 size_t len;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003748 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
3749 &len);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003750 if (wpa_s->mnc_len > 0) {
3751 wpa_s->imsi[len] = '\0';
3752 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
3753 wpa_s->imsi, wpa_s->mnc_len);
3754 } else {
3755 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
3756 }
3757}
3758#endif /* CONFIG_EAP_PROXY */
3759
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 if (pcsc_reader_init(wpa_s) < 0)
3761 return -1;
3762
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003763 if (wpas_init_ext_pw(wpa_s) < 0)
3764 return -1;
3765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 return 0;
3767}
3768
3769
3770static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003771 int notify, int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772{
Dmitry Shmidtea69e842013-05-13 14:52:28 -07003773 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 if (wpa_s->drv_priv) {
3775 wpa_supplicant_deauthenticate(wpa_s,
3776 WLAN_REASON_DEAUTH_LEAVING);
3777
3778 wpa_drv_set_countermeasures(wpa_s, 0);
3779 wpa_clear_keys(wpa_s, NULL);
3780 }
3781
3782 wpa_supplicant_cleanup(wpa_s);
3783
Dmitry Shmidt04949592012-07-19 12:16:46 -07003784#ifdef CONFIG_P2P
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003785 if (wpa_s == wpa_s->parent)
3786 wpas_p2p_group_remove(wpa_s, "*");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003787 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
3788 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
3789 "the management interface is being removed");
3790 wpas_p2p_deinit_global(wpa_s->global);
3791 }
3792#endif /* CONFIG_P2P */
3793
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003794 wpas_ctrl_radio_work_flush(wpa_s);
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08003795 radio_remove_interface(wpa_s);
3796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797 if (wpa_s->drv_priv)
3798 wpa_drv_deinit(wpa_s);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003799
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003800 if (notify)
3801 wpas_notify_iface_removed(wpa_s);
3802
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003803 if (terminate)
3804 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003805
3806 if (wpa_s->ctrl_iface) {
3807 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
3808 wpa_s->ctrl_iface = NULL;
3809 }
3810
3811 if (wpa_s->conf != NULL) {
Irfan Sheriff622b66d2011-08-03 09:11:49 -07003812 wpa_config_free(wpa_s->conf);
3813 wpa_s->conf = NULL;
3814 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003815
3816 os_free(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817}
3818
3819
3820/**
3821 * wpa_supplicant_add_iface - Add a new network interface
3822 * @global: Pointer to global data from wpa_supplicant_init()
3823 * @iface: Interface configuration options
3824 * Returns: Pointer to the created interface or %NULL on failure
3825 *
3826 * This function is used to add new network interfaces for %wpa_supplicant.
3827 * This can be called before wpa_supplicant_run() to add interfaces before the
3828 * main event loop has been started. In addition, new interfaces can be added
3829 * dynamically while %wpa_supplicant is already running. This could happen,
3830 * e.g., when a hotplug network adapter is inserted.
3831 */
3832struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
3833 struct wpa_interface *iface)
3834{
3835 struct wpa_supplicant *wpa_s;
3836 struct wpa_interface t_iface;
3837 struct wpa_ssid *ssid;
3838
3839 if (global == NULL || iface == NULL)
3840 return NULL;
3841
3842 wpa_s = wpa_supplicant_alloc();
3843 if (wpa_s == NULL)
3844 return NULL;
3845
3846 wpa_s->global = global;
3847
3848 t_iface = *iface;
3849 if (global->params.override_driver) {
3850 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
3851 "('%s' -> '%s')",
3852 iface->driver, global->params.override_driver);
3853 t_iface.driver = global->params.override_driver;
3854 }
3855 if (global->params.override_ctrl_interface) {
3856 wpa_printf(MSG_DEBUG, "Override interface parameter: "
3857 "ctrl_interface ('%s' -> '%s')",
3858 iface->ctrl_interface,
3859 global->params.override_ctrl_interface);
3860 t_iface.ctrl_interface =
3861 global->params.override_ctrl_interface;
3862 }
3863 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
3864 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
3865 iface->ifname);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003866 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 return NULL;
3868 }
3869
3870 /* Notify the control interfaces about new iface */
3871 if (wpas_notify_iface_added(wpa_s)) {
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003872 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 return NULL;
3874 }
3875
3876 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3877 wpas_notify_network_added(wpa_s, ssid);
3878
3879 wpa_s->next = global->ifaces;
3880 global->ifaces = wpa_s;
3881
3882 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003883 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884
3885 return wpa_s;
3886}
3887
3888
3889/**
3890 * wpa_supplicant_remove_iface - Remove a network interface
3891 * @global: Pointer to global data from wpa_supplicant_init()
3892 * @wpa_s: Pointer to the network interface to be removed
3893 * Returns: 0 if interface was removed, -1 if interface was not found
3894 *
3895 * This function can be used to dynamically remove network interfaces from
3896 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
3897 * addition, this function is used to remove all remaining interfaces when
3898 * %wpa_supplicant is terminated.
3899 */
3900int wpa_supplicant_remove_iface(struct wpa_global *global,
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003901 struct wpa_supplicant *wpa_s,
3902 int terminate)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903{
3904 struct wpa_supplicant *prev;
3905
3906 /* Remove interface from the global list of interfaces */
3907 prev = global->ifaces;
3908 if (prev == wpa_s) {
3909 global->ifaces = wpa_s->next;
3910 } else {
3911 while (prev && prev->next != wpa_s)
3912 prev = prev->next;
3913 if (prev == NULL)
3914 return -1;
3915 prev->next = wpa_s->next;
3916 }
3917
3918 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
3919
3920 if (global->p2p_group_formation == wpa_s)
3921 global->p2p_group_formation = NULL;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003922 if (global->p2p_invite_group == wpa_s)
3923 global->p2p_invite_group = NULL;
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07003924 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925
3926 return 0;
3927}
3928
3929
3930/**
3931 * wpa_supplicant_get_eap_mode - Get the current EAP mode
3932 * @wpa_s: Pointer to the network interface
3933 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
3934 */
3935const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
3936{
3937 const char *eapol_method;
3938
3939 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
3940 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3941 return "NO-EAP";
3942 }
3943
3944 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
3945 if (eapol_method == NULL)
3946 return "UNKNOWN-EAP";
3947
3948 return eapol_method;
3949}
3950
3951
3952/**
3953 * wpa_supplicant_get_iface - Get a new network interface
3954 * @global: Pointer to global data from wpa_supplicant_init()
3955 * @ifname: Interface name
3956 * Returns: Pointer to the interface or %NULL if not found
3957 */
3958struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
3959 const char *ifname)
3960{
3961 struct wpa_supplicant *wpa_s;
3962
3963 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3964 if (os_strcmp(wpa_s->ifname, ifname) == 0)
3965 return wpa_s;
3966 }
3967 return NULL;
3968}
3969
3970
3971#ifndef CONFIG_NO_WPA_MSG
3972static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
3973{
3974 struct wpa_supplicant *wpa_s = ctx;
3975 if (wpa_s == NULL)
3976 return NULL;
3977 return wpa_s->ifname;
3978}
3979#endif /* CONFIG_NO_WPA_MSG */
3980
3981
3982/**
3983 * wpa_supplicant_init - Initialize %wpa_supplicant
3984 * @params: Parameters for %wpa_supplicant
3985 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
3986 *
3987 * This function is used to initialize %wpa_supplicant. After successful
3988 * initialization, the returned data pointer can be used to add and remove
3989 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
3990 */
3991struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
3992{
3993 struct wpa_global *global;
3994 int ret, i;
3995
3996 if (params == NULL)
3997 return NULL;
3998
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003999#ifdef CONFIG_DRIVER_NDIS
4000 {
4001 void driver_ndis_init_ops(void);
4002 driver_ndis_init_ops();
4003 }
4004#endif /* CONFIG_DRIVER_NDIS */
4005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006#ifndef CONFIG_NO_WPA_MSG
4007 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
4008#endif /* CONFIG_NO_WPA_MSG */
4009
4010 wpa_debug_open_file(params->wpa_debug_file_path);
4011 if (params->wpa_debug_syslog)
4012 wpa_debug_open_syslog();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004013 if (params->wpa_debug_tracing) {
4014 ret = wpa_debug_open_linux_tracing();
4015 if (ret) {
4016 wpa_printf(MSG_ERROR,
4017 "Failed to enable trace logging");
4018 return NULL;
4019 }
4020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021
4022 ret = eap_register_methods();
4023 if (ret) {
4024 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
4025 if (ret == -2)
4026 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
4027 "the same EAP type.");
4028 return NULL;
4029 }
4030
4031 global = os_zalloc(sizeof(*global));
4032 if (global == NULL)
4033 return NULL;
4034 dl_list_init(&global->p2p_srv_bonjour);
4035 dl_list_init(&global->p2p_srv_upnp);
4036 global->params.daemonize = params->daemonize;
4037 global->params.wait_for_monitor = params->wait_for_monitor;
4038 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
4039 if (params->pid_file)
4040 global->params.pid_file = os_strdup(params->pid_file);
4041 if (params->ctrl_interface)
4042 global->params.ctrl_interface =
4043 os_strdup(params->ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004044 if (params->ctrl_interface_group)
4045 global->params.ctrl_interface_group =
4046 os_strdup(params->ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 if (params->override_driver)
4048 global->params.override_driver =
4049 os_strdup(params->override_driver);
4050 if (params->override_ctrl_interface)
4051 global->params.override_ctrl_interface =
4052 os_strdup(params->override_ctrl_interface);
4053 wpa_debug_level = global->params.wpa_debug_level =
4054 params->wpa_debug_level;
4055 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
4056 params->wpa_debug_show_keys;
4057 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
4058 params->wpa_debug_timestamp;
4059
4060 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
4061
4062 if (eloop_init()) {
4063 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
4064 wpa_supplicant_deinit(global);
4065 return NULL;
4066 }
4067
Jouni Malinen75ecf522011-06-27 15:19:46 -07004068 random_init(params->entropy_file);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069
4070 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
4071 if (global->ctrl_iface == NULL) {
4072 wpa_supplicant_deinit(global);
4073 return NULL;
4074 }
4075
4076 if (wpas_notify_supplicant_initialized(global)) {
4077 wpa_supplicant_deinit(global);
4078 return NULL;
4079 }
4080
4081 for (i = 0; wpa_drivers[i]; i++)
4082 global->drv_count++;
4083 if (global->drv_count == 0) {
4084 wpa_printf(MSG_ERROR, "No drivers enabled");
4085 wpa_supplicant_deinit(global);
4086 return NULL;
4087 }
4088 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
4089 if (global->drv_priv == NULL) {
4090 wpa_supplicant_deinit(global);
4091 return NULL;
4092 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004094#ifdef CONFIG_WIFI_DISPLAY
4095 if (wifi_display_init(global) < 0) {
4096 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
4097 wpa_supplicant_deinit(global);
4098 return NULL;
4099 }
4100#endif /* CONFIG_WIFI_DISPLAY */
4101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 return global;
4103}
4104
4105
4106/**
4107 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
4108 * @global: Pointer to global data from wpa_supplicant_init()
4109 * Returns: 0 after successful event loop run, -1 on failure
4110 *
4111 * This function starts the main event loop and continues running as long as
4112 * there are any remaining events. In most cases, this function is running as
4113 * long as the %wpa_supplicant process in still in use.
4114 */
4115int wpa_supplicant_run(struct wpa_global *global)
4116{
4117 struct wpa_supplicant *wpa_s;
4118
4119 if (global->params.daemonize &&
4120 wpa_supplicant_daemon(global->params.pid_file))
4121 return -1;
4122
4123 if (global->params.wait_for_monitor) {
4124 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
4125 if (wpa_s->ctrl_iface)
4126 wpa_supplicant_ctrl_iface_wait(
4127 wpa_s->ctrl_iface);
4128 }
4129
4130 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
4131 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
4132
4133 eloop_run();
4134
4135 return 0;
4136}
4137
4138
4139/**
4140 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
4141 * @global: Pointer to global data from wpa_supplicant_init()
4142 *
4143 * This function is called to deinitialize %wpa_supplicant and to free all
4144 * allocated resources. Remaining network interfaces will also be removed.
4145 */
4146void wpa_supplicant_deinit(struct wpa_global *global)
4147{
4148 int i;
4149
4150 if (global == NULL)
4151 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004152
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004153#ifdef CONFIG_WIFI_DISPLAY
4154 wifi_display_deinit(global);
4155#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156
4157 while (global->ifaces)
Dmitry Shmidte15c7b52011-08-03 15:04:35 -07004158 wpa_supplicant_remove_iface(global, global->ifaces, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004159
4160 if (global->ctrl_iface)
4161 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
4162
4163 wpas_notify_supplicant_deinitialized(global);
4164
4165 eap_peer_unregister_methods();
4166#ifdef CONFIG_AP
4167 eap_server_unregister_methods();
4168#endif /* CONFIG_AP */
4169
4170 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
4171 if (!global->drv_priv[i])
4172 continue;
4173 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
4174 }
4175 os_free(global->drv_priv);
4176
4177 random_deinit();
4178
4179 eloop_destroy();
4180
4181 if (global->params.pid_file) {
4182 os_daemonize_terminate(global->params.pid_file);
4183 os_free(global->params.pid_file);
4184 }
4185 os_free(global->params.ctrl_interface);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004186 os_free(global->params.ctrl_interface_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 os_free(global->params.override_driver);
4188 os_free(global->params.override_ctrl_interface);
4189
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004190 os_free(global->p2p_disallow_freq.range);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004191 os_free(global->p2p_go_avoid_freq.range);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004192 os_free(global->add_psk);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194 os_free(global);
4195 wpa_debug_close_syslog();
4196 wpa_debug_close_file();
Dmitry Shmidt04949592012-07-19 12:16:46 -07004197 wpa_debug_close_linux_tracing();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198}
4199
4200
4201void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
4202{
4203 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4204 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4205 char country[3];
4206 country[0] = wpa_s->conf->country[0];
4207 country[1] = wpa_s->conf->country[1];
4208 country[2] = '\0';
4209 if (wpa_drv_set_country(wpa_s, country) < 0) {
4210 wpa_printf(MSG_ERROR, "Failed to set country code "
4211 "'%s'", country);
4212 }
4213 }
4214
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004215 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
4216 wpas_init_ext_pw(wpa_s);
4217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218#ifdef CONFIG_WPS
4219 wpas_wps_update_config(wpa_s);
4220#endif /* CONFIG_WPS */
4221
4222#ifdef CONFIG_P2P
4223 wpas_p2p_update_config(wpa_s);
4224#endif /* CONFIG_P2P */
4225
4226 wpa_s->conf->changed_parameters = 0;
4227}
4228
4229
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230static void add_freq(int *freqs, int *num_freqs, int freq)
4231{
4232 int i;
4233
4234 for (i = 0; i < *num_freqs; i++) {
4235 if (freqs[i] == freq)
4236 return;
4237 }
4238
4239 freqs[*num_freqs] = freq;
4240 (*num_freqs)++;
4241}
4242
4243
4244static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
4245{
4246 struct wpa_bss *bss, *cbss;
4247 const int max_freqs = 10;
4248 int *freqs;
4249 int num_freqs = 0;
4250
4251 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
4252 if (freqs == NULL)
4253 return NULL;
4254
4255 cbss = wpa_s->current_bss;
4256
4257 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
4258 if (bss == cbss)
4259 continue;
4260 if (bss->ssid_len == cbss->ssid_len &&
4261 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
4262 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
4263 add_freq(freqs, &num_freqs, bss->freq);
4264 if (num_freqs == max_freqs)
4265 break;
4266 }
4267 }
4268
4269 if (num_freqs == 0) {
4270 os_free(freqs);
4271 freqs = NULL;
4272 }
4273
4274 return freqs;
4275}
4276
4277
4278void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
4279{
4280 int timeout;
4281 int count;
4282 int *freqs = NULL;
4283
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004284 wpas_connect_work_done(wpa_s);
4285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 /*
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004287 * Remove possible authentication timeout since the connection failed.
4288 */
4289 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
4290
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004291 if (wpa_s->disconnected) {
4292 /*
4293 * There is no point in blacklisting the AP if this event is
4294 * generated based on local request to disconnect.
4295 */
4296 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
4297 "indication since interface has been put into "
4298 "disconnected state");
4299 return;
4300 }
4301
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004302 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 * Add the failed BSSID into the blacklist and speed up next scan
4304 * attempt if there could be other APs that could accept association.
4305 * The current blacklist count indicates how many times we have tried
4306 * connecting to this AP and multiple attempts mean that other APs are
4307 * either not available or has already been tried, so that we can start
4308 * increasing the delay here to avoid constant scanning.
4309 */
4310 count = wpa_blacklist_add(wpa_s, bssid);
4311 if (count == 1 && wpa_s->current_bss) {
4312 /*
4313 * This BSS was not in the blacklist before. If there is
4314 * another BSS available for the same ESS, we should try that
4315 * next. Otherwise, we may as well try this one once more
4316 * before allowing other, likely worse, ESSes to be considered.
4317 */
4318 freqs = get_bss_freqs_in_ess(wpa_s);
4319 if (freqs) {
4320 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
4321 "has been seen; try it next");
4322 wpa_blacklist_add(wpa_s, bssid);
4323 /*
4324 * On the next scan, go through only the known channels
4325 * used in this ESS based on previous scans to speed up
4326 * common load balancing use case.
4327 */
4328 os_free(wpa_s->next_scan_freqs);
4329 wpa_s->next_scan_freqs = freqs;
4330 }
4331 }
4332
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004333 /*
4334 * Add previous failure count in case the temporary blacklist was
4335 * cleared due to no other BSSes being available.
4336 */
4337 count += wpa_s->extra_blacklist_count;
4338
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004339 if (count > 3 && wpa_s->current_ssid) {
4340 wpa_printf(MSG_DEBUG, "Continuous association failures - "
4341 "consider temporary network disabling");
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004342 wpas_auth_failed(wpa_s, "CONN_FAILED");
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004343 }
4344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345 switch (count) {
4346 case 1:
4347 timeout = 100;
4348 break;
4349 case 2:
4350 timeout = 500;
4351 break;
4352 case 3:
4353 timeout = 1000;
4354 break;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004355 case 4:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356 timeout = 5000;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004357 break;
4358 default:
4359 timeout = 10000;
4360 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 }
4362
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004363 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
4364 "ms", count, timeout);
4365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 /*
4367 * TODO: if more than one possible AP is available in scan results,
4368 * could try the other ones before requesting a new scan.
4369 */
4370 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
4371 1000 * (timeout % 1000));
4372}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004373
4374
4375int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
4376{
4377 return wpa_s->conf->ap_scan == 2 ||
4378 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
4379}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004380
Dmitry Shmidt04949592012-07-19 12:16:46 -07004381
4382#if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
4383int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
4384 struct wpa_ssid *ssid,
4385 const char *field,
4386 const char *value)
4387{
4388#ifdef IEEE8021X_EAPOL
4389 struct eap_peer_config *eap = &ssid->eap;
4390
4391 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
4392 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
4393 (const u8 *) value, os_strlen(value));
4394
4395 switch (wpa_supplicant_ctrl_req_from_string(field)) {
4396 case WPA_CTRL_REQ_EAP_IDENTITY:
4397 os_free(eap->identity);
4398 eap->identity = (u8 *) os_strdup(value);
4399 eap->identity_len = os_strlen(value);
4400 eap->pending_req_identity = 0;
4401 if (ssid == wpa_s->current_ssid)
4402 wpa_s->reassociate = 1;
4403 break;
4404 case WPA_CTRL_REQ_EAP_PASSWORD:
4405 os_free(eap->password);
4406 eap->password = (u8 *) os_strdup(value);
4407 eap->password_len = os_strlen(value);
4408 eap->pending_req_password = 0;
4409 if (ssid == wpa_s->current_ssid)
4410 wpa_s->reassociate = 1;
4411 break;
4412 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
4413 os_free(eap->new_password);
4414 eap->new_password = (u8 *) os_strdup(value);
4415 eap->new_password_len = os_strlen(value);
4416 eap->pending_req_new_password = 0;
4417 if (ssid == wpa_s->current_ssid)
4418 wpa_s->reassociate = 1;
4419 break;
4420 case WPA_CTRL_REQ_EAP_PIN:
4421 os_free(eap->pin);
4422 eap->pin = os_strdup(value);
4423 eap->pending_req_pin = 0;
4424 if (ssid == wpa_s->current_ssid)
4425 wpa_s->reassociate = 1;
4426 break;
4427 case WPA_CTRL_REQ_EAP_OTP:
4428 os_free(eap->otp);
4429 eap->otp = (u8 *) os_strdup(value);
4430 eap->otp_len = os_strlen(value);
4431 os_free(eap->pending_req_otp);
4432 eap->pending_req_otp = NULL;
4433 eap->pending_req_otp_len = 0;
4434 break;
4435 case WPA_CTRL_REQ_EAP_PASSPHRASE:
4436 os_free(eap->private_key_passwd);
4437 eap->private_key_passwd = (u8 *) os_strdup(value);
4438 eap->pending_req_passphrase = 0;
4439 if (ssid == wpa_s->current_ssid)
4440 wpa_s->reassociate = 1;
4441 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07004442 case WPA_CTRL_REQ_SIM:
4443 os_free(eap->external_sim_resp);
4444 eap->external_sim_resp = os_strdup(value);
4445 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004446 default:
4447 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
4448 return -1;
4449 }
4450
4451 return 0;
4452#else /* IEEE8021X_EAPOL */
4453 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
4454 return -1;
4455#endif /* IEEE8021X_EAPOL */
4456}
4457#endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
4458
4459
4460int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
4461{
4462 int i;
4463 unsigned int drv_enc;
4464
4465 if (ssid == NULL)
4466 return 1;
4467
4468 if (ssid->disabled)
4469 return 1;
4470
4471 if (wpa_s && wpa_s->drv_capa_known)
4472 drv_enc = wpa_s->drv_enc;
4473 else
4474 drv_enc = (unsigned int) -1;
4475
4476 for (i = 0; i < NUM_WEP_KEYS; i++) {
4477 size_t len = ssid->wep_key_len[i];
4478 if (len == 0)
4479 continue;
4480 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
4481 continue;
4482 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
4483 continue;
4484 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
4485 continue;
4486 return 1; /* invalid WEP key */
4487 }
4488
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004489 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
Dmitry Shmidt7dba0e52014-04-14 10:49:15 -07004490 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004491 return 1;
4492
Dmitry Shmidt04949592012-07-19 12:16:46 -07004493 return 0;
4494}
4495
4496
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004497int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004498{
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004499 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004500 return 1;
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07004501 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004502 return 0;
Dmitry Shmidt687922c2012-03-26 14:02:32 -07004503 return -1;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004504}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004505
4506
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004507void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004508{
4509 struct wpa_ssid *ssid = wpa_s->current_ssid;
4510 int dur;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004511 struct os_reltime now;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004512
4513 if (ssid == NULL) {
4514 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
4515 "SSID block");
4516 return;
4517 }
4518
4519 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
4520 return;
4521
4522 ssid->auth_failures++;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07004523
4524#ifdef CONFIG_P2P
4525 if (ssid->p2p_group &&
4526 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
4527 /*
4528 * Skip the wait time since there is a short timeout on the
4529 * connection to a P2P group.
4530 */
4531 return;
4532 }
4533#endif /* CONFIG_P2P */
4534
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004535 if (ssid->auth_failures > 50)
4536 dur = 300;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004537 else if (ssid->auth_failures > 10)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004538 dur = 120;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004539 else if (ssid->auth_failures > 5)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004540 dur = 90;
4541 else if (ssid->auth_failures > 3)
4542 dur = 60;
4543 else if (ssid->auth_failures > 2)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004544 dur = 30;
4545 else if (ssid->auth_failures > 1)
4546 dur = 20;
4547 else
4548 dur = 10;
4549
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08004550 if (ssid->auth_failures > 1 &&
4551 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
4552 dur += os_random() % (ssid->auth_failures * 10);
4553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004554 os_get_reltime(&now);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004555 if (now.sec + dur <= ssid->disabled_until.sec)
4556 return;
4557
4558 ssid->disabled_until.sec = now.sec + dur;
4559
4560 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004561 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004562 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004563 ssid->auth_failures, dur, reason);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004564}
4565
4566
4567void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
4568 struct wpa_ssid *ssid, int clear_failures)
4569{
4570 if (ssid == NULL)
4571 return;
4572
4573 if (ssid->disabled_until.sec) {
4574 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
4575 "id=%d ssid=\"%s\"",
4576 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
4577 }
4578 ssid->disabled_until.sec = 0;
4579 ssid->disabled_until.usec = 0;
4580 if (clear_failures)
4581 ssid->auth_failures = 0;
4582}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004583
4584
4585int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
4586{
4587 size_t i;
4588
4589 if (wpa_s->disallow_aps_bssid == NULL)
4590 return 0;
4591
4592 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
4593 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
4594 bssid, ETH_ALEN) == 0)
4595 return 1;
4596 }
4597
4598 return 0;
4599}
4600
4601
4602int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
4603 size_t ssid_len)
4604{
4605 size_t i;
4606
4607 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
4608 return 0;
4609
4610 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
4611 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
4612 if (ssid_len == s->ssid_len &&
4613 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4614 return 1;
4615 }
4616
4617 return 0;
4618}
4619
4620
4621/**
4622 * wpas_request_connection - Request a new connection
4623 * @wpa_s: Pointer to the network interface
4624 *
4625 * This function is used to request a new connection to be found. It will mark
4626 * the interface to allow reassociation and request a new scan to find a
4627 * suitable network to connect to.
4628 */
4629void wpas_request_connection(struct wpa_supplicant *wpa_s)
4630{
4631 wpa_s->normal_scans = 0;
4632 wpa_supplicant_reinit_autoscan(wpa_s);
4633 wpa_s->extra_blacklist_count = 0;
4634 wpa_s->disconnected = 0;
4635 wpa_s->reassociate = 1;
Dmitry Shmidt2f3b8de2013-03-01 09:32:50 -08004636
4637 if (wpa_supplicant_fast_associate(wpa_s) != 1)
4638 wpa_supplicant_req_scan(wpa_s, 0, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004639}
Dmitry Shmidtea69e842013-05-13 14:52:28 -07004640
4641
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004642void dump_freq_array(struct wpa_supplicant *wpa_s, const char *title,
4643 int *freq_array, unsigned int len)
4644{
4645 unsigned int i;
4646
4647 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
4648 len, title);
4649 for (i = 0; i < len; i++)
4650 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d", i, freq_array[i]);
4651}
4652
4653
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004654/*
4655 * Find the operating frequencies of any of the virtual interfaces that
4656 * are using the same radio as the current interface.
4657 */
4658int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
4659 int *freq_array, unsigned int len)
4660{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004661 struct wpa_supplicant *ifs;
4662 u8 bssid[ETH_ALEN];
4663 int freq;
4664 unsigned int idx = 0, i;
4665
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004666 wpa_dbg(wpa_s, MSG_DEBUG,
4667 "Determining shared radio frequencies (max len %u)", len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004668 os_memset(freq_array, 0, sizeof(int) * len);
4669
4670 /* First add the frequency of the local interface */
4671 if (wpa_s->current_ssid != NULL && wpa_s->assoc_freq != 0) {
4672 if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
4673 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)
4674 freq_array[idx++] = wpa_s->current_ssid->frequency;
4675 else if (wpa_drv_get_bssid(wpa_s, bssid) == 0)
4676 freq_array[idx++] = wpa_s->assoc_freq;
4677 }
4678
4679 /* If get_radio_name is not supported, use only the local freq */
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07004680 if (!wpa_driver_get_radio_name(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004681 freq = wpa_drv_shared_freq(wpa_s);
4682 if (freq > 0 && idx < len &&
4683 (idx == 0 || freq_array[0] != freq))
4684 freq_array[idx++] = freq;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004685 dump_freq_array(wpa_s, "No get_radio_name", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004686 return idx;
4687 }
4688
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08004689 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
4690 radio_list) {
4691 if (wpa_s == ifs)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004692 continue;
4693
4694 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
4695 continue;
4696
4697 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
4698 ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
4699 freq = ifs->current_ssid->frequency;
4700 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
4701 freq = ifs->assoc_freq;
4702 else
4703 continue;
4704
4705 /* Hold only distinct freqs */
4706 for (i = 0; i < idx; i++)
4707 if (freq_array[i] == freq)
4708 break;
4709
4710 if (i == idx)
4711 freq_array[idx++] = freq;
4712 }
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004713
4714 dump_freq_array(wpa_s, "completed iteration", freq_array, idx);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004715 return idx;
4716}