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