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