]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wpa_supplicant.c
Fix SELECT_NETWORK freq parameter
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
1 /*
2 * WPA Supplicant
3 * Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 *
8 * This file implements functions for registering and unregistering
9 * %wpa_supplicant interfaces. In addition, this file contains number of
10 * functions for managing network connections.
11 */
12
13 #include "includes.h"
14 #ifdef CONFIG_MATCH_IFACE
15 #include <net/if.h>
16 #include <fnmatch.h>
17 #endif /* CONFIG_MATCH_IFACE */
18
19 #include "common.h"
20 #include "crypto/random.h"
21 #include "crypto/sha1.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "eap_peer/eap.h"
24 #include "eap_peer/eap_proxy.h"
25 #include "eap_server/eap_methods.h"
26 #include "rsn_supp/wpa.h"
27 #include "eloop.h"
28 #include "config.h"
29 #include "utils/ext_password.h"
30 #include "l2_packet/l2_packet.h"
31 #include "wpa_supplicant_i.h"
32 #include "driver_i.h"
33 #include "ctrl_iface.h"
34 #include "pcsc_funcs.h"
35 #include "common/version.h"
36 #include "rsn_supp/preauth.h"
37 #include "rsn_supp/pmksa_cache.h"
38 #include "common/wpa_ctrl.h"
39 #include "common/ieee802_11_defs.h"
40 #include "common/hw_features_common.h"
41 #include "p2p/p2p.h"
42 #include "fst/fst.h"
43 #include "blacklist.h"
44 #include "wpas_glue.h"
45 #include "wps_supplicant.h"
46 #include "ibss_rsn.h"
47 #include "sme.h"
48 #include "gas_query.h"
49 #include "ap.h"
50 #include "p2p_supplicant.h"
51 #include "wifi_display.h"
52 #include "notify.h"
53 #include "bgscan.h"
54 #include "autoscan.h"
55 #include "bss.h"
56 #include "scan.h"
57 #include "offchannel.h"
58 #include "hs20_supplicant.h"
59 #include "wnm_sta.h"
60 #include "wpas_kay.h"
61 #include "mesh.h"
62
63 const char *const wpa_supplicant_version =
64 "wpa_supplicant v" VERSION_STR "\n"
65 "Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi> and contributors";
66
67 const char *const wpa_supplicant_license =
68 "This software may be distributed under the terms of the BSD license.\n"
69 "See README for more details.\n"
70 #ifdef EAP_TLS_OPENSSL
71 "\nThis product includes software developed by the OpenSSL Project\n"
72 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
73 #endif /* EAP_TLS_OPENSSL */
74 ;
75
76 #ifndef CONFIG_NO_STDOUT_DEBUG
77 /* Long text divided into parts in order to fit in C89 strings size limits. */
78 const char *const wpa_supplicant_full_license1 =
79 "";
80 const char *const wpa_supplicant_full_license2 =
81 "This software may be distributed under the terms of the BSD license.\n"
82 "\n"
83 "Redistribution and use in source and binary forms, with or without\n"
84 "modification, are permitted provided that the following conditions are\n"
85 "met:\n"
86 "\n";
87 const char *const wpa_supplicant_full_license3 =
88 "1. Redistributions of source code must retain the above copyright\n"
89 " notice, this list of conditions and the following disclaimer.\n"
90 "\n"
91 "2. Redistributions in binary form must reproduce the above copyright\n"
92 " notice, this list of conditions and the following disclaimer in the\n"
93 " documentation and/or other materials provided with the distribution.\n"
94 "\n";
95 const char *const wpa_supplicant_full_license4 =
96 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
97 " names of its contributors may be used to endorse or promote products\n"
98 " derived from this software without specific prior written permission.\n"
99 "\n"
100 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
101 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
102 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
103 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
104 const char *const wpa_supplicant_full_license5 =
105 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
106 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
107 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
108 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
109 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
110 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
111 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
112 "\n";
113 #endif /* CONFIG_NO_STDOUT_DEBUG */
114
115 /* Configure default/group WEP keys for static WEP */
116 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
117 {
118 int i, set = 0;
119
120 for (i = 0; i < NUM_WEP_KEYS; i++) {
121 if (ssid->wep_key_len[i] == 0)
122 continue;
123
124 set = 1;
125 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
126 i, i == ssid->wep_tx_keyidx, NULL, 0,
127 ssid->wep_key[i], ssid->wep_key_len[i]);
128 }
129
130 return set;
131 }
132
133
134 int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
135 struct wpa_ssid *ssid)
136 {
137 u8 key[32];
138 size_t keylen;
139 enum wpa_alg alg;
140 u8 seq[6] = { 0 };
141 int ret;
142
143 /* IBSS/WPA-None uses only one key (Group) for both receiving and
144 * sending unicast and multicast packets. */
145
146 if (ssid->mode != WPAS_MODE_IBSS) {
147 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
148 "IBSS/ad-hoc) for WPA-None", ssid->mode);
149 return -1;
150 }
151
152 if (!ssid->psk_set) {
153 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
154 "WPA-None");
155 return -1;
156 }
157
158 switch (wpa_s->group_cipher) {
159 case WPA_CIPHER_CCMP:
160 os_memcpy(key, ssid->psk, 16);
161 keylen = 16;
162 alg = WPA_ALG_CCMP;
163 break;
164 case WPA_CIPHER_GCMP:
165 os_memcpy(key, ssid->psk, 16);
166 keylen = 16;
167 alg = WPA_ALG_GCMP;
168 break;
169 case WPA_CIPHER_TKIP:
170 /* WPA-None uses the same Michael MIC key for both TX and RX */
171 os_memcpy(key, ssid->psk, 16 + 8);
172 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
173 keylen = 32;
174 alg = WPA_ALG_TKIP;
175 break;
176 default:
177 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
178 "WPA-None", wpa_s->group_cipher);
179 return -1;
180 }
181
182 /* TODO: should actually remember the previously used seq#, both for TX
183 * and RX from each STA.. */
184
185 ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
186 os_memset(key, 0, sizeof(key));
187 return ret;
188 }
189
190
191 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
192 {
193 struct wpa_supplicant *wpa_s = eloop_ctx;
194 const u8 *bssid = wpa_s->bssid;
195 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
196 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
197 wpa_s->wpa_state == WPA_ASSOCIATING))
198 bssid = wpa_s->pending_bssid;
199 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
200 MAC2STR(bssid));
201 wpa_blacklist_add(wpa_s, bssid);
202 wpa_sm_notify_disassoc(wpa_s->wpa);
203 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
204 wpa_s->reassociate = 1;
205
206 /*
207 * If we timed out, the AP or the local radio may be busy.
208 * So, wait a second until scanning again.
209 */
210 wpa_supplicant_req_scan(wpa_s, 1, 0);
211 }
212
213
214 /**
215 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
216 * @wpa_s: Pointer to wpa_supplicant data
217 * @sec: Number of seconds after which to time out authentication
218 * @usec: Number of microseconds after which to time out authentication
219 *
220 * This function is used to schedule a timeout for the current authentication
221 * attempt.
222 */
223 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
224 int sec, int usec)
225 {
226 if (wpa_s->conf->ap_scan == 0 &&
227 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
228 return;
229
230 wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
231 "%d usec", sec, usec);
232 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
233 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
234 }
235
236
237 /**
238 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
239 * @wpa_s: Pointer to wpa_supplicant data
240 *
241 * This function is used to cancel authentication timeout scheduled with
242 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
243 * been completed.
244 */
245 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
246 {
247 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
248 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
249 wpa_blacklist_del(wpa_s, wpa_s->bssid);
250 }
251
252
253 /**
254 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
255 * @wpa_s: Pointer to wpa_supplicant data
256 *
257 * This function is used to configure EAPOL state machine based on the selected
258 * authentication mode.
259 */
260 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
261 {
262 #ifdef IEEE8021X_EAPOL
263 struct eapol_config eapol_conf;
264 struct wpa_ssid *ssid = wpa_s->current_ssid;
265
266 #ifdef CONFIG_IBSS_RSN
267 if (ssid->mode == WPAS_MODE_IBSS &&
268 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
269 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
270 /*
271 * RSN IBSS authentication is per-STA and we can disable the
272 * per-BSSID EAPOL authentication.
273 */
274 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
275 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
276 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
277 return;
278 }
279 #endif /* CONFIG_IBSS_RSN */
280
281 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
282 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
283
284 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
285 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
286 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
287 else
288 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
289
290 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
291 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
292 eapol_conf.accept_802_1x_keys = 1;
293 eapol_conf.required_keys = 0;
294 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
295 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
296 }
297 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
298 eapol_conf.required_keys |=
299 EAPOL_REQUIRE_KEY_BROADCAST;
300 }
301
302 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
303 eapol_conf.required_keys = 0;
304 }
305 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
306 eapol_conf.workaround = ssid->eap_workaround;
307 eapol_conf.eap_disabled =
308 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
309 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
310 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
311 eapol_conf.external_sim = wpa_s->conf->external_sim;
312
313 #ifdef CONFIG_WPS
314 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
315 eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
316 if (wpa_s->current_bss) {
317 struct wpabuf *ie;
318 ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
319 WPS_IE_VENDOR_TYPE);
320 if (ie) {
321 if (wps_is_20(ie))
322 eapol_conf.wps |=
323 EAPOL_PEER_IS_WPS20_AP;
324 wpabuf_free(ie);
325 }
326 }
327 }
328 #endif /* CONFIG_WPS */
329
330 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
331
332 #ifdef CONFIG_MACSEC
333 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE && ssid->mka_psk_set)
334 ieee802_1x_create_preshared_mka(wpa_s, ssid);
335 else
336 ieee802_1x_alloc_kay_sm(wpa_s, ssid);
337 #endif /* CONFIG_MACSEC */
338 #endif /* IEEE8021X_EAPOL */
339 }
340
341
342 /**
343 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
344 * @wpa_s: Pointer to wpa_supplicant data
345 * @ssid: Configuration data for the network
346 *
347 * This function is used to configure WPA state machine and related parameters
348 * to a mode where WPA is not enabled. This is called as part of the
349 * authentication configuration when the selected network does not use WPA.
350 */
351 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
352 struct wpa_ssid *ssid)
353 {
354 int i;
355
356 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
357 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
358 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
359 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
360 else
361 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
362 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
363 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
364 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
365 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
366 wpa_s->group_cipher = WPA_CIPHER_NONE;
367 wpa_s->mgmt_group_cipher = 0;
368
369 for (i = 0; i < NUM_WEP_KEYS; i++) {
370 if (ssid->wep_key_len[i] > 5) {
371 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
372 wpa_s->group_cipher = WPA_CIPHER_WEP104;
373 break;
374 } else if (ssid->wep_key_len[i] > 0) {
375 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
376 wpa_s->group_cipher = WPA_CIPHER_WEP40;
377 break;
378 }
379 }
380
381 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
382 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
383 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
384 wpa_s->pairwise_cipher);
385 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
386 #ifdef CONFIG_IEEE80211W
387 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
388 wpa_s->mgmt_group_cipher);
389 #endif /* CONFIG_IEEE80211W */
390
391 pmksa_cache_clear_current(wpa_s->wpa);
392 }
393
394
395 void free_hw_features(struct wpa_supplicant *wpa_s)
396 {
397 int i;
398 if (wpa_s->hw.modes == NULL)
399 return;
400
401 for (i = 0; i < wpa_s->hw.num_modes; i++) {
402 os_free(wpa_s->hw.modes[i].channels);
403 os_free(wpa_s->hw.modes[i].rates);
404 }
405
406 os_free(wpa_s->hw.modes);
407 wpa_s->hw.modes = NULL;
408 }
409
410
411 static void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
412 {
413 struct wpa_bss_tmp_disallowed *bss, *prev;
414
415 dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
416 struct wpa_bss_tmp_disallowed, list) {
417 dl_list_del(&bss->list);
418 os_free(bss);
419 }
420 }
421
422
423 void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
424 {
425 struct fils_hlp_req *req;
426
427 while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
428 list)) != NULL) {
429 dl_list_del(&req->list);
430 wpabuf_free(req->pkt);
431 os_free(req);
432 }
433 }
434
435
436 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
437 {
438 int i;
439
440 bgscan_deinit(wpa_s);
441 autoscan_deinit(wpa_s);
442 scard_deinit(wpa_s->scard);
443 wpa_s->scard = NULL;
444 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
445 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
446 l2_packet_deinit(wpa_s->l2);
447 wpa_s->l2 = NULL;
448 if (wpa_s->l2_br) {
449 l2_packet_deinit(wpa_s->l2_br);
450 wpa_s->l2_br = NULL;
451 }
452 #ifdef CONFIG_TESTING_OPTIONS
453 l2_packet_deinit(wpa_s->l2_test);
454 wpa_s->l2_test = NULL;
455 os_free(wpa_s->get_pref_freq_list_override);
456 wpa_s->get_pref_freq_list_override = NULL;
457 #endif /* CONFIG_TESTING_OPTIONS */
458
459 if (wpa_s->conf != NULL) {
460 struct wpa_ssid *ssid;
461 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
462 wpas_notify_network_removed(wpa_s, ssid);
463 }
464
465 os_free(wpa_s->confname);
466 wpa_s->confname = NULL;
467
468 os_free(wpa_s->confanother);
469 wpa_s->confanother = NULL;
470
471 wpa_sm_set_eapol(wpa_s->wpa, NULL);
472 eapol_sm_deinit(wpa_s->eapol);
473 wpa_s->eapol = NULL;
474
475 rsn_preauth_deinit(wpa_s->wpa);
476
477 #ifdef CONFIG_TDLS
478 wpa_tdls_deinit(wpa_s->wpa);
479 #endif /* CONFIG_TDLS */
480
481 wmm_ac_clear_saved_tspecs(wpa_s);
482 pmksa_candidate_free(wpa_s->wpa);
483 wpa_sm_deinit(wpa_s->wpa);
484 wpa_s->wpa = NULL;
485 wpa_blacklist_clear(wpa_s);
486
487 wpa_bss_deinit(wpa_s);
488
489 wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
490 wpa_supplicant_cancel_scan(wpa_s);
491 wpa_supplicant_cancel_auth_timeout(wpa_s);
492 eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
493 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
494 eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
495 wpa_s, NULL);
496 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
497
498 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
499
500 wpas_wps_deinit(wpa_s);
501
502 wpabuf_free(wpa_s->pending_eapol_rx);
503 wpa_s->pending_eapol_rx = NULL;
504
505 #ifdef CONFIG_IBSS_RSN
506 ibss_rsn_deinit(wpa_s->ibss_rsn);
507 wpa_s->ibss_rsn = NULL;
508 #endif /* CONFIG_IBSS_RSN */
509
510 sme_deinit(wpa_s);
511
512 #ifdef CONFIG_AP
513 wpa_supplicant_ap_deinit(wpa_s);
514 #endif /* CONFIG_AP */
515
516 wpas_p2p_deinit(wpa_s);
517
518 #ifdef CONFIG_OFFCHANNEL
519 offchannel_deinit(wpa_s);
520 #endif /* CONFIG_OFFCHANNEL */
521
522 wpa_supplicant_cancel_sched_scan(wpa_s);
523
524 os_free(wpa_s->next_scan_freqs);
525 wpa_s->next_scan_freqs = NULL;
526
527 os_free(wpa_s->manual_scan_freqs);
528 wpa_s->manual_scan_freqs = NULL;
529 os_free(wpa_s->select_network_scan_freqs);
530 wpa_s->select_network_scan_freqs = NULL;
531
532 os_free(wpa_s->manual_sched_scan_freqs);
533 wpa_s->manual_sched_scan_freqs = NULL;
534
535 wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
536
537 /*
538 * Need to remove any pending gas-query radio work before the
539 * gas_query_deinit() call because gas_query::work has not yet been set
540 * for works that have not been started. gas_query_free() will be unable
541 * to cancel such pending radio works and once the pending gas-query
542 * radio work eventually gets removed, the deinit notification call to
543 * gas_query_start_cb() would result in dereferencing freed memory.
544 */
545 if (wpa_s->radio)
546 radio_remove_works(wpa_s, "gas-query", 0);
547 gas_query_deinit(wpa_s->gas);
548 wpa_s->gas = NULL;
549
550 free_hw_features(wpa_s);
551
552 ieee802_1x_dealloc_kay_sm(wpa_s);
553
554 os_free(wpa_s->bssid_filter);
555 wpa_s->bssid_filter = NULL;
556
557 os_free(wpa_s->disallow_aps_bssid);
558 wpa_s->disallow_aps_bssid = NULL;
559 os_free(wpa_s->disallow_aps_ssid);
560 wpa_s->disallow_aps_ssid = NULL;
561
562 wnm_bss_keep_alive_deinit(wpa_s);
563 #ifdef CONFIG_WNM
564 wnm_deallocate_memory(wpa_s);
565 #endif /* CONFIG_WNM */
566
567 ext_password_deinit(wpa_s->ext_pw);
568 wpa_s->ext_pw = NULL;
569
570 wpabuf_free(wpa_s->last_gas_resp);
571 wpa_s->last_gas_resp = NULL;
572 wpabuf_free(wpa_s->prev_gas_resp);
573 wpa_s->prev_gas_resp = NULL;
574
575 os_free(wpa_s->last_scan_res);
576 wpa_s->last_scan_res = NULL;
577
578 #ifdef CONFIG_HS20
579 if (wpa_s->drv_priv)
580 wpa_drv_configure_frame_filters(wpa_s, 0);
581 hs20_deinit(wpa_s);
582 #endif /* CONFIG_HS20 */
583
584 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
585 wpabuf_free(wpa_s->vendor_elem[i]);
586 wpa_s->vendor_elem[i] = NULL;
587 }
588
589 wmm_ac_notify_disassoc(wpa_s);
590
591 wpa_s->sched_scan_plans_num = 0;
592 os_free(wpa_s->sched_scan_plans);
593 wpa_s->sched_scan_plans = NULL;
594
595 #ifdef CONFIG_MBO
596 wpa_s->non_pref_chan_num = 0;
597 os_free(wpa_s->non_pref_chan);
598 wpa_s->non_pref_chan = NULL;
599 #endif /* CONFIG_MBO */
600
601 free_bss_tmp_disallowed(wpa_s);
602
603 wpabuf_free(wpa_s->lci);
604 wpa_s->lci = NULL;
605 wpas_clear_beacon_rep_data(wpa_s);
606
607 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
608 #ifdef CONFIG_MESH
609 {
610 struct external_pmksa_cache *entry;
611
612 while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
613 struct external_pmksa_cache,
614 list)) != NULL) {
615 dl_list_del(&entry->list);
616 os_free(entry->pmksa_cache);
617 os_free(entry);
618 }
619 }
620 #endif /* CONFIG_MESH */
621 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
622
623 wpas_flush_fils_hlp_req(wpa_s);
624
625 wpabuf_free(wpa_s->ric_ies);
626 wpa_s->ric_ies = NULL;
627 }
628
629
630 /**
631 * wpa_clear_keys - Clear keys configured for the driver
632 * @wpa_s: Pointer to wpa_supplicant data
633 * @addr: Previously used BSSID or %NULL if not available
634 *
635 * This function clears the encryption keys that has been previously configured
636 * for the driver.
637 */
638 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
639 {
640 int i, max;
641
642 #ifdef CONFIG_IEEE80211W
643 max = 6;
644 #else /* CONFIG_IEEE80211W */
645 max = 4;
646 #endif /* CONFIG_IEEE80211W */
647
648 /* MLME-DELETEKEYS.request */
649 for (i = 0; i < max; i++) {
650 if (wpa_s->keys_cleared & BIT(i))
651 continue;
652 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
653 NULL, 0);
654 }
655 if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
656 !is_zero_ether_addr(addr)) {
657 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
658 0);
659 /* MLME-SETPROTECTION.request(None) */
660 wpa_drv_mlme_setprotection(
661 wpa_s, addr,
662 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
663 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
664 }
665 wpa_s->keys_cleared = (u32) -1;
666 }
667
668
669 /**
670 * wpa_supplicant_state_txt - Get the connection state name as a text string
671 * @state: State (wpa_state; WPA_*)
672 * Returns: The state name as a printable text string
673 */
674 const char * wpa_supplicant_state_txt(enum wpa_states state)
675 {
676 switch (state) {
677 case WPA_DISCONNECTED:
678 return "DISCONNECTED";
679 case WPA_INACTIVE:
680 return "INACTIVE";
681 case WPA_INTERFACE_DISABLED:
682 return "INTERFACE_DISABLED";
683 case WPA_SCANNING:
684 return "SCANNING";
685 case WPA_AUTHENTICATING:
686 return "AUTHENTICATING";
687 case WPA_ASSOCIATING:
688 return "ASSOCIATING";
689 case WPA_ASSOCIATED:
690 return "ASSOCIATED";
691 case WPA_4WAY_HANDSHAKE:
692 return "4WAY_HANDSHAKE";
693 case WPA_GROUP_HANDSHAKE:
694 return "GROUP_HANDSHAKE";
695 case WPA_COMPLETED:
696 return "COMPLETED";
697 default:
698 return "UNKNOWN";
699 }
700 }
701
702
703 #ifdef CONFIG_BGSCAN
704
705 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
706 {
707 const char *name;
708
709 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
710 name = wpa_s->current_ssid->bgscan;
711 else
712 name = wpa_s->conf->bgscan;
713 if (name == NULL || name[0] == '\0')
714 return;
715 if (wpas_driver_bss_selection(wpa_s))
716 return;
717 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
718 return;
719 #ifdef CONFIG_P2P
720 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
721 return;
722 #endif /* CONFIG_P2P */
723
724 bgscan_deinit(wpa_s);
725 if (wpa_s->current_ssid) {
726 if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
727 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
728 "bgscan");
729 /*
730 * Live without bgscan; it is only used as a roaming
731 * optimization, so the initial connection is not
732 * affected.
733 */
734 } else {
735 struct wpa_scan_results *scan_res;
736 wpa_s->bgscan_ssid = wpa_s->current_ssid;
737 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
738 0);
739 if (scan_res) {
740 bgscan_notify_scan(wpa_s, scan_res);
741 wpa_scan_results_free(scan_res);
742 }
743 }
744 } else
745 wpa_s->bgscan_ssid = NULL;
746 }
747
748
749 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
750 {
751 if (wpa_s->bgscan_ssid != NULL) {
752 bgscan_deinit(wpa_s);
753 wpa_s->bgscan_ssid = NULL;
754 }
755 }
756
757 #endif /* CONFIG_BGSCAN */
758
759
760 static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
761 {
762 if (autoscan_init(wpa_s, 0))
763 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
764 }
765
766
767 static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
768 {
769 autoscan_deinit(wpa_s);
770 }
771
772
773 void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
774 {
775 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
776 wpa_s->wpa_state == WPA_SCANNING) {
777 autoscan_deinit(wpa_s);
778 wpa_supplicant_start_autoscan(wpa_s);
779 }
780 }
781
782
783 /**
784 * wpa_supplicant_set_state - Set current connection state
785 * @wpa_s: Pointer to wpa_supplicant data
786 * @state: The new connection state
787 *
788 * This function is called whenever the connection state changes, e.g.,
789 * association is completed for WPA/WPA2 4-Way Handshake is started.
790 */
791 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
792 enum wpa_states state)
793 {
794 enum wpa_states old_state = wpa_s->wpa_state;
795
796 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
797 wpa_supplicant_state_txt(wpa_s->wpa_state),
798 wpa_supplicant_state_txt(state));
799
800 if (state == WPA_INTERFACE_DISABLED) {
801 /* Assure normal scan when interface is restored */
802 wpa_s->normal_scans = 0;
803 }
804
805 if (state == WPA_COMPLETED) {
806 wpas_connect_work_done(wpa_s);
807 /* Reinitialize normal_scan counter */
808 wpa_s->normal_scans = 0;
809 }
810
811 #ifdef CONFIG_P2P
812 /*
813 * P2PS client has to reply to Probe Request frames received on the
814 * group operating channel. Enable Probe Request frame reporting for
815 * P2P connected client in case p2p_cli_probe configuration property is
816 * set to 1.
817 */
818 if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
819 wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
820 wpa_s->current_ssid->p2p_group) {
821 if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
822 wpa_dbg(wpa_s, MSG_DEBUG,
823 "P2P: Enable CLI Probe Request RX reporting");
824 wpa_s->p2p_cli_probe =
825 wpa_drv_probe_req_report(wpa_s, 1) >= 0;
826 } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
827 wpa_dbg(wpa_s, MSG_DEBUG,
828 "P2P: Disable CLI Probe Request RX reporting");
829 wpa_s->p2p_cli_probe = 0;
830 wpa_drv_probe_req_report(wpa_s, 0);
831 }
832 }
833 #endif /* CONFIG_P2P */
834
835 if (state != WPA_SCANNING)
836 wpa_supplicant_notify_scanning(wpa_s, 0);
837
838 if (state == WPA_COMPLETED && wpa_s->new_connection) {
839 struct wpa_ssid *ssid = wpa_s->current_ssid;
840 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
841 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
842 MACSTR " completed [id=%d id_str=%s]",
843 MAC2STR(wpa_s->bssid),
844 ssid ? ssid->id : -1,
845 ssid && ssid->id_str ? ssid->id_str : "");
846 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
847 wpas_clear_temp_disabled(wpa_s, ssid, 1);
848 wpa_blacklist_clear(wpa_s);
849 wpa_s->extra_blacklist_count = 0;
850 wpa_s->new_connection = 0;
851 wpa_drv_set_operstate(wpa_s, 1);
852 #ifndef IEEE8021X_EAPOL
853 wpa_drv_set_supp_port(wpa_s, 1);
854 #endif /* IEEE8021X_EAPOL */
855 wpa_s->after_wps = 0;
856 wpa_s->known_wps_freq = 0;
857 wpas_p2p_completed(wpa_s);
858
859 sme_sched_obss_scan(wpa_s, 1);
860 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
861 state == WPA_ASSOCIATED) {
862 wpa_s->new_connection = 1;
863 wpa_drv_set_operstate(wpa_s, 0);
864 #ifndef IEEE8021X_EAPOL
865 wpa_drv_set_supp_port(wpa_s, 0);
866 #endif /* IEEE8021X_EAPOL */
867 sme_sched_obss_scan(wpa_s, 0);
868 }
869 wpa_s->wpa_state = state;
870
871 #ifdef CONFIG_BGSCAN
872 if (state == WPA_COMPLETED)
873 wpa_supplicant_start_bgscan(wpa_s);
874 else if (state < WPA_ASSOCIATED)
875 wpa_supplicant_stop_bgscan(wpa_s);
876 #endif /* CONFIG_BGSCAN */
877
878 if (state == WPA_AUTHENTICATING)
879 wpa_supplicant_stop_autoscan(wpa_s);
880
881 if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
882 wpa_supplicant_start_autoscan(wpa_s);
883
884 if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
885 wmm_ac_notify_disassoc(wpa_s);
886
887 if (wpa_s->wpa_state != old_state) {
888 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
889
890 /*
891 * Notify the P2P Device interface about a state change in one
892 * of the interfaces.
893 */
894 wpas_p2p_indicate_state_change(wpa_s);
895
896 if (wpa_s->wpa_state == WPA_COMPLETED ||
897 old_state == WPA_COMPLETED)
898 wpas_notify_auth_changed(wpa_s);
899 }
900 }
901
902
903 void wpa_supplicant_terminate_proc(struct wpa_global *global)
904 {
905 int pending = 0;
906 #ifdef CONFIG_WPS
907 struct wpa_supplicant *wpa_s = global->ifaces;
908 while (wpa_s) {
909 struct wpa_supplicant *next = wpa_s->next;
910 if (wpas_wps_terminate_pending(wpa_s) == 1)
911 pending = 1;
912 #ifdef CONFIG_P2P
913 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
914 (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
915 wpas_p2p_disconnect(wpa_s);
916 #endif /* CONFIG_P2P */
917 wpa_s = next;
918 }
919 #endif /* CONFIG_WPS */
920 if (pending)
921 return;
922 eloop_terminate();
923 }
924
925
926 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
927 {
928 struct wpa_global *global = signal_ctx;
929 wpa_supplicant_terminate_proc(global);
930 }
931
932
933 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
934 {
935 enum wpa_states old_state = wpa_s->wpa_state;
936
937 wpa_s->pairwise_cipher = 0;
938 wpa_s->group_cipher = 0;
939 wpa_s->mgmt_group_cipher = 0;
940 wpa_s->key_mgmt = 0;
941 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
942 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
943
944 if (wpa_s->wpa_state != old_state)
945 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
946 }
947
948
949 /**
950 * wpa_supplicant_reload_configuration - Reload configuration data
951 * @wpa_s: Pointer to wpa_supplicant data
952 * Returns: 0 on success or -1 if configuration parsing failed
953 *
954 * This function can be used to request that the configuration data is reloaded
955 * (e.g., after configuration file change). This function is reloading
956 * configuration only for one interface, so this may need to be called multiple
957 * times if %wpa_supplicant is controlling multiple interfaces and all
958 * interfaces need reconfiguration.
959 */
960 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
961 {
962 struct wpa_config *conf;
963 int reconf_ctrl;
964 int old_ap_scan;
965
966 if (wpa_s->confname == NULL)
967 return -1;
968 conf = wpa_config_read(wpa_s->confname, NULL);
969 if (conf == NULL) {
970 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
971 "file '%s' - exiting", wpa_s->confname);
972 return -1;
973 }
974 wpa_config_read(wpa_s->confanother, conf);
975
976 conf->changed_parameters = (unsigned int) -1;
977
978 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
979 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
980 os_strcmp(conf->ctrl_interface,
981 wpa_s->conf->ctrl_interface) != 0);
982
983 if (reconf_ctrl && wpa_s->ctrl_iface) {
984 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
985 wpa_s->ctrl_iface = NULL;
986 }
987
988 eapol_sm_invalidate_cached_session(wpa_s->eapol);
989 if (wpa_s->current_ssid) {
990 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
991 wpa_s->own_disconnect_req = 1;
992 wpa_supplicant_deauthenticate(wpa_s,
993 WLAN_REASON_DEAUTH_LEAVING);
994 }
995
996 /*
997 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
998 * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
999 */
1000 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
1001 /*
1002 * Clear forced success to clear EAP state for next
1003 * authentication.
1004 */
1005 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
1006 }
1007 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1008 wpa_sm_set_config(wpa_s->wpa, NULL);
1009 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
1010 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1011 rsn_preauth_deinit(wpa_s->wpa);
1012
1013 old_ap_scan = wpa_s->conf->ap_scan;
1014 wpa_config_free(wpa_s->conf);
1015 wpa_s->conf = conf;
1016 if (old_ap_scan != wpa_s->conf->ap_scan)
1017 wpas_notify_ap_scan_changed(wpa_s);
1018
1019 if (reconf_ctrl)
1020 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
1021
1022 wpa_supplicant_update_config(wpa_s);
1023
1024 wpa_supplicant_clear_status(wpa_s);
1025 if (wpa_supplicant_enabled_networks(wpa_s)) {
1026 wpa_s->reassociate = 1;
1027 wpa_supplicant_req_scan(wpa_s, 0, 0);
1028 }
1029 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
1030 return 0;
1031 }
1032
1033
1034 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
1035 {
1036 struct wpa_global *global = signal_ctx;
1037 struct wpa_supplicant *wpa_s;
1038 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1039 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
1040 sig);
1041 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
1042 wpa_supplicant_terminate_proc(global);
1043 }
1044 }
1045
1046 if (wpa_debug_reopen_file() < 0) {
1047 /* Ignore errors since we cannot really do much to fix this */
1048 wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
1049 }
1050 }
1051
1052
1053 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1054 struct wpa_ssid *ssid,
1055 struct wpa_ie_data *ie)
1056 {
1057 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1058 if (ret) {
1059 if (ret == -2) {
1060 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1061 "from association info");
1062 }
1063 return -1;
1064 }
1065
1066 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
1067 "cipher suites");
1068 if (!(ie->group_cipher & ssid->group_cipher)) {
1069 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1070 "cipher 0x%x (mask 0x%x) - reject",
1071 ie->group_cipher, ssid->group_cipher);
1072 return -1;
1073 }
1074 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1075 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1076 "cipher 0x%x (mask 0x%x) - reject",
1077 ie->pairwise_cipher, ssid->pairwise_cipher);
1078 return -1;
1079 }
1080 if (!(ie->key_mgmt & ssid->key_mgmt)) {
1081 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1082 "management 0x%x (mask 0x%x) - reject",
1083 ie->key_mgmt, ssid->key_mgmt);
1084 return -1;
1085 }
1086
1087 #ifdef CONFIG_IEEE80211W
1088 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
1089 wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
1090 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1091 "that does not support management frame protection - "
1092 "reject");
1093 return -1;
1094 }
1095 #endif /* CONFIG_IEEE80211W */
1096
1097 return 0;
1098 }
1099
1100
1101 /**
1102 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1103 * @wpa_s: Pointer to wpa_supplicant data
1104 * @bss: Scan results for the selected BSS, or %NULL if not available
1105 * @ssid: Configuration data for the selected network
1106 * @wpa_ie: Buffer for the WPA/RSN IE
1107 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1108 * used buffer length in case the functions returns success.
1109 * Returns: 0 on success or -1 on failure
1110 *
1111 * This function is used to configure authentication and encryption parameters
1112 * based on the network configuration and scan result for the selected BSS (if
1113 * available).
1114 */
1115 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1116 struct wpa_bss *bss, struct wpa_ssid *ssid,
1117 u8 *wpa_ie, size_t *wpa_ie_len)
1118 {
1119 struct wpa_ie_data ie;
1120 int sel, proto;
1121 const u8 *bss_wpa, *bss_rsn, *bss_osen;
1122
1123 if (bss) {
1124 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
1125 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1126 bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
1127 } else
1128 bss_wpa = bss_rsn = bss_osen = NULL;
1129
1130 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
1131 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
1132 (ie.group_cipher & ssid->group_cipher) &&
1133 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1134 (ie.key_mgmt & ssid->key_mgmt)) {
1135 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1136 proto = WPA_PROTO_RSN;
1137 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
1138 wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
1139 (ie.group_cipher & ssid->group_cipher) &&
1140 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1141 (ie.key_mgmt & ssid->key_mgmt)) {
1142 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1143 proto = WPA_PROTO_WPA;
1144 #ifdef CONFIG_HS20
1145 } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
1146 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
1147 /* TODO: parse OSEN element */
1148 os_memset(&ie, 0, sizeof(ie));
1149 ie.group_cipher = WPA_CIPHER_CCMP;
1150 ie.pairwise_cipher = WPA_CIPHER_CCMP;
1151 ie.key_mgmt = WPA_KEY_MGMT_OSEN;
1152 proto = WPA_PROTO_OSEN;
1153 #endif /* CONFIG_HS20 */
1154 } else if (bss) {
1155 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1156 wpa_dbg(wpa_s, MSG_DEBUG,
1157 "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1158 ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
1159 ssid->key_mgmt);
1160 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
1161 MAC2STR(bss->bssid),
1162 wpa_ssid_txt(bss->ssid, bss->ssid_len),
1163 bss_wpa ? " WPA" : "",
1164 bss_rsn ? " RSN" : "",
1165 bss_osen ? " OSEN" : "");
1166 if (bss_rsn) {
1167 wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
1168 if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
1169 wpa_dbg(wpa_s, MSG_DEBUG,
1170 "Could not parse RSN element");
1171 } else {
1172 wpa_dbg(wpa_s, MSG_DEBUG,
1173 "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1174 ie.pairwise_cipher, ie.group_cipher,
1175 ie.key_mgmt);
1176 }
1177 }
1178 if (bss_wpa) {
1179 wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
1180 if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
1181 wpa_dbg(wpa_s, MSG_DEBUG,
1182 "Could not parse WPA element");
1183 } else {
1184 wpa_dbg(wpa_s, MSG_DEBUG,
1185 "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
1186 ie.pairwise_cipher, ie.group_cipher,
1187 ie.key_mgmt);
1188 }
1189 }
1190 return -1;
1191 } else {
1192 if (ssid->proto & WPA_PROTO_OSEN)
1193 proto = WPA_PROTO_OSEN;
1194 else if (ssid->proto & WPA_PROTO_RSN)
1195 proto = WPA_PROTO_RSN;
1196 else
1197 proto = WPA_PROTO_WPA;
1198 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1199 os_memset(&ie, 0, sizeof(ie));
1200 ie.group_cipher = ssid->group_cipher;
1201 ie.pairwise_cipher = ssid->pairwise_cipher;
1202 ie.key_mgmt = ssid->key_mgmt;
1203 #ifdef CONFIG_IEEE80211W
1204 ie.mgmt_group_cipher =
1205 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
1206 WPA_CIPHER_AES_128_CMAC : 0;
1207 #endif /* CONFIG_IEEE80211W */
1208 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
1209 "based on configuration");
1210 } else
1211 proto = ie.proto;
1212 }
1213
1214 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1215 "pairwise %d key_mgmt %d proto %d",
1216 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1217 #ifdef CONFIG_IEEE80211W
1218 if (ssid->ieee80211w) {
1219 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1220 ie.mgmt_group_cipher);
1221 }
1222 #endif /* CONFIG_IEEE80211W */
1223
1224 wpa_s->wpa_proto = proto;
1225 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1226 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
1227 !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
1228
1229 if (bss || !wpa_s->ap_ies_from_associnfo) {
1230 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
1231 bss_wpa ? 2 + bss_wpa[1] : 0) ||
1232 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
1233 bss_rsn ? 2 + bss_rsn[1] : 0))
1234 return -1;
1235 }
1236
1237 #ifdef CONFIG_NO_WPA
1238 wpa_s->group_cipher = WPA_CIPHER_NONE;
1239 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1240 #else /* CONFIG_NO_WPA */
1241 sel = ie.group_cipher & ssid->group_cipher;
1242 wpa_s->group_cipher = wpa_pick_group_cipher(sel);
1243 if (wpa_s->group_cipher < 0) {
1244 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
1245 "cipher");
1246 return -1;
1247 }
1248 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
1249 wpa_cipher_txt(wpa_s->group_cipher));
1250
1251 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1252 wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
1253 if (wpa_s->pairwise_cipher < 0) {
1254 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
1255 "cipher");
1256 return -1;
1257 }
1258 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
1259 wpa_cipher_txt(wpa_s->pairwise_cipher));
1260 #endif /* CONFIG_NO_WPA */
1261
1262 sel = ie.key_mgmt & ssid->key_mgmt;
1263 #ifdef CONFIG_SAE
1264 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
1265 sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
1266 #endif /* CONFIG_SAE */
1267 if (0) {
1268 #ifdef CONFIG_SUITEB192
1269 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
1270 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
1271 wpa_dbg(wpa_s, MSG_DEBUG,
1272 "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
1273 #endif /* CONFIG_SUITEB192 */
1274 #ifdef CONFIG_SUITEB
1275 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
1276 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
1277 wpa_dbg(wpa_s, MSG_DEBUG,
1278 "WPA: using KEY_MGMT 802.1X with Suite B");
1279 #endif /* CONFIG_SUITEB */
1280 #ifdef CONFIG_FILS
1281 #ifdef CONFIG_IEEE80211R
1282 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
1283 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
1284 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
1285 } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
1286 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
1287 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
1288 #endif /* CONFIG_IEEE80211R */
1289 } else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
1290 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
1291 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
1292 } else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
1293 wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
1294 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
1295 #endif /* CONFIG_FILS */
1296 #ifdef CONFIG_IEEE80211R
1297 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
1298 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
1299 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
1300 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
1301 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1302 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1303 #endif /* CONFIG_IEEE80211R */
1304 #ifdef CONFIG_SAE
1305 } else if (sel & WPA_KEY_MGMT_SAE) {
1306 wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
1307 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
1308 } else if (sel & WPA_KEY_MGMT_FT_SAE) {
1309 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
1310 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
1311 #endif /* CONFIG_SAE */
1312 #ifdef CONFIG_IEEE80211W
1313 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1314 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1315 wpa_dbg(wpa_s, MSG_DEBUG,
1316 "WPA: using KEY_MGMT 802.1X with SHA256");
1317 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1318 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1319 wpa_dbg(wpa_s, MSG_DEBUG,
1320 "WPA: using KEY_MGMT PSK with SHA256");
1321 #endif /* CONFIG_IEEE80211W */
1322 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1323 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1324 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1325 } else if (sel & WPA_KEY_MGMT_PSK) {
1326 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1327 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1328 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1329 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1330 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1331 #ifdef CONFIG_HS20
1332 } else if (sel & WPA_KEY_MGMT_OSEN) {
1333 wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
1334 wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
1335 #endif /* CONFIG_HS20 */
1336 } else {
1337 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1338 "authenticated key management type");
1339 return -1;
1340 }
1341
1342 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1343 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1344 wpa_s->pairwise_cipher);
1345 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1346
1347 #ifdef CONFIG_IEEE80211W
1348 sel = ie.mgmt_group_cipher;
1349 if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
1350 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1351 sel = 0;
1352 if (sel & WPA_CIPHER_AES_128_CMAC) {
1353 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1354 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1355 "AES-128-CMAC");
1356 } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
1357 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
1358 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1359 "BIP-GMAC-128");
1360 } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
1361 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
1362 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1363 "BIP-GMAC-256");
1364 } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
1365 wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
1366 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1367 "BIP-CMAC-256");
1368 } else {
1369 wpa_s->mgmt_group_cipher = 0;
1370 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1371 }
1372 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1373 wpa_s->mgmt_group_cipher);
1374 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
1375 wpas_get_ssid_pmf(wpa_s, ssid));
1376 #endif /* CONFIG_IEEE80211W */
1377
1378 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1379 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1380 return -1;
1381 }
1382
1383 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
1384 int psk_set = 0;
1385
1386 if (ssid->psk_set) {
1387 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL,
1388 NULL);
1389 psk_set = 1;
1390 }
1391 #ifndef CONFIG_NO_PBKDF2
1392 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1393 ssid->passphrase) {
1394 u8 psk[PMK_LEN];
1395 pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
1396 4096, psk, PMK_LEN);
1397 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1398 psk, PMK_LEN);
1399 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL, NULL);
1400 psk_set = 1;
1401 os_memset(psk, 0, sizeof(psk));
1402 }
1403 #endif /* CONFIG_NO_PBKDF2 */
1404 #ifdef CONFIG_EXT_PASSWORD
1405 if (ssid->ext_psk) {
1406 struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
1407 ssid->ext_psk);
1408 char pw_str[64 + 1];
1409 u8 psk[PMK_LEN];
1410
1411 if (pw == NULL) {
1412 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
1413 "found from external storage");
1414 return -1;
1415 }
1416
1417 if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
1418 wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
1419 "PSK length %d in external storage",
1420 (int) wpabuf_len(pw));
1421 ext_password_free(pw);
1422 return -1;
1423 }
1424
1425 os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
1426 pw_str[wpabuf_len(pw)] = '\0';
1427
1428 #ifndef CONFIG_NO_PBKDF2
1429 if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
1430 {
1431 pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
1432 4096, psk, PMK_LEN);
1433 os_memset(pw_str, 0, sizeof(pw_str));
1434 wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
1435 "external passphrase)",
1436 psk, PMK_LEN);
1437 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
1438 NULL);
1439 psk_set = 1;
1440 os_memset(psk, 0, sizeof(psk));
1441 } else
1442 #endif /* CONFIG_NO_PBKDF2 */
1443 if (wpabuf_len(pw) == 2 * PMK_LEN) {
1444 if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
1445 wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
1446 "Invalid PSK hex string");
1447 os_memset(pw_str, 0, sizeof(pw_str));
1448 ext_password_free(pw);
1449 return -1;
1450 }
1451 wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
1452 NULL);
1453 psk_set = 1;
1454 os_memset(psk, 0, sizeof(psk));
1455 } else {
1456 wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
1457 "PSK available");
1458 os_memset(pw_str, 0, sizeof(pw_str));
1459 ext_password_free(pw);
1460 return -1;
1461 }
1462
1463 os_memset(pw_str, 0, sizeof(pw_str));
1464 ext_password_free(pw);
1465 }
1466 #endif /* CONFIG_EXT_PASSWORD */
1467
1468 if (!psk_set) {
1469 wpa_msg(wpa_s, MSG_INFO,
1470 "No PSK available for association");
1471 return -1;
1472 }
1473 } else
1474 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1475
1476 return 0;
1477 }
1478
1479
1480 static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
1481 {
1482 *pos = 0x00;
1483
1484 switch (idx) {
1485 case 0: /* Bits 0-7 */
1486 break;
1487 case 1: /* Bits 8-15 */
1488 break;
1489 case 2: /* Bits 16-23 */
1490 #ifdef CONFIG_WNM
1491 *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
1492 *pos |= 0x08; /* Bit 19 - BSS Transition */
1493 #endif /* CONFIG_WNM */
1494 break;
1495 case 3: /* Bits 24-31 */
1496 #ifdef CONFIG_WNM
1497 *pos |= 0x02; /* Bit 25 - SSID List */
1498 #endif /* CONFIG_WNM */
1499 #ifdef CONFIG_INTERWORKING
1500 if (wpa_s->conf->interworking)
1501 *pos |= 0x80; /* Bit 31 - Interworking */
1502 #endif /* CONFIG_INTERWORKING */
1503 break;
1504 case 4: /* Bits 32-39 */
1505 #ifdef CONFIG_INTERWORKING
1506 if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
1507 *pos |= 0x01; /* Bit 32 - QoS Map */
1508 #endif /* CONFIG_INTERWORKING */
1509 break;
1510 case 5: /* Bits 40-47 */
1511 #ifdef CONFIG_HS20
1512 if (wpa_s->conf->hs20)
1513 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1514 #endif /* CONFIG_HS20 */
1515 #ifdef CONFIG_MBO
1516 *pos |= 0x40; /* Bit 46 - WNM-Notification */
1517 #endif /* CONFIG_MBO */
1518 break;
1519 case 6: /* Bits 48-55 */
1520 break;
1521 case 7: /* Bits 56-63 */
1522 break;
1523 case 8: /* Bits 64-71 */
1524 if (wpa_s->conf->ftm_responder)
1525 *pos |= 0x40; /* Bit 70 - FTM responder */
1526 if (wpa_s->conf->ftm_initiator)
1527 *pos |= 0x80; /* Bit 71 - FTM initiator */
1528 break;
1529 case 9: /* Bits 72-79 */
1530 #ifdef CONFIG_FILS
1531 *pos |= 0x01;
1532 #endif /* CONFIG_FILS */
1533 break;
1534 }
1535 }
1536
1537
1538 int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
1539 {
1540 u8 *pos = buf;
1541 u8 len = 10, i;
1542
1543 if (len < wpa_s->extended_capa_len)
1544 len = wpa_s->extended_capa_len;
1545 if (buflen < (size_t) len + 2) {
1546 wpa_printf(MSG_INFO,
1547 "Not enough room for building extended capabilities element");
1548 return -1;
1549 }
1550
1551 *pos++ = WLAN_EID_EXT_CAPAB;
1552 *pos++ = len;
1553 for (i = 0; i < len; i++, pos++) {
1554 wpas_ext_capab_byte(wpa_s, pos, i);
1555
1556 if (i < wpa_s->extended_capa_len) {
1557 *pos &= ~wpa_s->extended_capa_mask[i];
1558 *pos |= wpa_s->extended_capa[i];
1559 }
1560 }
1561
1562 while (len > 0 && buf[1 + len] == 0) {
1563 len--;
1564 buf[1] = len;
1565 }
1566 if (len == 0)
1567 return 0;
1568
1569 return 2 + len;
1570 }
1571
1572
1573 static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
1574 struct wpa_bss *test_bss)
1575 {
1576 struct wpa_bss *bss;
1577
1578 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1579 if (bss == test_bss)
1580 return 1;
1581 }
1582
1583 return 0;
1584 }
1585
1586
1587 static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
1588 struct wpa_ssid *test_ssid)
1589 {
1590 struct wpa_ssid *ssid;
1591
1592 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1593 if (ssid == test_ssid)
1594 return 1;
1595 }
1596
1597 return 0;
1598 }
1599
1600
1601 int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
1602 struct wpa_ssid *test_ssid)
1603 {
1604 if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
1605 return 0;
1606
1607 return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
1608 }
1609
1610
1611 void wpas_connect_work_free(struct wpa_connect_work *cwork)
1612 {
1613 if (cwork == NULL)
1614 return;
1615 os_free(cwork);
1616 }
1617
1618
1619 void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
1620 {
1621 struct wpa_connect_work *cwork;
1622 struct wpa_radio_work *work = wpa_s->connect_work;
1623
1624 if (!work)
1625 return;
1626
1627 wpa_s->connect_work = NULL;
1628 cwork = work->ctx;
1629 work->ctx = NULL;
1630 wpas_connect_work_free(cwork);
1631 radio_work_done(work);
1632 }
1633
1634
1635 int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
1636 {
1637 struct os_reltime now;
1638 u8 addr[ETH_ALEN];
1639
1640 os_get_reltime(&now);
1641 if (wpa_s->last_mac_addr_style == style &&
1642 wpa_s->last_mac_addr_change.sec != 0 &&
1643 !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
1644 wpa_s->conf->rand_addr_lifetime)) {
1645 wpa_msg(wpa_s, MSG_DEBUG,
1646 "Previously selected random MAC address has not yet expired");
1647 return 0;
1648 }
1649
1650 switch (style) {
1651 case 1:
1652 if (random_mac_addr(addr) < 0)
1653 return -1;
1654 break;
1655 case 2:
1656 os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
1657 if (random_mac_addr_keep_oui(addr) < 0)
1658 return -1;
1659 break;
1660 default:
1661 return -1;
1662 }
1663
1664 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
1665 wpa_msg(wpa_s, MSG_INFO,
1666 "Failed to set random MAC address");
1667 return -1;
1668 }
1669
1670 os_get_reltime(&wpa_s->last_mac_addr_change);
1671 wpa_s->mac_addr_changed = 1;
1672 wpa_s->last_mac_addr_style = style;
1673
1674 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1675 wpa_msg(wpa_s, MSG_INFO,
1676 "Could not update MAC address information");
1677 return -1;
1678 }
1679
1680 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
1681 MAC2STR(addr));
1682
1683 return 0;
1684 }
1685
1686
1687 int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
1688 {
1689 if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
1690 !wpa_s->conf->preassoc_mac_addr)
1691 return 0;
1692
1693 return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
1694 }
1695
1696
1697 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
1698
1699 /**
1700 * wpa_supplicant_associate - Request association
1701 * @wpa_s: Pointer to wpa_supplicant data
1702 * @bss: Scan results for the selected BSS, or %NULL if not available
1703 * @ssid: Configuration data for the selected network
1704 *
1705 * This function is used to request %wpa_supplicant to associate with a BSS.
1706 */
1707 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1708 struct wpa_bss *bss, struct wpa_ssid *ssid)
1709 {
1710 struct wpa_connect_work *cwork;
1711 int rand_style;
1712
1713 wpa_s->own_disconnect_req = 0;
1714
1715 /*
1716 * If we are starting a new connection, any previously pending EAPOL
1717 * RX cannot be valid anymore.
1718 */
1719 wpabuf_free(wpa_s->pending_eapol_rx);
1720 wpa_s->pending_eapol_rx = NULL;
1721
1722 if (ssid->mac_addr == -1)
1723 rand_style = wpa_s->conf->mac_addr;
1724 else
1725 rand_style = ssid->mac_addr;
1726
1727 wmm_ac_clear_saved_tspecs(wpa_s);
1728 wpa_s->reassoc_same_bss = 0;
1729 wpa_s->reassoc_same_ess = 0;
1730
1731 if (wpa_s->last_ssid == ssid) {
1732 wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
1733 wpa_s->reassoc_same_ess = 1;
1734 if (wpa_s->current_bss && wpa_s->current_bss == bss) {
1735 wmm_ac_save_tspecs(wpa_s);
1736 wpa_s->reassoc_same_bss = 1;
1737 }
1738 }
1739
1740 if (rand_style > 0 && !wpa_s->reassoc_same_ess) {
1741 if (wpas_update_random_addr(wpa_s, rand_style) < 0)
1742 return;
1743 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1744 } else if (rand_style == 0 && wpa_s->mac_addr_changed) {
1745 if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
1746 wpa_msg(wpa_s, MSG_INFO,
1747 "Could not restore permanent MAC address");
1748 return;
1749 }
1750 wpa_s->mac_addr_changed = 0;
1751 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
1752 wpa_msg(wpa_s, MSG_INFO,
1753 "Could not update MAC address information");
1754 return;
1755 }
1756 wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
1757 }
1758 wpa_s->last_ssid = ssid;
1759
1760 #ifdef CONFIG_IBSS_RSN
1761 ibss_rsn_deinit(wpa_s->ibss_rsn);
1762 wpa_s->ibss_rsn = NULL;
1763 #else /* CONFIG_IBSS_RSN */
1764 if (ssid->mode == WPAS_MODE_IBSS &&
1765 !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
1766 wpa_msg(wpa_s, MSG_INFO,
1767 "IBSS RSN not supported in the build");
1768 return;
1769 }
1770 #endif /* CONFIG_IBSS_RSN */
1771
1772 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1773 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1774 #ifdef CONFIG_AP
1775 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1776 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1777 "mode");
1778 return;
1779 }
1780 if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
1781 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1782 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
1783 wpas_p2p_ap_setup_failed(wpa_s);
1784 return;
1785 }
1786 wpa_s->current_bss = bss;
1787 #else /* CONFIG_AP */
1788 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1789 "the build");
1790 #endif /* CONFIG_AP */
1791 return;
1792 }
1793
1794 if (ssid->mode == WPAS_MODE_MESH) {
1795 #ifdef CONFIG_MESH
1796 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
1797 wpa_msg(wpa_s, MSG_INFO,
1798 "Driver does not support mesh mode");
1799 return;
1800 }
1801 if (bss)
1802 ssid->frequency = bss->freq;
1803 if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
1804 wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
1805 return;
1806 }
1807 wpa_s->current_bss = bss;
1808 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_STARTED "ssid=\"%s\" id=%d",
1809 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1810 ssid->id);
1811 #else /* CONFIG_MESH */
1812 wpa_msg(wpa_s, MSG_ERROR,
1813 "mesh mode support not included in the build");
1814 #endif /* CONFIG_MESH */
1815 return;
1816 }
1817
1818 #ifdef CONFIG_TDLS
1819 if (bss)
1820 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1821 bss->ie_len);
1822 #endif /* CONFIG_TDLS */
1823
1824 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1825 ssid->mode == IEEE80211_MODE_INFRA) {
1826 sme_authenticate(wpa_s, bss, ssid);
1827 return;
1828 }
1829
1830 if (wpa_s->connect_work) {
1831 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
1832 return;
1833 }
1834
1835 if (radio_work_pending(wpa_s, "connect")) {
1836 wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
1837 return;
1838 }
1839
1840 #ifdef CONFIG_SME
1841 if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
1842 /* Clear possibly set auth_alg, if any, from last attempt. */
1843 wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
1844 }
1845 #endif /* CONFIG_SME */
1846
1847 wpas_abort_ongoing_scan(wpa_s);
1848
1849 cwork = os_zalloc(sizeof(*cwork));
1850 if (cwork == NULL)
1851 return;
1852
1853 cwork->bss = bss;
1854 cwork->ssid = ssid;
1855
1856 if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
1857 wpas_start_assoc_cb, cwork) < 0) {
1858 os_free(cwork);
1859 }
1860 }
1861
1862
1863 static int bss_is_ibss(struct wpa_bss *bss)
1864 {
1865 return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
1866 IEEE80211_CAP_IBSS;
1867 }
1868
1869
1870 static int drv_supports_vht(struct wpa_supplicant *wpa_s,
1871 const struct wpa_ssid *ssid)
1872 {
1873 enum hostapd_hw_mode hw_mode;
1874 struct hostapd_hw_modes *mode = NULL;
1875 u8 channel;
1876 int i;
1877
1878 hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
1879 if (hw_mode == NUM_HOSTAPD_MODES)
1880 return 0;
1881 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
1882 if (wpa_s->hw.modes[i].mode == hw_mode) {
1883 mode = &wpa_s->hw.modes[i];
1884 break;
1885 }
1886 }
1887
1888 if (!mode)
1889 return 0;
1890
1891 return mode->vht_capab != 0;
1892 }
1893
1894
1895 void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
1896 const struct wpa_ssid *ssid,
1897 struct hostapd_freq_params *freq)
1898 {
1899 enum hostapd_hw_mode hw_mode;
1900 struct hostapd_hw_modes *mode = NULL;
1901 int ht40plus[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
1902 184, 192 };
1903 int vht80[] = { 36, 52, 100, 116, 132, 149 };
1904 struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
1905 u8 channel;
1906 int i, chan_idx, ht40 = -1, res, obss_scan = 1;
1907 unsigned int j, k;
1908 struct hostapd_freq_params vht_freq;
1909 int chwidth, seg0, seg1;
1910 u32 vht_caps = 0;
1911
1912 freq->freq = ssid->frequency;
1913
1914 for (j = 0; j < wpa_s->last_scan_res_used; j++) {
1915 struct wpa_bss *bss = wpa_s->last_scan_res[j];
1916
1917 if (ssid->mode != WPAS_MODE_IBSS)
1918 break;
1919
1920 /* Don't adjust control freq in case of fixed_freq */
1921 if (ssid->fixed_freq)
1922 break;
1923
1924 if (!bss_is_ibss(bss))
1925 continue;
1926
1927 if (ssid->ssid_len == bss->ssid_len &&
1928 os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0) {
1929 wpa_printf(MSG_DEBUG,
1930 "IBSS already found in scan results, adjust control freq: %d",
1931 bss->freq);
1932 freq->freq = bss->freq;
1933 obss_scan = 0;
1934 break;
1935 }
1936 }
1937
1938 /* For IBSS check HT_IBSS flag */
1939 if (ssid->mode == WPAS_MODE_IBSS &&
1940 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
1941 return;
1942
1943 if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
1944 wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
1945 wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
1946 wpa_printf(MSG_DEBUG,
1947 "IBSS: WEP/TKIP detected, do not try to enable HT");
1948 return;
1949 }
1950
1951 hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
1952 for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
1953 if (wpa_s->hw.modes[i].mode == hw_mode) {
1954 mode = &wpa_s->hw.modes[i];
1955 break;
1956 }
1957 }
1958
1959 if (!mode)
1960 return;
1961
1962 #ifdef CONFIG_HT_OVERRIDES
1963 if (ssid->disable_ht) {
1964 freq->ht_enabled = 0;
1965 return;
1966 }
1967 #endif /* CONFIG_HT_OVERRIDES */
1968
1969 freq->ht_enabled = ht_supported(mode);
1970 if (!freq->ht_enabled)
1971 return;
1972
1973 /* Setup higher BW only for 5 GHz */
1974 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
1975 return;
1976
1977 for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
1978 pri_chan = &mode->channels[chan_idx];
1979 if (pri_chan->chan == channel)
1980 break;
1981 pri_chan = NULL;
1982 }
1983 if (!pri_chan)
1984 return;
1985
1986 /* Check primary channel flags */
1987 if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
1988 return;
1989
1990 #ifdef CONFIG_HT_OVERRIDES
1991 if (ssid->disable_ht40)
1992 return;
1993 #endif /* CONFIG_HT_OVERRIDES */
1994
1995 /* Check/setup HT40+/HT40- */
1996 for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
1997 if (ht40plus[j] == channel) {
1998 ht40 = 1;
1999 break;
2000 }
2001 }
2002
2003 /* Find secondary channel */
2004 for (i = 0; i < mode->num_channels; i++) {
2005 sec_chan = &mode->channels[i];
2006 if (sec_chan->chan == channel + ht40 * 4)
2007 break;
2008 sec_chan = NULL;
2009 }
2010 if (!sec_chan)
2011 return;
2012
2013 /* Check secondary channel flags */
2014 if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2015 return;
2016
2017 freq->channel = pri_chan->chan;
2018
2019 if (ht40 == -1) {
2020 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
2021 return;
2022 } else {
2023 if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
2024 return;
2025 }
2026 freq->sec_channel_offset = ht40;
2027
2028 if (obss_scan) {
2029 struct wpa_scan_results *scan_res;
2030
2031 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
2032 if (scan_res == NULL) {
2033 /* Back to HT20 */
2034 freq->sec_channel_offset = 0;
2035 return;
2036 }
2037
2038 res = check_40mhz_5g(mode, scan_res, pri_chan->chan,
2039 sec_chan->chan);
2040 switch (res) {
2041 case 0:
2042 /* Back to HT20 */
2043 freq->sec_channel_offset = 0;
2044 break;
2045 case 1:
2046 /* Configuration allowed */
2047 break;
2048 case 2:
2049 /* Switch pri/sec channels */
2050 freq->freq = hw_get_freq(mode, sec_chan->chan);
2051 freq->sec_channel_offset = -freq->sec_channel_offset;
2052 freq->channel = sec_chan->chan;
2053 break;
2054 default:
2055 freq->sec_channel_offset = 0;
2056 break;
2057 }
2058
2059 wpa_scan_results_free(scan_res);
2060 }
2061
2062 wpa_printf(MSG_DEBUG,
2063 "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
2064 freq->channel, freq->sec_channel_offset);
2065
2066 if (!drv_supports_vht(wpa_s, ssid))
2067 return;
2068
2069 /* For IBSS check VHT_IBSS flag */
2070 if (ssid->mode == WPAS_MODE_IBSS &&
2071 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
2072 return;
2073
2074 vht_freq = *freq;
2075
2076 #ifdef CONFIG_VHT_OVERRIDES
2077 if (ssid->disable_vht) {
2078 freq->vht_enabled = 0;
2079 return;
2080 }
2081 #endif /* CONFIG_VHT_OVERRIDES */
2082
2083 vht_freq.vht_enabled = vht_supported(mode);
2084 if (!vht_freq.vht_enabled)
2085 return;
2086
2087 /* setup center_freq1, bandwidth */
2088 for (j = 0; j < ARRAY_SIZE(vht80); j++) {
2089 if (freq->channel >= vht80[j] &&
2090 freq->channel < vht80[j] + 16)
2091 break;
2092 }
2093
2094 if (j == ARRAY_SIZE(vht80))
2095 return;
2096
2097 for (i = vht80[j]; i < vht80[j] + 16; i += 4) {
2098 struct hostapd_channel_data *chan;
2099
2100 chan = hw_get_channel_chan(mode, i, NULL);
2101 if (!chan)
2102 return;
2103
2104 /* Back to HT configuration if channel not usable */
2105 if (chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
2106 return;
2107 }
2108
2109 chwidth = VHT_CHANWIDTH_80MHZ;
2110 seg0 = vht80[j] + 6;
2111 seg1 = 0;
2112
2113 if (ssid->max_oper_chwidth == VHT_CHANWIDTH_80P80MHZ) {
2114 /* setup center_freq2, bandwidth */
2115 for (k = 0; k < ARRAY_SIZE(vht80); k++) {
2116 /* Only accept 80 MHz segments separated by a gap */
2117 if (j == k || abs(vht80[j] - vht80[k]) == 16)
2118 continue;
2119 for (i = vht80[k]; i < vht80[k] + 16; i += 4) {
2120 struct hostapd_channel_data *chan;
2121
2122 chan = hw_get_channel_chan(mode, i, NULL);
2123 if (!chan)
2124 continue;
2125
2126 if (chan->flag & (HOSTAPD_CHAN_DISABLED |
2127 HOSTAPD_CHAN_NO_IR |
2128 HOSTAPD_CHAN_RADAR))
2129 continue;
2130
2131 /* Found a suitable second segment for 80+80 */
2132 chwidth = VHT_CHANWIDTH_80P80MHZ;
2133 vht_caps |=
2134 VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
2135 seg1 = vht80[k] + 6;
2136 }
2137
2138 if (chwidth == VHT_CHANWIDTH_80P80MHZ)
2139 break;
2140 }
2141 } else if (ssid->max_oper_chwidth == VHT_CHANWIDTH_160MHZ) {
2142 if (freq->freq == 5180) {
2143 chwidth = VHT_CHANWIDTH_160MHZ;
2144 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2145 seg0 = 50;
2146 } else if (freq->freq == 5520) {
2147 chwidth = VHT_CHANWIDTH_160MHZ;
2148 vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
2149 seg0 = 114;
2150 }
2151 }
2152
2153 if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
2154 freq->channel, freq->ht_enabled,
2155 vht_freq.vht_enabled,
2156 freq->sec_channel_offset,
2157 chwidth, seg0, seg1, vht_caps) != 0)
2158 return;
2159
2160 *freq = vht_freq;
2161
2162 wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
2163 freq->center_freq1, freq->center_freq2, freq->bandwidth);
2164 }
2165
2166
2167 static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
2168 {
2169 struct wpa_connect_work *cwork = work->ctx;
2170 struct wpa_bss *bss = cwork->bss;
2171 struct wpa_ssid *ssid = cwork->ssid;
2172 struct wpa_supplicant *wpa_s = work->wpa_s;
2173 u8 wpa_ie[200];
2174 size_t wpa_ie_len;
2175 int use_crypt, ret, i, bssid_changed;
2176 int algs = WPA_AUTH_ALG_OPEN;
2177 unsigned int cipher_pairwise, cipher_group;
2178 struct wpa_driver_associate_params params;
2179 int wep_keys_set = 0;
2180 int assoc_failed = 0;
2181 struct wpa_ssid *old_ssid;
2182 u8 prev_bssid[ETH_ALEN];
2183 #ifdef CONFIG_HT_OVERRIDES
2184 struct ieee80211_ht_capabilities htcaps;
2185 struct ieee80211_ht_capabilities htcaps_mask;
2186 #endif /* CONFIG_HT_OVERRIDES */
2187 #ifdef CONFIG_VHT_OVERRIDES
2188 struct ieee80211_vht_capabilities vhtcaps;
2189 struct ieee80211_vht_capabilities vhtcaps_mask;
2190 #endif /* CONFIG_VHT_OVERRIDES */
2191
2192 if (deinit) {
2193 if (work->started) {
2194 wpa_s->connect_work = NULL;
2195
2196 /* cancel possible auth. timeout */
2197 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
2198 NULL);
2199 }
2200 wpas_connect_work_free(cwork);
2201 return;
2202 }
2203
2204 wpa_s->connect_work = work;
2205
2206 if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
2207 wpas_network_disabled(wpa_s, ssid)) {
2208 wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
2209 wpas_connect_work_done(wpa_s);
2210 return;
2211 }
2212
2213 os_memcpy(prev_bssid, wpa_s->bssid, ETH_ALEN);
2214 os_memset(&params, 0, sizeof(params));
2215 wpa_s->reassociate = 0;
2216 wpa_s->eap_expected_failure = 0;
2217 if (bss &&
2218 (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
2219 #ifdef CONFIG_IEEE80211R
2220 const u8 *ie, *md = NULL;
2221 #endif /* CONFIG_IEEE80211R */
2222 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
2223 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
2224 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
2225 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
2226 os_memset(wpa_s->bssid, 0, ETH_ALEN);
2227 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
2228 if (bssid_changed)
2229 wpas_notify_bssid_changed(wpa_s);
2230 #ifdef CONFIG_IEEE80211R
2231 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
2232 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
2233 md = ie + 2;
2234 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
2235 if (md) {
2236 /* Prepare for the next transition */
2237 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
2238 }
2239 #endif /* CONFIG_IEEE80211R */
2240 #ifdef CONFIG_WPS
2241 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
2242 wpa_s->conf->ap_scan == 2 &&
2243 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
2244 /* Use ap_scan==1 style network selection to find the network
2245 */
2246 wpas_connect_work_done(wpa_s);
2247 wpa_s->scan_req = MANUAL_SCAN_REQ;
2248 wpa_s->reassociate = 1;
2249 wpa_supplicant_req_scan(wpa_s, 0, 0);
2250 return;
2251 #endif /* CONFIG_WPS */
2252 } else {
2253 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
2254 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
2255 if (bss)
2256 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
2257 else
2258 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2259 }
2260 if (!wpa_s->pno)
2261 wpa_supplicant_cancel_sched_scan(wpa_s);
2262
2263 wpa_supplicant_cancel_scan(wpa_s);
2264
2265 /* Starting new association, so clear the possibly used WPA IE from the
2266 * previous association. */
2267 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
2268
2269 #ifdef IEEE8021X_EAPOL
2270 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2271 if (ssid->leap) {
2272 if (ssid->non_leap == 0)
2273 algs = WPA_AUTH_ALG_LEAP;
2274 else
2275 algs |= WPA_AUTH_ALG_LEAP;
2276 }
2277 }
2278 #endif /* IEEE8021X_EAPOL */
2279 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
2280 if (ssid->auth_alg) {
2281 algs = ssid->auth_alg;
2282 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
2283 "0x%x", algs);
2284 }
2285
2286 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
2287 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
2288 wpa_key_mgmt_wpa(ssid->key_mgmt)) {
2289 int try_opportunistic;
2290 try_opportunistic = (ssid->proactive_key_caching < 0 ?
2291 wpa_s->conf->okc :
2292 ssid->proactive_key_caching) &&
2293 (ssid->proto & WPA_PROTO_RSN);
2294 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
2295 ssid, try_opportunistic, NULL) == 0)
2296 eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
2297 wpa_ie_len = sizeof(wpa_ie);
2298 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
2299 wpa_ie, &wpa_ie_len)) {
2300 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
2301 "key management and encryption suites");
2302 wpas_connect_work_done(wpa_s);
2303 return;
2304 }
2305 } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
2306 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
2307 /*
2308 * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
2309 * use non-WPA since the scan results did not indicate that the
2310 * AP is using WPA or WPA2.
2311 */
2312 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2313 wpa_ie_len = 0;
2314 wpa_s->wpa_proto = 0;
2315 } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
2316 wpa_ie_len = sizeof(wpa_ie);
2317 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
2318 wpa_ie, &wpa_ie_len)) {
2319 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
2320 "key management and encryption suites (no "
2321 "scan results)");
2322 wpas_connect_work_done(wpa_s);
2323 return;
2324 }
2325 #ifdef CONFIG_WPS
2326 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
2327 struct wpabuf *wps_ie;
2328 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
2329 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
2330 wpa_ie_len = wpabuf_len(wps_ie);
2331 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
2332 } else
2333 wpa_ie_len = 0;
2334 wpabuf_free(wps_ie);
2335 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2336 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
2337 params.wps = WPS_MODE_PRIVACY;
2338 else
2339 params.wps = WPS_MODE_OPEN;
2340 wpa_s->wpa_proto = 0;
2341 #endif /* CONFIG_WPS */
2342 } else {
2343 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
2344 wpa_ie_len = 0;
2345 wpa_s->wpa_proto = 0;
2346 }
2347
2348 #ifdef CONFIG_P2P
2349 if (wpa_s->global->p2p) {
2350 u8 *pos;
2351 size_t len;
2352 int res;
2353 pos = wpa_ie + wpa_ie_len;
2354 len = sizeof(wpa_ie) - wpa_ie_len;
2355 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
2356 ssid->p2p_group);
2357 if (res >= 0)
2358 wpa_ie_len += res;
2359 }
2360
2361 wpa_s->cross_connect_disallowed = 0;
2362 if (bss) {
2363 struct wpabuf *p2p;
2364 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
2365 if (p2p) {
2366 wpa_s->cross_connect_disallowed =
2367 p2p_get_cross_connect_disallowed(p2p);
2368 wpabuf_free(p2p);
2369 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
2370 "connection",
2371 wpa_s->cross_connect_disallowed ?
2372 "disallows" : "allows");
2373 }
2374 }
2375
2376 os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
2377 #endif /* CONFIG_P2P */
2378
2379 if (bss) {
2380 wpa_ie_len += wpas_supp_op_class_ie(wpa_s, bss->freq,
2381 wpa_ie + wpa_ie_len,
2382 sizeof(wpa_ie) -
2383 wpa_ie_len);
2384 }
2385
2386 /*
2387 * Workaround: Add Extended Capabilities element only if the AP
2388 * included this element in Beacon/Probe Response frames. Some older
2389 * APs seem to have interoperability issues if this element is
2390 * included, so while the standard may require us to include the
2391 * element in all cases, it is justifiable to skip it to avoid
2392 * interoperability issues.
2393 */
2394 if (ssid->p2p_group)
2395 wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
2396 else
2397 wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
2398
2399 if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
2400 u8 ext_capab[18];
2401 int ext_capab_len;
2402 ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
2403 sizeof(ext_capab));
2404 if (ext_capab_len > 0) {
2405 u8 *pos = wpa_ie;
2406 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
2407 pos += 2 + pos[1];
2408 os_memmove(pos + ext_capab_len, pos,
2409 wpa_ie_len - (pos - wpa_ie));
2410 wpa_ie_len += ext_capab_len;
2411 os_memcpy(pos, ext_capab, ext_capab_len);
2412 }
2413 }
2414
2415 #ifdef CONFIG_HS20
2416 if (is_hs20_network(wpa_s, ssid, bss)) {
2417 struct wpabuf *hs20;
2418
2419 hs20 = wpabuf_alloc(20);
2420 if (hs20) {
2421 int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
2422 size_t len;
2423
2424 wpas_hs20_add_indication(hs20, pps_mo_id);
2425 len = sizeof(wpa_ie) - wpa_ie_len;
2426 if (wpabuf_len(hs20) <= len) {
2427 os_memcpy(wpa_ie + wpa_ie_len,
2428 wpabuf_head(hs20), wpabuf_len(hs20));
2429 wpa_ie_len += wpabuf_len(hs20);
2430 }
2431 wpabuf_free(hs20);
2432
2433 hs20_configure_frame_filters(wpa_s);
2434 }
2435 }
2436 #endif /* CONFIG_HS20 */
2437
2438 if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
2439 struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
2440 size_t len;
2441
2442 len = sizeof(wpa_ie) - wpa_ie_len;
2443 if (wpabuf_len(buf) <= len) {
2444 os_memcpy(wpa_ie + wpa_ie_len,
2445 wpabuf_head(buf), wpabuf_len(buf));
2446 wpa_ie_len += wpabuf_len(buf);
2447 }
2448 }
2449
2450 #ifdef CONFIG_FST
2451 if (wpa_s->fst_ies) {
2452 int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
2453
2454 if (wpa_ie_len + fst_ies_len <= sizeof(wpa_ie)) {
2455 os_memcpy(wpa_ie + wpa_ie_len,
2456 wpabuf_head(wpa_s->fst_ies), fst_ies_len);
2457 wpa_ie_len += fst_ies_len;
2458 }
2459 }
2460 #endif /* CONFIG_FST */
2461
2462 #ifdef CONFIG_MBO
2463 if (bss && wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
2464 int len;
2465
2466 len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
2467 sizeof(wpa_ie) - wpa_ie_len);
2468 if (len >= 0)
2469 wpa_ie_len += len;
2470 }
2471 #endif /* CONFIG_MBO */
2472
2473 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
2474 use_crypt = 1;
2475 cipher_pairwise = wpa_s->pairwise_cipher;
2476 cipher_group = wpa_s->group_cipher;
2477 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
2478 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2479 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
2480 use_crypt = 0;
2481 if (wpa_set_wep_keys(wpa_s, ssid)) {
2482 use_crypt = 1;
2483 wep_keys_set = 1;
2484 }
2485 }
2486 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
2487 use_crypt = 0;
2488
2489 #ifdef IEEE8021X_EAPOL
2490 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2491 if ((ssid->eapol_flags &
2492 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
2493 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
2494 !wep_keys_set) {
2495 use_crypt = 0;
2496 } else {
2497 /* Assume that dynamic WEP-104 keys will be used and
2498 * set cipher suites in order for drivers to expect
2499 * encryption. */
2500 cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
2501 }
2502 }
2503 #endif /* IEEE8021X_EAPOL */
2504
2505 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2506 /* Set the key before (and later after) association */
2507 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
2508 }
2509
2510 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
2511 if (bss) {
2512 params.ssid = bss->ssid;
2513 params.ssid_len = bss->ssid_len;
2514 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
2515 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
2516 wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
2517 MACSTR " freq=%u MHz based on scan results "
2518 "(bssid_set=%d wps=%d)",
2519 MAC2STR(bss->bssid), bss->freq,
2520 ssid->bssid_set,
2521 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
2522 params.bssid = bss->bssid;
2523 params.freq.freq = bss->freq;
2524 }
2525 params.bssid_hint = bss->bssid;
2526 params.freq_hint = bss->freq;
2527 params.pbss = bss_is_pbss(bss);
2528 } else {
2529 params.ssid = ssid->ssid;
2530 params.ssid_len = ssid->ssid_len;
2531 params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
2532 }
2533
2534 if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
2535 wpa_s->conf->ap_scan == 2) {
2536 params.bssid = ssid->bssid;
2537 params.fixed_bssid = 1;
2538 }
2539
2540 /* Initial frequency for IBSS/mesh */
2541 if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
2542 ssid->frequency > 0 && params.freq.freq == 0)
2543 ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
2544
2545 if (ssid->mode == WPAS_MODE_IBSS) {
2546 params.fixed_freq = ssid->fixed_freq;
2547 if (ssid->beacon_int)
2548 params.beacon_int = ssid->beacon_int;
2549 else
2550 params.beacon_int = wpa_s->conf->beacon_int;
2551 }
2552
2553 params.wpa_ie = wpa_ie;
2554 params.wpa_ie_len = wpa_ie_len;
2555 params.pairwise_suite = cipher_pairwise;
2556 params.group_suite = cipher_group;
2557 params.key_mgmt_suite = wpa_s->key_mgmt;
2558 params.wpa_proto = wpa_s->wpa_proto;
2559 params.auth_alg = algs;
2560 params.mode = ssid->mode;
2561 params.bg_scan_period = ssid->bg_scan_period;
2562 for (i = 0; i < NUM_WEP_KEYS; i++) {
2563 if (ssid->wep_key_len[i])
2564 params.wep_key[i] = ssid->wep_key[i];
2565 params.wep_key_len[i] = ssid->wep_key_len[i];
2566 }
2567 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
2568
2569 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
2570 (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
2571 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
2572 params.passphrase = ssid->passphrase;
2573 if (ssid->psk_set)
2574 params.psk = ssid->psk;
2575 }
2576
2577 if (wpa_s->conf->key_mgmt_offload) {
2578 if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
2579 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
2580 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
2581 params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
2582 params.req_key_mgmt_offload =
2583 ssid->proactive_key_caching < 0 ?
2584 wpa_s->conf->okc : ssid->proactive_key_caching;
2585 else
2586 params.req_key_mgmt_offload = 1;
2587
2588 if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
2589 params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
2590 params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
2591 ssid->psk_set)
2592 params.psk = ssid->psk;
2593 }
2594
2595 params.drop_unencrypted = use_crypt;
2596
2597 #ifdef CONFIG_IEEE80211W
2598 params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
2599 if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
2600 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
2601 struct wpa_ie_data ie;
2602 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
2603 ie.capabilities &
2604 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
2605 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
2606 "MFP: require MFP");
2607 params.mgmt_frame_protection =
2608 MGMT_FRAME_PROTECTION_REQUIRED;
2609 }
2610 }
2611 #endif /* CONFIG_IEEE80211W */
2612
2613 params.p2p = ssid->p2p_group;
2614
2615 if (wpa_s->p2pdev->set_sta_uapsd)
2616 params.uapsd = wpa_s->p2pdev->sta_uapsd;
2617 else
2618 params.uapsd = -1;
2619
2620 #ifdef CONFIG_HT_OVERRIDES
2621 os_memset(&htcaps, 0, sizeof(htcaps));
2622 os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
2623 params.htcaps = (u8 *) &htcaps;
2624 params.htcaps_mask = (u8 *) &htcaps_mask;
2625 wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
2626 #endif /* CONFIG_HT_OVERRIDES */
2627 #ifdef CONFIG_VHT_OVERRIDES
2628 os_memset(&vhtcaps, 0, sizeof(vhtcaps));
2629 os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
2630 params.vhtcaps = &vhtcaps;
2631 params.vhtcaps_mask = &vhtcaps_mask;
2632 wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
2633 #endif /* CONFIG_VHT_OVERRIDES */
2634
2635 #ifdef CONFIG_P2P
2636 /*
2637 * If multi-channel concurrency is not supported, check for any
2638 * frequency conflict. In case of any frequency conflict, remove the
2639 * least prioritized connection.
2640 */
2641 if (wpa_s->num_multichan_concurrent < 2) {
2642 int freq, num;
2643 num = get_shared_radio_freqs(wpa_s, &freq, 1);
2644 if (num > 0 && freq > 0 && freq != params.freq.freq) {
2645 wpa_printf(MSG_DEBUG,
2646 "Assoc conflicting freq found (%d != %d)",
2647 freq, params.freq.freq);
2648 if (wpas_p2p_handle_frequency_conflicts(
2649 wpa_s, params.freq.freq, ssid) < 0) {
2650 wpas_connect_work_done(wpa_s);
2651 return;
2652 }
2653 }
2654 }
2655 #endif /* CONFIG_P2P */
2656
2657 if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
2658 wpa_s->current_ssid)
2659 params.prev_bssid = prev_bssid;
2660
2661 ret = wpa_drv_associate(wpa_s, &params);
2662 if (ret < 0) {
2663 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
2664 "failed");
2665 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
2666 /*
2667 * The driver is known to mean what is saying, so we
2668 * can stop right here; the association will not
2669 * succeed.
2670 */
2671 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
2672 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
2673 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
2674 return;
2675 }
2676 /* try to continue anyway; new association will be tried again
2677 * after timeout */
2678 assoc_failed = 1;
2679 }
2680
2681 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
2682 /* Set the key after the association just in case association
2683 * cleared the previously configured key. */
2684 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
2685 /* No need to timeout authentication since there is no key
2686 * management. */
2687 wpa_supplicant_cancel_auth_timeout(wpa_s);
2688 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
2689 #ifdef CONFIG_IBSS_RSN
2690 } else if (ssid->mode == WPAS_MODE_IBSS &&
2691 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
2692 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
2693 /*
2694 * RSN IBSS authentication is per-STA and we can disable the
2695 * per-BSSID authentication.
2696 */
2697 wpa_supplicant_cancel_auth_timeout(wpa_s);
2698 #endif /* CONFIG_IBSS_RSN */
2699 } else {
2700 /* Timeout for IEEE 802.11 authentication and association */
2701 int timeout = 60;
2702
2703 if (assoc_failed) {
2704 /* give IBSS a bit more time */
2705 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
2706 } else if (wpa_s->conf->ap_scan == 1) {
2707 /* give IBSS a bit more time */
2708 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
2709 }
2710 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
2711 }
2712
2713 if (wep_keys_set &&
2714 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
2715 /* Set static WEP keys again */
2716 wpa_set_wep_keys(wpa_s, ssid);
2717 }
2718
2719 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
2720 /*
2721 * Do not allow EAP session resumption between different
2722 * network configurations.
2723 */
2724 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2725 }
2726 old_ssid = wpa_s->current_ssid;
2727 wpa_s->current_ssid = ssid;
2728
2729 if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
2730 wpa_s->current_bss = bss;
2731 #ifdef CONFIG_HS20
2732 hs20_configure_frame_filters(wpa_s);
2733 #endif /* CONFIG_HS20 */
2734 }
2735
2736 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
2737 wpa_supplicant_initiate_eapol(wpa_s);
2738 if (old_ssid != wpa_s->current_ssid)
2739 wpas_notify_network_changed(wpa_s);
2740 }
2741
2742
2743 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
2744 const u8 *addr)
2745 {
2746 struct wpa_ssid *old_ssid;
2747
2748 wpas_connect_work_done(wpa_s);
2749 wpa_clear_keys(wpa_s, addr);
2750 old_ssid = wpa_s->current_ssid;
2751 wpa_supplicant_mark_disassoc(wpa_s);
2752 wpa_sm_set_config(wpa_s->wpa, NULL);
2753 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2754 if (old_ssid != wpa_s->current_ssid)
2755 wpas_notify_network_changed(wpa_s);
2756 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
2757 }
2758
2759
2760 /**
2761 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
2762 * @wpa_s: Pointer to wpa_supplicant data
2763 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
2764 *
2765 * This function is used to request %wpa_supplicant to deauthenticate from the
2766 * current AP.
2767 */
2768 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
2769 int reason_code)
2770 {
2771 u8 *addr = NULL;
2772 union wpa_event_data event;
2773 int zero_addr = 0;
2774
2775 wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
2776 " pending_bssid=" MACSTR " reason=%d state=%s",
2777 MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
2778 reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
2779
2780 if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
2781 (wpa_s->wpa_state == WPA_AUTHENTICATING ||
2782 wpa_s->wpa_state == WPA_ASSOCIATING))
2783 addr = wpa_s->pending_bssid;
2784 else if (!is_zero_ether_addr(wpa_s->bssid))
2785 addr = wpa_s->bssid;
2786 else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
2787 /*
2788 * When using driver-based BSS selection, we may not know the
2789 * BSSID with which we are currently trying to associate. We
2790 * need to notify the driver of this disconnection even in such
2791 * a case, so use the all zeros address here.
2792 */
2793 addr = wpa_s->bssid;
2794 zero_addr = 1;
2795 }
2796
2797 #ifdef CONFIG_TDLS
2798 wpa_tdls_teardown_peers(wpa_s->wpa);
2799 #endif /* CONFIG_TDLS */
2800
2801 #ifdef CONFIG_MESH
2802 if (wpa_s->ifmsh) {
2803 wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
2804 wpa_s->ifname);
2805 wpa_supplicant_leave_mesh(wpa_s);
2806 }
2807 #endif /* CONFIG_MESH */
2808
2809 if (addr) {
2810 wpa_drv_deauthenticate(wpa_s, addr, reason_code);
2811 os_memset(&event, 0, sizeof(event));
2812 event.deauth_info.reason_code = (u16) reason_code;
2813 event.deauth_info.locally_generated = 1;
2814 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
2815 if (zero_addr)
2816 addr = NULL;
2817 }
2818
2819 wpa_supplicant_clear_connection(wpa_s, addr);
2820 }
2821
2822 static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
2823 struct wpa_ssid *ssid)
2824 {
2825 if (!ssid || !ssid->disabled || ssid->disabled == 2)
2826 return;
2827
2828 ssid->disabled = 0;
2829 wpas_clear_temp_disabled(wpa_s, ssid, 1);
2830 wpas_notify_network_enabled_changed(wpa_s, ssid);
2831
2832 /*
2833 * Try to reassociate since there is no current configuration and a new
2834 * network was made available.
2835 */
2836 if (!wpa_s->current_ssid && !wpa_s->disconnected)
2837 wpa_s->reassociate = 1;
2838 }
2839
2840
2841 /**
2842 * wpa_supplicant_add_network - Add a new network
2843 * @wpa_s: wpa_supplicant structure for a network interface
2844 * Returns: The new network configuration or %NULL if operation failed
2845 *
2846 * This function performs the following operations:
2847 * 1. Adds a new network.
2848 * 2. Send network addition notification.
2849 * 3. Marks the network disabled.
2850 * 4. Set network default parameters.
2851 */
2852 struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
2853 {
2854 struct wpa_ssid *ssid;
2855
2856 ssid = wpa_config_add_network(wpa_s->conf);
2857 if (!ssid)
2858 return NULL;
2859 wpas_notify_network_added(wpa_s, ssid);
2860 ssid->disabled = 1;
2861 wpa_config_set_network_defaults(ssid);
2862
2863 return ssid;
2864 }
2865
2866
2867 /**
2868 * wpa_supplicant_remove_network - Remove a configured network based on id
2869 * @wpa_s: wpa_supplicant structure for a network interface
2870 * @id: Unique network id to search for
2871 * Returns: 0 on success, or -1 if the network was not found, -2 if the network
2872 * could not be removed
2873 *
2874 * This function performs the following operations:
2875 * 1. Removes the network.
2876 * 2. Send network removal notification.
2877 * 3. Update internal state machines.
2878 * 4. Stop any running sched scans.
2879 */
2880 int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
2881 {
2882 struct wpa_ssid *ssid;
2883 int was_disabled;
2884
2885 ssid = wpa_config_get_network(wpa_s->conf, id);
2886 if (!ssid)
2887 return -1;
2888 wpas_notify_network_removed(wpa_s, ssid);
2889
2890 if (wpa_s->last_ssid == ssid)
2891 wpa_s->last_ssid = NULL;
2892
2893 if (ssid == wpa_s->current_ssid || !wpa_s->current_ssid) {
2894 #ifdef CONFIG_SME
2895 wpa_s->sme.prev_bssid_set = 0;
2896 #endif /* CONFIG_SME */
2897 /*
2898 * Invalidate the EAP session cache if the current or
2899 * previously used network is removed.
2900 */
2901 eapol_sm_invalidate_cached_session(wpa_s->eapol);
2902 }
2903
2904 if (ssid == wpa_s->current_ssid) {
2905 wpa_sm_set_config(wpa_s->wpa, NULL);
2906 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
2907
2908 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2909 wpa_s->own_disconnect_req = 1;
2910 wpa_supplicant_deauthenticate(wpa_s,
2911 WLAN_REASON_DEAUTH_LEAVING);
2912 }
2913
2914 was_disabled = ssid->disabled;
2915
2916 if (wpa_config_remove_network(wpa_s->conf, id) < 0)
2917 return -2;
2918
2919 if (!was_disabled && wpa_s->sched_scanning) {
2920 wpa_printf(MSG_DEBUG,
2921 "Stop ongoing sched_scan to remove network from filters");
2922 wpa_supplicant_cancel_sched_scan(wpa_s);
2923 wpa_supplicant_req_scan(wpa_s, 0, 0);
2924 }
2925
2926 return 0;
2927 }
2928
2929
2930 /**
2931 * wpa_supplicant_enable_network - Mark a configured network as enabled
2932 * @wpa_s: wpa_supplicant structure for a network interface
2933 * @ssid: wpa_ssid structure for a configured network or %NULL
2934 *
2935 * Enables the specified network or all networks if no network specified.
2936 */
2937 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
2938 struct wpa_ssid *ssid)
2939 {
2940 if (ssid == NULL) {
2941 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2942 wpa_supplicant_enable_one_network(wpa_s, ssid);
2943 } else
2944 wpa_supplicant_enable_one_network(wpa_s, ssid);
2945
2946 if (wpa_s->reassociate && !wpa_s->disconnected &&
2947 (!wpa_s->current_ssid ||
2948 wpa_s->wpa_state == WPA_DISCONNECTED ||
2949 wpa_s->wpa_state == WPA_SCANNING)) {
2950 if (wpa_s->sched_scanning) {
2951 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
2952 "new network to scan filters");
2953 wpa_supplicant_cancel_sched_scan(wpa_s);
2954 }
2955
2956 if (wpa_supplicant_fast_associate(wpa_s) != 1) {
2957 wpa_s->scan_req = NORMAL_SCAN_REQ;
2958 wpa_supplicant_req_scan(wpa_s, 0, 0);
2959 }
2960 }
2961 }
2962
2963
2964 /**
2965 * wpa_supplicant_disable_network - Mark a configured network as disabled
2966 * @wpa_s: wpa_supplicant structure for a network interface
2967 * @ssid: wpa_ssid structure for a configured network or %NULL
2968 *
2969 * Disables the specified network or all networks if no network specified.
2970 */
2971 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
2972 struct wpa_ssid *ssid)
2973 {
2974 struct wpa_ssid *other_ssid;
2975 int was_disabled;
2976
2977 if (ssid == NULL) {
2978 if (wpa_s->sched_scanning)
2979 wpa_supplicant_cancel_sched_scan(wpa_s);
2980
2981 for (other_ssid = wpa_s->conf->ssid; other_ssid;
2982 other_ssid = other_ssid->next) {
2983 was_disabled = other_ssid->disabled;
2984 if (was_disabled == 2)
2985 continue; /* do not change persistent P2P group
2986 * data */
2987
2988 other_ssid->disabled = 1;
2989
2990 if (was_disabled != other_ssid->disabled)
2991 wpas_notify_network_enabled_changed(
2992 wpa_s, other_ssid);
2993 }
2994 if (wpa_s->current_ssid)
2995 wpa_supplicant_deauthenticate(
2996 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
2997 } else if (ssid->disabled != 2) {
2998 if (ssid == wpa_s->current_ssid)
2999 wpa_supplicant_deauthenticate(
3000 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3001
3002 was_disabled = ssid->disabled;
3003
3004 ssid->disabled = 1;
3005
3006 if (was_disabled != ssid->disabled) {
3007 wpas_notify_network_enabled_changed(wpa_s, ssid);
3008 if (wpa_s->sched_scanning) {
3009 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
3010 "to remove network from filters");
3011 wpa_supplicant_cancel_sched_scan(wpa_s);
3012 wpa_supplicant_req_scan(wpa_s, 0, 0);
3013 }
3014 }
3015 }
3016 }
3017
3018
3019 /**
3020 * wpa_supplicant_select_network - Attempt association with a network
3021 * @wpa_s: wpa_supplicant structure for a network interface
3022 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
3023 */
3024 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
3025 struct wpa_ssid *ssid)
3026 {
3027
3028 struct wpa_ssid *other_ssid;
3029 int disconnected = 0;
3030
3031 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
3032 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3033 wpa_s->own_disconnect_req = 1;
3034 wpa_supplicant_deauthenticate(
3035 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3036 disconnected = 1;
3037 }
3038
3039 if (ssid)
3040 wpas_clear_temp_disabled(wpa_s, ssid, 1);
3041
3042 /*
3043 * Mark all other networks disabled or mark all networks enabled if no
3044 * network specified.
3045 */
3046 for (other_ssid = wpa_s->conf->ssid; other_ssid;
3047 other_ssid = other_ssid->next) {
3048 int was_disabled = other_ssid->disabled;
3049 if (was_disabled == 2)
3050 continue; /* do not change persistent P2P group data */
3051
3052 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
3053 if (was_disabled && !other_ssid->disabled)
3054 wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
3055
3056 if (was_disabled != other_ssid->disabled)
3057 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
3058 }
3059
3060 if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
3061 wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3062 /* We are already associated with the selected network */
3063 wpa_printf(MSG_DEBUG, "Already associated with the "
3064 "selected network - do nothing");
3065 return;
3066 }
3067
3068 if (ssid) {
3069 wpa_s->current_ssid = ssid;
3070 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
3071 wpa_s->connect_without_scan =
3072 (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
3073
3074 /*
3075 * Don't optimize next scan freqs since a new ESS has been
3076 * selected.
3077 */
3078 os_free(wpa_s->next_scan_freqs);
3079 wpa_s->next_scan_freqs = NULL;
3080 } else {
3081 wpa_s->connect_without_scan = NULL;
3082 }
3083
3084 wpa_s->disconnected = 0;
3085 wpa_s->reassociate = 1;
3086
3087 if (wpa_s->connect_without_scan ||
3088 wpa_supplicant_fast_associate(wpa_s) != 1) {
3089 wpa_s->scan_req = NORMAL_SCAN_REQ;
3090 wpas_scan_reset_sched_scan(wpa_s);
3091 wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
3092 }
3093
3094 if (ssid)
3095 wpas_notify_network_selected(wpa_s, ssid);
3096 }
3097
3098
3099 /**
3100 * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
3101 * @wpa_s: wpa_supplicant structure for a network interface
3102 * @pkcs11_engine_path: PKCS #11 engine path or NULL
3103 * @pkcs11_module_path: PKCS #11 module path or NULL
3104 * Returns: 0 on success; -1 on failure
3105 *
3106 * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
3107 * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
3108 * module path fails the paths will be reset to the default value (NULL).
3109 */
3110 int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
3111 const char *pkcs11_engine_path,
3112 const char *pkcs11_module_path)
3113 {
3114 char *pkcs11_engine_path_copy = NULL;
3115 char *pkcs11_module_path_copy = NULL;
3116
3117 if (pkcs11_engine_path != NULL) {
3118 pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
3119 if (pkcs11_engine_path_copy == NULL)
3120 return -1;
3121 }
3122 if (pkcs11_module_path != NULL) {
3123 pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
3124 if (pkcs11_module_path_copy == NULL) {
3125 os_free(pkcs11_engine_path_copy);
3126 return -1;
3127 }
3128 }
3129
3130 os_free(wpa_s->conf->pkcs11_engine_path);
3131 os_free(wpa_s->conf->pkcs11_module_path);
3132 wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
3133 wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
3134
3135 wpa_sm_set_eapol(wpa_s->wpa, NULL);
3136 eapol_sm_deinit(wpa_s->eapol);
3137 wpa_s->eapol = NULL;
3138 if (wpa_supplicant_init_eapol(wpa_s)) {
3139 /* Error -> Reset paths to the default value (NULL) once. */
3140 if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
3141 wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
3142 NULL);
3143
3144 return -1;
3145 }
3146 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
3147
3148 return 0;
3149 }
3150
3151
3152 /**
3153 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
3154 * @wpa_s: wpa_supplicant structure for a network interface
3155 * @ap_scan: AP scan mode
3156 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
3157 *
3158 */
3159 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
3160 {
3161
3162 int old_ap_scan;
3163
3164 if (ap_scan < 0 || ap_scan > 2)
3165 return -1;
3166
3167 if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
3168 wpa_printf(MSG_INFO,
3169 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
3170 }
3171
3172 #ifdef ANDROID
3173 if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
3174 wpa_s->wpa_state >= WPA_ASSOCIATING &&
3175 wpa_s->wpa_state < WPA_COMPLETED) {
3176 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
3177 "associating", wpa_s->conf->ap_scan, ap_scan);
3178 return 0;
3179 }
3180 #endif /* ANDROID */
3181
3182 old_ap_scan = wpa_s->conf->ap_scan;
3183 wpa_s->conf->ap_scan = ap_scan;
3184
3185 if (old_ap_scan != wpa_s->conf->ap_scan)
3186 wpas_notify_ap_scan_changed(wpa_s);
3187
3188 return 0;
3189 }
3190
3191
3192 /**
3193 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
3194 * @wpa_s: wpa_supplicant structure for a network interface
3195 * @expire_age: Expiration age in seconds
3196 * Returns: 0 if succeed or -1 if expire_age has an invalid value
3197 *
3198 */
3199 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
3200 unsigned int bss_expire_age)
3201 {
3202 if (bss_expire_age < 10) {
3203 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
3204 bss_expire_age);
3205 return -1;
3206 }
3207 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
3208 bss_expire_age);
3209 wpa_s->conf->bss_expiration_age = bss_expire_age;
3210
3211 return 0;
3212 }
3213
3214
3215 /**
3216 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
3217 * @wpa_s: wpa_supplicant structure for a network interface
3218 * @expire_count: number of scans after which an unseen BSS is reclaimed
3219 * Returns: 0 if succeed or -1 if expire_count has an invalid value
3220 *
3221 */
3222 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
3223 unsigned int bss_expire_count)
3224 {
3225 if (bss_expire_count < 1) {
3226 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
3227 bss_expire_count);
3228 return -1;
3229 }
3230 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
3231 bss_expire_count);
3232 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
3233
3234 return 0;
3235 }
3236
3237
3238 /**
3239 * wpa_supplicant_set_scan_interval - Set scan interval
3240 * @wpa_s: wpa_supplicant structure for a network interface
3241 * @scan_interval: scan interval in seconds
3242 * Returns: 0 if succeed or -1 if scan_interval has an invalid value
3243 *
3244 */
3245 int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
3246 int scan_interval)
3247 {
3248 if (scan_interval < 0) {
3249 wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
3250 scan_interval);
3251 return -1;
3252 }
3253 wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
3254 scan_interval);
3255 wpa_supplicant_update_scan_int(wpa_s, scan_interval);
3256
3257 return 0;
3258 }
3259
3260
3261 /**
3262 * wpa_supplicant_set_debug_params - Set global debug params
3263 * @global: wpa_global structure
3264 * @debug_level: debug level
3265 * @debug_timestamp: determines if show timestamp in debug data
3266 * @debug_show_keys: determines if show keys in debug data
3267 * Returns: 0 if succeed or -1 if debug_level has wrong value
3268 */
3269 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
3270 int debug_timestamp, int debug_show_keys)
3271 {
3272
3273 int old_level, old_timestamp, old_show_keys;
3274
3275 /* check for allowed debuglevels */
3276 if (debug_level != MSG_EXCESSIVE &&
3277 debug_level != MSG_MSGDUMP &&
3278 debug_level != MSG_DEBUG &&
3279 debug_level != MSG_INFO &&
3280 debug_level != MSG_WARNING &&
3281 debug_level != MSG_ERROR)
3282 return -1;
3283
3284 old_level = wpa_debug_level;
3285 old_timestamp = wpa_debug_timestamp;
3286 old_show_keys = wpa_debug_show_keys;
3287
3288 wpa_debug_level = debug_level;
3289 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
3290 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
3291
3292 if (wpa_debug_level != old_level)
3293 wpas_notify_debug_level_changed(global);
3294 if (wpa_debug_timestamp != old_timestamp)
3295 wpas_notify_debug_timestamp_changed(global);
3296 if (wpa_debug_show_keys != old_show_keys)
3297 wpas_notify_debug_show_keys_changed(global);
3298
3299 return 0;
3300 }
3301
3302
3303 /**
3304 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
3305 * @wpa_s: Pointer to wpa_supplicant data
3306 * Returns: A pointer to the current network structure or %NULL on failure
3307 */
3308 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
3309 {
3310 struct wpa_ssid *entry;
3311 u8 ssid[SSID_MAX_LEN];
3312 int res;
3313 size_t ssid_len;
3314 u8 bssid[ETH_ALEN];
3315 int wired;
3316
3317 res = wpa_drv_get_ssid(wpa_s, ssid);
3318 if (res < 0) {
3319 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
3320 "driver");
3321 return NULL;
3322 }
3323 ssid_len = res;
3324
3325 if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
3326 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
3327 "driver");
3328 return NULL;
3329 }
3330
3331 wired = wpa_s->conf->ap_scan == 0 &&
3332 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
3333
3334 entry = wpa_s->conf->ssid;
3335 while (entry) {
3336 if (!wpas_network_disabled(wpa_s, entry) &&
3337 ((ssid_len == entry->ssid_len &&
3338 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
3339 (!entry->bssid_set ||
3340 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
3341 return entry;
3342 #ifdef CONFIG_WPS
3343 if (!wpas_network_disabled(wpa_s, entry) &&
3344 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
3345 (entry->ssid == NULL || entry->ssid_len == 0) &&
3346 (!entry->bssid_set ||
3347 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
3348 return entry;
3349 #endif /* CONFIG_WPS */
3350
3351 if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
3352 entry->ssid_len == 0 &&
3353 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
3354 return entry;
3355
3356 entry = entry->next;
3357 }
3358
3359 return NULL;
3360 }
3361
3362
3363 static int select_driver(struct wpa_supplicant *wpa_s, int i)
3364 {
3365 struct wpa_global *global = wpa_s->global;
3366
3367 if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
3368 global->drv_priv[i] = wpa_drivers[i]->global_init(global);
3369 if (global->drv_priv[i] == NULL) {
3370 wpa_printf(MSG_ERROR, "Failed to initialize driver "
3371 "'%s'", wpa_drivers[i]->name);
3372 return -1;
3373 }
3374 }
3375
3376 wpa_s->driver = wpa_drivers[i];
3377 wpa_s->global_drv_priv = global->drv_priv[i];
3378
3379 return 0;
3380 }
3381
3382
3383 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
3384 const char *name)
3385 {
3386 int i;
3387 size_t len;
3388 const char *pos, *driver = name;
3389
3390 if (wpa_s == NULL)
3391 return -1;
3392
3393 if (wpa_drivers[0] == NULL) {
3394 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
3395 "wpa_supplicant");
3396 return -1;
3397 }
3398
3399 if (name == NULL) {
3400 /* default to first driver in the list */
3401 return select_driver(wpa_s, 0);
3402 }
3403
3404 do {
3405 pos = os_strchr(driver, ',');
3406 if (pos)
3407 len = pos - driver;
3408 else
3409 len = os_strlen(driver);
3410
3411 for (i = 0; wpa_drivers[i]; i++) {
3412 if (os_strlen(wpa_drivers[i]->name) == len &&
3413 os_strncmp(driver, wpa_drivers[i]->name, len) ==
3414 0) {
3415 /* First driver that succeeds wins */
3416 if (select_driver(wpa_s, i) == 0)
3417 return 0;
3418 }
3419 }
3420
3421 driver = pos + 1;
3422 } while (pos);
3423
3424 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
3425 return -1;
3426 }
3427
3428
3429 /**
3430 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
3431 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
3432 * with struct wpa_driver_ops::init()
3433 * @src_addr: Source address of the EAPOL frame
3434 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
3435 * @len: Length of the EAPOL data
3436 *
3437 * This function is called for each received EAPOL frame. Most driver
3438 * interfaces rely on more generic OS mechanism for receiving frames through
3439 * l2_packet, but if such a mechanism is not available, the driver wrapper may
3440 * take care of received EAPOL frames and deliver them to the core supplicant
3441 * code by calling this function.
3442 */
3443 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
3444 const u8 *buf, size_t len)
3445 {
3446 struct wpa_supplicant *wpa_s = ctx;
3447
3448 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
3449 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
3450
3451 #ifdef CONFIG_TESTING_OPTIONS
3452 if (wpa_s->ignore_auth_resp) {
3453 wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
3454 return;
3455 }
3456 #endif /* CONFIG_TESTING_OPTIONS */
3457
3458 #ifdef CONFIG_PEERKEY
3459 if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
3460 wpa_s->current_ssid->peerkey &&
3461 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
3462 wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
3463 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
3464 return;
3465 }
3466 #endif /* CONFIG_PEERKEY */
3467
3468 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
3469 (wpa_s->last_eapol_matches_bssid &&
3470 #ifdef CONFIG_AP
3471 !wpa_s->ap_iface &&
3472 #endif /* CONFIG_AP */
3473 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
3474 /*
3475 * There is possible race condition between receiving the
3476 * association event and the EAPOL frame since they are coming
3477 * through different paths from the driver. In order to avoid
3478 * issues in trying to process the EAPOL frame before receiving
3479 * association information, lets queue it for processing until
3480 * the association event is received. This may also be needed in
3481 * driver-based roaming case, so also use src_addr != BSSID as a
3482 * trigger if we have previously confirmed that the
3483 * Authenticator uses BSSID as the src_addr (which is not the
3484 * case with wired IEEE 802.1X).
3485 */
3486 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
3487 "of received EAPOL frame (state=%s bssid=" MACSTR ")",
3488 wpa_supplicant_state_txt(wpa_s->wpa_state),
3489 MAC2STR(wpa_s->bssid));
3490 wpabuf_free(wpa_s->pending_eapol_rx);
3491 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
3492 if (wpa_s->pending_eapol_rx) {
3493 os_get_reltime(&wpa_s->pending_eapol_rx_time);
3494 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
3495 ETH_ALEN);
3496 }
3497 return;
3498 }
3499
3500 wpa_s->last_eapol_matches_bssid =
3501 os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
3502
3503 #ifdef CONFIG_AP
3504 if (wpa_s->ap_iface) {
3505 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
3506 return;
3507 }
3508 #endif /* CONFIG_AP */
3509
3510 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
3511 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
3512 "no key management is configured");
3513 return;
3514 }
3515
3516 if (wpa_s->eapol_received == 0 &&
3517 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
3518 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
3519 wpa_s->wpa_state != WPA_COMPLETED) &&
3520 (wpa_s->current_ssid == NULL ||
3521 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
3522 /* Timeout for completing IEEE 802.1X and WPA authentication */
3523 int timeout = 10;
3524
3525 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
3526 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
3527 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
3528 /* Use longer timeout for IEEE 802.1X/EAP */
3529 timeout = 70;
3530 }
3531
3532 #ifdef CONFIG_WPS
3533 if (wpa_s->current_ssid && wpa_s->current_bss &&
3534 (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
3535 eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
3536 /*
3537 * Use shorter timeout if going through WPS AP iteration
3538 * for PIN config method with an AP that does not
3539 * advertise Selected Registrar.
3540 */
3541 struct wpabuf *wps_ie;
3542
3543 wps_ie = wpa_bss_get_vendor_ie_multi(
3544 wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
3545 if (wps_ie &&
3546 !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
3547 timeout = 10;
3548 wpabuf_free(wps_ie);
3549 }
3550 #endif /* CONFIG_WPS */
3551
3552 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
3553 }
3554 wpa_s->eapol_received++;
3555
3556 if (wpa_s->countermeasures) {
3557 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
3558 "EAPOL packet");
3559 return;
3560 }
3561
3562 #ifdef CONFIG_IBSS_RSN
3563 if (wpa_s->current_ssid &&
3564 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
3565 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
3566 return;
3567 }
3568 #endif /* CONFIG_IBSS_RSN */
3569
3570 /* Source address of the incoming EAPOL frame could be compared to the
3571 * current BSSID. However, it is possible that a centralized
3572 * Authenticator could be using another MAC address than the BSSID of
3573 * an AP, so just allow any address to be used for now. The replies are
3574 * still sent to the current BSSID (if available), though. */
3575
3576 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
3577 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
3578 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
3579 return;
3580 wpa_drv_poll(wpa_s);
3581 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
3582 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
3583 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
3584 /*
3585 * Set portValid = TRUE here since we are going to skip 4-way
3586 * handshake processing which would normally set portValid. We
3587 * need this to allow the EAPOL state machines to be completed
3588 * without going through EAPOL-Key handshake.
3589 */
3590 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
3591 }
3592 }
3593
3594
3595 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
3596 {
3597 if ((!wpa_s->p2p_mgmt ||
3598 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
3599 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
3600 l2_packet_deinit(wpa_s->l2);
3601 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
3602 wpa_drv_get_mac_addr(wpa_s),
3603 ETH_P_EAPOL,
3604 wpa_supplicant_rx_eapol, wpa_s, 0);
3605 if (wpa_s->l2 == NULL)
3606 return -1;
3607 } else {
3608 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
3609 if (addr)
3610 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
3611 }
3612
3613 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
3614 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
3615 return -1;
3616 }
3617
3618 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
3619
3620 return 0;
3621 }
3622
3623
3624 static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
3625 const u8 *buf, size_t len)
3626 {
3627 struct wpa_supplicant *wpa_s = ctx;
3628 const struct l2_ethhdr *eth;
3629
3630 if (len < sizeof(*eth))
3631 return;
3632 eth = (const struct l2_ethhdr *) buf;
3633
3634 if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
3635 !(eth->h_dest[0] & 0x01)) {
3636 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
3637 " (bridge - not for this interface - ignore)",
3638 MAC2STR(src_addr), MAC2STR(eth->h_dest));
3639 return;
3640 }
3641
3642 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
3643 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
3644 wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
3645 len - sizeof(*eth));
3646 }
3647
3648
3649 /**
3650 * wpa_supplicant_driver_init - Initialize driver interface parameters
3651 * @wpa_s: Pointer to wpa_supplicant data
3652 * Returns: 0 on success, -1 on failure
3653 *
3654 * This function is called to initialize driver interface parameters.
3655 * wpa_drv_init() must have been called before this function to initialize the
3656 * driver interface.
3657 */
3658 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
3659 {
3660 static int interface_count = 0;
3661
3662 if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
3663 return -1;
3664
3665 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
3666 MAC2STR(wpa_s->own_addr));
3667 os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
3668 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
3669
3670 if (wpa_s->bridge_ifname[0]) {
3671 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
3672 "interface '%s'", wpa_s->bridge_ifname);
3673 wpa_s->l2_br = l2_packet_init_bridge(
3674 wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
3675 ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
3676 if (wpa_s->l2_br == NULL) {
3677 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
3678 "connection for the bridge interface '%s'",
3679 wpa_s->bridge_ifname);
3680 return -1;
3681 }
3682 }
3683
3684 if (wpa_s->conf->ap_scan == 2 &&
3685 os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
3686 wpa_printf(MSG_INFO,
3687 "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
3688 }
3689
3690 wpa_clear_keys(wpa_s, NULL);
3691
3692 /* Make sure that TKIP countermeasures are not left enabled (could
3693 * happen if wpa_supplicant is killed during countermeasures. */
3694 wpa_drv_set_countermeasures(wpa_s, 0);
3695
3696 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
3697 wpa_drv_flush_pmkid(wpa_s);
3698
3699 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
3700 wpa_s->prev_scan_wildcard = 0;
3701
3702 if (wpa_supplicant_enabled_networks(wpa_s)) {
3703 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
3704 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
3705 interface_count = 0;
3706 }
3707 #ifndef ANDROID
3708 if (!wpa_s->p2p_mgmt &&
3709 wpa_supplicant_delayed_sched_scan(wpa_s,
3710 interface_count % 3,
3711 100000))
3712 wpa_supplicant_req_scan(wpa_s, interface_count % 3,
3713 100000);
3714 #endif /* ANDROID */
3715 interface_count++;
3716 } else
3717 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
3718
3719 return 0;
3720 }
3721
3722
3723 static int wpa_supplicant_daemon(const char *pid_file)
3724 {
3725 wpa_printf(MSG_DEBUG, "Daemonize..");
3726 return os_daemonize(pid_file);
3727 }
3728
3729
3730 static struct wpa_supplicant *
3731 wpa_supplicant_alloc(struct wpa_supplicant *parent)
3732 {
3733 struct wpa_supplicant *wpa_s;
3734
3735 wpa_s = os_zalloc(sizeof(*wpa_s));
3736 if (wpa_s == NULL)
3737 return NULL;
3738 wpa_s->scan_req = INITIAL_SCAN_REQ;
3739 wpa_s->scan_interval = 5;
3740 wpa_s->new_connection = 1;
3741 wpa_s->parent = parent ? parent : wpa_s;
3742 wpa_s->p2pdev = wpa_s->parent;
3743 wpa_s->sched_scanning = 0;
3744
3745 dl_list_init(&wpa_s->bss_tmp_disallowed);
3746 dl_list_init(&wpa_s->fils_hlp_req);
3747
3748 return wpa_s;
3749 }
3750
3751
3752 #ifdef CONFIG_HT_OVERRIDES
3753
3754 static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
3755 struct ieee80211_ht_capabilities *htcaps,
3756 struct ieee80211_ht_capabilities *htcaps_mask,
3757 const char *ht_mcs)
3758 {
3759 /* parse ht_mcs into hex array */
3760 int i;
3761 const char *tmp = ht_mcs;
3762 char *end = NULL;
3763
3764 /* If ht_mcs is null, do not set anything */
3765 if (!ht_mcs)
3766 return 0;
3767
3768 /* This is what we are setting in the kernel */
3769 os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
3770
3771 wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
3772
3773 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
3774 long v;
3775
3776 errno = 0;
3777 v = strtol(tmp, &end, 16);
3778
3779 if (errno == 0) {
3780 wpa_msg(wpa_s, MSG_DEBUG,
3781 "htcap value[%i]: %ld end: %p tmp: %p",
3782 i, v, end, tmp);
3783 if (end == tmp)
3784 break;
3785
3786 htcaps->supported_mcs_set[i] = v;
3787 tmp = end;
3788 } else {
3789 wpa_msg(wpa_s, MSG_ERROR,
3790 "Failed to parse ht-mcs: %s, error: %s\n",
3791 ht_mcs, strerror(errno));
3792 return -1;
3793 }
3794 }
3795
3796 /*
3797 * If we were able to parse any values, then set mask for the MCS set.
3798 */
3799 if (i) {
3800 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
3801 IEEE80211_HT_MCS_MASK_LEN - 1);
3802 /* skip the 3 reserved bits */
3803 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
3804 0x1f;
3805 }
3806
3807 return 0;
3808 }
3809
3810
3811 static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
3812 struct ieee80211_ht_capabilities *htcaps,
3813 struct ieee80211_ht_capabilities *htcaps_mask,
3814 int disabled)
3815 {
3816 le16 msk;
3817
3818 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
3819
3820 if (disabled == -1)
3821 return 0;
3822
3823 msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
3824 htcaps_mask->ht_capabilities_info |= msk;
3825 if (disabled)
3826 htcaps->ht_capabilities_info &= msk;
3827 else
3828 htcaps->ht_capabilities_info |= msk;
3829
3830 return 0;
3831 }
3832
3833
3834 static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
3835 struct ieee80211_ht_capabilities *htcaps,
3836 struct ieee80211_ht_capabilities *htcaps_mask,
3837 int factor)
3838 {
3839 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
3840
3841 if (factor == -1)
3842 return 0;
3843
3844 if (factor < 0 || factor > 3) {
3845 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
3846 "Must be 0-3 or -1", factor);
3847 return -EINVAL;
3848 }
3849
3850 htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
3851 htcaps->a_mpdu_params &= ~0x3;
3852 htcaps->a_mpdu_params |= factor & 0x3;
3853
3854 return 0;
3855 }
3856
3857
3858 static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
3859 struct ieee80211_ht_capabilities *htcaps,
3860 struct ieee80211_ht_capabilities *htcaps_mask,
3861 int density)
3862 {
3863 wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
3864
3865 if (density == -1)
3866 return 0;
3867
3868 if (density < 0 || density > 7) {
3869 wpa_msg(wpa_s, MSG_ERROR,
3870 "ampdu_density: %d out of range. Must be 0-7 or -1.",
3871 density);
3872 return -EINVAL;
3873 }
3874
3875 htcaps_mask->a_mpdu_params |= 0x1C;
3876 htcaps->a_mpdu_params &= ~(0x1C);
3877 htcaps->a_mpdu_params |= (density << 2) & 0x1C;
3878
3879 return 0;
3880 }
3881
3882
3883 static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
3884 struct ieee80211_ht_capabilities *htcaps,
3885 struct ieee80211_ht_capabilities *htcaps_mask,
3886 int disabled)
3887 {
3888 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
3889
3890 set_disable_ht40(htcaps, disabled);
3891 set_disable_ht40(htcaps_mask, 0);
3892
3893 return 0;
3894 }
3895
3896
3897 static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
3898 struct ieee80211_ht_capabilities *htcaps,
3899 struct ieee80211_ht_capabilities *htcaps_mask,
3900 int disabled)
3901 {
3902 /* Masking these out disables SGI */
3903 le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
3904 HT_CAP_INFO_SHORT_GI40MHZ);
3905
3906 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
3907
3908 if (disabled)
3909 htcaps->ht_capabilities_info &= ~msk;
3910 else
3911 htcaps->ht_capabilities_info |= msk;
3912
3913 htcaps_mask->ht_capabilities_info |= msk;
3914
3915 return 0;
3916 }
3917
3918
3919 static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
3920 struct ieee80211_ht_capabilities *htcaps,
3921 struct ieee80211_ht_capabilities *htcaps_mask,
3922 int disabled)
3923 {
3924 /* Masking these out disables LDPC */
3925 le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
3926
3927 wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
3928
3929 if (disabled)
3930 htcaps->ht_capabilities_info &= ~msk;
3931 else
3932 htcaps->ht_capabilities_info |= msk;
3933
3934 htcaps_mask->ht_capabilities_info |= msk;
3935
3936 return 0;
3937 }
3938
3939
3940 void wpa_supplicant_apply_ht_overrides(
3941 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3942 struct wpa_driver_associate_params *params)
3943 {
3944 struct ieee80211_ht_capabilities *htcaps;
3945 struct ieee80211_ht_capabilities *htcaps_mask;
3946
3947 if (!ssid)
3948 return;
3949
3950 params->disable_ht = ssid->disable_ht;
3951 if (!params->htcaps || !params->htcaps_mask)
3952 return;
3953
3954 htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
3955 htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
3956 wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
3957 wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
3958 ssid->disable_max_amsdu);
3959 wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
3960 wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
3961 wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
3962 wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
3963 wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
3964
3965 if (ssid->ht40_intolerant) {
3966 le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
3967 htcaps->ht_capabilities_info |= bit;
3968 htcaps_mask->ht_capabilities_info |= bit;
3969 }
3970 }
3971
3972 #endif /* CONFIG_HT_OVERRIDES */
3973
3974
3975 #ifdef CONFIG_VHT_OVERRIDES
3976 void wpa_supplicant_apply_vht_overrides(
3977 struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
3978 struct wpa_driver_associate_params *params)
3979 {
3980 struct ieee80211_vht_capabilities *vhtcaps;
3981 struct ieee80211_vht_capabilities *vhtcaps_mask;
3982
3983 if (!ssid)
3984 return;
3985
3986 params->disable_vht = ssid->disable_vht;
3987
3988 vhtcaps = (void *) params->vhtcaps;
3989 vhtcaps_mask = (void *) params->vhtcaps_mask;
3990
3991 if (!vhtcaps || !vhtcaps_mask)
3992 return;
3993
3994 vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
3995 vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
3996
3997 #ifdef CONFIG_HT_OVERRIDES
3998 /* if max ampdu is <= 3, we have to make the HT cap the same */
3999 if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
4000 int max_ampdu;
4001
4002 max_ampdu = (ssid->vht_capa &
4003 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
4004 VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
4005
4006 max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
4007 wpa_set_ampdu_factor(wpa_s,
4008 (void *) params->htcaps,
4009 (void *) params->htcaps_mask,
4010 max_ampdu);
4011 }
4012 #endif /* CONFIG_HT_OVERRIDES */
4013
4014 #define OVERRIDE_MCS(i) \
4015 if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
4016 vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
4017 host_to_le16(3 << 2 * (i - 1)); \
4018 vhtcaps->vht_supported_mcs_set.tx_map |= \
4019 host_to_le16(ssid->vht_tx_mcs_nss_ ##i << \
4020 2 * (i - 1)); \
4021 } \
4022 if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
4023 vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
4024 host_to_le16(3 << 2 * (i - 1)); \
4025 vhtcaps->vht_supported_mcs_set.rx_map |= \
4026 host_to_le16(ssid->vht_rx_mcs_nss_ ##i << \
4027 2 * (i - 1)); \
4028 }
4029
4030 OVERRIDE_MCS(1);
4031 OVERRIDE_MCS(2);
4032 OVERRIDE_MCS(3);
4033 OVERRIDE_MCS(4);
4034 OVERRIDE_MCS(5);
4035 OVERRIDE_MCS(6);
4036 OVERRIDE_MCS(7);
4037 OVERRIDE_MCS(8);
4038 }
4039 #endif /* CONFIG_VHT_OVERRIDES */
4040
4041
4042 static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
4043 {
4044 #ifdef PCSC_FUNCS
4045 size_t len;
4046
4047 if (!wpa_s->conf->pcsc_reader)
4048 return 0;
4049
4050 wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
4051 if (!wpa_s->scard)
4052 return 1;
4053
4054 if (wpa_s->conf->pcsc_pin &&
4055 scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
4056 scard_deinit(wpa_s->scard);
4057 wpa_s->scard = NULL;
4058 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
4059 return -1;
4060 }
4061
4062 len = sizeof(wpa_s->imsi) - 1;
4063 if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
4064 scard_deinit(wpa_s->scard);
4065 wpa_s->scard = NULL;
4066 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
4067 return -1;
4068 }
4069 wpa_s->imsi[len] = '\0';
4070
4071 wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
4072
4073 wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
4074 wpa_s->imsi, wpa_s->mnc_len);
4075
4076 wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
4077 eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
4078 #endif /* PCSC_FUNCS */
4079
4080 return 0;
4081 }
4082
4083
4084 int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
4085 {
4086 char *val, *pos;
4087
4088 ext_password_deinit(wpa_s->ext_pw);
4089 wpa_s->ext_pw = NULL;
4090 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
4091
4092 if (!wpa_s->conf->ext_password_backend)
4093 return 0;
4094
4095 val = os_strdup(wpa_s->conf->ext_password_backend);
4096 if (val == NULL)
4097 return -1;
4098 pos = os_strchr(val, ':');
4099 if (pos)
4100 *pos++ = '\0';
4101
4102 wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
4103
4104 wpa_s->ext_pw = ext_password_init(val, pos);
4105 os_free(val);
4106 if (wpa_s->ext_pw == NULL) {
4107 wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
4108 return -1;
4109 }
4110 eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
4111
4112 return 0;
4113 }
4114
4115
4116 #ifdef CONFIG_FST
4117
4118 static const u8 * wpas_fst_get_bssid_cb(void *ctx)
4119 {
4120 struct wpa_supplicant *wpa_s = ctx;
4121
4122 return (is_zero_ether_addr(wpa_s->bssid) ||
4123 wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
4124 }
4125
4126
4127 static void wpas_fst_get_channel_info_cb(void *ctx,
4128 enum hostapd_hw_mode *hw_mode,
4129 u8 *channel)
4130 {
4131 struct wpa_supplicant *wpa_s = ctx;
4132
4133 if (wpa_s->current_bss) {
4134 *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
4135 channel);
4136 } else if (wpa_s->hw.num_modes) {
4137 *hw_mode = wpa_s->hw.modes[0].mode;
4138 } else {
4139 WPA_ASSERT(0);
4140 *hw_mode = 0;
4141 }
4142 }
4143
4144
4145 static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
4146 {
4147 struct wpa_supplicant *wpa_s = ctx;
4148
4149 *modes = wpa_s->hw.modes;
4150 return wpa_s->hw.num_modes;
4151 }
4152
4153
4154 static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
4155 {
4156 struct wpa_supplicant *wpa_s = ctx;
4157
4158 wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
4159 wpa_s->fst_ies = fst_ies;
4160 }
4161
4162
4163 static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
4164 {
4165 struct wpa_supplicant *wpa_s = ctx;
4166
4167 if (os_memcmp(wpa_s->bssid, da, ETH_ALEN) != 0) {
4168 wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
4169 __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
4170 return -1;
4171 }
4172 return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
4173 wpa_s->own_addr, wpa_s->bssid,
4174 wpabuf_head(data), wpabuf_len(data),
4175 0);
4176 }
4177
4178
4179 static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
4180 {
4181 struct wpa_supplicant *wpa_s = ctx;
4182
4183 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
4184 return wpa_s->received_mb_ies;
4185 }
4186
4187
4188 static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
4189 const u8 *buf, size_t size)
4190 {
4191 struct wpa_supplicant *wpa_s = ctx;
4192 struct mb_ies_info info;
4193
4194 WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
4195
4196 if (!mb_ies_info_by_ies(&info, buf, size)) {
4197 wpabuf_free(wpa_s->received_mb_ies);
4198 wpa_s->received_mb_ies = mb_ies_by_info(&info);
4199 }
4200 }
4201
4202
4203 static const u8 * wpas_fst_get_peer_first(void *ctx,
4204 struct fst_get_peer_ctx **get_ctx,
4205 Boolean mb_only)
4206 {
4207 struct wpa_supplicant *wpa_s = ctx;
4208
4209 *get_ctx = NULL;
4210 if (!is_zero_ether_addr(wpa_s->bssid))
4211 return (wpa_s->received_mb_ies || !mb_only) ?
4212 wpa_s->bssid : NULL;
4213 return NULL;
4214 }
4215
4216
4217 static const u8 * wpas_fst_get_peer_next(void *ctx,
4218 struct fst_get_peer_ctx **get_ctx,
4219 Boolean mb_only)
4220 {
4221 return NULL;
4222 }
4223
4224 void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
4225 struct fst_wpa_obj *iface_obj)
4226 {
4227 iface_obj->ctx = wpa_s;
4228 iface_obj->get_bssid = wpas_fst_get_bssid_cb;
4229 iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
4230 iface_obj->get_hw_modes = wpas_fst_get_hw_modes;
4231 iface_obj->set_ies = wpas_fst_set_ies_cb;
4232 iface_obj->send_action = wpas_fst_send_action_cb;
4233 iface_obj->get_mb_ie = wpas_fst_get_mb_ie_cb;
4234 iface_obj->update_mb_ie = wpas_fst_update_mb_ie_cb;
4235 iface_obj->get_peer_first = wpas_fst_get_peer_first;
4236 iface_obj->get_peer_next = wpas_fst_get_peer_next;
4237 }
4238 #endif /* CONFIG_FST */
4239
4240 static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
4241 const struct wpa_driver_capa *capa)
4242 {
4243 struct wowlan_triggers *triggers;
4244 int ret = 0;
4245
4246 if (!wpa_s->conf->wowlan_triggers)
4247 return 0;
4248
4249 triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
4250 if (triggers) {
4251 ret = wpa_drv_wowlan(wpa_s, triggers);
4252 os_free(triggers);
4253 }
4254 return ret;
4255 }
4256
4257
4258 enum wpa_radio_work_band wpas_freq_to_band(int freq)
4259 {
4260 if (freq < 3000)
4261 return BAND_2_4_GHZ;
4262 if (freq > 50000)
4263 return BAND_60_GHZ;
4264 return BAND_5_GHZ;
4265 }
4266
4267
4268 unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
4269 {
4270 int i;
4271 unsigned int band = 0;
4272
4273 if (freqs) {
4274 /* freqs are specified for the radio work */
4275 for (i = 0; freqs[i]; i++)
4276 band |= wpas_freq_to_band(freqs[i]);
4277 } else {
4278 /*
4279 * freqs are not specified, implies all
4280 * the supported freqs by HW
4281 */
4282 for (i = 0; i < wpa_s->hw.num_modes; i++) {
4283 if (wpa_s->hw.modes[i].num_channels != 0) {
4284 if (wpa_s->hw.modes[i].mode ==
4285 HOSTAPD_MODE_IEEE80211B ||
4286 wpa_s->hw.modes[i].mode ==
4287 HOSTAPD_MODE_IEEE80211G)
4288 band |= BAND_2_4_GHZ;
4289 else if (wpa_s->hw.modes[i].mode ==
4290 HOSTAPD_MODE_IEEE80211A)
4291 band |= BAND_5_GHZ;
4292 else if (wpa_s->hw.modes[i].mode ==
4293 HOSTAPD_MODE_IEEE80211AD)
4294 band |= BAND_60_GHZ;
4295 else if (wpa_s->hw.modes[i].mode ==
4296 HOSTAPD_MODE_IEEE80211ANY)
4297 band = BAND_2_4_GHZ | BAND_5_GHZ |
4298 BAND_60_GHZ;
4299 }
4300 }
4301 }
4302
4303 return band;
4304 }
4305
4306
4307 static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
4308 const char *rn)
4309 {
4310 struct wpa_supplicant *iface = wpa_s->global->ifaces;
4311 struct wpa_radio *radio;
4312
4313 while (rn && iface) {
4314 radio = iface->radio;
4315 if (radio && os_strcmp(rn, radio->name) == 0) {
4316 wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
4317 wpa_s->ifname, rn);
4318 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
4319 return radio;
4320 }
4321
4322 iface = iface->next;
4323 }
4324
4325 wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
4326 wpa_s->ifname, rn ? rn : "N/A");
4327 radio = os_zalloc(sizeof(*radio));
4328 if (radio == NULL)
4329 return NULL;
4330
4331 if (rn)
4332 os_strlcpy(radio->name, rn, sizeof(radio->name));
4333 dl_list_init(&radio->ifaces);
4334 dl_list_init(&radio->work);
4335 dl_list_add(&radio->ifaces, &wpa_s->radio_list);
4336
4337 return radio;
4338 }
4339
4340
4341 static void radio_work_free(struct wpa_radio_work *work)
4342 {
4343 if (work->wpa_s->scan_work == work) {
4344 /* This should not really happen. */
4345 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
4346 work->type, work, work->started);
4347 work->wpa_s->scan_work = NULL;
4348 }
4349
4350 #ifdef CONFIG_P2P
4351 if (work->wpa_s->p2p_scan_work == work) {
4352 /* This should not really happen. */
4353 wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
4354 work->type, work, work->started);
4355 work->wpa_s->p2p_scan_work = NULL;
4356 }
4357 #endif /* CONFIG_P2P */
4358
4359 if (work->started) {
4360 work->wpa_s->radio->num_active_works--;
4361 wpa_dbg(work->wpa_s, MSG_DEBUG,
4362 "radio_work_free('%s'@%p: num_active_works --> %u",
4363 work->type, work,
4364 work->wpa_s->radio->num_active_works);
4365 }
4366
4367 dl_list_del(&work->list);
4368 os_free(work);
4369 }
4370
4371
4372 static int radio_work_is_connect(struct wpa_radio_work *work)
4373 {
4374 return os_strcmp(work->type, "sme-connect") == 0 ||
4375 os_strcmp(work->type, "connect") == 0;
4376 }
4377
4378
4379 static int radio_work_is_scan(struct wpa_radio_work *work)
4380 {
4381 return os_strcmp(work->type, "scan") == 0 ||
4382 os_strcmp(work->type, "p2p-scan") == 0;
4383 }
4384
4385
4386 static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
4387 {
4388 struct wpa_radio_work *active_work = NULL;
4389 struct wpa_radio_work *tmp;
4390
4391 /* Get the active work to know the type and band. */
4392 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
4393 if (tmp->started) {
4394 active_work = tmp;
4395 break;
4396 }
4397 }
4398
4399 if (!active_work) {
4400 /* No active work, start one */
4401 radio->num_active_works = 0;
4402 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
4403 list) {
4404 if (os_strcmp(tmp->type, "scan") == 0 &&
4405 radio->external_scan_running &&
4406 (((struct wpa_driver_scan_params *)
4407 tmp->ctx)->only_new_results ||
4408 tmp->wpa_s->clear_driver_scan_cache))
4409 continue;
4410 return tmp;
4411 }
4412 return NULL;
4413 }
4414
4415 if (radio_work_is_connect(active_work)) {
4416 /*
4417 * If the active work is either connect or sme-connect,
4418 * do not parallelize them with other radio works.
4419 */
4420 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
4421 "Do not parallelize radio work with %s",
4422 active_work->type);
4423 return NULL;
4424 }
4425
4426 dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
4427 if (tmp->started)
4428 continue;
4429
4430 /*
4431 * If connect or sme-connect are enqueued, parallelize only
4432 * those operations ahead of them in the queue.
4433 */
4434 if (radio_work_is_connect(tmp))
4435 break;
4436
4437 /* Serialize parallel scan and p2p_scan operations on the same
4438 * interface since the driver_nl80211 mechanism for tracking
4439 * scan cookies does not yet have support for this. */
4440 if (active_work->wpa_s == tmp->wpa_s &&
4441 radio_work_is_scan(active_work) &&
4442 radio_work_is_scan(tmp)) {
4443 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
4444 "Do not start work '%s' when another work '%s' is already scheduled",
4445 tmp->type, active_work->type);
4446 continue;
4447 }
4448 /*
4449 * Check that the radio works are distinct and
4450 * on different bands.
4451 */
4452 if (os_strcmp(active_work->type, tmp->type) != 0 &&
4453 (active_work->bands != tmp->bands)) {
4454 /*
4455 * If a scan has to be scheduled through nl80211 scan
4456 * interface and if an external scan is already running,
4457 * do not schedule the scan since it is likely to get
4458 * rejected by kernel.
4459 */
4460 if (os_strcmp(tmp->type, "scan") == 0 &&
4461 radio->external_scan_running &&
4462 (((struct wpa_driver_scan_params *)
4463 tmp->ctx)->only_new_results ||
4464 tmp->wpa_s->clear_driver_scan_cache))
4465 continue;
4466
4467 wpa_dbg(active_work->wpa_s, MSG_DEBUG,
4468 "active_work:%s new_work:%s",
4469 active_work->type, tmp->type);
4470 return tmp;
4471 }
4472 }
4473
4474 /* Did not find a radio work to schedule in parallel. */
4475 return NULL;
4476 }
4477
4478
4479 static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
4480 {
4481 struct wpa_radio *radio = eloop_ctx;
4482 struct wpa_radio_work *work;
4483 struct os_reltime now, diff;
4484 struct wpa_supplicant *wpa_s;
4485
4486 work = dl_list_first(&radio->work, struct wpa_radio_work, list);
4487 if (work == NULL) {
4488 radio->num_active_works = 0;
4489 return;
4490 }
4491
4492 wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
4493 radio_list);
4494
4495 if (!(wpa_s &&
4496 wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
4497 if (work->started)
4498 return; /* already started and still in progress */
4499
4500 if (wpa_s && wpa_s->radio->external_scan_running) {
4501 wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
4502 return;
4503 }
4504 } else {
4505 work = NULL;
4506 if (radio->num_active_works < MAX_ACTIVE_WORKS) {
4507 /* get the work to schedule next */
4508 work = radio_work_get_next_work(radio);
4509 }
4510 if (!work)
4511 return;
4512 }
4513
4514 wpa_s = work->wpa_s;
4515 os_get_reltime(&now);
4516 os_reltime_sub(&now, &work->time, &diff);
4517 wpa_dbg(wpa_s, MSG_DEBUG,
4518 "Starting radio work '%s'@%p after %ld.%06ld second wait",
4519 work->type, work, diff.sec, diff.usec);
4520 work->started = 1;
4521 work->time = now;
4522 radio->num_active_works++;
4523
4524 work->cb(work, 0);
4525
4526 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
4527 radio->num_active_works < MAX_ACTIVE_WORKS)
4528 radio_work_check_next(wpa_s);
4529 }
4530
4531
4532 /*
4533 * This function removes both started and pending radio works running on
4534 * the provided interface's radio.
4535 * Prior to the removal of the radio work, its callback (cb) is called with
4536 * deinit set to be 1. Each work's callback is responsible for clearing its
4537 * internal data and restoring to a correct state.
4538 * @wpa_s: wpa_supplicant data
4539 * @type: type of works to be removed
4540 * @remove_all: 1 to remove all the works on this radio, 0 to remove only
4541 * this interface's works.
4542 */
4543 void radio_remove_works(struct wpa_supplicant *wpa_s,
4544 const char *type, int remove_all)
4545 {
4546 struct wpa_radio_work *work, *tmp;
4547 struct wpa_radio *radio = wpa_s->radio;
4548
4549 dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
4550 list) {
4551 if (type && os_strcmp(type, work->type) != 0)
4552 continue;
4553
4554 /* skip other ifaces' works */
4555 if (!remove_all && work->wpa_s != wpa_s)
4556 continue;
4557
4558 wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
4559 work->type, work, work->started ? " (started)" : "");
4560 work->cb(work, 1);
4561 radio_work_free(work);
4562 }
4563
4564 /* in case we removed the started work */
4565 radio_work_check_next(wpa_s);
4566 }
4567
4568
4569 static void radio_remove_interface(struct wpa_supplicant *wpa_s)
4570 {
4571 struct wpa_radio *radio = wpa_s->radio;
4572
4573 if (!radio)
4574 return;
4575
4576 wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
4577 wpa_s->ifname, radio->name);
4578 dl_list_del(&wpa_s->radio_list);
4579 radio_remove_works(wpa_s, NULL, 0);
4580 wpa_s->radio = NULL;
4581 if (!dl_list_empty(&radio->ifaces))
4582 return; /* Interfaces remain for this radio */
4583
4584 wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
4585 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
4586 os_free(radio);
4587 }
4588
4589
4590 void radio_work_check_next(struct wpa_supplicant *wpa_s)
4591 {
4592 struct wpa_radio *radio = wpa_s->radio;
4593
4594 if (dl_list_empty(&radio->work))
4595 return;
4596 if (wpa_s->ext_work_in_progress) {
4597 wpa_printf(MSG_DEBUG,
4598 "External radio work in progress - delay start of pending item");
4599 return;
4600 }
4601 eloop_cancel_timeout(radio_start_next_work, radio, NULL);
4602 eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
4603 }
4604
4605
4606 /**
4607 * radio_add_work - Add a radio work item
4608 * @wpa_s: Pointer to wpa_supplicant data
4609 * @freq: Frequency of the offchannel operation in MHz or 0
4610 * @type: Unique identifier for each type of work
4611 * @next: Force as the next work to be executed
4612 * @cb: Callback function for indicating when radio is available
4613 * @ctx: Context pointer for the work (work->ctx in cb())
4614 * Returns: 0 on success, -1 on failure
4615 *
4616 * This function is used to request time for an operation that requires
4617 * exclusive radio control. Once the radio is available, the registered callback
4618 * function will be called. radio_work_done() must be called once the exclusive
4619 * radio operation has been completed, so that the radio is freed for other
4620 * operations. The special case of deinit=1 is used to free the context data
4621 * during interface removal. That does not allow the callback function to start
4622 * the radio operation, i.e., it must free any resources allocated for the radio
4623 * work and return.
4624 *
4625 * The @freq parameter can be used to indicate a single channel on which the
4626 * offchannel operation will occur. This may allow multiple radio work
4627 * operations to be performed in parallel if they apply for the same channel.
4628 * Setting this to 0 indicates that the work item may use multiple channels or
4629 * requires exclusive control of the radio.
4630 */
4631 int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
4632 const char *type, int next,
4633 void (*cb)(struct wpa_radio_work *work, int deinit),
4634 void *ctx)
4635 {
4636 struct wpa_radio *radio = wpa_s->radio;
4637 struct wpa_radio_work *work;
4638 int was_empty;
4639
4640 work = os_zalloc(sizeof(*work));
4641 if (work == NULL)
4642 return -1;
4643 wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
4644 os_get_reltime(&work->time);
4645 work->freq = freq;
4646 work->type = type;
4647 work->wpa_s = wpa_s;
4648 work->cb = cb;
4649 work->ctx = ctx;
4650
4651 if (freq)
4652 work->bands = wpas_freq_to_band(freq);
4653 else if (os_strcmp(type, "scan") == 0 ||
4654 os_strcmp(type, "p2p-scan") == 0)
4655 work->bands = wpas_get_bands(wpa_s,
4656 ((struct wpa_driver_scan_params *)
4657 ctx)->freqs);
4658 else
4659 work->bands = wpas_get_bands(wpa_s, NULL);
4660
4661 was_empty = dl_list_empty(&wpa_s->radio->work);
4662 if (next)
4663 dl_list_add(&wpa_s->radio->work, &work->list);
4664 else
4665 dl_list_add_tail(&wpa_s->radio->work, &work->list);
4666 if (was_empty) {
4667 wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
4668 radio_work_check_next(wpa_s);
4669 } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
4670 && radio->num_active_works < MAX_ACTIVE_WORKS) {
4671 wpa_dbg(wpa_s, MSG_DEBUG,
4672 "Try to schedule a radio work (num_active_works=%u)",
4673 radio->num_active_works);
4674 radio_work_check_next(wpa_s);
4675 }
4676
4677 return 0;
4678 }
4679
4680
4681 /**
4682 * radio_work_done - Indicate that a radio work item has been completed
4683 * @work: Completed work
4684 *
4685 * This function is called once the callback function registered with
4686 * radio_add_work() has completed its work.
4687 */
4688 void radio_work_done(struct wpa_radio_work *work)
4689 {
4690 struct wpa_supplicant *wpa_s = work->wpa_s;
4691 struct os_reltime now, diff;
4692 unsigned int started = work->started;
4693
4694 os_get_reltime(&now);
4695 os_reltime_sub(&now, &work->time, &diff);
4696 wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
4697 work->type, work, started ? "done" : "canceled",
4698 diff.sec, diff.usec);
4699 radio_work_free(work);
4700 if (started)
4701 radio_work_check_next(wpa_s);
4702 }
4703
4704
4705 struct wpa_radio_work *
4706 radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
4707 {
4708 struct wpa_radio_work *work;
4709 struct wpa_radio *radio = wpa_s->radio;
4710
4711 dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
4712 if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
4713 return work;
4714 }
4715
4716 return NULL;
4717 }
4718
4719
4720 static int wpas_init_driver(struct wpa_supplicant *wpa_s,
4721 struct wpa_interface *iface)
4722 {
4723 const char *ifname, *driver, *rn;
4724
4725 driver = iface->driver;
4726 next_driver:
4727 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
4728 return -1;
4729
4730 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
4731 if (wpa_s->drv_priv == NULL) {
4732 const char *pos;
4733 pos = driver ? os_strchr(driver, ',') : NULL;
4734 if (pos) {
4735 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
4736 "driver interface - try next driver wrapper");
4737 driver = pos + 1;
4738 goto next_driver;
4739 }
4740 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
4741 "interface");
4742 return -1;
4743 }
4744 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
4745 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
4746 "driver_param '%s'", wpa_s->conf->driver_param);
4747 return -1;
4748 }
4749
4750 ifname = wpa_drv_get_ifname(wpa_s);
4751 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
4752 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
4753 "interface name with '%s'", ifname);
4754 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
4755 }
4756
4757 rn = wpa_driver_get_radio_name(wpa_s);
4758 if (rn && rn[0] == '\0')
4759 rn = NULL;
4760
4761 wpa_s->radio = radio_add_interface(wpa_s, rn);
4762 if (wpa_s->radio == NULL)
4763 return -1;
4764
4765 return 0;
4766 }
4767
4768
4769 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
4770 struct wpa_interface *iface)
4771 {
4772 struct wpa_driver_capa capa;
4773 int capa_res;
4774
4775 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
4776 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
4777 iface->confname ? iface->confname : "N/A",
4778 iface->driver ? iface->driver : "default",
4779 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
4780 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
4781
4782 if (iface->confname) {
4783 #ifdef CONFIG_BACKEND_FILE
4784 wpa_s->confname = os_rel2abs_path(iface->confname);
4785 if (wpa_s->confname == NULL) {
4786 wpa_printf(MSG_ERROR, "Failed to get absolute path "
4787 "for configuration file '%s'.",
4788 iface->confname);
4789 return -1;
4790 }
4791 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
4792 iface->confname, wpa_s->confname);
4793 #else /* CONFIG_BACKEND_FILE */
4794 wpa_s->confname = os_strdup(iface->confname);
4795 #endif /* CONFIG_BACKEND_FILE */
4796 wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
4797 if (wpa_s->conf == NULL) {
4798 wpa_printf(MSG_ERROR, "Failed to read or parse "
4799 "configuration '%s'.", wpa_s->confname);
4800 return -1;
4801 }
4802 wpa_s->confanother = os_rel2abs_path(iface->confanother);
4803 wpa_config_read(wpa_s->confanother, wpa_s->conf);
4804
4805 /*
4806 * Override ctrl_interface and driver_param if set on command
4807 * line.
4808 */
4809 if (iface->ctrl_interface) {
4810 os_free(wpa_s->conf->ctrl_interface);
4811 wpa_s->conf->ctrl_interface =
4812 os_strdup(iface->ctrl_interface);
4813 }
4814
4815 if (iface->driver_param) {
4816 os_free(wpa_s->conf->driver_param);
4817 wpa_s->conf->driver_param =
4818 os_strdup(iface->driver_param);
4819 }
4820
4821 if (iface->p2p_mgmt && !iface->ctrl_interface) {
4822 os_free(wpa_s->conf->ctrl_interface);
4823 wpa_s->conf->ctrl_interface = NULL;
4824 }
4825 } else
4826 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
4827 iface->driver_param);
4828
4829 if (wpa_s->conf == NULL) {
4830 wpa_printf(MSG_ERROR, "\nNo configuration found.");
4831 return -1;
4832 }
4833
4834 if (iface->ifname == NULL) {
4835 wpa_printf(MSG_ERROR, "\nInterface name is required.");
4836 return -1;
4837 }
4838 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
4839 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
4840 iface->ifname);
4841 return -1;
4842 }
4843 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
4844
4845 if (iface->bridge_ifname) {
4846 if (os_strlen(iface->bridge_ifname) >=
4847 sizeof(wpa_s->bridge_ifname)) {
4848 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
4849 "name '%s'.", iface->bridge_ifname);
4850 return -1;
4851 }
4852 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
4853 sizeof(wpa_s->bridge_ifname));
4854 }
4855
4856 /* RSNA Supplicant Key Management - INITIALIZE */
4857 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
4858 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
4859
4860 /* Initialize driver interface and register driver event handler before
4861 * L2 receive handler so that association events are processed before
4862 * EAPOL-Key packets if both become available for the same select()
4863 * call. */
4864 if (wpas_init_driver(wpa_s, iface) < 0)
4865 return -1;
4866
4867 if (wpa_supplicant_init_wpa(wpa_s) < 0)
4868 return -1;
4869
4870 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
4871 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
4872 NULL);
4873 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
4874
4875 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
4876 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
4877 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
4878 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
4879 "dot11RSNAConfigPMKLifetime");
4880 return -1;
4881 }
4882
4883 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
4884 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
4885 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
4886 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
4887 "dot11RSNAConfigPMKReauthThreshold");
4888 return -1;
4889 }
4890
4891 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
4892 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
4893 wpa_s->conf->dot11RSNAConfigSATimeout)) {
4894 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
4895 "dot11RSNAConfigSATimeout");
4896 return -1;
4897 }
4898
4899 wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
4900 &wpa_s->hw.num_modes,
4901 &wpa_s->hw.flags);
4902 if (wpa_s->hw.modes) {
4903 u16 i;
4904
4905 for (i = 0; i < wpa_s->hw.num_modes; i++) {
4906 if (wpa_s->hw.modes[i].vht_capab) {
4907 wpa_s->hw_capab = CAPAB_VHT;
4908 break;
4909 }
4910
4911 if (wpa_s->hw.modes[i].ht_capab &
4912 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
4913 wpa_s->hw_capab = CAPAB_HT40;
4914 else if (wpa_s->hw.modes[i].ht_capab &&
4915 wpa_s->hw_capab == CAPAB_NO_HT_VHT)
4916 wpa_s->hw_capab = CAPAB_HT;
4917 }
4918 }
4919
4920 capa_res = wpa_drv_get_capa(wpa_s, &capa);
4921 if (capa_res == 0) {
4922 wpa_s->drv_capa_known = 1;
4923 wpa_s->drv_flags = capa.flags;
4924 wpa_s->drv_enc = capa.enc;
4925 wpa_s->drv_smps_modes = capa.smps_modes;
4926 wpa_s->drv_rrm_flags = capa.rrm_flags;
4927 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
4928 wpa_s->max_scan_ssids = capa.max_scan_ssids;
4929 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
4930 wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
4931 wpa_s->max_sched_scan_plan_interval =
4932 capa.max_sched_scan_plan_interval;
4933 wpa_s->max_sched_scan_plan_iterations =
4934 capa.max_sched_scan_plan_iterations;
4935 wpa_s->sched_scan_supported = capa.sched_scan_supported;
4936 wpa_s->max_match_sets = capa.max_match_sets;
4937 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
4938 wpa_s->max_stations = capa.max_stations;
4939 wpa_s->extended_capa = capa.extended_capa;
4940 wpa_s->extended_capa_mask = capa.extended_capa_mask;
4941 wpa_s->extended_capa_len = capa.extended_capa_len;
4942 wpa_s->num_multichan_concurrent =
4943 capa.num_multichan_concurrent;
4944 wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
4945
4946 if (capa.mac_addr_rand_scan_supported)
4947 wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
4948 if (wpa_s->sched_scan_supported &&
4949 capa.mac_addr_rand_sched_scan_supported)
4950 wpa_s->mac_addr_rand_supported |=
4951 (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
4952 }
4953 if (wpa_s->max_remain_on_chan == 0)
4954 wpa_s->max_remain_on_chan = 1000;
4955
4956 /*
4957 * Only take p2p_mgmt parameters when P2P Device is supported.
4958 * Doing it here as it determines whether l2_packet_init() will be done
4959 * during wpa_supplicant_driver_init().
4960 */
4961 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
4962 wpa_s->p2p_mgmt = iface->p2p_mgmt;
4963 else
4964 iface->p2p_mgmt = 1;
4965
4966 if (wpa_s->num_multichan_concurrent == 0)
4967 wpa_s->num_multichan_concurrent = 1;
4968
4969 if (wpa_supplicant_driver_init(wpa_s) < 0)
4970 return -1;
4971
4972 #ifdef CONFIG_TDLS
4973 if ((!iface->p2p_mgmt ||
4974 !(wpa_s->drv_flags &
4975 WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
4976 wpa_tdls_init(wpa_s->wpa))
4977 return -1;
4978 #endif /* CONFIG_TDLS */
4979
4980 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
4981 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
4982 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
4983 return -1;
4984 }
4985
4986 #ifdef CONFIG_FST
4987 if (wpa_s->conf->fst_group_id) {
4988 struct fst_iface_cfg cfg;
4989 struct fst_wpa_obj iface_obj;
4990
4991 fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
4992 os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
4993 sizeof(cfg.group_id));
4994 cfg.priority = wpa_s->conf->fst_priority;
4995 cfg.llt = wpa_s->conf->fst_llt;
4996
4997 wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
4998 &iface_obj, &cfg);
4999 if (!wpa_s->fst) {
5000 wpa_msg(wpa_s, MSG_ERROR,
5001 "FST: Cannot attach iface %s to group %s",
5002 wpa_s->ifname, cfg.group_id);
5003 return -1;
5004 }
5005 }
5006 #endif /* CONFIG_FST */
5007
5008 if (wpas_wps_init(wpa_s))
5009 return -1;
5010
5011 if (wpa_supplicant_init_eapol(wpa_s) < 0)
5012 return -1;
5013 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
5014
5015 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
5016 if (wpa_s->ctrl_iface == NULL) {
5017 wpa_printf(MSG_ERROR,
5018 "Failed to initialize control interface '%s'.\n"
5019 "You may have another wpa_supplicant process "
5020 "already running or the file was\n"
5021 "left by an unclean termination of wpa_supplicant "
5022 "in which case you will need\n"
5023 "to manually remove this file before starting "
5024 "wpa_supplicant again.\n",
5025 wpa_s->conf->ctrl_interface);
5026 return -1;
5027 }
5028
5029 wpa_s->gas = gas_query_init(wpa_s);
5030 if (wpa_s->gas == NULL) {
5031 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
5032 return -1;
5033 }
5034
5035 if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
5036 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
5037 return -1;
5038 }
5039
5040 if (wpa_bss_init(wpa_s) < 0)
5041 return -1;
5042
5043 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
5044 #ifdef CONFIG_MESH
5045 dl_list_init(&wpa_s->mesh_external_pmksa_cache);
5046 #endif /* CONFIG_MESH */
5047 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
5048
5049 /*
5050 * Set Wake-on-WLAN triggers, if configured.
5051 * Note: We don't restore/remove the triggers on shutdown (it doesn't
5052 * have effect anyway when the interface is down).
5053 */
5054 if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
5055 return -1;
5056
5057 #ifdef CONFIG_EAP_PROXY
5058 {
5059 size_t len;
5060 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
5061 &len);
5062 if (wpa_s->mnc_len > 0) {
5063 wpa_s->imsi[len] = '\0';
5064 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
5065 wpa_s->imsi, wpa_s->mnc_len);
5066 } else {
5067 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
5068 }
5069 }
5070 #endif /* CONFIG_EAP_PROXY */
5071
5072 if (pcsc_reader_init(wpa_s) < 0)
5073 return -1;
5074
5075 if (wpas_init_ext_pw(wpa_s) < 0)
5076 return -1;
5077
5078 wpas_rrm_reset(wpa_s);
5079
5080 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
5081
5082 #ifdef CONFIG_HS20
5083 hs20_init(wpa_s);
5084 #endif /* CONFIG_HS20 */
5085 #ifdef CONFIG_MBO
5086 wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
5087 #endif /* CONFIG_MBO */
5088
5089 wpa_supplicant_set_default_scan_ies(wpa_s);
5090
5091 return 0;
5092 }
5093
5094
5095 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
5096 int notify, int terminate)
5097 {
5098 struct wpa_global *global = wpa_s->global;
5099 struct wpa_supplicant *iface, *prev;
5100
5101 if (wpa_s == wpa_s->parent)
5102 wpas_p2p_group_remove(wpa_s, "*");
5103
5104 iface = global->ifaces;
5105 while (iface) {
5106 if (iface->p2pdev == wpa_s)
5107 iface->p2pdev = iface->parent;
5108 if (iface == wpa_s || iface->parent != wpa_s) {
5109 iface = iface->next;
5110 continue;
5111 }
5112 wpa_printf(MSG_DEBUG,
5113 "Remove remaining child interface %s from parent %s",
5114 iface->ifname, wpa_s->ifname);
5115 prev = iface;
5116 iface = iface->next;
5117 wpa_supplicant_remove_iface(global, prev, terminate);
5118 }
5119
5120 wpa_s->disconnected = 1;
5121 if (wpa_s->drv_priv) {
5122 wpa_supplicant_deauthenticate(wpa_s,
5123 WLAN_REASON_DEAUTH_LEAVING);
5124
5125 wpa_drv_set_countermeasures(wpa_s, 0);
5126 wpa_clear_keys(wpa_s, NULL);
5127 }
5128
5129 wpa_supplicant_cleanup(wpa_s);
5130 wpas_p2p_deinit_iface(wpa_s);
5131
5132 wpas_ctrl_radio_work_flush(wpa_s);
5133 radio_remove_interface(wpa_s);
5134
5135 #ifdef CONFIG_FST
5136 if (wpa_s->fst) {
5137 fst_detach(wpa_s->fst);
5138 wpa_s->fst = NULL;
5139 }
5140 if (wpa_s->received_mb_ies) {
5141 wpabuf_free(wpa_s->received_mb_ies);
5142 wpa_s->received_mb_ies = NULL;
5143 }
5144 #endif /* CONFIG_FST */
5145
5146 if (wpa_s->drv_priv)
5147 wpa_drv_deinit(wpa_s);
5148
5149 if (notify)
5150 wpas_notify_iface_removed(wpa_s);
5151
5152 if (terminate)
5153 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
5154
5155 if (wpa_s->ctrl_iface) {
5156 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
5157 wpa_s->ctrl_iface = NULL;
5158 }
5159
5160 #ifdef CONFIG_MESH
5161 if (wpa_s->ifmsh) {
5162 wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
5163 wpa_s->ifmsh = NULL;
5164 }
5165 #endif /* CONFIG_MESH */
5166
5167 if (wpa_s->conf != NULL) {
5168 wpa_config_free(wpa_s->conf);
5169 wpa_s->conf = NULL;
5170 }
5171
5172 os_free(wpa_s->ssids_from_scan_req);
5173
5174 os_free(wpa_s);
5175 }
5176
5177
5178 #ifdef CONFIG_MATCH_IFACE
5179
5180 /**
5181 * wpa_supplicant_match_iface - Match an interface description to a name
5182 * @global: Pointer to global data from wpa_supplicant_init()
5183 * @ifname: Name of the interface to match
5184 * Returns: Pointer to the created interface description or %NULL on failure
5185 */
5186 struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
5187 const char *ifname)
5188 {
5189 int i;
5190 struct wpa_interface *iface, *miface;
5191
5192 for (i = 0; i < global->params.match_iface_count; i++) {
5193 miface = &global->params.match_ifaces[i];
5194 if (!miface->ifname ||
5195 fnmatch(miface->ifname, ifname, 0) == 0) {
5196 iface = os_zalloc(sizeof(*iface));
5197 if (!iface)
5198 return NULL;
5199 *iface = *miface;
5200 iface->ifname = ifname;
5201 return iface;
5202 }
5203 }
5204
5205 return NULL;
5206 }
5207
5208
5209 /**
5210 * wpa_supplicant_match_existing - Match existing interfaces
5211 * @global: Pointer to global data from wpa_supplicant_init()
5212 * Returns: 0 on success, -1 on failure
5213 */
5214 static int wpa_supplicant_match_existing(struct wpa_global *global)
5215 {
5216 struct if_nameindex *ifi, *ifp;
5217 struct wpa_supplicant *wpa_s;
5218 struct wpa_interface *iface;
5219
5220 ifp = if_nameindex();
5221 if (!ifp) {
5222 wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
5223 return -1;
5224 }
5225
5226 for (ifi = ifp; ifi->if_name; ifi++) {
5227 wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
5228 if (wpa_s)
5229 continue;
5230 iface = wpa_supplicant_match_iface(global, ifi->if_name);
5231 if (iface) {
5232 wpa_s = wpa_supplicant_add_iface(global, iface, NULL);
5233 os_free(iface);
5234 if (wpa_s)
5235 wpa_s->matched = 1;
5236 }
5237 }
5238
5239 if_freenameindex(ifp);
5240 return 0;
5241 }
5242
5243 #endif /* CONFIG_MATCH_IFACE */
5244
5245
5246 /**
5247 * wpa_supplicant_add_iface - Add a new network interface
5248 * @global: Pointer to global data from wpa_supplicant_init()
5249 * @iface: Interface configuration options
5250 * @parent: Parent interface or %NULL to assign new interface as parent
5251 * Returns: Pointer to the created interface or %NULL on failure
5252 *
5253 * This function is used to add new network interfaces for %wpa_supplicant.
5254 * This can be called before wpa_supplicant_run() to add interfaces before the
5255 * main event loop has been started. In addition, new interfaces can be added
5256 * dynamically while %wpa_supplicant is already running. This could happen,
5257 * e.g., when a hotplug network adapter is inserted.
5258 */
5259 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
5260 struct wpa_interface *iface,
5261 struct wpa_supplicant *parent)
5262 {
5263 struct wpa_supplicant *wpa_s;
5264 struct wpa_interface t_iface;
5265 struct wpa_ssid *ssid;
5266
5267 if (global == NULL || iface == NULL)
5268 return NULL;
5269
5270 wpa_s = wpa_supplicant_alloc(parent);
5271 if (wpa_s == NULL)
5272 return NULL;
5273
5274 wpa_s->global = global;
5275
5276 t_iface = *iface;
5277 if (global->params.override_driver) {
5278 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
5279 "('%s' -> '%s')",
5280 iface->driver, global->params.override_driver);
5281 t_iface.driver = global->params.override_driver;
5282 }
5283 if (global->params.override_ctrl_interface) {
5284 wpa_printf(MSG_DEBUG, "Override interface parameter: "
5285 "ctrl_interface ('%s' -> '%s')",
5286 iface->ctrl_interface,
5287 global->params.override_ctrl_interface);
5288 t_iface.ctrl_interface =
5289 global->params.override_ctrl_interface;
5290 }
5291 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
5292 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
5293 iface->ifname);
5294 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
5295 return NULL;
5296 }
5297
5298 if (iface->p2p_mgmt == 0) {
5299 /* Notify the control interfaces about new iface */
5300 if (wpas_notify_iface_added(wpa_s)) {
5301 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
5302 return NULL;
5303 }
5304
5305 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
5306 wpas_notify_network_added(wpa_s, ssid);
5307 }
5308
5309 wpa_s->next = global->ifaces;
5310 global->ifaces = wpa_s;
5311
5312 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
5313 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
5314
5315 #ifdef CONFIG_P2P
5316 if (wpa_s->global->p2p == NULL &&
5317 !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
5318 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5319 wpas_p2p_add_p2pdev_interface(
5320 wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
5321 wpa_printf(MSG_INFO,
5322 "P2P: Failed to enable P2P Device interface");
5323 /* Try to continue without. P2P will be disabled. */
5324 }
5325 #endif /* CONFIG_P2P */
5326
5327 return wpa_s;
5328 }
5329
5330
5331 /**
5332 * wpa_supplicant_remove_iface - Remove a network interface
5333 * @global: Pointer to global data from wpa_supplicant_init()
5334 * @wpa_s: Pointer to the network interface to be removed
5335 * Returns: 0 if interface was removed, -1 if interface was not found
5336 *
5337 * This function can be used to dynamically remove network interfaces from
5338 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
5339 * addition, this function is used to remove all remaining interfaces when
5340 * %wpa_supplicant is terminated.
5341 */
5342 int wpa_supplicant_remove_iface(struct wpa_global *global,
5343 struct wpa_supplicant *wpa_s,
5344 int terminate)
5345 {
5346 struct wpa_supplicant *prev;
5347 #ifdef CONFIG_MESH
5348 unsigned int mesh_if_created = wpa_s->mesh_if_created;
5349 char *ifname = NULL;
5350 struct wpa_supplicant *parent = wpa_s->parent;
5351 #endif /* CONFIG_MESH */
5352
5353 /* Remove interface from the global list of interfaces */
5354 prev = global->ifaces;
5355 if (prev == wpa_s) {
5356 global->ifaces = wpa_s->next;
5357 } else {
5358 while (prev && prev->next != wpa_s)
5359 prev = prev->next;
5360 if (prev == NULL)
5361 return -1;
5362 prev->next = wpa_s->next;
5363 }
5364
5365 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
5366
5367 #ifdef CONFIG_MESH
5368 if (mesh_if_created) {
5369 ifname = os_strdup(wpa_s->ifname);
5370 if (ifname == NULL) {
5371 wpa_dbg(wpa_s, MSG_ERROR,
5372 "mesh: Failed to malloc ifname");
5373 return -1;
5374 }
5375 }
5376 #endif /* CONFIG_MESH */
5377
5378 if (global->p2p_group_formation == wpa_s)
5379 global->p2p_group_formation = NULL;
5380 if (global->p2p_invite_group == wpa_s)
5381 global->p2p_invite_group = NULL;
5382 wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
5383
5384 #ifdef CONFIG_MESH
5385 if (mesh_if_created) {
5386 wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
5387 os_free(ifname);
5388 }
5389 #endif /* CONFIG_MESH */
5390
5391 return 0;
5392 }
5393
5394
5395 /**
5396 * wpa_supplicant_get_eap_mode - Get the current EAP mode
5397 * @wpa_s: Pointer to the network interface
5398 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
5399 */
5400 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
5401 {
5402 const char *eapol_method;
5403
5404 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
5405 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
5406 return "NO-EAP";
5407 }
5408
5409 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
5410 if (eapol_method == NULL)
5411 return "UNKNOWN-EAP";
5412
5413 return eapol_method;
5414 }
5415
5416
5417 /**
5418 * wpa_supplicant_get_iface - Get a new network interface
5419 * @global: Pointer to global data from wpa_supplicant_init()
5420 * @ifname: Interface name
5421 * Returns: Pointer to the interface or %NULL if not found
5422 */
5423 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
5424 const char *ifname)
5425 {
5426 struct wpa_supplicant *wpa_s;
5427
5428 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5429 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5430 return wpa_s;
5431 }
5432 return NULL;
5433 }
5434
5435
5436 #ifndef CONFIG_NO_WPA_MSG
5437 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
5438 {
5439 struct wpa_supplicant *wpa_s = ctx;
5440 if (wpa_s == NULL)
5441 return NULL;
5442 return wpa_s->ifname;
5443 }
5444 #endif /* CONFIG_NO_WPA_MSG */
5445
5446
5447 #ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
5448 #define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
5449 #endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
5450
5451 /* Periodic cleanup tasks */
5452 static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
5453 {
5454 struct wpa_global *global = eloop_ctx;
5455 struct wpa_supplicant *wpa_s;
5456
5457 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
5458 wpas_periodic, global, NULL);
5459
5460 #ifdef CONFIG_P2P
5461 if (global->p2p)
5462 p2p_expire_peers(global->p2p);
5463 #endif /* CONFIG_P2P */
5464
5465 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5466 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
5467 #ifdef CONFIG_AP
5468 ap_periodic(wpa_s);
5469 #endif /* CONFIG_AP */
5470 }
5471 }
5472
5473
5474 /**
5475 * wpa_supplicant_init - Initialize %wpa_supplicant
5476 * @params: Parameters for %wpa_supplicant
5477 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
5478 *
5479 * This function is used to initialize %wpa_supplicant. After successful
5480 * initialization, the returned data pointer can be used to add and remove
5481 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
5482 */
5483 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
5484 {
5485 struct wpa_global *global;
5486 int ret, i;
5487
5488 if (params == NULL)
5489 return NULL;
5490
5491 #ifdef CONFIG_DRIVER_NDIS
5492 {
5493 void driver_ndis_init_ops(void);
5494 driver_ndis_init_ops();
5495 }
5496 #endif /* CONFIG_DRIVER_NDIS */
5497
5498 #ifndef CONFIG_NO_WPA_MSG
5499 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
5500 #endif /* CONFIG_NO_WPA_MSG */
5501
5502 if (params->wpa_debug_file_path)
5503 wpa_debug_open_file(params->wpa_debug_file_path);
5504 else
5505 wpa_debug_setup_stdout();
5506 if (params->wpa_debug_syslog)
5507 wpa_debug_open_syslog();
5508 if (params->wpa_debug_tracing) {
5509 ret = wpa_debug_open_linux_tracing();
5510 if (ret) {
5511 wpa_printf(MSG_ERROR,
5512 "Failed to enable trace logging");
5513 return NULL;
5514 }
5515 }
5516
5517 ret = eap_register_methods();
5518 if (ret) {
5519 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
5520 if (ret == -2)
5521 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
5522 "the same EAP type.");
5523 return NULL;
5524 }
5525
5526 global = os_zalloc(sizeof(*global));
5527 if (global == NULL)
5528 return NULL;
5529 dl_list_init(&global->p2p_srv_bonjour);
5530 dl_list_init(&global->p2p_srv_upnp);
5531 global->params.daemonize = params->daemonize;
5532 global->params.wait_for_monitor = params->wait_for_monitor;
5533 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
5534 if (params->pid_file)
5535 global->params.pid_file = os_strdup(params->pid_file);
5536 if (params->ctrl_interface)
5537 global->params.ctrl_interface =
5538 os_strdup(params->ctrl_interface);
5539 if (params->ctrl_interface_group)
5540 global->params.ctrl_interface_group =
5541 os_strdup(params->ctrl_interface_group);
5542 if (params->override_driver)
5543 global->params.override_driver =
5544 os_strdup(params->override_driver);
5545 if (params->override_ctrl_interface)
5546 global->params.override_ctrl_interface =
5547 os_strdup(params->override_ctrl_interface);
5548 #ifdef CONFIG_MATCH_IFACE
5549 global->params.match_iface_count = params->match_iface_count;
5550 if (params->match_iface_count) {
5551 global->params.match_ifaces =
5552 os_calloc(params->match_iface_count,
5553 sizeof(struct wpa_interface));
5554 os_memcpy(global->params.match_ifaces,
5555 params->match_ifaces,
5556 params->match_iface_count *
5557 sizeof(struct wpa_interface));
5558 }
5559 #endif /* CONFIG_MATCH_IFACE */
5560 #ifdef CONFIG_P2P
5561 if (params->conf_p2p_dev)
5562 global->params.conf_p2p_dev =
5563 os_strdup(params->conf_p2p_dev);
5564 #endif /* CONFIG_P2P */
5565 wpa_debug_level = global->params.wpa_debug_level =
5566 params->wpa_debug_level;
5567 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
5568 params->wpa_debug_show_keys;
5569 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
5570 params->wpa_debug_timestamp;
5571
5572 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
5573
5574 if (eloop_init()) {
5575 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
5576 wpa_supplicant_deinit(global);
5577 return NULL;
5578 }
5579
5580 random_init(params->entropy_file);
5581
5582 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
5583 if (global->ctrl_iface == NULL) {
5584 wpa_supplicant_deinit(global);
5585 return NULL;
5586 }
5587
5588 if (wpas_notify_supplicant_initialized(global)) {
5589 wpa_supplicant_deinit(global);
5590 return NULL;
5591 }
5592
5593 for (i = 0; wpa_drivers[i]; i++)
5594 global->drv_count++;
5595 if (global->drv_count == 0) {
5596 wpa_printf(MSG_ERROR, "No drivers enabled");
5597 wpa_supplicant_deinit(global);
5598 return NULL;
5599 }
5600 global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
5601 if (global->drv_priv == NULL) {
5602 wpa_supplicant_deinit(global);
5603 return NULL;
5604 }
5605
5606 #ifdef CONFIG_WIFI_DISPLAY
5607 if (wifi_display_init(global) < 0) {
5608 wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
5609 wpa_supplicant_deinit(global);
5610 return NULL;
5611 }
5612 #endif /* CONFIG_WIFI_DISPLAY */
5613
5614 eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
5615 wpas_periodic, global, NULL);
5616
5617 return global;
5618 }
5619
5620
5621 /**
5622 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
5623 * @global: Pointer to global data from wpa_supplicant_init()
5624 * Returns: 0 after successful event loop run, -1 on failure
5625 *
5626 * This function starts the main event loop and continues running as long as
5627 * there are any remaining events. In most cases, this function is running as
5628 * long as the %wpa_supplicant process in still in use.
5629 */
5630 int wpa_supplicant_run(struct wpa_global *global)
5631 {
5632 struct wpa_supplicant *wpa_s;
5633
5634 if (global->params.daemonize &&
5635 (wpa_supplicant_daemon(global->params.pid_file) ||
5636 eloop_sock_requeue()))
5637 return -1;
5638
5639 #ifdef CONFIG_MATCH_IFACE
5640 if (wpa_supplicant_match_existing(global))
5641 return -1;
5642 #endif
5643
5644 if (global->params.wait_for_monitor) {
5645 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
5646 if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
5647 wpa_supplicant_ctrl_iface_wait(
5648 wpa_s->ctrl_iface);
5649 }
5650
5651 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
5652 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
5653
5654 eloop_run();
5655
5656 return 0;
5657 }
5658
5659
5660 /**
5661 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
5662 * @global: Pointer to global data from wpa_supplicant_init()
5663 *
5664 * This function is called to deinitialize %wpa_supplicant and to free all
5665 * allocated resources. Remaining network interfaces will also be removed.
5666 */
5667 void wpa_supplicant_deinit(struct wpa_global *global)
5668 {
5669 int i;
5670
5671 if (global == NULL)
5672 return;
5673
5674 eloop_cancel_timeout(wpas_periodic, global, NULL);
5675
5676 #ifdef CONFIG_WIFI_DISPLAY
5677 wifi_display_deinit(global);
5678 #endif /* CONFIG_WIFI_DISPLAY */
5679
5680 while (global->ifaces)
5681 wpa_supplicant_remove_iface(global, global->ifaces, 1);
5682
5683 if (global->ctrl_iface)
5684 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
5685
5686 wpas_notify_supplicant_deinitialized(global);
5687
5688 eap_peer_unregister_methods();
5689 #ifdef CONFIG_AP
5690 eap_server_unregister_methods();
5691 #endif /* CONFIG_AP */
5692
5693 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
5694 if (!global->drv_priv[i])
5695 continue;
5696 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
5697 }
5698 os_free(global->drv_priv);
5699
5700 random_deinit();
5701
5702 eloop_destroy();
5703
5704 if (global->params.pid_file) {
5705 os_daemonize_terminate(global->params.pid_file);
5706 os_free(global->params.pid_file);
5707 }
5708 os_free(global->params.ctrl_interface);
5709 os_free(global->params.ctrl_interface_group);
5710 os_free(global->params.override_driver);
5711 os_free(global->params.override_ctrl_interface);
5712 #ifdef CONFIG_MATCH_IFACE
5713 os_free(global->params.match_ifaces);
5714 #endif /* CONFIG_MATCH_IFACE */
5715 #ifdef CONFIG_P2P
5716 os_free(global->params.conf_p2p_dev);
5717 #endif /* CONFIG_P2P */
5718
5719 os_free(global->p2p_disallow_freq.range);
5720 os_free(global->p2p_go_avoid_freq.range);
5721 os_free(global->add_psk);
5722
5723 os_free(global);
5724 wpa_debug_close_syslog();
5725 wpa_debug_close_file();
5726 wpa_debug_close_linux_tracing();
5727 }
5728
5729
5730 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
5731 {
5732 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5733 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5734 char country[3];
5735 country[0] = wpa_s->conf->country[0];
5736 country[1] = wpa_s->conf->country[1];
5737 country[2] = '\0';
5738 if (wpa_drv_set_country(wpa_s, country) < 0) {
5739 wpa_printf(MSG_ERROR, "Failed to set country code "
5740 "'%s'", country);
5741 }
5742 }
5743
5744 if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
5745 wpas_init_ext_pw(wpa_s);
5746
5747 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
5748 wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
5749
5750 #ifdef CONFIG_WPS
5751 wpas_wps_update_config(wpa_s);
5752 #endif /* CONFIG_WPS */
5753 wpas_p2p_update_config(wpa_s);
5754 wpa_s->conf->changed_parameters = 0;
5755 }
5756
5757
5758 void add_freq(int *freqs, int *num_freqs, int freq)
5759 {
5760 int i;
5761
5762 for (i = 0; i < *num_freqs; i++) {
5763 if (freqs[i] == freq)
5764 return;
5765 }
5766
5767 freqs[*num_freqs] = freq;
5768 (*num_freqs)++;
5769 }
5770
5771
5772 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
5773 {
5774 struct wpa_bss *bss, *cbss;
5775 const int max_freqs = 10;
5776 int *freqs;
5777 int num_freqs = 0;
5778
5779 freqs = os_calloc(max_freqs + 1, sizeof(int));
5780 if (freqs == NULL)
5781 return NULL;
5782
5783 cbss = wpa_s->current_bss;
5784
5785 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
5786 if (bss == cbss)
5787 continue;
5788 if (bss->ssid_len == cbss->ssid_len &&
5789 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
5790 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
5791 add_freq(freqs, &num_freqs, bss->freq);
5792 if (num_freqs == max_freqs)
5793 break;
5794 }
5795 }
5796
5797 if (num_freqs == 0) {
5798 os_free(freqs);
5799 freqs = NULL;
5800 }
5801
5802 return freqs;
5803 }
5804
5805
5806 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
5807 {
5808 int timeout;
5809 int count;
5810 int *freqs = NULL;
5811
5812 wpas_connect_work_done(wpa_s);
5813
5814 /*
5815 * Remove possible authentication timeout since the connection failed.
5816 */
5817 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
5818
5819 /*
5820 * There is no point in blacklisting the AP if this event is
5821 * generated based on local request to disconnect.
5822 */
5823 if (wpa_s->own_disconnect_req) {
5824 wpa_s->own_disconnect_req = 0;
5825 wpa_dbg(wpa_s, MSG_DEBUG,
5826 "Ignore connection failure due to local request to disconnect");
5827 return;
5828 }
5829 if (wpa_s->disconnected) {
5830 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
5831 "indication since interface has been put into "
5832 "disconnected state");
5833 return;
5834 }
5835
5836 /*
5837 * Add the failed BSSID into the blacklist and speed up next scan
5838 * attempt if there could be other APs that could accept association.
5839 * The current blacklist count indicates how many times we have tried
5840 * connecting to this AP and multiple attempts mean that other APs are
5841 * either not available or has already been tried, so that we can start
5842 * increasing the delay here to avoid constant scanning.
5843 */
5844 count = wpa_blacklist_add(wpa_s, bssid);
5845 if (count == 1 && wpa_s->current_bss) {
5846 /*
5847 * This BSS was not in the blacklist before. If there is
5848 * another BSS available for the same ESS, we should try that
5849 * next. Otherwise, we may as well try this one once more
5850 * before allowing other, likely worse, ESSes to be considered.
5851 */
5852 freqs = get_bss_freqs_in_ess(wpa_s);
5853 if (freqs) {
5854 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
5855 "has been seen; try it next");
5856 wpa_blacklist_add(wpa_s, bssid);
5857 /*
5858 * On the next scan, go through only the known channels
5859 * used in this ESS based on previous scans to speed up
5860 * common load balancing use case.
5861 */
5862 os_free(wpa_s->next_scan_freqs);
5863 wpa_s->next_scan_freqs = freqs;
5864 }
5865 }
5866
5867 /*
5868 * Add previous failure count in case the temporary blacklist was
5869 * cleared due to no other BSSes being available.
5870 */
5871 count += wpa_s->extra_blacklist_count;
5872
5873 if (count > 3 && wpa_s->current_ssid) {
5874 wpa_printf(MSG_DEBUG, "Continuous association failures - "
5875 "consider temporary network disabling");
5876 wpas_auth_failed(wpa_s, "CONN_FAILED");
5877 }
5878
5879 switch (count) {
5880 case 1:
5881 timeout = 100;
5882 break;
5883 case 2:
5884 timeout = 500;
5885 break;
5886 case 3:
5887 timeout = 1000;
5888 break;
5889 case 4:
5890 timeout = 5000;
5891 break;
5892 default:
5893 timeout = 10000;
5894 break;
5895 }
5896
5897 wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
5898 "ms", count, timeout);
5899
5900 /*
5901 * TODO: if more than one possible AP is available in scan results,
5902 * could try the other ones before requesting a new scan.
5903 */
5904 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
5905 1000 * (timeout % 1000));
5906 }
5907
5908
5909 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
5910 {
5911 return wpa_s->conf->ap_scan == 2 ||
5912 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
5913 }
5914
5915
5916 #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
5917 int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
5918 struct wpa_ssid *ssid,
5919 const char *field,
5920 const char *value)
5921 {
5922 #ifdef IEEE8021X_EAPOL
5923 struct eap_peer_config *eap = &ssid->eap;
5924
5925 wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
5926 wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
5927 (const u8 *) value, os_strlen(value));
5928
5929 switch (wpa_supplicant_ctrl_req_from_string(field)) {
5930 case WPA_CTRL_REQ_EAP_IDENTITY:
5931 os_free(eap->identity);
5932 eap->identity = (u8 *) os_strdup(value);
5933 eap->identity_len = os_strlen(value);
5934 eap->pending_req_identity = 0;
5935 if (ssid == wpa_s->current_ssid)
5936 wpa_s->reassociate = 1;
5937 break;
5938 case WPA_CTRL_REQ_EAP_PASSWORD:
5939 bin_clear_free(eap->password, eap->password_len);
5940 eap->password = (u8 *) os_strdup(value);
5941 eap->password_len = os_strlen(value);
5942 eap->pending_req_password = 0;
5943 if (ssid == wpa_s->current_ssid)
5944 wpa_s->reassociate = 1;
5945 break;
5946 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
5947 bin_clear_free(eap->new_password, eap->new_password_len);
5948 eap->new_password = (u8 *) os_strdup(value);
5949 eap->new_password_len = os_strlen(value);
5950 eap->pending_req_new_password = 0;
5951 if (ssid == wpa_s->current_ssid)
5952 wpa_s->reassociate = 1;
5953 break;
5954 case WPA_CTRL_REQ_EAP_PIN:
5955 str_clear_free(eap->pin);
5956 eap->pin = os_strdup(value);
5957 eap->pending_req_pin = 0;
5958 if (ssid == wpa_s->current_ssid)
5959 wpa_s->reassociate = 1;
5960 break;
5961 case WPA_CTRL_REQ_EAP_OTP:
5962 bin_clear_free(eap->otp, eap->otp_len);
5963 eap->otp = (u8 *) os_strdup(value);
5964 eap->otp_len = os_strlen(value);
5965 os_free(eap->pending_req_otp);
5966 eap->pending_req_otp = NULL;
5967 eap->pending_req_otp_len = 0;
5968 break;
5969 case WPA_CTRL_REQ_EAP_PASSPHRASE:
5970 str_clear_free(eap->private_key_passwd);
5971 eap->private_key_passwd = os_strdup(value);
5972 eap->pending_req_passphrase = 0;
5973 if (ssid == wpa_s->current_ssid)
5974 wpa_s->reassociate = 1;
5975 break;
5976 case WPA_CTRL_REQ_SIM:
5977 str_clear_free(eap->external_sim_resp);
5978 eap->external_sim_resp = os_strdup(value);
5979 eap->pending_req_sim = 0;
5980 break;
5981 case WPA_CTRL_REQ_PSK_PASSPHRASE:
5982 if (wpa_config_set(ssid, "psk", value, 0) < 0)
5983 return -1;
5984 ssid->mem_only_psk = 1;
5985 if (ssid->passphrase)
5986 wpa_config_update_psk(ssid);
5987 if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
5988 wpa_supplicant_req_scan(wpa_s, 0, 0);
5989 break;
5990 case WPA_CTRL_REQ_EXT_CERT_CHECK:
5991 if (eap->pending_ext_cert_check != PENDING_CHECK)
5992 return -1;
5993 if (os_strcmp(value, "good") == 0)
5994 eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
5995 else if (os_strcmp(value, "bad") == 0)
5996 eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
5997 else
5998 return -1;
5999 break;
6000 default:
6001 wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
6002 return -1;
6003 }
6004
6005 return 0;
6006 #else /* IEEE8021X_EAPOL */
6007 wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
6008 return -1;
6009 #endif /* IEEE8021X_EAPOL */
6010 }
6011 #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
6012
6013
6014 int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
6015 {
6016 int i;
6017 unsigned int drv_enc;
6018
6019 if (wpa_s->p2p_mgmt)
6020 return 1; /* no normal network profiles on p2p_mgmt interface */
6021
6022 if (ssid == NULL)
6023 return 1;
6024
6025 if (ssid->disabled)
6026 return 1;
6027
6028 if (wpa_s->drv_capa_known)
6029 drv_enc = wpa_s->drv_enc;
6030 else
6031 drv_enc = (unsigned int) -1;
6032
6033 for (i = 0; i < NUM_WEP_KEYS; i++) {
6034 size_t len = ssid->wep_key_len[i];
6035 if (len == 0)
6036 continue;
6037 if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
6038 continue;
6039 if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
6040 continue;
6041 if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
6042 continue;
6043 return 1; /* invalid WEP key */
6044 }
6045
6046 if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
6047 (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
6048 !ssid->mem_only_psk)
6049 return 1;
6050
6051 return 0;
6052 }
6053
6054
6055 int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
6056 {
6057 #ifdef CONFIG_IEEE80211W
6058 if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
6059 if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
6060 !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
6061 /*
6062 * Driver does not support BIP -- ignore pmf=1 default
6063 * since the connection with PMF would fail and the
6064 * configuration does not require PMF to be enabled.
6065 */
6066 return NO_MGMT_FRAME_PROTECTION;
6067 }
6068
6069 if (ssid &&
6070 (ssid->key_mgmt &
6071 ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
6072 WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
6073 /*
6074 * Do not use the default PMF value for non-RSN networks
6075 * since PMF is available only with RSN and pmf=2
6076 * configuration would otherwise prevent connections to
6077 * all open networks.
6078 */
6079 return NO_MGMT_FRAME_PROTECTION;
6080 }
6081
6082 return wpa_s->conf->pmf;
6083 }
6084
6085 return ssid->ieee80211w;
6086 #else /* CONFIG_IEEE80211W */
6087 return NO_MGMT_FRAME_PROTECTION;
6088 #endif /* CONFIG_IEEE80211W */
6089 }
6090
6091
6092 int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
6093 {
6094 if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
6095 return 1;
6096 if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
6097 return 0;
6098 return -1;
6099 }
6100
6101
6102 void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
6103 {
6104 struct wpa_ssid *ssid = wpa_s->current_ssid;
6105 int dur;
6106 struct os_reltime now;
6107
6108 if (ssid == NULL) {
6109 wpa_printf(MSG_DEBUG, "Authentication failure but no known "
6110 "SSID block");
6111 return;
6112 }
6113
6114 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
6115 return;
6116
6117 ssid->auth_failures++;
6118
6119 #ifdef CONFIG_P2P
6120 if (ssid->p2p_group &&
6121 (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
6122 /*
6123 * Skip the wait time since there is a short timeout on the
6124 * connection to a P2P group.
6125 */
6126 return;
6127 }
6128 #endif /* CONFIG_P2P */
6129
6130 if (ssid->auth_failures > 50)
6131 dur = 300;
6132 else if (ssid->auth_failures > 10)
6133 dur = 120;
6134 else if (ssid->auth_failures > 5)
6135 dur = 90;
6136 else if (ssid->auth_failures > 3)
6137 dur = 60;
6138 else if (ssid->auth_failures > 2)
6139 dur = 30;
6140 else if (ssid->auth_failures > 1)
6141 dur = 20;
6142 else
6143 dur = 10;
6144
6145 if (ssid->auth_failures > 1 &&
6146 wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
6147 dur += os_random() % (ssid->auth_failures * 10);
6148
6149 os_get_reltime(&now);
6150 if (now.sec + dur <= ssid->disabled_until.sec)
6151 return;
6152
6153 ssid->disabled_until.sec = now.sec + dur;
6154
6155 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
6156 "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
6157 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
6158 ssid->auth_failures, dur, reason);
6159 }
6160
6161
6162 void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
6163 struct wpa_ssid *ssid, int clear_failures)
6164 {
6165 if (ssid == NULL)
6166 return;
6167
6168 if (ssid->disabled_until.sec) {
6169 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
6170 "id=%d ssid=\"%s\"",
6171 ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
6172 }
6173 ssid->disabled_until.sec = 0;
6174 ssid->disabled_until.usec = 0;
6175 if (clear_failures)
6176 ssid->auth_failures = 0;
6177 }
6178
6179
6180 int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
6181 {
6182 size_t i;
6183
6184 if (wpa_s->disallow_aps_bssid == NULL)
6185 return 0;
6186
6187 for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
6188 if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
6189 bssid, ETH_ALEN) == 0)
6190 return 1;
6191 }
6192
6193 return 0;
6194 }
6195
6196
6197 int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
6198 size_t ssid_len)
6199 {
6200 size_t i;
6201
6202 if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
6203 return 0;
6204
6205 for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
6206 struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
6207 if (ssid_len == s->ssid_len &&
6208 os_memcmp(ssid, s->ssid, ssid_len) == 0)
6209 return 1;
6210 }
6211
6212 return 0;
6213 }
6214
6215
6216 /**
6217 * wpas_request_connection - Request a new connection
6218 * @wpa_s: Pointer to the network interface
6219 *
6220 * This function is used to request a new connection to be found. It will mark
6221 * the interface to allow reassociation and request a new scan to find a
6222 * suitable network to connect to.
6223 */
6224 void wpas_request_connection(struct wpa_supplicant *wpa_s)
6225 {
6226 wpa_s->normal_scans = 0;
6227 wpa_s->scan_req = NORMAL_SCAN_REQ;
6228 wpa_supplicant_reinit_autoscan(wpa_s);
6229 wpa_s->extra_blacklist_count = 0;
6230 wpa_s->disconnected = 0;
6231 wpa_s->reassociate = 1;
6232
6233 if (wpa_supplicant_fast_associate(wpa_s) != 1)
6234 wpa_supplicant_req_scan(wpa_s, 0, 0);
6235 else
6236 wpa_s->reattach = 0;
6237 }
6238
6239
6240 /**
6241 * wpas_request_disconnection - Request disconnection
6242 * @wpa_s: Pointer to the network interface
6243 *
6244 * This function is used to request disconnection from the currently connected
6245 * network. This will stop any ongoing scans and initiate deauthentication.
6246 */
6247 void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
6248 {
6249 #ifdef CONFIG_SME
6250 wpa_s->sme.prev_bssid_set = 0;
6251 #endif /* CONFIG_SME */
6252 wpa_s->reassociate = 0;
6253 wpa_s->disconnected = 1;
6254 wpa_supplicant_cancel_sched_scan(wpa_s);
6255 wpa_supplicant_cancel_scan(wpa_s);
6256 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
6257 eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
6258 }
6259
6260
6261 void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
6262 struct wpa_used_freq_data *freqs_data,
6263 unsigned int len)
6264 {
6265 unsigned int i;
6266
6267 wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
6268 len, title);
6269 for (i = 0; i < len; i++) {
6270 struct wpa_used_freq_data *cur = &freqs_data[i];
6271 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
6272 i, cur->freq, cur->flags);
6273 }
6274 }
6275
6276
6277 /*
6278 * Find the operating frequencies of any of the virtual interfaces that
6279 * are using the same radio as the current interface, and in addition, get
6280 * information about the interface types that are using the frequency.
6281 */
6282 int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
6283 struct wpa_used_freq_data *freqs_data,
6284 unsigned int len)
6285 {
6286 struct wpa_supplicant *ifs;
6287 u8 bssid[ETH_ALEN];
6288 int freq;
6289 unsigned int idx = 0, i;
6290
6291 wpa_dbg(wpa_s, MSG_DEBUG,
6292 "Determining shared radio frequencies (max len %u)", len);
6293 os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
6294
6295 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6296 radio_list) {
6297 if (idx == len)
6298 break;
6299
6300 if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
6301 continue;
6302
6303 if (ifs->current_ssid->mode == WPAS_MODE_AP ||
6304 ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
6305 ifs->current_ssid->mode == WPAS_MODE_MESH)
6306 freq = ifs->current_ssid->frequency;
6307 else if (wpa_drv_get_bssid(ifs, bssid) == 0)
6308 freq = ifs->assoc_freq;
6309 else
6310 continue;
6311
6312 /* Hold only distinct freqs */
6313 for (i = 0; i < idx; i++)
6314 if (freqs_data[i].freq == freq)
6315 break;
6316
6317 if (i == idx)
6318 freqs_data[idx++].freq = freq;
6319
6320 if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
6321 freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
6322 WPA_FREQ_USED_BY_P2P_CLIENT :
6323 WPA_FREQ_USED_BY_INFRA_STATION;
6324 }
6325 }
6326
6327 dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
6328 return idx;
6329 }
6330
6331
6332 /*
6333 * Find the operating frequencies of any of the virtual interfaces that
6334 * are using the same radio as the current interface.
6335 */
6336 int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
6337 int *freq_array, unsigned int len)
6338 {
6339 struct wpa_used_freq_data *freqs_data;
6340 int num, i;
6341
6342 os_memset(freq_array, 0, sizeof(int) * len);
6343
6344 freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
6345 if (!freqs_data)
6346 return -1;
6347
6348 num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
6349 for (i = 0; i < num; i++)
6350 freq_array[i] = freqs_data[i].freq;
6351
6352 os_free(freqs_data);
6353
6354 return num;
6355 }
6356
6357
6358 struct wpa_supplicant *
6359 wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
6360 {
6361 switch (frame) {
6362 #ifdef CONFIG_P2P
6363 case VENDOR_ELEM_PROBE_REQ_P2P:
6364 case VENDOR_ELEM_PROBE_RESP_P2P:
6365 case VENDOR_ELEM_PROBE_RESP_P2P_GO:
6366 case VENDOR_ELEM_BEACON_P2P_GO:
6367 case VENDOR_ELEM_P2P_PD_REQ:
6368 case VENDOR_ELEM_P2P_PD_RESP:
6369 case VENDOR_ELEM_P2P_GO_NEG_REQ:
6370 case VENDOR_ELEM_P2P_GO_NEG_RESP:
6371 case VENDOR_ELEM_P2P_GO_NEG_CONF:
6372 case VENDOR_ELEM_P2P_INV_REQ:
6373 case VENDOR_ELEM_P2P_INV_RESP:
6374 case VENDOR_ELEM_P2P_ASSOC_REQ:
6375 case VENDOR_ELEM_P2P_ASSOC_RESP:
6376 return wpa_s->p2pdev;
6377 #endif /* CONFIG_P2P */
6378 default:
6379 return wpa_s;
6380 }
6381 }
6382
6383
6384 void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
6385 {
6386 unsigned int i;
6387 char buf[30];
6388
6389 wpa_printf(MSG_DEBUG, "Update vendor elements");
6390
6391 for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
6392 if (wpa_s->vendor_elem[i]) {
6393 int res;
6394
6395 res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
6396 if (!os_snprintf_error(sizeof(buf), res)) {
6397 wpa_hexdump_buf(MSG_DEBUG, buf,
6398 wpa_s->vendor_elem[i]);
6399 }
6400 }
6401 }
6402
6403 #ifdef CONFIG_P2P
6404 if (wpa_s->parent == wpa_s &&
6405 wpa_s->global->p2p &&
6406 !wpa_s->global->p2p_disabled)
6407 p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
6408 #endif /* CONFIG_P2P */
6409 }
6410
6411
6412 int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
6413 const u8 *elem, size_t len)
6414 {
6415 u8 *ie, *end;
6416
6417 ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
6418 end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
6419
6420 for (; ie + 1 < end; ie += 2 + ie[1]) {
6421 if (ie + len > end)
6422 break;
6423 if (os_memcmp(ie, elem, len) != 0)
6424 continue;
6425
6426 if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
6427 wpabuf_free(wpa_s->vendor_elem[frame]);
6428 wpa_s->vendor_elem[frame] = NULL;
6429 } else {
6430 os_memmove(ie, ie + len, end - (ie + len));
6431 wpa_s->vendor_elem[frame]->used -= len;
6432 }
6433 wpas_vendor_elem_update(wpa_s);
6434 return 0;
6435 }
6436
6437 return -1;
6438 }
6439
6440
6441 struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
6442 u16 num_modes, enum hostapd_hw_mode mode)
6443 {
6444 u16 i;
6445
6446 for (i = 0; i < num_modes; i++) {
6447 if (modes[i].mode == mode)
6448 return &modes[i];
6449 }
6450
6451 return NULL;
6452 }
6453
6454
6455 static struct
6456 wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
6457 const u8 *bssid)
6458 {
6459 struct wpa_bss_tmp_disallowed *bss;
6460
6461 dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
6462 struct wpa_bss_tmp_disallowed, list) {
6463 if (os_memcmp(bssid, bss->bssid, ETH_ALEN) == 0)
6464 return bss;
6465 }
6466
6467 return NULL;
6468 }
6469
6470
6471 void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
6472 unsigned int sec)
6473 {
6474 struct wpa_bss_tmp_disallowed *bss;
6475 struct os_reltime until;
6476
6477 os_get_reltime(&until);
6478 until.sec += sec;
6479
6480 bss = wpas_get_disallowed_bss(wpa_s, bssid);
6481 if (bss) {
6482 bss->disallowed_until = until;
6483 return;
6484 }
6485
6486 bss = os_malloc(sizeof(*bss));
6487 if (!bss) {
6488 wpa_printf(MSG_DEBUG,
6489 "Failed to allocate memory for temp disallow BSS");
6490 return;
6491 }
6492
6493 bss->disallowed_until = until;
6494 os_memcpy(bss->bssid, bssid, ETH_ALEN);
6495 dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
6496 }
6497
6498
6499 int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s, const u8 *bssid)
6500 {
6501 struct wpa_bss_tmp_disallowed *bss = NULL, *tmp, *prev;
6502 struct os_reltime now, age;
6503
6504 os_get_reltime(&now);
6505
6506 dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
6507 struct wpa_bss_tmp_disallowed, list) {
6508 if (!os_reltime_before(&now, &tmp->disallowed_until)) {
6509 /* This BSS is not disallowed anymore */
6510 dl_list_del(&tmp->list);
6511 os_free(tmp);
6512 continue;
6513 }
6514 if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
6515 bss = tmp;
6516 break;
6517 }
6518 }
6519 if (!bss)
6520 return 0;
6521
6522 os_reltime_sub(&bss->disallowed_until, &now, &age);
6523 wpa_printf(MSG_DEBUG,
6524 "BSS " MACSTR " disabled for %ld.%0ld seconds",
6525 MAC2STR(bss->bssid), age.sec, age.usec);
6526 return 1;
6527 }