]> git.ipfire.org Git - thirdparty/hostap.git/blob - wpa_supplicant/wpa_supplicant.c
GAS: Add a generic GAS query module
[thirdparty/hostap.git] / wpa_supplicant / wpa_supplicant.c
1 /*
2 * WPA Supplicant
3 * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 *
14 * This file implements functions for registering and unregistering
15 * %wpa_supplicant interfaces. In addition, this file contains number of
16 * functions for managing network connections.
17 */
18
19 #include "includes.h"
20
21 #include "common.h"
22 #include "crypto/random.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "eap_peer/eap.h"
25 #include "eap_server/eap_methods.h"
26 #include "rsn_supp/wpa.h"
27 #include "eloop.h"
28 #include "config.h"
29 #include "l2_packet/l2_packet.h"
30 #include "wpa_supplicant_i.h"
31 #include "driver_i.h"
32 #include "ctrl_iface.h"
33 #include "pcsc_funcs.h"
34 #include "common/version.h"
35 #include "rsn_supp/preauth.h"
36 #include "rsn_supp/pmksa_cache.h"
37 #include "common/wpa_ctrl.h"
38 #include "mlme.h"
39 #include "common/ieee802_11_defs.h"
40 #include "p2p/p2p.h"
41 #include "blacklist.h"
42 #include "wpas_glue.h"
43 #include "wps_supplicant.h"
44 #include "ibss_rsn.h"
45 #include "sme.h"
46 #include "gas_query.h"
47 #include "ap.h"
48 #include "p2p_supplicant.h"
49 #include "notify.h"
50 #include "bgscan.h"
51 #include "bss.h"
52 #include "scan.h"
53
54 const char *wpa_supplicant_version =
55 "wpa_supplicant v" VERSION_STR "\n"
56 "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
57
58 const char *wpa_supplicant_license =
59 "This program is free software. You can distribute it and/or modify it\n"
60 "under the terms of the GNU General Public License version 2.\n"
61 "\n"
62 "Alternatively, this software may be distributed under the terms of the\n"
63 "BSD license. See README and COPYING for more details.\n"
64 #ifdef EAP_TLS_OPENSSL
65 "\nThis product includes software developed by the OpenSSL Project\n"
66 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
67 #endif /* EAP_TLS_OPENSSL */
68 ;
69
70 #ifndef CONFIG_NO_STDOUT_DEBUG
71 /* Long text divided into parts in order to fit in C89 strings size limits. */
72 const char *wpa_supplicant_full_license1 =
73 "This program is free software; you can redistribute it and/or modify\n"
74 "it under the terms of the GNU General Public License version 2 as\n"
75 "published by the Free Software Foundation.\n"
76 "\n"
77 "This program is distributed in the hope that it will be useful,\n"
78 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
79 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
80 "GNU General Public License for more details.\n"
81 "\n";
82 const char *wpa_supplicant_full_license2 =
83 "You should have received a copy of the GNU General Public License\n"
84 "along with this program; if not, write to the Free Software\n"
85 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
86 "\n"
87 "Alternatively, this software may be distributed under the terms of the\n"
88 "BSD license.\n"
89 "\n"
90 "Redistribution and use in source and binary forms, with or without\n"
91 "modification, are permitted provided that the following conditions are\n"
92 "met:\n"
93 "\n";
94 const char *wpa_supplicant_full_license3 =
95 "1. Redistributions of source code must retain the above copyright\n"
96 " notice, this list of conditions and the following disclaimer.\n"
97 "\n"
98 "2. Redistributions in binary form must reproduce the above copyright\n"
99 " notice, this list of conditions and the following disclaimer in the\n"
100 " documentation and/or other materials provided with the distribution.\n"
101 "\n";
102 const char *wpa_supplicant_full_license4 =
103 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
104 " names of its contributors may be used to endorse or promote products\n"
105 " derived from this software without specific prior written permission.\n"
106 "\n"
107 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
108 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
109 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
110 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
111 const char *wpa_supplicant_full_license5 =
112 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
113 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
114 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
115 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
116 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
117 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
118 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
119 "\n";
120 #endif /* CONFIG_NO_STDOUT_DEBUG */
121
122 extern int wpa_debug_level;
123 extern int wpa_debug_show_keys;
124 extern int wpa_debug_timestamp;
125 extern struct wpa_driver_ops *wpa_drivers[];
126
127 /* Configure default/group WEP keys for static WEP */
128 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
129 {
130 int i, set = 0;
131
132 for (i = 0; i < NUM_WEP_KEYS; i++) {
133 if (ssid->wep_key_len[i] == 0)
134 continue;
135
136 set = 1;
137 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
138 i, i == ssid->wep_tx_keyidx, NULL, 0,
139 ssid->wep_key[i], ssid->wep_key_len[i]);
140 }
141
142 return set;
143 }
144
145
146 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
147 struct wpa_ssid *ssid)
148 {
149 u8 key[32];
150 size_t keylen;
151 enum wpa_alg alg;
152 u8 seq[6] = { 0 };
153
154 /* IBSS/WPA-None uses only one key (Group) for both receiving and
155 * sending unicast and multicast packets. */
156
157 if (ssid->mode != WPAS_MODE_IBSS) {
158 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
159 "IBSS/ad-hoc) for WPA-None", ssid->mode);
160 return -1;
161 }
162
163 if (!ssid->psk_set) {
164 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
165 "WPA-None");
166 return -1;
167 }
168
169 switch (wpa_s->group_cipher) {
170 case WPA_CIPHER_CCMP:
171 os_memcpy(key, ssid->psk, 16);
172 keylen = 16;
173 alg = WPA_ALG_CCMP;
174 break;
175 case WPA_CIPHER_TKIP:
176 /* WPA-None uses the same Michael MIC key for both TX and RX */
177 os_memcpy(key, ssid->psk, 16 + 8);
178 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
179 keylen = 32;
180 alg = WPA_ALG_TKIP;
181 break;
182 default:
183 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
184 "WPA-None", wpa_s->group_cipher);
185 return -1;
186 }
187
188 /* TODO: should actually remember the previously used seq#, both for TX
189 * and RX from each STA.. */
190
191 return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
192 }
193
194
195 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
196 {
197 struct wpa_supplicant *wpa_s = eloop_ctx;
198 const u8 *bssid = wpa_s->bssid;
199 if (is_zero_ether_addr(bssid))
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_disassociate(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 && 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->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
305 eapol_conf.required_keys = 0;
306 }
307 if (wpa_s->conf)
308 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
309 eapol_conf.workaround = ssid->eap_workaround;
310 eapol_conf.eap_disabled =
311 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
312 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
313 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
314 eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
315 #endif /* IEEE8021X_EAPOL */
316 }
317
318
319 /**
320 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
321 * @wpa_s: Pointer to wpa_supplicant data
322 * @ssid: Configuration data for the network
323 *
324 * This function is used to configure WPA state machine and related parameters
325 * to a mode where WPA is not enabled. This is called as part of the
326 * authentication configuration when the selected network does not use WPA.
327 */
328 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
329 struct wpa_ssid *ssid)
330 {
331 int i;
332
333 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
334 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
335 else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
336 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
337 else
338 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
339 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
340 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
341 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
342 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
343 wpa_s->group_cipher = WPA_CIPHER_NONE;
344 wpa_s->mgmt_group_cipher = 0;
345
346 for (i = 0; i < NUM_WEP_KEYS; i++) {
347 if (ssid->wep_key_len[i] > 5) {
348 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
349 wpa_s->group_cipher = WPA_CIPHER_WEP104;
350 break;
351 } else if (ssid->wep_key_len[i] > 0) {
352 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
353 wpa_s->group_cipher = WPA_CIPHER_WEP40;
354 break;
355 }
356 }
357
358 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
359 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
360 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
361 wpa_s->pairwise_cipher);
362 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
363 #ifdef CONFIG_IEEE80211W
364 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
365 wpa_s->mgmt_group_cipher);
366 #endif /* CONFIG_IEEE80211W */
367
368 pmksa_cache_clear_current(wpa_s->wpa);
369 }
370
371
372 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
373 {
374 bgscan_deinit(wpa_s);
375 scard_deinit(wpa_s->scard);
376 wpa_s->scard = NULL;
377 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
378 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
379 l2_packet_deinit(wpa_s->l2);
380 wpa_s->l2 = NULL;
381 if (wpa_s->l2_br) {
382 l2_packet_deinit(wpa_s->l2_br);
383 wpa_s->l2_br = NULL;
384 }
385
386 if (wpa_s->ctrl_iface) {
387 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
388 wpa_s->ctrl_iface = NULL;
389 }
390 if (wpa_s->conf != NULL) {
391 struct wpa_ssid *ssid;
392 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
393 wpas_notify_network_removed(wpa_s, ssid);
394 wpa_config_free(wpa_s->conf);
395 wpa_s->conf = NULL;
396 }
397
398 os_free(wpa_s->confname);
399 wpa_s->confname = NULL;
400
401 wpa_sm_set_eapol(wpa_s->wpa, NULL);
402 eapol_sm_deinit(wpa_s->eapol);
403 wpa_s->eapol = NULL;
404
405 rsn_preauth_deinit(wpa_s->wpa);
406
407 #ifdef CONFIG_TDLS
408 wpa_tdls_deinit(wpa_s->wpa);
409 #endif /* CONFIG_TDLS */
410
411 pmksa_candidate_free(wpa_s->wpa);
412 wpa_sm_deinit(wpa_s->wpa);
413 wpa_s->wpa = NULL;
414 wpa_blacklist_clear(wpa_s);
415
416 wpa_bss_deinit(wpa_s);
417
418 wpa_supplicant_cancel_scan(wpa_s);
419 wpa_supplicant_cancel_auth_timeout(wpa_s);
420
421 ieee80211_sta_deinit(wpa_s);
422
423 wpas_wps_deinit(wpa_s);
424
425 wpabuf_free(wpa_s->pending_eapol_rx);
426 wpa_s->pending_eapol_rx = NULL;
427
428 #ifdef CONFIG_IBSS_RSN
429 ibss_rsn_deinit(wpa_s->ibss_rsn);
430 wpa_s->ibss_rsn = NULL;
431 #endif /* CONFIG_IBSS_RSN */
432
433 sme_deinit(wpa_s);
434
435 #ifdef CONFIG_AP
436 wpa_supplicant_ap_deinit(wpa_s);
437 #endif /* CONFIG_AP */
438
439 #ifdef CONFIG_P2P
440 wpas_p2p_deinit(wpa_s);
441 #endif /* CONFIG_P2P */
442
443 os_free(wpa_s->next_scan_freqs);
444 wpa_s->next_scan_freqs = NULL;
445
446 gas_query_deinit(wpa_s->gas);
447 wpa_s->gas = NULL;
448 }
449
450
451 /**
452 * wpa_clear_keys - Clear keys configured for the driver
453 * @wpa_s: Pointer to wpa_supplicant data
454 * @addr: Previously used BSSID or %NULL if not available
455 *
456 * This function clears the encryption keys that has been previously configured
457 * for the driver.
458 */
459 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
460 {
461 if (wpa_s->keys_cleared) {
462 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
463 * timing issues with keys being cleared just before new keys
464 * are set or just after association or something similar. This
465 * shows up in group key handshake failing often because of the
466 * client not receiving the first encrypted packets correctly.
467 * Skipping some of the extra key clearing steps seems to help
468 * in completing group key handshake more reliably. */
469 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
470 "skip key clearing");
471 return;
472 }
473
474 /* MLME-DELETEKEYS.request */
475 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
476 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
477 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
478 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
479 #ifdef CONFIG_IEEE80211W
480 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
481 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
482 #endif /* CONFIG_IEEE80211W */
483 if (addr) {
484 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
485 0);
486 /* MLME-SETPROTECTION.request(None) */
487 wpa_drv_mlme_setprotection(
488 wpa_s, addr,
489 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
490 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
491 }
492 wpa_s->keys_cleared = 1;
493 }
494
495
496 /**
497 * wpa_supplicant_state_txt - Get the connection state name as a text string
498 * @state: State (wpa_state; WPA_*)
499 * Returns: The state name as a printable text string
500 */
501 const char * wpa_supplicant_state_txt(enum wpa_states state)
502 {
503 switch (state) {
504 case WPA_DISCONNECTED:
505 return "DISCONNECTED";
506 case WPA_INACTIVE:
507 return "INACTIVE";
508 case WPA_INTERFACE_DISABLED:
509 return "INTERFACE_DISABLED";
510 case WPA_SCANNING:
511 return "SCANNING";
512 case WPA_AUTHENTICATING:
513 return "AUTHENTICATING";
514 case WPA_ASSOCIATING:
515 return "ASSOCIATING";
516 case WPA_ASSOCIATED:
517 return "ASSOCIATED";
518 case WPA_4WAY_HANDSHAKE:
519 return "4WAY_HANDSHAKE";
520 case WPA_GROUP_HANDSHAKE:
521 return "GROUP_HANDSHAKE";
522 case WPA_COMPLETED:
523 return "COMPLETED";
524 default:
525 return "UNKNOWN";
526 }
527 }
528
529
530 #ifdef CONFIG_BGSCAN
531
532 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
533 {
534 if (wpas_driver_bss_selection(wpa_s))
535 return;
536 if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
537 return;
538
539 bgscan_deinit(wpa_s);
540 if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
541 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
542 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
543 "bgscan");
544 /*
545 * Live without bgscan; it is only used as a roaming
546 * optimization, so the initial connection is not
547 * affected.
548 */
549 } else
550 wpa_s->bgscan_ssid = wpa_s->current_ssid;
551 } else
552 wpa_s->bgscan_ssid = NULL;
553 }
554
555
556 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
557 {
558 if (wpa_s->bgscan_ssid != NULL) {
559 bgscan_deinit(wpa_s);
560 wpa_s->bgscan_ssid = NULL;
561 }
562 }
563
564 #endif /* CONFIG_BGSCAN */
565
566
567 /**
568 * wpa_supplicant_set_state - Set current connection state
569 * @wpa_s: Pointer to wpa_supplicant data
570 * @state: The new connection state
571 *
572 * This function is called whenever the connection state changes, e.g.,
573 * association is completed for WPA/WPA2 4-Way Handshake is started.
574 */
575 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
576 enum wpa_states state)
577 {
578 enum wpa_states old_state = wpa_s->wpa_state;
579
580 wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
581 wpa_supplicant_state_txt(wpa_s->wpa_state),
582 wpa_supplicant_state_txt(state));
583
584 if (state != WPA_SCANNING)
585 wpa_supplicant_notify_scanning(wpa_s, 0);
586
587 if (state == WPA_COMPLETED && wpa_s->new_connection) {
588 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
589 struct wpa_ssid *ssid = wpa_s->current_ssid;
590 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
591 MACSTR " completed %s [id=%d id_str=%s]",
592 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
593 "(reauth)" : "(auth)",
594 ssid ? ssid->id : -1,
595 ssid && ssid->id_str ? ssid->id_str : "");
596 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
597 wpa_s->new_connection = 0;
598 wpa_s->reassociated_connection = 1;
599 wpa_drv_set_operstate(wpa_s, 1);
600 #ifndef IEEE8021X_EAPOL
601 wpa_drv_set_supp_port(wpa_s, 1);
602 #endif /* IEEE8021X_EAPOL */
603 wpa_s->after_wps = 0;
604 #ifdef CONFIG_P2P
605 wpas_p2p_completed(wpa_s);
606 #endif /* CONFIG_P2P */
607 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
608 state == WPA_ASSOCIATED) {
609 wpa_s->new_connection = 1;
610 wpa_drv_set_operstate(wpa_s, 0);
611 #ifndef IEEE8021X_EAPOL
612 wpa_drv_set_supp_port(wpa_s, 0);
613 #endif /* IEEE8021X_EAPOL */
614 }
615 wpa_s->wpa_state = state;
616
617 #ifdef CONFIG_BGSCAN
618 if (state == WPA_COMPLETED)
619 wpa_supplicant_start_bgscan(wpa_s);
620 else
621 wpa_supplicant_stop_bgscan(wpa_s);
622 #endif /* CONFIG_BGSCAN */
623
624 if (wpa_s->wpa_state != old_state) {
625 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
626
627 if (wpa_s->wpa_state == WPA_COMPLETED ||
628 old_state == WPA_COMPLETED)
629 wpas_notify_auth_changed(wpa_s);
630 }
631 }
632
633
634 void wpa_supplicant_terminate_proc(struct wpa_global *global)
635 {
636 int pending = 0;
637 #ifdef CONFIG_WPS
638 struct wpa_supplicant *wpa_s = global->ifaces;
639 while (wpa_s) {
640 if (wpas_wps_terminate_pending(wpa_s) == 1)
641 pending = 1;
642 wpa_s = wpa_s->next;
643 }
644 #endif /* CONFIG_WPS */
645 if (pending)
646 return;
647 eloop_terminate();
648 }
649
650
651 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
652 {
653 struct wpa_global *global = signal_ctx;
654 struct wpa_supplicant *wpa_s;
655 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
656 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
657 "received", sig);
658 }
659 wpa_supplicant_terminate_proc(global);
660 }
661
662
663 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
664 {
665 enum wpa_states old_state = wpa_s->wpa_state;
666
667 wpa_s->pairwise_cipher = 0;
668 wpa_s->group_cipher = 0;
669 wpa_s->mgmt_group_cipher = 0;
670 wpa_s->key_mgmt = 0;
671 if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
672 wpa_s->wpa_state = WPA_DISCONNECTED;
673
674 if (wpa_s->wpa_state != old_state)
675 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
676 }
677
678
679 /**
680 * wpa_supplicant_reload_configuration - Reload configuration data
681 * @wpa_s: Pointer to wpa_supplicant data
682 * Returns: 0 on success or -1 if configuration parsing failed
683 *
684 * This function can be used to request that the configuration data is reloaded
685 * (e.g., after configuration file change). This function is reloading
686 * configuration only for one interface, so this may need to be called multiple
687 * times if %wpa_supplicant is controlling multiple interfaces and all
688 * interfaces need reconfiguration.
689 */
690 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
691 {
692 struct wpa_config *conf;
693 int reconf_ctrl;
694 int old_ap_scan;
695
696 if (wpa_s->confname == NULL)
697 return -1;
698 conf = wpa_config_read(wpa_s->confname);
699 if (conf == NULL) {
700 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
701 "file '%s' - exiting", wpa_s->confname);
702 return -1;
703 }
704 conf->changed_parameters = (unsigned int) -1;
705
706 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
707 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
708 os_strcmp(conf->ctrl_interface,
709 wpa_s->conf->ctrl_interface) != 0);
710
711 if (reconf_ctrl && wpa_s->ctrl_iface) {
712 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
713 wpa_s->ctrl_iface = NULL;
714 }
715
716 eapol_sm_invalidate_cached_session(wpa_s->eapol);
717 if (wpa_s->current_ssid) {
718 wpa_supplicant_deauthenticate(wpa_s,
719 WLAN_REASON_DEAUTH_LEAVING);
720 }
721
722 /*
723 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
724 * pkcs11_engine_path, pkcs11_module_path.
725 */
726 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
727 /*
728 * Clear forced success to clear EAP state for next
729 * authentication.
730 */
731 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
732 }
733 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
734 wpa_sm_set_config(wpa_s->wpa, NULL);
735 wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
736 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
737 rsn_preauth_deinit(wpa_s->wpa);
738
739 old_ap_scan = wpa_s->conf->ap_scan;
740 wpa_config_free(wpa_s->conf);
741 wpa_s->conf = conf;
742 if (old_ap_scan != wpa_s->conf->ap_scan)
743 wpas_notify_ap_scan_changed(wpa_s);
744
745 if (reconf_ctrl)
746 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
747
748 wpa_supplicant_update_config(wpa_s);
749
750 wpa_supplicant_clear_status(wpa_s);
751 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
752 wpa_s->reassociate = 1;
753 wpa_supplicant_req_scan(wpa_s, 0, 0);
754 }
755 wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
756 return 0;
757 }
758
759
760 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
761 {
762 struct wpa_global *global = signal_ctx;
763 struct wpa_supplicant *wpa_s;
764 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
765 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
766 sig);
767 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
768 wpa_supplicant_terminate_proc(global);
769 }
770 }
771 }
772
773
774 enum wpa_cipher cipher_suite2driver(int cipher)
775 {
776 switch (cipher) {
777 case WPA_CIPHER_NONE:
778 return CIPHER_NONE;
779 case WPA_CIPHER_WEP40:
780 return CIPHER_WEP40;
781 case WPA_CIPHER_WEP104:
782 return CIPHER_WEP104;
783 case WPA_CIPHER_CCMP:
784 return CIPHER_CCMP;
785 case WPA_CIPHER_TKIP:
786 default:
787 return CIPHER_TKIP;
788 }
789 }
790
791
792 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
793 {
794 switch (key_mgmt) {
795 case WPA_KEY_MGMT_NONE:
796 return KEY_MGMT_NONE;
797 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
798 return KEY_MGMT_802_1X_NO_WPA;
799 case WPA_KEY_MGMT_IEEE8021X:
800 return KEY_MGMT_802_1X;
801 case WPA_KEY_MGMT_WPA_NONE:
802 return KEY_MGMT_WPA_NONE;
803 case WPA_KEY_MGMT_FT_IEEE8021X:
804 return KEY_MGMT_FT_802_1X;
805 case WPA_KEY_MGMT_FT_PSK:
806 return KEY_MGMT_FT_PSK;
807 case WPA_KEY_MGMT_IEEE8021X_SHA256:
808 return KEY_MGMT_802_1X_SHA256;
809 case WPA_KEY_MGMT_PSK_SHA256:
810 return KEY_MGMT_PSK_SHA256;
811 case WPA_KEY_MGMT_WPS:
812 return KEY_MGMT_WPS;
813 case WPA_KEY_MGMT_PSK:
814 default:
815 return KEY_MGMT_PSK;
816 }
817 }
818
819
820 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
821 struct wpa_ssid *ssid,
822 struct wpa_ie_data *ie)
823 {
824 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
825 if (ret) {
826 if (ret == -2) {
827 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
828 "from association info");
829 }
830 return -1;
831 }
832
833 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
834 "cipher suites");
835 if (!(ie->group_cipher & ssid->group_cipher)) {
836 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
837 "cipher 0x%x (mask 0x%x) - reject",
838 ie->group_cipher, ssid->group_cipher);
839 return -1;
840 }
841 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
842 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
843 "cipher 0x%x (mask 0x%x) - reject",
844 ie->pairwise_cipher, ssid->pairwise_cipher);
845 return -1;
846 }
847 if (!(ie->key_mgmt & ssid->key_mgmt)) {
848 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
849 "management 0x%x (mask 0x%x) - reject",
850 ie->key_mgmt, ssid->key_mgmt);
851 return -1;
852 }
853
854 #ifdef CONFIG_IEEE80211W
855 if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
856 ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
857 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
858 "that does not support management frame protection - "
859 "reject");
860 return -1;
861 }
862 #endif /* CONFIG_IEEE80211W */
863
864 return 0;
865 }
866
867
868 /**
869 * wpa_supplicant_set_suites - Set authentication and encryption parameters
870 * @wpa_s: Pointer to wpa_supplicant data
871 * @bss: Scan results for the selected BSS, or %NULL if not available
872 * @ssid: Configuration data for the selected network
873 * @wpa_ie: Buffer for the WPA/RSN IE
874 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
875 * used buffer length in case the functions returns success.
876 * Returns: 0 on success or -1 on failure
877 *
878 * This function is used to configure authentication and encryption parameters
879 * based on the network configuration and scan result for the selected BSS (if
880 * available).
881 */
882 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
883 struct wpa_bss *bss, struct wpa_ssid *ssid,
884 u8 *wpa_ie, size_t *wpa_ie_len)
885 {
886 struct wpa_ie_data ie;
887 int sel, proto;
888 const u8 *bss_wpa, *bss_rsn;
889
890 if (bss) {
891 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
892 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
893 } else
894 bss_wpa = bss_rsn = NULL;
895
896 if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
897 wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
898 (ie.group_cipher & ssid->group_cipher) &&
899 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
900 (ie.key_mgmt & ssid->key_mgmt)) {
901 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
902 proto = WPA_PROTO_RSN;
903 } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
904 wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
905 (ie.group_cipher & ssid->group_cipher) &&
906 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
907 (ie.key_mgmt & ssid->key_mgmt)) {
908 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
909 proto = WPA_PROTO_WPA;
910 } else if (bss) {
911 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
912 return -1;
913 } else {
914 if (ssid->proto & WPA_PROTO_RSN)
915 proto = WPA_PROTO_RSN;
916 else
917 proto = WPA_PROTO_WPA;
918 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
919 os_memset(&ie, 0, sizeof(ie));
920 ie.group_cipher = ssid->group_cipher;
921 ie.pairwise_cipher = ssid->pairwise_cipher;
922 ie.key_mgmt = ssid->key_mgmt;
923 #ifdef CONFIG_IEEE80211W
924 ie.mgmt_group_cipher =
925 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
926 WPA_CIPHER_AES_128_CMAC : 0;
927 #endif /* CONFIG_IEEE80211W */
928 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
929 "based on configuration");
930 } else
931 proto = ie.proto;
932 }
933
934 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
935 "pairwise %d key_mgmt %d proto %d",
936 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
937 #ifdef CONFIG_IEEE80211W
938 if (ssid->ieee80211w) {
939 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
940 ie.mgmt_group_cipher);
941 }
942 #endif /* CONFIG_IEEE80211W */
943
944 wpa_s->wpa_proto = proto;
945 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
946 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
947 !!(ssid->proto & WPA_PROTO_RSN));
948
949 if (bss || !wpa_s->ap_ies_from_associnfo) {
950 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
951 bss_wpa ? 2 + bss_wpa[1] : 0) ||
952 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
953 bss_rsn ? 2 + bss_rsn[1] : 0))
954 return -1;
955 }
956
957 sel = ie.group_cipher & ssid->group_cipher;
958 if (sel & WPA_CIPHER_CCMP) {
959 wpa_s->group_cipher = WPA_CIPHER_CCMP;
960 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
961 } else if (sel & WPA_CIPHER_TKIP) {
962 wpa_s->group_cipher = WPA_CIPHER_TKIP;
963 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
964 } else if (sel & WPA_CIPHER_WEP104) {
965 wpa_s->group_cipher = WPA_CIPHER_WEP104;
966 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
967 } else if (sel & WPA_CIPHER_WEP40) {
968 wpa_s->group_cipher = WPA_CIPHER_WEP40;
969 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
970 } else {
971 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
972 "cipher");
973 return -1;
974 }
975
976 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
977 if (sel & WPA_CIPHER_CCMP) {
978 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
979 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
980 } else if (sel & WPA_CIPHER_TKIP) {
981 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
982 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
983 } else if (sel & WPA_CIPHER_NONE) {
984 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
985 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
986 } else {
987 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
988 "cipher");
989 return -1;
990 }
991
992 sel = ie.key_mgmt & ssid->key_mgmt;
993 if (0) {
994 #ifdef CONFIG_IEEE80211R
995 } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
996 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
997 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
998 } else if (sel & WPA_KEY_MGMT_FT_PSK) {
999 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
1000 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
1001 #endif /* CONFIG_IEEE80211R */
1002 #ifdef CONFIG_IEEE80211W
1003 } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1004 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1005 wpa_dbg(wpa_s, MSG_DEBUG,
1006 "WPA: using KEY_MGMT 802.1X with SHA256");
1007 } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1008 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1009 wpa_dbg(wpa_s, MSG_DEBUG,
1010 "WPA: using KEY_MGMT PSK with SHA256");
1011 #endif /* CONFIG_IEEE80211W */
1012 } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1013 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1014 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1015 } else if (sel & WPA_KEY_MGMT_PSK) {
1016 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1017 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1018 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1019 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1020 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1021 } else {
1022 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1023 "authenticated key management type");
1024 return -1;
1025 }
1026
1027 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1028 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1029 wpa_s->pairwise_cipher);
1030 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1031
1032 #ifdef CONFIG_IEEE80211W
1033 sel = ie.mgmt_group_cipher;
1034 if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1035 !(ie.capabilities & WPA_CAPABILITY_MFPC))
1036 sel = 0;
1037 if (sel & WPA_CIPHER_AES_128_CMAC) {
1038 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1039 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1040 "AES-128-CMAC");
1041 } else {
1042 wpa_s->mgmt_group_cipher = 0;
1043 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1044 }
1045 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1046 wpa_s->mgmt_group_cipher);
1047 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1048 #endif /* CONFIG_IEEE80211W */
1049
1050 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1051 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1052 return -1;
1053 }
1054
1055 if (ssid->key_mgmt &
1056 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
1057 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1058 else
1059 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1060
1061 return 0;
1062 }
1063
1064
1065 /**
1066 * wpa_supplicant_associate - Request association
1067 * @wpa_s: Pointer to wpa_supplicant data
1068 * @bss: Scan results for the selected BSS, or %NULL if not available
1069 * @ssid: Configuration data for the selected network
1070 *
1071 * This function is used to request %wpa_supplicant to associate with a BSS.
1072 */
1073 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1074 struct wpa_bss *bss, struct wpa_ssid *ssid)
1075 {
1076 u8 wpa_ie[200];
1077 size_t wpa_ie_len;
1078 int use_crypt, ret, i, bssid_changed;
1079 int algs = WPA_AUTH_ALG_OPEN;
1080 enum wpa_cipher cipher_pairwise, cipher_group;
1081 struct wpa_driver_associate_params params;
1082 int wep_keys_set = 0;
1083 struct wpa_driver_capa capa;
1084 int assoc_failed = 0;
1085 struct wpa_ssid *old_ssid;
1086
1087 #ifdef CONFIG_IBSS_RSN
1088 ibss_rsn_deinit(wpa_s->ibss_rsn);
1089 wpa_s->ibss_rsn = NULL;
1090 #endif /* CONFIG_IBSS_RSN */
1091
1092 if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1093 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1094 #ifdef CONFIG_AP
1095 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1096 wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1097 "mode");
1098 return;
1099 }
1100 wpa_supplicant_create_ap(wpa_s, ssid);
1101 wpa_s->current_bss = bss;
1102 #else /* CONFIG_AP */
1103 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1104 "the build");
1105 #endif /* CONFIG_AP */
1106 return;
1107 }
1108
1109 #ifdef CONFIG_TDLS
1110 if (bss)
1111 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1112 bss->ie_len);
1113 #endif /* CONFIG_TDLS */
1114
1115 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1116 ssid->mode == IEEE80211_MODE_INFRA) {
1117 sme_authenticate(wpa_s, bss, ssid);
1118 return;
1119 }
1120
1121 os_memset(&params, 0, sizeof(params));
1122 wpa_s->reassociate = 0;
1123 if (bss && !wpas_driver_bss_selection(wpa_s)) {
1124 #ifdef CONFIG_IEEE80211R
1125 const u8 *ie, *md = NULL;
1126 #endif /* CONFIG_IEEE80211R */
1127 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1128 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1129 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1130 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1131 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1132 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1133 if (bssid_changed)
1134 wpas_notify_bssid_changed(wpa_s);
1135 #ifdef CONFIG_IEEE80211R
1136 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1137 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1138 md = ie + 2;
1139 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1140 if (md) {
1141 /* Prepare for the next transition */
1142 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1143 }
1144 #endif /* CONFIG_IEEE80211R */
1145 #ifdef CONFIG_WPS
1146 } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1147 wpa_s->conf->ap_scan == 2 &&
1148 (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1149 /* Use ap_scan==1 style network selection to find the network
1150 */
1151 wpa_s->scan_req = 2;
1152 wpa_s->reassociate = 1;
1153 wpa_supplicant_req_scan(wpa_s, 0, 0);
1154 return;
1155 #endif /* CONFIG_WPS */
1156 } else {
1157 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1158 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1159 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1160 }
1161 wpa_supplicant_cancel_scan(wpa_s);
1162
1163 /* Starting new association, so clear the possibly used WPA IE from the
1164 * previous association. */
1165 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1166
1167 #ifdef IEEE8021X_EAPOL
1168 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1169 if (ssid->leap) {
1170 if (ssid->non_leap == 0)
1171 algs = WPA_AUTH_ALG_LEAP;
1172 else
1173 algs |= WPA_AUTH_ALG_LEAP;
1174 }
1175 }
1176 #endif /* IEEE8021X_EAPOL */
1177 wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1178 if (ssid->auth_alg) {
1179 algs = ssid->auth_alg;
1180 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1181 "0x%x", algs);
1182 }
1183
1184 if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1185 wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1186 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
1187 WPA_KEY_MGMT_FT_IEEE8021X |
1188 WPA_KEY_MGMT_FT_PSK |
1189 WPA_KEY_MGMT_IEEE8021X_SHA256 |
1190 WPA_KEY_MGMT_PSK_SHA256))) {
1191 int try_opportunistic;
1192 try_opportunistic = ssid->proactive_key_caching &&
1193 (ssid->proto & WPA_PROTO_RSN);
1194 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1195 wpa_s->current_ssid,
1196 try_opportunistic) == 0)
1197 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1198 wpa_ie_len = sizeof(wpa_ie);
1199 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1200 wpa_ie, &wpa_ie_len)) {
1201 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1202 "key management and encryption suites");
1203 return;
1204 }
1205 } else if (ssid->key_mgmt &
1206 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1207 WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
1208 WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
1209 WPA_KEY_MGMT_IEEE8021X_SHA256)) {
1210 wpa_ie_len = sizeof(wpa_ie);
1211 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1212 wpa_ie, &wpa_ie_len)) {
1213 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1214 "key management and encryption suites (no "
1215 "scan results)");
1216 return;
1217 }
1218 #ifdef CONFIG_WPS
1219 } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1220 struct wpabuf *wps_ie;
1221 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1222 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1223 wpa_ie_len = wpabuf_len(wps_ie);
1224 os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1225 } else
1226 wpa_ie_len = 0;
1227 wpabuf_free(wps_ie);
1228 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1229 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1230 params.wps = WPS_MODE_PRIVACY;
1231 else
1232 params.wps = WPS_MODE_OPEN;
1233 wpa_s->wpa_proto = 0;
1234 #endif /* CONFIG_WPS */
1235 } else {
1236 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1237 wpa_ie_len = 0;
1238 wpa_s->wpa_proto = 0;
1239 }
1240
1241 #ifdef CONFIG_P2P
1242 if (wpa_s->global->p2p) {
1243 u8 *pos;
1244 size_t len;
1245 int res;
1246 int p2p_group;
1247 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
1248 pos = wpa_ie + wpa_ie_len;
1249 len = sizeof(wpa_ie) - wpa_ie_len;
1250 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
1251 if (res >= 0)
1252 wpa_ie_len += res;
1253 }
1254
1255 wpa_s->cross_connect_disallowed = 0;
1256 if (bss) {
1257 struct wpabuf *p2p;
1258 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1259 if (p2p) {
1260 wpa_s->cross_connect_disallowed =
1261 p2p_get_cross_connect_disallowed(p2p);
1262 wpabuf_free(p2p);
1263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1264 "connection",
1265 wpa_s->cross_connect_disallowed ?
1266 "disallows" : "allows");
1267 }
1268 }
1269 #endif /* CONFIG_P2P */
1270
1271 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1272 use_crypt = 1;
1273 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1274 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1275 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1276 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1277 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1278 use_crypt = 0;
1279 if (wpa_set_wep_keys(wpa_s, ssid)) {
1280 use_crypt = 1;
1281 wep_keys_set = 1;
1282 }
1283 }
1284 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1285 use_crypt = 0;
1286
1287 #ifdef IEEE8021X_EAPOL
1288 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1289 if ((ssid->eapol_flags &
1290 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1291 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1292 !wep_keys_set) {
1293 use_crypt = 0;
1294 } else {
1295 /* Assume that dynamic WEP-104 keys will be used and
1296 * set cipher suites in order for drivers to expect
1297 * encryption. */
1298 cipher_pairwise = cipher_group = CIPHER_WEP104;
1299 }
1300 }
1301 #endif /* IEEE8021X_EAPOL */
1302
1303 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1304 /* Set the key before (and later after) association */
1305 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1306 }
1307
1308 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1309 if (bss) {
1310 params.ssid = bss->ssid;
1311 params.ssid_len = bss->ssid_len;
1312 if (!wpas_driver_bss_selection(wpa_s)) {
1313 params.bssid = bss->bssid;
1314 params.freq = bss->freq;
1315 }
1316 } else {
1317 params.ssid = ssid->ssid;
1318 params.ssid_len = ssid->ssid_len;
1319 }
1320 if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1321 params.freq == 0)
1322 params.freq = ssid->frequency; /* Initial channel for IBSS */
1323 params.wpa_ie = wpa_ie;
1324 params.wpa_ie_len = wpa_ie_len;
1325 params.pairwise_suite = cipher_pairwise;
1326 params.group_suite = cipher_group;
1327 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1328 params.wpa_proto = wpa_s->wpa_proto;
1329 params.auth_alg = algs;
1330 params.mode = ssid->mode;
1331 for (i = 0; i < NUM_WEP_KEYS; i++) {
1332 if (ssid->wep_key_len[i])
1333 params.wep_key[i] = ssid->wep_key[i];
1334 params.wep_key_len[i] = ssid->wep_key_len[i];
1335 }
1336 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1337
1338 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1339 (params.key_mgmt_suite == KEY_MGMT_PSK ||
1340 params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1341 params.passphrase = ssid->passphrase;
1342 if (ssid->psk_set)
1343 params.psk = ssid->psk;
1344 }
1345
1346 params.drop_unencrypted = use_crypt;
1347
1348 #ifdef CONFIG_IEEE80211W
1349 params.mgmt_frame_protection = ssid->ieee80211w;
1350 if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1351 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1352 struct wpa_ie_data ie;
1353 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1354 ie.capabilities &
1355 (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1356 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1357 "MFP: require MFP");
1358 params.mgmt_frame_protection =
1359 MGMT_FRAME_PROTECTION_REQUIRED;
1360 }
1361 }
1362 #endif /* CONFIG_IEEE80211W */
1363
1364 params.p2p = ssid->p2p_group;
1365
1366 if (wpa_s->parent->set_sta_uapsd)
1367 params.uapsd = wpa_s->parent->sta_uapsd;
1368 else
1369 params.uapsd = -1;
1370
1371 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1372 ret = ieee80211_sta_associate(wpa_s, &params);
1373 else
1374 ret = wpa_drv_associate(wpa_s, &params);
1375 if (ret < 0) {
1376 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1377 "failed");
1378 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1379 /*
1380 * The driver is known to mean what is saying, so we
1381 * can stop right here; the association will not
1382 * succeed.
1383 */
1384 wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1385 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1386 return;
1387 }
1388 /* try to continue anyway; new association will be tried again
1389 * after timeout */
1390 assoc_failed = 1;
1391 }
1392
1393 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1394 /* Set the key after the association just in case association
1395 * cleared the previously configured key. */
1396 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1397 /* No need to timeout authentication since there is no key
1398 * management. */
1399 wpa_supplicant_cancel_auth_timeout(wpa_s);
1400 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1401 #ifdef CONFIG_IBSS_RSN
1402 } else if (ssid->mode == WPAS_MODE_IBSS &&
1403 wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1404 wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1405 /*
1406 * RSN IBSS authentication is per-STA and we can disable the
1407 * per-BSSID authentication.
1408 */
1409 wpa_supplicant_cancel_auth_timeout(wpa_s);
1410 #endif /* CONFIG_IBSS_RSN */
1411 } else {
1412 /* Timeout for IEEE 802.11 authentication and association */
1413 int timeout = 60;
1414
1415 if (assoc_failed) {
1416 /* give IBSS a bit more time */
1417 timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1418 } else if (wpa_s->conf->ap_scan == 1) {
1419 /* give IBSS a bit more time */
1420 timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1421 }
1422 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1423 }
1424
1425 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1426 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1427 /* Set static WEP keys again */
1428 wpa_set_wep_keys(wpa_s, ssid);
1429 }
1430
1431 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1432 /*
1433 * Do not allow EAP session resumption between different
1434 * network configurations.
1435 */
1436 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1437 }
1438 old_ssid = wpa_s->current_ssid;
1439 wpa_s->current_ssid = ssid;
1440 wpa_s->current_bss = bss;
1441 wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1442 wpa_supplicant_initiate_eapol(wpa_s);
1443 if (old_ssid != wpa_s->current_ssid)
1444 wpas_notify_network_changed(wpa_s);
1445 }
1446
1447
1448 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1449 const u8 *addr)
1450 {
1451 struct wpa_ssid *old_ssid;
1452
1453 wpa_clear_keys(wpa_s, addr);
1454 wpa_supplicant_mark_disassoc(wpa_s);
1455 old_ssid = wpa_s->current_ssid;
1456 wpa_s->current_ssid = NULL;
1457 wpa_s->current_bss = NULL;
1458 wpa_sm_set_config(wpa_s->wpa, NULL);
1459 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1460 if (old_ssid != wpa_s->current_ssid)
1461 wpas_notify_network_changed(wpa_s);
1462 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1463 }
1464
1465
1466 /**
1467 * wpa_supplicant_disassociate - Disassociate the current connection
1468 * @wpa_s: Pointer to wpa_supplicant data
1469 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1470 *
1471 * This function is used to request %wpa_supplicant to disassociate with the
1472 * current AP.
1473 */
1474 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1475 int reason_code)
1476 {
1477 u8 *addr = NULL;
1478
1479 if (!is_zero_ether_addr(wpa_s->bssid)) {
1480 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1481 ieee80211_sta_disassociate(wpa_s, reason_code);
1482 else
1483 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1484 addr = wpa_s->bssid;
1485 }
1486
1487 wpa_supplicant_clear_connection(wpa_s, addr);
1488 }
1489
1490
1491 /**
1492 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1493 * @wpa_s: Pointer to wpa_supplicant data
1494 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1495 *
1496 * This function is used to request %wpa_supplicant to deauthenticate from the
1497 * current AP.
1498 */
1499 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1500 int reason_code)
1501 {
1502 u8 *addr = NULL;
1503
1504 if (!is_zero_ether_addr(wpa_s->bssid)) {
1505 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1506 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1507 else
1508 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1509 reason_code);
1510 addr = wpa_s->bssid;
1511 }
1512
1513 wpa_supplicant_clear_connection(wpa_s, addr);
1514 }
1515
1516
1517 /**
1518 * wpa_supplicant_enable_network - Mark a configured network as enabled
1519 * @wpa_s: wpa_supplicant structure for a network interface
1520 * @ssid: wpa_ssid structure for a configured network or %NULL
1521 *
1522 * Enables the specified network or all networks if no network specified.
1523 */
1524 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1525 struct wpa_ssid *ssid)
1526 {
1527 struct wpa_ssid *other_ssid;
1528 int was_disabled;
1529
1530 if (ssid == NULL) {
1531 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1532 other_ssid = other_ssid->next) {
1533 if (other_ssid->disabled == 2)
1534 continue; /* do not change persistent P2P group
1535 * data */
1536 if (other_ssid == wpa_s->current_ssid &&
1537 other_ssid->disabled)
1538 wpa_s->reassociate = 1;
1539
1540 was_disabled = other_ssid->disabled;
1541
1542 other_ssid->disabled = 0;
1543
1544 if (was_disabled != other_ssid->disabled)
1545 wpas_notify_network_enabled_changed(
1546 wpa_s, other_ssid);
1547 }
1548 if (wpa_s->reassociate)
1549 wpa_supplicant_req_scan(wpa_s, 0, 0);
1550 } else if (ssid->disabled && ssid->disabled != 2) {
1551 if (wpa_s->current_ssid == NULL) {
1552 /*
1553 * Try to reassociate since there is no current
1554 * configuration and a new network was made available.
1555 */
1556 wpa_s->reassociate = 1;
1557 wpa_supplicant_req_scan(wpa_s, 0, 0);
1558 }
1559
1560 was_disabled = ssid->disabled;
1561
1562 ssid->disabled = 0;
1563
1564 if (was_disabled != ssid->disabled)
1565 wpas_notify_network_enabled_changed(wpa_s, ssid);
1566 }
1567 }
1568
1569
1570 /**
1571 * wpa_supplicant_disable_network - Mark a configured network as disabled
1572 * @wpa_s: wpa_supplicant structure for a network interface
1573 * @ssid: wpa_ssid structure for a configured network or %NULL
1574 *
1575 * Disables the specified network or all networks if no network specified.
1576 */
1577 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1578 struct wpa_ssid *ssid)
1579 {
1580 struct wpa_ssid *other_ssid;
1581 int was_disabled;
1582
1583 if (ssid == NULL) {
1584 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1585 other_ssid = other_ssid->next) {
1586 was_disabled = other_ssid->disabled;
1587 if (was_disabled == 2)
1588 continue; /* do not change persistent P2P group
1589 * data */
1590
1591 other_ssid->disabled = 1;
1592
1593 if (was_disabled != other_ssid->disabled)
1594 wpas_notify_network_enabled_changed(
1595 wpa_s, other_ssid);
1596 }
1597 if (wpa_s->current_ssid)
1598 wpa_supplicant_disassociate(
1599 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1600 } else if (ssid->disabled != 2) {
1601 if (ssid == wpa_s->current_ssid)
1602 wpa_supplicant_disassociate(
1603 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1604
1605 was_disabled = ssid->disabled;
1606
1607 ssid->disabled = 1;
1608
1609 if (was_disabled != ssid->disabled)
1610 wpas_notify_network_enabled_changed(wpa_s, ssid);
1611 }
1612 }
1613
1614
1615 /**
1616 * wpa_supplicant_select_network - Attempt association with a network
1617 * @wpa_s: wpa_supplicant structure for a network interface
1618 * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1619 */
1620 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1621 struct wpa_ssid *ssid)
1622 {
1623
1624 struct wpa_ssid *other_ssid;
1625
1626 if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
1627 wpa_supplicant_disassociate(
1628 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1629
1630 /*
1631 * Mark all other networks disabled or mark all networks enabled if no
1632 * network specified.
1633 */
1634 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1635 other_ssid = other_ssid->next) {
1636 int was_disabled = other_ssid->disabled;
1637 if (was_disabled == 2)
1638 continue; /* do not change persistent P2P group data */
1639
1640 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1641
1642 if (was_disabled != other_ssid->disabled)
1643 wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1644 }
1645 wpa_s->connect_without_scan = NULL;
1646 wpa_s->disconnected = 0;
1647 wpa_s->reassociate = 1;
1648 wpa_supplicant_req_scan(wpa_s, 0, 0);
1649
1650 if (ssid)
1651 wpas_notify_network_selected(wpa_s, ssid);
1652 }
1653
1654
1655 /**
1656 * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1657 * @wpa_s: wpa_supplicant structure for a network interface
1658 * @ap_scan: AP scan mode
1659 * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1660 *
1661 */
1662 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1663 {
1664
1665 int old_ap_scan;
1666
1667 if (ap_scan < 0 || ap_scan > 2)
1668 return -1;
1669
1670 old_ap_scan = wpa_s->conf->ap_scan;
1671 wpa_s->conf->ap_scan = ap_scan;
1672
1673 if (old_ap_scan != wpa_s->conf->ap_scan)
1674 wpas_notify_ap_scan_changed(wpa_s);
1675
1676 return 0;
1677 }
1678
1679
1680 /**
1681 * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1682 * @wpa_s: wpa_supplicant structure for a network interface
1683 * @expire_age: Expiration age in seconds
1684 * Returns: 0 if succeed or -1 if expire_age has an invalid value
1685 *
1686 */
1687 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1688 unsigned int bss_expire_age)
1689 {
1690 if (bss_expire_age < 10) {
1691 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1692 bss_expire_age);
1693 return -1;
1694 }
1695 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1696 bss_expire_age);
1697 wpa_s->conf->bss_expiration_age = bss_expire_age;
1698
1699 return 0;
1700 }
1701
1702
1703 /**
1704 * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1705 * @wpa_s: wpa_supplicant structure for a network interface
1706 * @expire_count: number of scans after which an unseen BSS is reclaimed
1707 * Returns: 0 if succeed or -1 if expire_count has an invalid value
1708 *
1709 */
1710 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1711 unsigned int bss_expire_count)
1712 {
1713 if (bss_expire_count < 1) {
1714 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1715 bss_expire_count);
1716 return -1;
1717 }
1718 wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1719 bss_expire_count);
1720 wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1721
1722 return 0;
1723 }
1724
1725
1726 /**
1727 * wpa_supplicant_set_debug_params - Set global debug params
1728 * @global: wpa_global structure
1729 * @debug_level: debug level
1730 * @debug_timestamp: determines if show timestamp in debug data
1731 * @debug_show_keys: determines if show keys in debug data
1732 * Returns: 0 if succeed or -1 if debug_level has wrong value
1733 */
1734 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1735 int debug_timestamp, int debug_show_keys)
1736 {
1737
1738 int old_level, old_timestamp, old_show_keys;
1739
1740 /* check for allowed debuglevels */
1741 if (debug_level != MSG_EXCESSIVE &&
1742 debug_level != MSG_MSGDUMP &&
1743 debug_level != MSG_DEBUG &&
1744 debug_level != MSG_INFO &&
1745 debug_level != MSG_WARNING &&
1746 debug_level != MSG_ERROR)
1747 return -1;
1748
1749 old_level = wpa_debug_level;
1750 old_timestamp = wpa_debug_timestamp;
1751 old_show_keys = wpa_debug_show_keys;
1752
1753 wpa_debug_level = debug_level;
1754 wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1755 wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1756
1757 if (wpa_debug_level != old_level)
1758 wpas_notify_debug_level_changed(global);
1759 if (wpa_debug_timestamp != old_timestamp)
1760 wpas_notify_debug_timestamp_changed(global);
1761 if (wpa_debug_show_keys != old_show_keys)
1762 wpas_notify_debug_show_keys_changed(global);
1763
1764 return 0;
1765 }
1766
1767
1768 /**
1769 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1770 * @wpa_s: Pointer to wpa_supplicant data
1771 * Returns: A pointer to the current network structure or %NULL on failure
1772 */
1773 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1774 {
1775 struct wpa_ssid *entry;
1776 u8 ssid[MAX_SSID_LEN];
1777 int res;
1778 size_t ssid_len;
1779 u8 bssid[ETH_ALEN];
1780 int wired;
1781
1782 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
1783 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1784 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1785 "MLME");
1786 return NULL;
1787 }
1788 } else {
1789 res = wpa_drv_get_ssid(wpa_s, ssid);
1790 if (res < 0) {
1791 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1792 "driver");
1793 return NULL;
1794 }
1795 ssid_len = res;
1796 }
1797
1798 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
1799 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1800 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1801 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1802 "driver");
1803 return NULL;
1804 }
1805
1806 wired = wpa_s->conf->ap_scan == 0 &&
1807 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1808
1809 entry = wpa_s->conf->ssid;
1810 while (entry) {
1811 if (!entry->disabled &&
1812 ((ssid_len == entry->ssid_len &&
1813 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1814 (!entry->bssid_set ||
1815 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1816 return entry;
1817 #ifdef CONFIG_WPS
1818 if (!entry->disabled &&
1819 (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1820 (entry->ssid == NULL || entry->ssid_len == 0) &&
1821 (!entry->bssid_set ||
1822 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1823 return entry;
1824 #endif /* CONFIG_WPS */
1825 entry = entry->next;
1826 }
1827
1828 return NULL;
1829 }
1830
1831
1832 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1833 const char *name)
1834 {
1835 int i;
1836 size_t len;
1837 const char *pos, *driver = name;
1838
1839 if (wpa_s == NULL)
1840 return -1;
1841
1842 if (wpa_drivers[0] == NULL) {
1843 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1844 "wpa_supplicant");
1845 return -1;
1846 }
1847
1848 if (name == NULL) {
1849 /* default to first driver in the list */
1850 wpa_s->driver = wpa_drivers[0];
1851 wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
1852 return 0;
1853 }
1854
1855 do {
1856 pos = os_strchr(driver, ',');
1857 if (pos)
1858 len = pos - driver;
1859 else
1860 len = os_strlen(driver);
1861
1862 for (i = 0; wpa_drivers[i]; i++) {
1863 if (os_strlen(wpa_drivers[i]->name) == len &&
1864 os_strncmp(driver, wpa_drivers[i]->name, len) ==
1865 0) {
1866 wpa_s->driver = wpa_drivers[i];
1867 wpa_s->global_drv_priv =
1868 wpa_s->global->drv_priv[i];
1869 return 0;
1870 }
1871 }
1872
1873 driver = pos + 1;
1874 } while (pos);
1875
1876 wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1877 return -1;
1878 }
1879
1880
1881 /**
1882 * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1883 * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1884 * with struct wpa_driver_ops::init()
1885 * @src_addr: Source address of the EAPOL frame
1886 * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1887 * @len: Length of the EAPOL data
1888 *
1889 * This function is called for each received EAPOL frame. Most driver
1890 * interfaces rely on more generic OS mechanism for receiving frames through
1891 * l2_packet, but if such a mechanism is not available, the driver wrapper may
1892 * take care of received EAPOL frames and deliver them to the core supplicant
1893 * code by calling this function.
1894 */
1895 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1896 const u8 *buf, size_t len)
1897 {
1898 struct wpa_supplicant *wpa_s = ctx;
1899
1900 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1901 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1902
1903 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1904 /*
1905 * There is possible race condition between receiving the
1906 * association event and the EAPOL frame since they are coming
1907 * through different paths from the driver. In order to avoid
1908 * issues in trying to process the EAPOL frame before receiving
1909 * association information, lets queue it for processing until
1910 * the association event is received.
1911 */
1912 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1913 "of received EAPOL frame");
1914 wpabuf_free(wpa_s->pending_eapol_rx);
1915 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1916 if (wpa_s->pending_eapol_rx) {
1917 os_get_time(&wpa_s->pending_eapol_rx_time);
1918 os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1919 ETH_ALEN);
1920 }
1921 return;
1922 }
1923
1924 #ifdef CONFIG_AP
1925 if (wpa_s->ap_iface) {
1926 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1927 return;
1928 }
1929 #endif /* CONFIG_AP */
1930
1931 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1932 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
1933 "no key management is configured");
1934 return;
1935 }
1936
1937 if (wpa_s->eapol_received == 0 &&
1938 (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
1939 !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
1940 wpa_s->wpa_state != WPA_COMPLETED) &&
1941 (wpa_s->current_ssid == NULL ||
1942 wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
1943 /* Timeout for completing IEEE 802.1X and WPA authentication */
1944 wpa_supplicant_req_auth_timeout(
1945 wpa_s,
1946 (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
1947 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
1948 wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
1949 70 : 10, 0);
1950 }
1951 wpa_s->eapol_received++;
1952
1953 if (wpa_s->countermeasures) {
1954 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
1955 "EAPOL packet");
1956 return;
1957 }
1958
1959 #ifdef CONFIG_IBSS_RSN
1960 if (wpa_s->current_ssid &&
1961 wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
1962 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
1963 return;
1964 }
1965 #endif /* CONFIG_IBSS_RSN */
1966
1967 /* Source address of the incoming EAPOL frame could be compared to the
1968 * current BSSID. However, it is possible that a centralized
1969 * Authenticator could be using another MAC address than the BSSID of
1970 * an AP, so just allow any address to be used for now. The replies are
1971 * still sent to the current BSSID (if available), though. */
1972
1973 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1974 if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
1975 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1976 return;
1977 wpa_drv_poll(wpa_s);
1978 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
1979 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1980 else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
1981 /*
1982 * Set portValid = TRUE here since we are going to skip 4-way
1983 * handshake processing which would normally set portValid. We
1984 * need this to allow the EAPOL state machines to be completed
1985 * without going through EAPOL-Key handshake.
1986 */
1987 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
1988 }
1989 }
1990
1991
1992 /**
1993 * wpa_supplicant_driver_init - Initialize driver interface parameters
1994 * @wpa_s: Pointer to wpa_supplicant data
1995 * Returns: 0 on success, -1 on failure
1996 *
1997 * This function is called to initialize driver interface parameters.
1998 * wpa_drv_init() must have been called before this function to initialize the
1999 * driver interface.
2000 */
2001 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2002 {
2003 static int interface_count = 0;
2004
2005 if (wpa_s->driver->send_eapol) {
2006 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2007 if (addr)
2008 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2009 } else if (!(wpa_s->drv_flags &
2010 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2011 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2012 wpa_drv_get_mac_addr(wpa_s),
2013 ETH_P_EAPOL,
2014 wpa_supplicant_rx_eapol, wpa_s, 0);
2015 if (wpa_s->l2 == NULL)
2016 return -1;
2017 } else {
2018 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2019 if (addr)
2020 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2021 }
2022
2023 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2024 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2025 return -1;
2026 }
2027
2028 wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2029 MAC2STR(wpa_s->own_addr));
2030
2031 if (wpa_s->bridge_ifname[0]) {
2032 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2033 "interface '%s'", wpa_s->bridge_ifname);
2034 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2035 wpa_s->own_addr,
2036 ETH_P_EAPOL,
2037 wpa_supplicant_rx_eapol, wpa_s,
2038 0);
2039 if (wpa_s->l2_br == NULL) {
2040 wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2041 "connection for the bridge interface '%s'",
2042 wpa_s->bridge_ifname);
2043 return -1;
2044 }
2045 }
2046
2047 wpa_clear_keys(wpa_s, NULL);
2048
2049 /* Make sure that TKIP countermeasures are not left enabled (could
2050 * happen if wpa_supplicant is killed during countermeasures. */
2051 wpa_drv_set_countermeasures(wpa_s, 0);
2052
2053 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2054 wpa_drv_flush_pmkid(wpa_s);
2055
2056 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2057 if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2058 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2059 interface_count++;
2060 } else
2061 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2062
2063 return 0;
2064 }
2065
2066
2067 static int wpa_supplicant_daemon(const char *pid_file)
2068 {
2069 wpa_printf(MSG_DEBUG, "Daemonize..");
2070 return os_daemonize(pid_file);
2071 }
2072
2073
2074 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2075 {
2076 struct wpa_supplicant *wpa_s;
2077
2078 wpa_s = os_zalloc(sizeof(*wpa_s));
2079 if (wpa_s == NULL)
2080 return NULL;
2081 wpa_s->scan_req = 1;
2082 wpa_s->scan_interval = 5;
2083 wpa_s->new_connection = 1;
2084 wpa_s->parent = wpa_s;
2085
2086 return wpa_s;
2087 }
2088
2089
2090 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2091 struct wpa_interface *iface)
2092 {
2093 const char *ifname, *driver;
2094 struct wpa_driver_capa capa;
2095
2096 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2097 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2098 iface->confname ? iface->confname : "N/A",
2099 iface->driver ? iface->driver : "default",
2100 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2101 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2102
2103 if (iface->confname) {
2104 #ifdef CONFIG_BACKEND_FILE
2105 wpa_s->confname = os_rel2abs_path(iface->confname);
2106 if (wpa_s->confname == NULL) {
2107 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2108 "for configuration file '%s'.",
2109 iface->confname);
2110 return -1;
2111 }
2112 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2113 iface->confname, wpa_s->confname);
2114 #else /* CONFIG_BACKEND_FILE */
2115 wpa_s->confname = os_strdup(iface->confname);
2116 #endif /* CONFIG_BACKEND_FILE */
2117 wpa_s->conf = wpa_config_read(wpa_s->confname);
2118 if (wpa_s->conf == NULL) {
2119 wpa_printf(MSG_ERROR, "Failed to read or parse "
2120 "configuration '%s'.", wpa_s->confname);
2121 return -1;
2122 }
2123
2124 /*
2125 * Override ctrl_interface and driver_param if set on command
2126 * line.
2127 */
2128 if (iface->ctrl_interface) {
2129 os_free(wpa_s->conf->ctrl_interface);
2130 wpa_s->conf->ctrl_interface =
2131 os_strdup(iface->ctrl_interface);
2132 }
2133
2134 if (iface->driver_param) {
2135 os_free(wpa_s->conf->driver_param);
2136 wpa_s->conf->driver_param =
2137 os_strdup(iface->driver_param);
2138 }
2139 } else
2140 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2141 iface->driver_param);
2142
2143 if (wpa_s->conf == NULL) {
2144 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2145 return -1;
2146 }
2147
2148 if (iface->ifname == NULL) {
2149 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2150 return -1;
2151 }
2152 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2153 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2154 iface->ifname);
2155 return -1;
2156 }
2157 os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2158
2159 if (iface->bridge_ifname) {
2160 if (os_strlen(iface->bridge_ifname) >=
2161 sizeof(wpa_s->bridge_ifname)) {
2162 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2163 "name '%s'.", iface->bridge_ifname);
2164 return -1;
2165 }
2166 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2167 sizeof(wpa_s->bridge_ifname));
2168 }
2169
2170 /* RSNA Supplicant Key Management - INITIALIZE */
2171 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2172 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2173
2174 /* Initialize driver interface and register driver event handler before
2175 * L2 receive handler so that association events are processed before
2176 * EAPOL-Key packets if both become available for the same select()
2177 * call. */
2178 driver = iface->driver;
2179 next_driver:
2180 if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2181 return -1;
2182
2183 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2184 if (wpa_s->drv_priv == NULL) {
2185 const char *pos;
2186 pos = driver ? os_strchr(driver, ',') : NULL;
2187 if (pos) {
2188 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2189 "driver interface - try next driver wrapper");
2190 driver = pos + 1;
2191 goto next_driver;
2192 }
2193 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2194 "interface");
2195 return -1;
2196 }
2197 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2198 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2199 "driver_param '%s'", wpa_s->conf->driver_param);
2200 return -1;
2201 }
2202
2203 ifname = wpa_drv_get_ifname(wpa_s);
2204 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2205 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2206 "interface name with '%s'", ifname);
2207 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2208 }
2209
2210 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2211 return -1;
2212
2213 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2214 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2215 NULL);
2216 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2217
2218 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2219 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2220 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2221 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2222 "dot11RSNAConfigPMKLifetime");
2223 return -1;
2224 }
2225
2226 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2227 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2228 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2229 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2230 "dot11RSNAConfigPMKReauthThreshold");
2231 return -1;
2232 }
2233
2234 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2235 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2236 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2237 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2238 "dot11RSNAConfigSATimeout");
2239 return -1;
2240 }
2241
2242 if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2243 wpa_s->drv_flags = capa.flags;
2244 if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2245 if (ieee80211_sta_init(wpa_s))
2246 return -1;
2247 }
2248 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2249 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2250 wpa_s->max_stations = capa.max_stations;
2251 }
2252 if (wpa_s->max_remain_on_chan == 0)
2253 wpa_s->max_remain_on_chan = 1000;
2254
2255 if (wpa_supplicant_driver_init(wpa_s) < 0)
2256 return -1;
2257
2258 #ifdef CONFIG_TDLS
2259 if (wpa_tdls_init(wpa_s->wpa))
2260 return -1;
2261 #endif /* CONFIG_TDLS */
2262
2263 if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2264 wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2265 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2266 return -1;
2267 }
2268
2269 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2270
2271 if (wpas_wps_init(wpa_s))
2272 return -1;
2273
2274 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2275 return -1;
2276 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2277
2278 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2279 if (wpa_s->ctrl_iface == NULL) {
2280 wpa_printf(MSG_ERROR,
2281 "Failed to initialize control interface '%s'.\n"
2282 "You may have another wpa_supplicant process "
2283 "already running or the file was\n"
2284 "left by an unclean termination of wpa_supplicant "
2285 "in which case you will need\n"
2286 "to manually remove this file before starting "
2287 "wpa_supplicant again.\n",
2288 wpa_s->conf->ctrl_interface);
2289 return -1;
2290 }
2291
2292 wpa_s->gas = gas_query_init(wpa_s);
2293 if (wpa_s->gas == NULL) {
2294 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2295 return -1;
2296 }
2297
2298 #ifdef CONFIG_P2P
2299 if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2300 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2301 return -1;
2302 }
2303 #endif /* CONFIG_P2P */
2304
2305 if (wpa_bss_init(wpa_s) < 0)
2306 return -1;
2307
2308 return 0;
2309 }
2310
2311
2312 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2313 int notify)
2314 {
2315 if (wpa_s->drv_priv) {
2316 wpa_supplicant_deauthenticate(wpa_s,
2317 WLAN_REASON_DEAUTH_LEAVING);
2318
2319 wpa_drv_set_countermeasures(wpa_s, 0);
2320 wpa_clear_keys(wpa_s, NULL);
2321 }
2322
2323 wpa_supplicant_cleanup(wpa_s);
2324
2325 if (notify)
2326 wpas_notify_iface_removed(wpa_s);
2327
2328 if (wpa_s->drv_priv)
2329 wpa_drv_deinit(wpa_s);
2330 }
2331
2332
2333 /**
2334 * wpa_supplicant_add_iface - Add a new network interface
2335 * @global: Pointer to global data from wpa_supplicant_init()
2336 * @iface: Interface configuration options
2337 * Returns: Pointer to the created interface or %NULL on failure
2338 *
2339 * This function is used to add new network interfaces for %wpa_supplicant.
2340 * This can be called before wpa_supplicant_run() to add interfaces before the
2341 * main event loop has been started. In addition, new interfaces can be added
2342 * dynamically while %wpa_supplicant is already running. This could happen,
2343 * e.g., when a hotplug network adapter is inserted.
2344 */
2345 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2346 struct wpa_interface *iface)
2347 {
2348 struct wpa_supplicant *wpa_s;
2349 struct wpa_interface t_iface;
2350 struct wpa_ssid *ssid;
2351
2352 if (global == NULL || iface == NULL)
2353 return NULL;
2354
2355 wpa_s = wpa_supplicant_alloc();
2356 if (wpa_s == NULL)
2357 return NULL;
2358
2359 wpa_s->global = global;
2360
2361 t_iface = *iface;
2362 if (global->params.override_driver) {
2363 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2364 "('%s' -> '%s')",
2365 iface->driver, global->params.override_driver);
2366 t_iface.driver = global->params.override_driver;
2367 }
2368 if (global->params.override_ctrl_interface) {
2369 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2370 "ctrl_interface ('%s' -> '%s')",
2371 iface->ctrl_interface,
2372 global->params.override_ctrl_interface);
2373 t_iface.ctrl_interface =
2374 global->params.override_ctrl_interface;
2375 }
2376 if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2377 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2378 iface->ifname);
2379 wpa_supplicant_deinit_iface(wpa_s, 0);
2380 os_free(wpa_s);
2381 return NULL;
2382 }
2383
2384 /* Notify the control interfaces about new iface */
2385 if (wpas_notify_iface_added(wpa_s)) {
2386 wpa_supplicant_deinit_iface(wpa_s, 1);
2387 os_free(wpa_s);
2388 return NULL;
2389 }
2390
2391 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2392 wpas_notify_network_added(wpa_s, ssid);
2393
2394 wpa_s->next = global->ifaces;
2395 global->ifaces = wpa_s;
2396
2397 wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2398
2399 return wpa_s;
2400 }
2401
2402
2403 /**
2404 * wpa_supplicant_remove_iface - Remove a network interface
2405 * @global: Pointer to global data from wpa_supplicant_init()
2406 * @wpa_s: Pointer to the network interface to be removed
2407 * Returns: 0 if interface was removed, -1 if interface was not found
2408 *
2409 * This function can be used to dynamically remove network interfaces from
2410 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2411 * addition, this function is used to remove all remaining interfaces when
2412 * %wpa_supplicant is terminated.
2413 */
2414 int wpa_supplicant_remove_iface(struct wpa_global *global,
2415 struct wpa_supplicant *wpa_s)
2416 {
2417 struct wpa_supplicant *prev;
2418
2419 /* Remove interface from the global list of interfaces */
2420 prev = global->ifaces;
2421 if (prev == wpa_s) {
2422 global->ifaces = wpa_s->next;
2423 } else {
2424 while (prev && prev->next != wpa_s)
2425 prev = prev->next;
2426 if (prev == NULL)
2427 return -1;
2428 prev->next = wpa_s->next;
2429 }
2430
2431 wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2432
2433 if (global->p2p_group_formation == wpa_s)
2434 global->p2p_group_formation = NULL;
2435 wpa_supplicant_deinit_iface(wpa_s, 1);
2436 os_free(wpa_s);
2437
2438 return 0;
2439 }
2440
2441
2442 /**
2443 * wpa_supplicant_get_eap_mode - Get the current EAP mode
2444 * @wpa_s: Pointer to the network interface
2445 * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2446 */
2447 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2448 {
2449 const char *eapol_method;
2450
2451 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2452 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2453 return "NO-EAP";
2454 }
2455
2456 eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2457 if (eapol_method == NULL)
2458 return "UNKNOWN-EAP";
2459
2460 return eapol_method;
2461 }
2462
2463
2464 /**
2465 * wpa_supplicant_get_iface - Get a new network interface
2466 * @global: Pointer to global data from wpa_supplicant_init()
2467 * @ifname: Interface name
2468 * Returns: Pointer to the interface or %NULL if not found
2469 */
2470 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2471 const char *ifname)
2472 {
2473 struct wpa_supplicant *wpa_s;
2474
2475 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2476 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2477 return wpa_s;
2478 }
2479 return NULL;
2480 }
2481
2482
2483 #ifndef CONFIG_NO_WPA_MSG
2484 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2485 {
2486 struct wpa_supplicant *wpa_s = ctx;
2487 if (wpa_s == NULL)
2488 return NULL;
2489 return wpa_s->ifname;
2490 }
2491 #endif /* CONFIG_NO_WPA_MSG */
2492
2493
2494 /**
2495 * wpa_supplicant_init - Initialize %wpa_supplicant
2496 * @params: Parameters for %wpa_supplicant
2497 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2498 *
2499 * This function is used to initialize %wpa_supplicant. After successful
2500 * initialization, the returned data pointer can be used to add and remove
2501 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2502 */
2503 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2504 {
2505 struct wpa_global *global;
2506 int ret, i;
2507
2508 if (params == NULL)
2509 return NULL;
2510
2511 #ifndef CONFIG_NO_WPA_MSG
2512 wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2513 #endif /* CONFIG_NO_WPA_MSG */
2514
2515 wpa_debug_open_file(params->wpa_debug_file_path);
2516 if (params->wpa_debug_syslog)
2517 wpa_debug_open_syslog();
2518
2519 ret = eap_register_methods();
2520 if (ret) {
2521 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2522 if (ret == -2)
2523 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2524 "the same EAP type.");
2525 return NULL;
2526 }
2527
2528 global = os_zalloc(sizeof(*global));
2529 if (global == NULL)
2530 return NULL;
2531 dl_list_init(&global->p2p_srv_bonjour);
2532 dl_list_init(&global->p2p_srv_upnp);
2533 global->params.daemonize = params->daemonize;
2534 global->params.wait_for_monitor = params->wait_for_monitor;
2535 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2536 if (params->pid_file)
2537 global->params.pid_file = os_strdup(params->pid_file);
2538 if (params->ctrl_interface)
2539 global->params.ctrl_interface =
2540 os_strdup(params->ctrl_interface);
2541 if (params->override_driver)
2542 global->params.override_driver =
2543 os_strdup(params->override_driver);
2544 if (params->override_ctrl_interface)
2545 global->params.override_ctrl_interface =
2546 os_strdup(params->override_ctrl_interface);
2547 wpa_debug_level = global->params.wpa_debug_level =
2548 params->wpa_debug_level;
2549 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2550 params->wpa_debug_show_keys;
2551 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2552 params->wpa_debug_timestamp;
2553
2554 wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2555
2556 if (eloop_init()) {
2557 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2558 wpa_supplicant_deinit(global);
2559 return NULL;
2560 }
2561
2562 random_init(params->entropy_file);
2563
2564 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2565 if (global->ctrl_iface == NULL) {
2566 wpa_supplicant_deinit(global);
2567 return NULL;
2568 }
2569
2570 if (wpas_notify_supplicant_initialized(global)) {
2571 wpa_supplicant_deinit(global);
2572 return NULL;
2573 }
2574
2575 for (i = 0; wpa_drivers[i]; i++)
2576 global->drv_count++;
2577 if (global->drv_count == 0) {
2578 wpa_printf(MSG_ERROR, "No drivers enabled");
2579 wpa_supplicant_deinit(global);
2580 return NULL;
2581 }
2582 global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2583 if (global->drv_priv == NULL) {
2584 wpa_supplicant_deinit(global);
2585 return NULL;
2586 }
2587 for (i = 0; wpa_drivers[i]; i++) {
2588 if (!wpa_drivers[i]->global_init)
2589 continue;
2590 global->drv_priv[i] = wpa_drivers[i]->global_init();
2591 if (global->drv_priv[i] == NULL) {
2592 wpa_printf(MSG_ERROR, "Failed to initialize driver "
2593 "'%s'", wpa_drivers[i]->name);
2594 wpa_supplicant_deinit(global);
2595 return NULL;
2596 }
2597 }
2598
2599 return global;
2600 }
2601
2602
2603 /**
2604 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2605 * @global: Pointer to global data from wpa_supplicant_init()
2606 * Returns: 0 after successful event loop run, -1 on failure
2607 *
2608 * This function starts the main event loop and continues running as long as
2609 * there are any remaining events. In most cases, this function is running as
2610 * long as the %wpa_supplicant process in still in use.
2611 */
2612 int wpa_supplicant_run(struct wpa_global *global)
2613 {
2614 struct wpa_supplicant *wpa_s;
2615
2616 if (global->params.daemonize &&
2617 wpa_supplicant_daemon(global->params.pid_file))
2618 return -1;
2619
2620 if (global->params.wait_for_monitor) {
2621 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2622 if (wpa_s->ctrl_iface)
2623 wpa_supplicant_ctrl_iface_wait(
2624 wpa_s->ctrl_iface);
2625 }
2626
2627 eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2628 eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2629
2630 eloop_run();
2631
2632 return 0;
2633 }
2634
2635
2636 /**
2637 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2638 * @global: Pointer to global data from wpa_supplicant_init()
2639 *
2640 * This function is called to deinitialize %wpa_supplicant and to free all
2641 * allocated resources. Remaining network interfaces will also be removed.
2642 */
2643 void wpa_supplicant_deinit(struct wpa_global *global)
2644 {
2645 int i;
2646
2647 if (global == NULL)
2648 return;
2649
2650 #ifdef CONFIG_P2P
2651 wpas_p2p_deinit_global(global);
2652 #endif /* CONFIG_P2P */
2653
2654 while (global->ifaces)
2655 wpa_supplicant_remove_iface(global, global->ifaces);
2656
2657 if (global->ctrl_iface)
2658 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2659
2660 wpas_notify_supplicant_deinitialized(global);
2661
2662 eap_peer_unregister_methods();
2663 #ifdef CONFIG_AP
2664 eap_server_unregister_methods();
2665 #endif /* CONFIG_AP */
2666
2667 for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
2668 if (!global->drv_priv[i])
2669 continue;
2670 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
2671 }
2672 os_free(global->drv_priv);
2673
2674 random_deinit();
2675
2676 eloop_destroy();
2677
2678 if (global->params.pid_file) {
2679 os_daemonize_terminate(global->params.pid_file);
2680 os_free(global->params.pid_file);
2681 }
2682 os_free(global->params.ctrl_interface);
2683 os_free(global->params.override_driver);
2684 os_free(global->params.override_ctrl_interface);
2685
2686 os_free(global);
2687 wpa_debug_close_syslog();
2688 wpa_debug_close_file();
2689 }
2690
2691
2692 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
2693 {
2694 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
2695 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2696 char country[3];
2697 country[0] = wpa_s->conf->country[0];
2698 country[1] = wpa_s->conf->country[1];
2699 country[2] = '\0';
2700 if (wpa_drv_set_country(wpa_s, country) < 0) {
2701 wpa_printf(MSG_ERROR, "Failed to set country code "
2702 "'%s'", country);
2703 }
2704 }
2705
2706 #ifdef CONFIG_WPS
2707 wpas_wps_update_config(wpa_s);
2708 #endif /* CONFIG_WPS */
2709
2710 #ifdef CONFIG_P2P
2711 wpas_p2p_update_config(wpa_s);
2712 #endif /* CONFIG_P2P */
2713
2714 wpa_s->conf->changed_parameters = 0;
2715 }
2716
2717
2718 void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
2719 size_t num_hw_features)
2720 {
2721 size_t i;
2722
2723 if (hw_features == NULL)
2724 return;
2725
2726 for (i = 0; i < num_hw_features; i++) {
2727 os_free(hw_features[i].channels);
2728 os_free(hw_features[i].rates);
2729 }
2730
2731 os_free(hw_features);
2732 }
2733
2734
2735 static void add_freq(int *freqs, int *num_freqs, int freq)
2736 {
2737 int i;
2738
2739 for (i = 0; i < *num_freqs; i++) {
2740 if (freqs[i] == freq)
2741 return;
2742 }
2743
2744 freqs[*num_freqs] = freq;
2745 (*num_freqs)++;
2746 }
2747
2748
2749 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
2750 {
2751 struct wpa_bss *bss, *cbss;
2752 const int max_freqs = 10;
2753 int *freqs;
2754 int num_freqs = 0;
2755
2756 freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
2757 if (freqs == NULL)
2758 return NULL;
2759
2760 cbss = wpa_s->current_bss;
2761
2762 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
2763 if (bss == cbss)
2764 continue;
2765 if (bss->ssid_len == cbss->ssid_len &&
2766 os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
2767 wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
2768 add_freq(freqs, &num_freqs, bss->freq);
2769 if (num_freqs == max_freqs)
2770 break;
2771 }
2772 }
2773
2774 if (num_freqs == 0) {
2775 os_free(freqs);
2776 freqs = NULL;
2777 }
2778
2779 return freqs;
2780 }
2781
2782
2783 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
2784 {
2785 int timeout;
2786 int count;
2787 int *freqs = NULL;
2788
2789 /*
2790 * Add the failed BSSID into the blacklist and speed up next scan
2791 * attempt if there could be other APs that could accept association.
2792 * The current blacklist count indicates how many times we have tried
2793 * connecting to this AP and multiple attempts mean that other APs are
2794 * either not available or has already been tried, so that we can start
2795 * increasing the delay here to avoid constant scanning.
2796 */
2797 count = wpa_blacklist_add(wpa_s, bssid);
2798 if (count == 1 && wpa_s->current_bss) {
2799 /*
2800 * This BSS was not in the blacklist before. If there is
2801 * another BSS available for the same ESS, we should try that
2802 * next. Otherwise, we may as well try this one once more
2803 * before allowing other, likely worse, ESSes to be considered.
2804 */
2805 freqs = get_bss_freqs_in_ess(wpa_s);
2806 if (freqs) {
2807 wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
2808 "has been seen; try it next");
2809 wpa_blacklist_add(wpa_s, bssid);
2810 /*
2811 * On the next scan, go through only the known channels
2812 * used in this ESS based on previous scans to speed up
2813 * common load balancing use case.
2814 */
2815 os_free(wpa_s->next_scan_freqs);
2816 wpa_s->next_scan_freqs = freqs;
2817 }
2818 }
2819
2820 switch (count) {
2821 case 1:
2822 timeout = 100;
2823 break;
2824 case 2:
2825 timeout = 500;
2826 break;
2827 case 3:
2828 timeout = 1000;
2829 break;
2830 default:
2831 timeout = 5000;
2832 }
2833
2834 /*
2835 * TODO: if more than one possible AP is available in scan results,
2836 * could try the other ones before requesting a new scan.
2837 */
2838 wpa_supplicant_req_scan(wpa_s, timeout / 1000,
2839 1000 * (timeout % 1000));
2840 }
2841
2842
2843 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
2844 {
2845 return wpa_s->conf->ap_scan == 2 ||
2846 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
2847 }